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());
24 for (
auto &el : buf.b8) {
28 buf.b8[7] &= (std::uint8_t) 0b00101111;
29 buf.b8[9] &= (std::uint8_t) 0b10011111;
37 std::default_random_engine engine {r()};
39 return generate(engine);
42 [[nodiscard]] std::string string()
const {
45 result.reserve(
sizeof(
uuid_t) * 2 + 4);
47 auto hex = util::hex(*
this,
true);
48 auto hex_view = hex.to_string_view();
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)
56 auto last_slice = hex_view.substr(20, 12);
58 for (
auto &slice : slices) {
59 std::copy(std::begin(slice), std::end(slice), std::back_inserter(result));
61 result.push_back(
'-');
64 std::copy(std::begin(last_slice), std::end(last_slice), std::back_inserter(result));
69 constexpr bool operator==(
const uuid_t &other)
const {
70 return b64[0] == other.b64[0] && b64[1] == other.b64[1];
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]));
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]));