Commit 7831c888 authored by Dominik Charousset's avatar Dominik Charousset

Make UUID hashable

parent c2be26e7
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <cstdint> #include <cstdint>
#include <type_traits> #include <type_traits>
#include "caf/byte.hpp"
#include "caf/detail/ieee_754.hpp" #include "caf/detail/ieee_754.hpp"
#include "caf/inspector_access.hpp" #include "caf/inspector_access.hpp"
#include "caf/save_inspector_base.hpp" #include "caf/save_inspector_base.hpp"
...@@ -111,6 +112,10 @@ public: ...@@ -111,6 +112,10 @@ public:
return true; return true;
} }
bool value(byte x) noexcept {
return value(static_cast<uint8_t>(x));
}
bool value(bool x) noexcept { bool value(bool x) noexcept {
auto tmp = static_cast<uint8_t>(x); auto tmp = static_cast<uint8_t>(x);
return value(tmp); return value(tmp);
......
...@@ -107,6 +107,9 @@ public: ...@@ -107,6 +107,9 @@ public:
/// (`randomized` UUIDs). /// (`randomized` UUIDs).
uint64_t node() const noexcept; uint64_t node() const noexcept;
/// Returns a platform-specific hash value for this UUID.
size_t hash() const noexcept;
/// Creates a random UUID. /// Creates a random UUID.
static uuid random() noexcept; static uuid random() noexcept;
...@@ -169,3 +172,14 @@ bool inspect(Inspector& f, uuid& x) { ...@@ -169,3 +172,14 @@ bool inspect(Inspector& f, uuid& x) {
} }
} // namespace caf } // namespace caf
namespace std {
template <>
struct hash<caf::uuid> {
size_t operator()(const caf::uuid& x) const noexcept {
return x.hash();
}
};
} // namespace std
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "caf/detail/network_order.hpp" #include "caf/detail/network_order.hpp"
#include "caf/detail/parser/add_ascii.hpp" #include "caf/detail/parser/add_ascii.hpp"
#include "caf/expected.hpp" #include "caf/expected.hpp"
#include "caf/hash/fnv.hpp"
#include "caf/message.hpp" #include "caf/message.hpp"
#include "caf/parser_state.hpp" #include "caf/parser_state.hpp"
...@@ -107,6 +108,10 @@ uint64_t uuid::node() const noexcept { ...@@ -107,6 +108,10 @@ uint64_t uuid::node() const noexcept {
return detail::from_network_order(result); return detail::from_network_order(result);
} }
size_t uuid::hash() const noexcept {
return hash::fnv<size_t>::compute(bytes_);
}
namespace { namespace {
enum parse_result { enum parse_result {
......
...@@ -161,4 +161,19 @@ SCENARIO("UUIDs are inspectable") { ...@@ -161,4 +161,19 @@ SCENARIO("UUIDs are inspectable") {
} }
} }
SCENARIO("UUIDs are hashable") {
GIVEN("two UUIDs ") {
auto id1 = "2ee4ded7-69c0-4dd6-876d-02e446b21784"_uuid;
auto id2 = "a6155548-2994-4833-b4e3-9823f5f15fe9"_uuid;
WHEN("retrieving a hash value for the UUIDs") {
THEN("the UUIDs return different hash values") {
std::hash<uuid> f;
CHECK_EQ(id1.hash(), f(id1));
CHECK_EQ(id2.hash(), f(id2));
CHECK_NE(f(id1), f(id2));
}
}
}
}
CAF_TEST_FIXTURE_SCOPE_END() CAF_TEST_FIXTURE_SCOPE_END()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment