52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
#ifndef MATADOR_UUID_HPP
|
|
#define MATADOR_UUID_HPP
|
|
|
|
#include <array>
|
|
#include <string>
|
|
#include <cstdint>
|
|
|
|
namespace matador::utils {
|
|
|
|
class uuid {
|
|
public:
|
|
using uuid_array = std::array<uint32_t, 4>;
|
|
|
|
// Default constructor initializes to zero
|
|
uuid() = default;
|
|
|
|
// Static UUID v4 generator
|
|
static uuid generate();
|
|
|
|
// Returns UUID as a string
|
|
[[nodiscard]] std::string str() const;
|
|
|
|
// Access to internal data
|
|
[[nodiscard]] const uuid_array& data() const;
|
|
|
|
// Reset UUID to zero
|
|
void clear() { _data.fill(0); }
|
|
|
|
// Comparisons
|
|
friend bool operator==(const uuid& lhs, const uuid& rhs);
|
|
friend bool operator!=(const uuid& lhs, const uuid& rhs);
|
|
friend bool operator<(const uuid& lhs, const uuid& rhs);
|
|
|
|
private:
|
|
uuid_array _data{0, 0, 0, 0};
|
|
};
|
|
}
|
|
|
|
// Hash specialization to allow use in unordered containers
|
|
template <>
|
|
struct std::hash<matador::utils::uuid> {
|
|
std::size_t operator()(const matador::utils::uuid& u) const noexcept {
|
|
std::size_t h = 0;
|
|
for (const uint32_t val : u.data()) {
|
|
h ^= std::hash<uint32_t>{}(val) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
|
}
|
|
return h;
|
|
}
|
|
};
|
|
|
|
#endif //MATADOR_UUID_HPP
|