LeviLamina
Loading...
Searching...
No Matches
UUID.h
1#pragma once
2
3#include "ll/api/Expected.h"
4#include "mc/_HeaderOutputPredefine.h"
5
6namespace mce {
7
8class UUID {
9public:
10 uint64 a, b;
11
12 [[nodiscard]] constexpr UUID(uint64 a = 0, uint64 b = 0) noexcept : a(a), b(b) {}
13
14 [[nodiscard]] inline explicit UUID(std::string const& uuidStr) : UUID(fromString(uuidStr)) {}
15
16 [[nodiscard]] inline explicit UUID(std::string_view uuidStr) : UUID(fromString(std::string{uuidStr})) {}
17
18 LLNDAPI static mce::UUID random();
19
20 [[nodiscard]] inline static mce::UUID fromStringHash(std::string_view sv) {
21 return {ll::hash_utils::doHash(sv), ll::hash_utils::doHash2(sv)};
22 }
23
24 [[nodiscard]] inline explicit operator bool() const { return a != 0 && b != 0; }
25
26 [[nodiscard]] inline explicit operator std::string() const { return asString(); }
27
28 [[nodiscard]] constexpr bool operator==(UUID const& other) const { return (a == other.a) && (b == other.b); }
29
30 [[nodiscard]] constexpr std::strong_ordering operator<=>(UUID const& other) const {
31 if (a != other.a) {
32 return a <=> other.a;
33 }
34 return b <=> other.b;
35 }
36
37 LLNDAPI static bool canParse(std::string_view in);
38
39 LLNDAPI static ll::Expected<mce::UUID> tryFromString(::std::string_view str);
40
41public:
42 // member functions
43 // NOLINTBEGIN
44 MCFOLD ::std::string asString() const;
45 // NOLINTEND
46
47public:
48 // static functions
49 // NOLINTBEGIN
50 MCAPI static ::mce::UUID fromString(::std::string_view in);
51 // NOLINTEND
52
53public:
54 // static variables
55 // NOLINTBEGIN
56 MCAPI static ::mce::UUID& EMPTY();
57
58 MCAPI static uint64& STRING_LENGTH();
59 // NOLINTEND
60};
61
62} // namespace mce
63
64namespace std {
65template <>
66struct hash<mce::UUID> {
67 size_t operator()(mce::UUID const& id) const noexcept { return id.a ^ (522133279 * id.b); }
68};
69} // namespace std
70
71#include "ll/api/reflection/Serializer.h"
72
73template <>
74struct ll::reflection::Serializer<mce::UUID> {
75 static std::string to_string(mce::UUID const& t) { return t.asString(); }
76 static ll::Expected<mce::UUID> from_string(std::string_view s) { return mce::UUID::tryFromString(s); }
77
78 template <typename J>
79 static ll::Expected<> serialize(mce::UUID const& t, J& j) {
80 j = to_string(t);
81 return {};
82 }
83 template <typename J>
84 static ll::Expected<mce::UUID> deserialize(J const& j) {
85 if (!j.is_string()) return makeDeserStringTypeError();
86 return from_string(std::string_view{j});
87 }
88};
Definition UUID.h:8
STL namespace.
Definition Serializer.h:9