-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUUID.cpp
More file actions
105 lines (104 loc) · 2.53 KB
/
Copy pathUUID.cpp
File metadata and controls
105 lines (104 loc) · 2.53 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
95
96
97
98
99
100
101
102
103
104
105
#include "UUID.h"
#include <charconv>
#include <algorithm>
namespace sh::core
{
SH_CORE_API UUID::UUID(std::string_view str)
{
// 32글자여야 함
if (str.size() != uuid.size() * 8)
{
this->operator=(Generate());
return;
}
for (int i = 0; i < str.size(); i += 8)
{
const std::string_view sub{ str.substr(i, 8) };
uint32_t value{};
auto [ptr, ec] = std::from_chars(sub.data(), sub.data() + sub.size(), value, 16);
if (ec == std::errc{})
uuid[i / 8] = value;
else
{
this->operator=(GenerateEmptyUUID());
return;
}
}
}
UUID::UUID(const std::array<uint32_t, 4>& uuid)
{
this->uuid = uuid;
}
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 auto UUID::operator==(const UUID& other) const noexcept -> bool
{
for (int i = 0; i < uuid.size(); ++i)
{
if (uuid[i] != other.uuid[i])
return false;
}
return true;
}
SH_CORE_API auto UUID::operator==(std::string_view str) const noexcept -> bool
{
if (str.size() != 32)
return false;
return operator==(core::UUID{ str });
}
SH_CORE_API auto UUID::operator!=(const UUID& other) const noexcept -> bool
{
return !operator==(other);
}
SH_CORE_API auto UUID::operator!=(std::string_view str) const noexcept -> bool
{
return !operator==(str);
}
SH_CORE_API auto UUID::Generate() -> UUID
{
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());
return uuid;
}
SH_CORE_API auto UUID::GenerateEmptyUUID() -> UUID
{
return UUID{ std::array<uint32_t, 4> { 0, 0, 0, 0 } };
}
SH_CORE_API auto UUID::ToString() const -> std::string
{
std::string uuidStr(32, '0');
for (int i = 0; i < 4; ++i)
{
std::array<char, 9> buffer{ '0','0','0','0','0','0','0','0','\0' };
auto [ptr, err] = std::to_chars(buffer.data(), buffer.data() + 8, uuid[i], 16);
const std::size_t len = ptr - buffer.data();
// ex) ab가 나왔다면, 000000ab로 저장해야한다.
for (int j = 0; j < len; ++j)
uuidStr[i * 8 + 8 - len + j] = buffer[j];
}
return uuidStr;
}
SH_CORE_API auto UUID::GetRawData() const -> const std::array<uint32_t, 4>&
{
return uuid;
}
SH_CORE_API auto UUID::IsEmpty() const -> bool
{
for (uint32_t i : uuid)
{
if (i != 0)
return false;
}
return true;
}
}//namespace