2#ifndef LIBSBX_MATH_UUID_HPP_
3#define LIBSBX_MATH_UUID_HPP_
5#if defined(SBX_MATH_UUID_USE_V4)
11#include <fmt/format.h>
13#include <range/v3/all.hpp>
17#include <libsbx/math/random.hpp>
21struct invalid_uuid_exception : std::runtime_error {
23 invalid_uuid_exception(std::string_view uuid)
24 : std::runtime_error{fmt::format(
"String '{}' is not a valid uuid", uuid)} { }
30 friend struct fmt::formatter<sbx::math::uuid>;
31 friend struct std::hash<sbx::math::uuid>;
33 struct null_uui_tag { };
39 using value_type = std::uint64_t;
42 : _value{random::next<value_type>()} {
80 static auto null() -> uuid {
81 return uuid{null_uui_tag{}};
84 auto operator==(
const uuid& other)
const noexcept ->
bool {
85 return _value == other._value;
96 operator value_type()
const {
113struct fmt::formatter<sbx::math::uuid> {
115 template<
typename ParseContext>
116 constexpr auto parse(ParseContext& context) ->
decltype(context.begin()) {
117 return context.begin();
120 template<
typename FormatContext>
121 auto format(
const sbx::math::uuid& uuid, FormatContext& context) ->
decltype(context.out()) {
122 return fmt::format_to(context.out(),
"{}",
static_cast<sbx::math::uuid::value_type
>(uuid));
142struct std::hash<sbx::math::uuid> {
143 auto operator()(
const sbx::math::uuid& uuid)
const noexcept -> std::size_t {
144 return static_cast<sbx::math::uuid::value_type
>(uuid);
153#include <fmt/format.h>
155#include <libsbx/math/random.hpp>
159template<std::
unsigned_
integral Type>
164 using value_type = Type;
167 : _value{random::next<value_type>()} { }
173 static constexpr auto from_value(
const value_type value) ->
basic_uuid {
177 static constexpr auto create() ->
basic_uuid {
178 return basic_uuid{random::next<value_type>()};
181 constexpr auto operator==(
const basic_uuid& other)
const noexcept ->
bool {
182 return _value == other._value;
185 constexpr auto operator<(
const basic_uuid& other)
const noexcept ->
bool {
186 return _value < other._value;
189 constexpr auto value()
const noexcept -> value_type {
206template<std::
unsigned_
integral Type>
207struct fmt::formatter<sbx::math::basic_uuid<Type>> {
209 template<
typename ParseContext>
210 constexpr auto parse(ParseContext& context) ->
decltype(context.begin()) {
211 return context.begin();
214 template<
typename FormatContext>
216 static constexpr auto width =
sizeof(Type) * 2;
218 return fmt::format_to(context.out(),
"{:0{}x}", uuid.value(), width);
223template<std::
unsigned_
integral Type>
224struct std::hash<sbx::math::basic_uuid<Type>> {