Sunshine latest
Self-hosted game stream host for Moonlight.
uuid.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <random>
9
13namespace uuid_util {
14 union uuid_t {
15 std::uint8_t b8[16];
16 std::uint16_t b16[8];
17 std::uint32_t b32[4];
18 std::uint64_t b64[2];
19
20 static uuid_t 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 generate() {
35 std::random_device r;
36
37 std::default_random_engine engine {r()};
38
39 return generate(engine);
40 }
41
42 [[nodiscard]] std::string string() const {
43 std::string result;
44
45 result.reserve(sizeof(uuid_t) * 2 + 4);
46
47 auto hex = util::hex(*this, true);
48 auto hex_view = hex.to_string_view();
49
50 std::string_view slices[] = {
51 hex_view.substr(0, 8),
52 hex_view.substr(8, 4),
53 hex_view.substr(12, 4),
54 hex_view.substr(16, 4)
55 };
56 auto last_slice = hex_view.substr(20, 12);
57
58 for (auto &slice : slices) {
59 std::copy(std::begin(slice), std::end(slice), std::back_inserter(result));
60
61 result.push_back('-');
62 }
63
64 std::copy(std::begin(last_slice), std::end(last_slice), std::back_inserter(result));
65
66 return result;
67 }
68
69 constexpr bool operator==(const uuid_t &other) const {
70 return b64[0] == other.b64[0] && b64[1] == other.b64[1];
71 }
72
73 constexpr bool operator<(const uuid_t &other) const {
74 return (b64[0] < other.b64[0] || (b64[0] == other.b64[0] && b64[1] < other.b64[1]));
75 }
76
77 constexpr bool 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} // namespace uuid_util
UUID utilities.
Definition uuid.h:13
Definition uuid.h:14