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
Guid.cpp
Go to the documentation of this file.
1
7
8#include <sstream>
9
11
12#include "Guid.hpp"
13
14static constexpr const char* const s_chars {"abcdef"};
15
16namespace galaxy
17{
18 namespace utils
19 {
21 : m_id {""}
22 {
23 math::Random rng;
24
25 m_id += s_chars[static_cast<int>(std::floor(rng.gen<float>(0.0f, 1.0f) * 6))];
26
27 for (auto i = 1; i < 32; i++)
28 {
29 const auto random = static_cast<int>(rng.gen<float>(0.0f, 1.0f) * 16);
30
31 if (i == 8 || i == 12 || i == 16 || i == 20)
32 {
33 m_id += '-';
34 }
35
36 std::stringstream hex;
37 hex << std::hex << (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random));
38
39 m_id += hex.str();
40 }
41 }
42
43 Guid::Guid(Guid&& g) noexcept
44 {
45 this->m_id = std::move(g.m_id);
46 }
47
48 Guid& Guid::operator=(Guid&& g) noexcept
49 {
50 if (this != &g)
51 {
52 this->m_id = std::move(g.m_id);
53 }
54
55 return *this;
56 }
57
58 Guid::Guid(const Guid& g) noexcept
59 {
60 this->m_id = g.m_id;
61 }
62
63 Guid& Guid::operator=(const Guid& g) noexcept
64 {
65 if (this != &g)
66 {
67 this->m_id = g.m_id;
68 }
69
70 return *this;
71 }
72
73 Guid::~Guid() noexcept
74 {
75 }
76
77 const std::string& Guid::to_string() const noexcept
78 {
79 return m_id;
80 }
81
82 bool Guid::operator==(const Guid& rhs) noexcept
83 {
84 return m_id == rhs.m_id;
85 }
86
87 bool Guid::operator!=(const Guid& rhs) noexcept
88 {
89 return !operator==(rhs);
90 }
91 } // namespace utils
92} // namespace galaxy
static constexpr const char *const s_chars
Guid.cpp galaxy.
Definition Guid.cpp:14
Generates random numbers.
Definition Random.hpp:37
T gen(const T min, const T max) noexcept
Generate a random number of type T.
Definition Random.hpp:105
Contains a 128bit randomly generated GUID, along with helper functions.
Definition Guid.hpp:22
bool operator!=(const Guid &rhs) noexcept
Inequality comparison.
Definition Guid.cpp:87
Guid()
Constructor.
Definition Guid.cpp:20
Guid & operator=(Guid &&) noexcept
Move assignment operator.
Definition Guid.cpp:48
const std::string & to_string() const noexcept
Get the GUID as a string.
Definition Guid.cpp:77
~Guid() noexcept
Destructor.
Definition Guid.cpp:73
std::string m_id
Guid.
Definition Guid.hpp:88
bool operator==(const Guid &rhs) noexcept
Equality comparison.
Definition Guid.cpp:82
Timer.hpp galaxy.
Definition Async.hpp:17