Sunshine/src/uuid.h

84 lines
1.9 KiB
C
Raw Normal View History

// Created by loki on 8-2-19.
#ifndef T_MAN_UUID_H
#define T_MAN_UUID_H
#include <random>
2023-03-07 20:26:03 -05:00
namespace uuid_util {
2023-03-27 21:45:29 -04:00
union uuid_t {
std::uint8_t b8[16];
std::uint16_t b16[8];
std::uint32_t b32[4];
std::uint64_t b64[2];
2023-03-27 21:45:29 -04:00
static uuid_t
generate(std::default_random_engine &engine) {
std::uniform_int_distribution<std::uint8_t> dist(0, std::numeric_limits<std::uint8_t>::max());
2023-03-27 21:45:29 -04:00
uuid_t buf;
for (auto &el : buf.b8) {
el = dist(engine);
}
2023-03-27 21:45:29 -04:00
buf.b8[7] &= (std::uint8_t) 0b00101111;
buf.b8[9] &= (std::uint8_t) 0b10011111;
2023-03-27 21:45:29 -04:00
return buf;
}
2023-03-27 21:45:29 -04:00
static uuid_t
generate() {
std::random_device r;
2023-03-27 21:45:29 -04:00
std::default_random_engine engine { r() };
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
return generate(engine);
}
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
[[nodiscard]] std::string
string() const {
std::string result;
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
result.reserve(sizeof(uuid_t) * 2 + 4);
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
auto hex = util::hex(*this, true);
auto hex_view = hex.to_string_view();
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
std::string_view slices[] = {
hex_view.substr(0, 8),
hex_view.substr(8, 4),
hex_view.substr(12, 4),
hex_view.substr(16, 4)
};
auto last_slice = hex_view.substr(20, 12);
for (auto &slice : slices) {
std::copy(std::begin(slice), std::end(slice), std::back_inserter(result));
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
result.push_back('-');
}
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
std::copy(std::begin(last_slice), std::end(last_slice), std::back_inserter(result));
2020-01-20 23:08:44 +01:00
2023-03-27 21:45:29 -04:00
return result;
}
2023-03-27 21:45:29 -04:00
constexpr bool
operator==(const uuid_t &other) const {
return b64[0] == other.b64[0] && b64[1] == other.b64[1];
}
2023-03-27 21:45:29 -04:00
constexpr bool
operator<(const uuid_t &other) const {
return (b64[0] < other.b64[0] || (b64[0] == other.b64[0] && b64[1] < other.b64[1]));
}
constexpr bool
operator>(const uuid_t &other) const {
return (b64[0] > other.b64[0] || (b64[0] == other.b64[0] && b64[1] > other.b64[1]));
}
};
} // namespace uuid_util
#endif // T_MAN_UUID_H