Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <array>
9#include <cstddef>
10#include <random>
11
15namespace uuid_util {
19 union uuid_t {
20 std::array<std::uint8_t, 16> b8;
21 std::array<std::uint16_t, 8> b16;
22 std::array<std::uint32_t, 4> b32;
23 std::array<std::uint64_t, 2> b64;
24
31 static uuid_t generate(std::default_random_engine &engine) {
32 std::uniform_int_distribution<std::uint8_t> dist(0, std::numeric_limits<std::uint8_t>::max());
33
34 uuid_t buf;
35 for (auto &el : buf.b8) {
36 el = dist(engine);
37 }
38
39 buf.b8[7] = std::to_integer<std::uint8_t>(std::byte {buf.b8[7]} & std::byte {0b00101111});
40 buf.b8[9] = std::to_integer<std::uint8_t>(std::byte {buf.b8[9]} & std::byte {0b10011111});
41
42 return buf;
43 }
44
50 static uuid_t generate() {
51 std::random_device r;
52
53 std::default_random_engine engine {r()};
54
55 return generate(engine);
56 }
57
63 [[nodiscard]] std::string string() const {
64 std::string result;
65
66 result.reserve(sizeof(uuid_t) * 2 + 4);
67
68 auto hex = util::hex(*this, true);
69 auto hex_view = hex.to_string_view();
70
71 std::array<std::string_view, 4> slices = {
72 hex_view.substr(0, 8),
73 hex_view.substr(8, 4),
74 hex_view.substr(12, 4),
75 hex_view.substr(16, 4)
76 };
77 auto last_slice = hex_view.substr(20, 12);
78
79 for (auto &slice : slices) {
80 std::ranges::copy(slice, std::back_inserter(result));
81
82 result.push_back('-');
83 }
84
85 std::ranges::copy(last_slice, std::back_inserter(result));
86
87 return result;
88 }
89
96 constexpr bool operator==(const uuid_t &other) const {
97 return b64[0] == other.b64[0] && b64[1] == other.b64[1];
98 }
99
106 constexpr bool operator<(const uuid_t &other) const {
107 return (b64[0] < other.b64[0] || (b64[0] == other.b64[0] && b64[1] < other.b64[1]));
108 }
109
116 constexpr bool operator>(const uuid_t &other) const {
117 return (b64[0] > other.b64[0] || (b64[0] == other.b64[0] && b64[1] > other.b64[1]));
118 }
119 };
120} // namespace uuid_util
UUID utilities.
Definition uuid.h:15
UUID value exposed through multiple integer views.
Definition uuid.h:19
std::string string() const
Format the UUID using canonical text form.
Definition uuid.h:63
constexpr bool operator<(const uuid_t &other) const
Order UUID values by their 64-bit word representation.
Definition uuid.h:106
std::array< std::uint32_t, 4 > b32
UUID viewed as 32-bit words.
Definition uuid.h:22
std::array< std::uint8_t, 16 > b8
UUID bytes.
Definition uuid.h:20
static uuid_t generate()
Generate a UUID value.
Definition uuid.h:50
constexpr bool operator>(const uuid_t &other) const
Order UUID values by their 64-bit word representation.
Definition uuid.h:116
std::array< std::uint64_t, 2 > b64
UUID viewed as 64-bit words.
Definition uuid.h:23
std::array< std::uint16_t, 8 > b16
UUID viewed as 16-bit words.
Definition uuid.h:21
static uuid_t generate(std::default_random_engine &engine)
Generate UUID bytes and apply the established version and variant masks.
Definition uuid.h:31
constexpr bool operator==(const uuid_t &other) const
Compare two UUID values for equality.
Definition uuid.h:96
Hex< T > hex(const T &elem, bool rev=false)
Serialize an element as hexadecimal text.
Definition utility.h:476