1#ifndef LIBSBX_MATH_UUID_HPP_
2#define LIBSBX_MATH_UUID_HPP_
8#include <range/v3/all.hpp>
12#include <libsbx/math/random.hpp>
19 friend struct std::hash<sbx::math::uuid>;
23 static const
uuid null;
29 for (
auto&
byte : _bytes) {
30 byte = random::next<std::uint8_t>();
36 _bytes[6] = 0x40 | (_bytes[6] & 0xf);
37 _bytes[8] = 0x80 | (_bytes[8] & 0x3f);
42 auto operator==(
const uuid& other)
const noexcept ->
bool {
43 return std::ranges::equal(_bytes, other._bytes);
48 uuid(std::array<std::uint8_t, 16u>&& bytes)
49 : _bytes{std::move(bytes)} { }
51 std::array<std::uint8_t, 16u> _bytes;
58struct fmt::formatter<sbx::math::uuid> {
60 template<
typename ParseContext>
61 constexpr auto parse(ParseContext& context) ->
decltype(context.begin()) {
62 return context.begin();
65 template<
typename FormatContext>
66 auto format(
const sbx::math::uuid& uuid, FormatContext& context) ->
decltype(context.out()) {
67 for (
const auto [i,
byte] : ranges::views::enumerate(uuid._bytes)) {
68 fmt::format_to(context.out(),
"{:02x}",
byte);
70 if (i == 3 || i == 5 || i == 7 || i == 9) {
71 fmt::format_to(context.out(),
"-");
80struct std::hash<sbx::math::uuid> {
81 auto operator()(
const sbx::math::uuid& uuid)
const noexcept -> std::size_t {
82 auto seed = std::size_t{0};
84 for (
const auto&
byte : uuid._bytes) {
85 sbx::utility::hash_combine(seed,
byte);