libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
uuid.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstdint>
14#include <cstring>
15#include <promeki/namespace.h>
16#include <promeki/string.h>
17#include <promeki/error.h>
18#include <promeki/result.h>
19#include <promeki/array.h>
20#include <promeki/datatype.h>
21
22PROMEKI_NAMESPACE_BEGIN
23
24class DataStream;
25
47class UUID {
48 public:
49 PROMEKI_DATATYPE(UUID, DataTypeUUID, 1)
50
51
52 using DataFormat = Array<uint8_t, 16>;
53
64 static UUID generate(int version = 4);
65
76 static UUID generateV1();
77
84 static UUID generateV3(const UUID &ns, const String &name);
85
90 static UUID generateV4();
91
98 static UUID generateV5(const UUID &ns, const String &name);
99
106 static UUID generateV7(int64_t timestampMs = -1);
107
115 static Result<UUID> fromString(const String &string);
116
118 UUID() : d{} {}
119
121 UUID(const UUID &u) : d(u.d) {}
122
124 UUID(UUID &&u) noexcept : d(std::move(u.d)) {}
125
127 UUID(const DataFormat &val) : d(val) {}
128
130 UUID(DataFormat &&val) noexcept : d(std::move(val)) {}
131
133 UUID &operator=(const UUID &val) {
134 d = val.d;
135 return *this;
136 }
137
139 UUID &operator=(UUID &&val) noexcept {
140 d = std::move(val.d);
141 return *this;
142 }
143
145 UUID &operator=(const DataFormat &val) {
146 d = val;
147 return *this;
148 }
149
151 UUID &operator=(DataFormat &&val) noexcept {
152 d = std::move(val);
153 return *this;
154 }
155
157 bool operator==(const UUID &other) const { return d == other.d; }
158
160 bool operator!=(const UUID &other) const { return d != other.d; }
161
163 bool operator<(const UUID &other) const { return std::memcmp(d.data(), other.d.data(), 16) < 0; }
164
166 bool operator>(const UUID &other) const { return std::memcmp(d.data(), other.d.data(), 16) > 0; }
167
169 bool operator<=(const UUID &other) const { return std::memcmp(d.data(), other.d.data(), 16) <= 0; }
170
172 bool operator>=(const UUID &other) const { return std::memcmp(d.data(), other.d.data(), 16) >= 0; }
173
175 operator String() const { return toString(); }
176
181 bool isValid() const { return !d.isZero(); }
182
187 int version() const {
188 if (!isValid()) return 0;
189 return (d[6] >> 4) & 0x0F;
190 }
191
196 String toString() const;
197
202 const DataFormat &data() const { return d; }
203
208 const uint8_t *raw() const { return d.data(); }
209
215 Error writeToStream(DataStream &s) const;
216
222 template <uint32_t V> static Result<UUID> readFromStream(DataStream &s);
223
224 private:
225 DataFormat d;
226};
227
228PROMEKI_NAMESPACE_END
229
230PROMEKI_FORMAT_VIA_TOSTRING(promeki::UUID);
231
232#endif // PROMEKI_ENABLE_CORE