[ox/std] Add hash function for UUID
This commit is contained in:
parent
b089bf460b
commit
b66f61c217
26
deps/ox/src/ox/std/uuid.hpp
vendored
26
deps/ox/src/ox/std/uuid.hpp
vendored
@ -8,9 +8,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "bit.hpp"
|
||||||
#include "ignore.hpp"
|
#include "ignore.hpp"
|
||||||
#include "istring.hpp"
|
#include "istring.hpp"
|
||||||
#include "buffer.hpp"
|
#include "buffer.hpp"
|
||||||
|
#include "hash.hpp"
|
||||||
#include "random.hpp"
|
#include "random.hpp"
|
||||||
#include "ranges.hpp"
|
#include "ranges.hpp"
|
||||||
#include "stringview.hpp"
|
#include "stringview.hpp"
|
||||||
@ -105,7 +107,7 @@ class UUID {
|
|||||||
static ox::Result<UUID> generate() noexcept;
|
static ox::Result<UUID> generate() noexcept;
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr auto value() const noexcept {
|
constexpr auto const&value() const noexcept {
|
||||||
return m_value;
|
return m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,8 +120,8 @@ class UUID {
|
|||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
constexpr uint64_t zero = 0;
|
constexpr uint64_t zero = 0;
|
||||||
return ox::memcmp(&zero, m_value.data() + 0, 8) == 0
|
return memcmp(&zero, m_value.data() + 0, 8) == 0
|
||||||
&& ox::memcmp(&zero, m_value.data() + 8, 8) == 0;
|
&& memcmp(&zero, m_value.data() + 8, 8) == 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -192,6 +194,24 @@ class UUID {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct hash<ox::UUID> {
|
||||||
|
[[nodiscard]]
|
||||||
|
constexpr size_t operator()(ox::UUID v) const noexcept {
|
||||||
|
size_t out{};
|
||||||
|
if (std::is_constant_evaluated()) {
|
||||||
|
for (auto i = 0u; i < sizeof(out); ++i) {
|
||||||
|
out |= static_cast<size_t>(v.value()[i]) << (i * 8);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
memcpy(&out, &v, sizeof(out));
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr Error model(T *io, ox::CommonPtrWith<UUID> auto *obj) noexcept {
|
constexpr Error model(T *io, ox::CommonPtrWith<UUID> auto *obj) noexcept {
|
||||||
oxReturnError(io->template setTypeInfo<UUID>());
|
oxReturnError(io->template setTypeInfo<UUID>());
|
||||||
|
Loading…
Reference in New Issue
Block a user