Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
uuid.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <random>
9#include <cstdio>
10
14namespace uuid_util {
15 union uuid_t {
16 std::uint8_t b8[16];
17 std::uint16_t b16[8];
18 std::uint32_t b32[4];
19 std::uint64_t b64[2];
20
21 static uuid_t generate(std::default_random_engine &engine) {
22 std::uniform_int_distribution<std::uint8_t> dist(0, std::numeric_limits<std::uint8_t>::max());
23
24 uuid_t buf;
25 for (auto &el : buf.b8) {
26 el = dist(engine);
27 }
28
29 buf.b8[7] &= (std::uint8_t) 0b00101111;
30 buf.b8[9] &= (std::uint8_t) 0b10011111;
31
32 return buf;
33 }
34
35 static uuid_t generate() {
36 std::random_device r;
37
38 std::default_random_engine engine {r()};
39
40 return generate(engine);
41 }
42
43 static uuid_t
44 parse(std::string& uuid_str) {
45 if (uuid_str.length() != 36) {
46 throw std::invalid_argument("Invalid UUID string length");
47 }
48
49 uuid_t uuid;
50 unsigned int temp16_1;
51 unsigned int temp32_1, temp32_2;
52
53 // Parse UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
54 unsigned int data1, data2;
55 std::sscanf(
56 uuid_str.c_str(), "%8x-%4x-%4x-%4x-%8x%4x",
57 &uuid.b32[0], &data1, &data2, &temp16_1, &temp32_1, &temp32_2
58 );
59
60 // Assign parsed values into uuid_t structure
61 uuid.b16[2] = static_cast<std::uint16_t>(data1);
62 uuid.b16[3] = static_cast<std::uint16_t>(data2);
63
64 // Manually splitting the last segments into bytes
65 uuid.b8[8] = (temp16_1 >> 8) & 0xFF;
66 uuid.b8[9] = temp16_1 & 0xFF;
67 uuid.b8[10] = (temp32_1 >> 24) & 0xFF;
68 uuid.b8[11] = (temp32_1 >> 16) & 0xFF;
69 uuid.b8[12] = (temp32_1 >> 8) & 0xFF;
70 uuid.b8[13] = temp32_1 & 0xFF;
71 uuid.b8[14] = (temp32_2 >> 8) & 0xFF;
72 uuid.b8[15] = temp32_2 & 0xFF;
73
74 return uuid;
75 }
76
77 [[nodiscard]] std::string string() const {
78 std::string result;
79
80 result.reserve(sizeof(uuid_t) * 2 + 4);
81
82 auto hex = util::hex(*this, true);
83 auto hex_view = hex.to_string_view();
84
85 std::string_view slices[] = {
86 hex_view.substr(0, 8),
87 hex_view.substr(8, 4),
88 hex_view.substr(12, 4),
89 hex_view.substr(16, 4)
90 };
91 auto last_slice = hex_view.substr(20, 12);
92
93 for (auto &slice : slices) {
94 std::copy(std::begin(slice), std::end(slice), std::back_inserter(result));
95
96 result.push_back('-');
97 }
98
99 std::copy(std::begin(last_slice), std::end(last_slice), std::back_inserter(result));
100
101 return result;
102 }
103
104 constexpr bool operator==(const uuid_t &other) const {
105 return b64[0] == other.b64[0] && b64[1] == other.b64[1];
106 }
107
108 constexpr bool operator<(const uuid_t &other) const {
109 return (b64[0] < other.b64[0] || (b64[0] == other.b64[0] && b64[1] < other.b64[1]));
110 }
111
112 constexpr bool operator>(const uuid_t &other) const {
113 return (b64[0] > other.b64[0] || (b64[0] == other.b64[0] && b64[1] > other.b64[1]));
114 }
115 };
116} // namespace uuid_util
UUID utilities.
Definition uuid.h:14