galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
UUID.cpp
Go to the documentation of this file.
1
7
8#include <iomanip>
9#include <random>
10#include <sstream>
11
12#include "UUID.hpp"
13
14namespace galaxy
15{
17 {
18 std::mt19937_64 engine {std::random_device()()};
19 std::uniform_int_distribution<std::uint64_t> dist {0, 255};
20
21 for (auto index = 0; index < 16; ++index)
22 {
23 m_uuid[index] = static_cast<unsigned char>(dist(engine));
24 }
25
26 m_uuid[6] = ((m_uuid[6] & 0x0f) | 0x40); // Version 4
27 m_uuid[8] = ((m_uuid[8] & 0x3f) | 0x80); // Variant is 10
28
29 std::stringstream hex;
30 hex << std::hex << std::setfill('0');
31
32 for (int i = 0; i < 16; ++i)
33 {
34 hex << std::setw(2) << static_cast<int>(m_uuid[i]);
35
36 if (i == 3 || i == 5 || i == 7 || i == 9)
37 {
38 hex << '-';
39 }
40 }
41
42 m_str = hex.str();
43 }
44
45 UUID::UUID(UUID&& g) noexcept
46 {
47 this->m_uuid = std::move(g.m_uuid);
48 this->m_str = std::move(g.m_str);
49 }
50
51 UUID& UUID::operator=(UUID&& g) noexcept
52 {
53 if (this != &g)
54 {
55 this->m_uuid = std::move(g.m_uuid);
56 this->m_str = std::move(g.m_str);
57 }
58
59 return *this;
60 }
61
62 UUID::UUID(const UUID& g) noexcept
63 {
64 this->m_uuid = g.m_uuid;
65 this->m_str = g.m_str;
66 }
67
68 UUID& UUID::operator=(const UUID& g) noexcept
69 {
70 if (this != &g)
71 {
72 this->m_uuid = g.m_uuid;
73 this->m_str = g.m_str;
74 }
75
76 return *this;
77 }
78
79 UUID::~UUID() noexcept
80 {
81 }
82
83 std::size_t UUID::hash() noexcept
84 {
85 std::size_t h = 0;
86 for (auto b : m_uuid)
87 {
88 h ^= static_cast<std::size_t>(b) + 0x9e3779b9 + (h << 6) + (h >> 2);
89 }
90 return h;
91 }
92
93 const std::string& UUID::str() const noexcept
94 {
95 return m_str;
96 }
97
98 bool UUID::operator==(const UUID& rhs) noexcept
99 {
100 return m_uuid == rhs.m_uuid;
101 }
102
103 bool UUID::operator!=(const UUID& rhs) noexcept
104 {
105 return !operator==(rhs);
106 }
107} // namespace galaxy
Contains a 128bit randomly generated UUID, along with helper functions.
Definition UUID.hpp:21
std::array< unsigned char, 16 > m_uuid
UUID.
Definition UUID.hpp:95
UUID()
Constructor.
Definition UUID.cpp:16
bool operator==(const UUID &rhs) noexcept
Equality comparison.
Definition UUID.cpp:98
const std::string & str() const noexcept
Get the UUID as a string.
Definition UUID.cpp:93
bool operator!=(const UUID &rhs) noexcept
Inequality comparison.
Definition UUID.cpp:103
std::string m_str
String representation.
Definition UUID.hpp:100
UUID & operator=(UUID &&) noexcept
Move assignment operator.
Definition UUID.cpp:51
~UUID() noexcept
Destructor.
Definition UUID.cpp:79
std::size_t hash() noexcept
Get the UUID as a hash.
Definition UUID.cpp:83
Animated.cpp galaxy.
Definition Animated.cpp:16