forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUUID.cpp
More file actions
94 lines (87 loc) · 1.88 KB
/
Copy pathUUID.cpp
File metadata and controls
94 lines (87 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "PCH.h"
#include "UUID.h"
#include "Util.h"
#include <charconv>
namespace sh::core
{
SH_CORE_API UUID::UUID(std::string_view str)
{
if (str.size() == uuid.size() * 8)
{
try
{
for (int i = 0; i < str.size(); i += 8)
{
std::string sub{ str.substr(i, 8) };
uuid[i / 8] = std::stoul(sub, nullptr, 16);
}
}
catch (const std::exception& e)
{
this->operator=(Generate());
}
}
else
{
this->operator=(Generate());
}
}
SH_CORE_API UUID::UUID(const UUID& other) noexcept
{
for (int i = 0; i < uuid.size(); ++i)
uuid[i] = other.uuid[i];
}
SH_CORE_API auto UUID::operator=(const UUID& other) noexcept -> UUID&
{
for (int i = 0; i < uuid.size(); ++i)
uuid[i] = other.uuid[i];
return *this;
}
SH_CORE_API bool UUID::operator==(const UUID& other) const noexcept
{
for (int i = 0; i < uuid.size(); ++i)
{
if (uuid[i] != other.uuid[i])
return false;
}
return true;
}
SH_CORE_API bool UUID::operator!=(const UUID& other) const noexcept
{
for (int i = 0; i < uuid.size(); ++i)
{
if (uuid[i] != other.uuid[i])
return true;
}
return false;
}
SH_CORE_API auto UUID::Generate() -> UUID
{
static std::stack<UUID> uuids;
if (uuids.empty())
{
for (int i = 0; i < CHACHE_SIZE; ++i)
{
UUID uuid{};
for (int i = 0; i < 4; ++i)
uuid.uuid[i] = Util::RandomRange(static_cast<uint32_t>(0), std::numeric_limits<uint32_t>::max());
uuids.push(std::move(uuid));
}
}
UUID uuid = uuids.top();
uuids.pop();
return uuid;
}
SH_CORE_API auto UUID::ToString() const -> std::string
{
std::string result{};
result.reserve(32);
for (int i = 0; i < 4; ++i)
{
std::array<char, 9> buffer{ '0','0','0','0','0','0','0','0','\0' };
std::to_chars(buffer.data(), buffer.data() + 8, uuid[i], 16);
result += std::string{ buffer.data(), 8 };
}
return result;
}
}//namespace