GlistEngine
Loading...
Searching...
No Matches
gUUID.h
Go to the documentation of this file.
1//
2// Created by Metehan Gezer on 14/09/2025.
3//
4
5#ifndef UTILS_GUUID_H
6#define UTILS_GUUID_H
7
8#include <array>
9#include <cstdint>
10#include <string>
11
12class gUUID {
13public:
14 using bytes = std::array<std::uint8_t, 16>;
15
16 gUUID() noexcept;
17 explicit gUUID(const bytes& b) noexcept;
18
19 static gUUID generateV4();
20
21 static gUUID fromString(std::string s, bool* ok = nullptr) noexcept;
22
23 std::string toString() const;
24
25 bytes toBytes() const noexcept;
26
27 bool isNil() const noexcept { return hi == 0 && lo == 0; }
28
29 friend bool operator==(const gUUID& a, const gUUID& b) noexcept {
30 return a.hi == b.hi && a.lo == b.lo;
31 }
32
33 friend bool operator!=(const gUUID& a, const gUUID& b) noexcept {
34 return !(a == b);
35 }
36
37 friend bool operator<(const gUUID& a, const gUUID& b) noexcept {
38 return a.hi < b.hi || (a.hi == b.hi && a.lo < b.lo);
39 }
40
41
42 std::uint64_t getHigh() const noexcept { return hi; }
43
44 std::uint64_t getLow() const noexcept { return lo; }
45
46private:
47 std::uint64_t hi;
48 std::uint64_t lo;
49
50 void fromBytes(const bytes& b) noexcept;
51
52};
53
54
55// hashing support
56namespace std {
57template<> struct hash<gUUID> {
58 size_t operator()(const gUUID& u) const noexcept {
59 // 64-bit mix (xorshift64*)
60 std::uint64_t x = u.getHigh() ^ (u.getLow() * 0x9E3779B97F4A7C15ull);
61 x ^= x >> 30; x *= 0xBF58476D1CE4E5B9ull;
62 x ^= x >> 27; x *= 0x94D049BB133111EBull;
63 x ^= x >> 31;
64 return x;
65 }
66};
67}
68
69#endif//UTILS_GUUID_H
Definition gUUID.h:12
bytes toBytes() const noexcept
bool isNil() const noexcept
Definition gUUID.h:27
friend bool operator<(const gUUID &a, const gUUID &b) noexcept
Definition gUUID.h:37
std::array< std::uint8_t, 16 > bytes
Definition gUUID.h:14
friend bool operator!=(const gUUID &a, const gUUID &b) noexcept
Definition gUUID.h:33
gUUID() noexcept
std::uint64_t getLow() const noexcept
Definition gUUID.h:44
std::uint64_t getHigh() const noexcept
Definition gUUID.h:42
std::string toString() const
static gUUID generateV4()
static gUUID fromString(std::string s, bool *ok=nullptr) noexcept
friend bool operator==(const gUUID &a, const gUUID &b) noexcept
Definition gUUID.h:29
float a
Definition gColor.h:22
float b
Definition gColor.h:22
Definition gUUID.h:56