Sunshine v2025.118.151840
Self-hosted game stream host for Moonlight.
uuid.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <random>
8
12namespace uuid_util {
13 union uuid_t {
14 std::uint8_t b8[16];
15 std::uint16_t b16[8];
16 std::uint32_t b32[4];
17 std::uint64_t b64[2];
18
19 static uuid_t
20 generate(std::default_random_engine &engine) {
21 std::uniform_int_distribution<std::uint8_t> dist(0, std::numeric_limits<std::uint8_t>::max());
22
23 uuid_t buf;
24 for (auto &el : buf.b8) {
25 el = dist(engine);
26 }
27
28 buf.b8[7] &= (std::uint8_t) 0b00101111;
29 buf.b8[9] &= (std::uint8_t) 0b10011111;
30
31 return buf;
32 }
33
34 static uuid_t
35 generate() {
36 std::random_device r;
37
38 std::default_random_engine engine { r() };
39
40 return generate(engine);
41 }
42
43 [[nodiscard]] std::string
44 string() const {
45 std::string result;
46
47 result.reserve(sizeof(uuid_t) * 2 + 4);
48
49 auto hex = util::hex(*this, true);
50 auto hex_view = hex.to_string_view();
51
52 std::string_view slices[] = {
53 hex_view.substr(0, 8),
54 hex_view.substr(8, 4),
55 hex_view.substr(12, 4),
56 hex_view.substr(16, 4)
57 };
58 auto last_slice = hex_view.substr(20, 12);
59
60 for (auto &slice : slices) {
61 std::copy(std::begin(slice), std::end(slice), std::back_inserter(result));
62
63 result.push_back('-');
64 }
65
66 std::copy(std::begin(last_slice), std::end(last_slice), std::back_inserter(result));
67
68 return result;
69 }
70
71 constexpr bool
72 operator==(const uuid_t &other) const {
73 return b64[0] == other.b64[0] && b64[1] == other.b64[1];
74 }
75
76 constexpr bool
77 operator<(const uuid_t &other) const {
78 return (b64[0] < other.b64[0] || (b64[0] == other.b64[0] && b64[1] < other.b64[1]));
79 }
80
81 constexpr bool
82 operator>(const uuid_t &other) const {
83 return (b64[0] > other.b64[0] || (b64[0] == other.b64[0] && b64[1] > other.b64[1]));
84 }
85 };
86} // namespace uuid_util
UUID utilities.
Definition uuid.h:12
Definition uuid.h:13