libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
uuid.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
11#include <cstring>
13#include <promeki/core/string.h>
14#include <promeki/core/error.h>
15#include <promeki/core/array.h>
16
18
34class UUID {
35 public:
38
49 static UUID generate(int version = 4);
50
55 static UUID generateV1();
56
63 static UUID generateV3(const UUID &ns, const String &name);
64
69 static UUID generateV4();
70
77 static UUID generateV5(const UUID &ns, const String &name);
78
86
93 static UUID fromString(const char *string, Error *err = nullptr);
94
96 UUID() : d{} { }
97
99 UUID(const UUID &u) : d(u.d) { }
100
102 UUID(const UUID &&u) : d(std::move(u.d)) { }
103
105 UUID(const DataFormat &val) : d(val) { }
106
108 UUID(const DataFormat &&val) : d(std::move(val)) { }
109
111 explicit UUID(const char *str) : d(fromString(str).data()) { }
112
114 explicit UUID(const String &str) : d(fromString(str.cstr()).data()) { }
115
118 d = val.d;
119 return *this;
120 }
121
123 UUID &operator=(const UUID &&val) {
124 d = std::move(val.d);
125 return *this;
126 }
127
130 d = val;
131 return *this;
132 }
133
136 d = std::move(val);
137 return *this;
138 }
139
141 bool operator==(const UUID &other) const {
142 return d == other.d;
143 }
144
146 bool operator!=(const UUID &other) const {
147 return d != other.d;
148 }
149
151 bool operator<(const UUID &other) const {
152 return std::memcmp(d.data(), other.d.data(), 16) < 0;
153 }
154
156 bool operator>(const UUID &other) const {
157 return std::memcmp(d.data(), other.d.data(), 16) > 0;
158 }
159
161 bool operator<=(const UUID &other) const {
162 return std::memcmp(d.data(), other.d.data(), 16) <= 0;
163 }
164
166 bool operator>=(const UUID &other) const {
167 return std::memcmp(d.data(), other.d.data(), 16) >= 0;
168 }
169
171 operator String() const {
172 return toString();
173 }
174
179 bool isValid() const {
180 return !d.isZero();
181 }
182
187 int version() const {
188 if(!isValid()) return 0;
189 return (d[6] >> 4) & 0x0F;
190 }
191
197
202 const DataFormat &data() const {
203 return d;
204 }
205
210 const uint8_t *raw() const {
211 return d.data();
212 }
213
214 private:
215 DataFormat d;
216};
217
bool isZero() const
Returns true if all elements are zero.
Definition array.h:297
T * data()
Returns a mutable pointer to the underlying contiguous storage.
Definition array.h:281
Lightweight error code wrapper for the promeki library.
Definition error.h:39
Dynamic array container wrapping std::vector.
Definition list.h:40
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Universally Unique Identifier (UUID).
Definition uuid.h:34
bool operator>=(const UUID &other) const
Greater-than-or-equal comparison for ordering (lexicographic).
Definition uuid.h:166
int version() const
Returns the UUID version number.
Definition uuid.h:187
UUID(const char *str)
Constructs a UUID by parsing a C-string.
Definition uuid.h:111
bool isValid() const
Returns true if this UUID is not all-zero.
Definition uuid.h:179
bool operator>(const UUID &other) const
Greater-than comparison for ordering (lexicographic).
Definition uuid.h:156
bool operator<(const UUID &other) const
Less-than comparison for ordering (lexicographic).
Definition uuid.h:151
UUID(const String &str)
Constructs a UUID by parsing a String.
Definition uuid.h:114
UUID(const DataFormat &val)
Constructs a UUID from raw 16-byte data.
Definition uuid.h:105
UUID & operator=(const UUID &val)
Copy assignment operator.
Definition uuid.h:117
static UUID fromString(const char *string, Error *err=nullptr)
Parses a UUID from a string representation.
UUID()
Constructs an invalid (all-zero) UUID.
Definition uuid.h:96
const DataFormat & data() const
Returns a const reference to the raw 16-byte data.
Definition uuid.h:202
String toString() const
Returns the standard string representation of the UUID.
static UUID generateV7(int64_t timestampMs=-1)
Generates a version 7 (Unix timestamp + random) UUID.
static UUID generateV1()
Generates a version 1 (timestamp + MAC) UUID.
UUID & operator=(const DataFormat &&val)
Move-assigns from raw 16-byte data.
Definition uuid.h:135
static UUID generateV4()
Generates a random version 4 UUID.
bool operator!=(const UUID &other) const
Returns true if the UUIDs are not equal.
Definition uuid.h:146
const uint8_t * raw() const
Returns a pointer to the raw byte data.
Definition uuid.h:210
UUID & operator=(const DataFormat &val)
Assigns from raw 16-byte data.
Definition uuid.h:129
static UUID generate(int version=4)
Convenience generator that dispatches to the appropriate generateVn() function.
static UUID generateV3(const UUID &ns, const String &name)
Generates a version 3 (MD5 namespace) UUID.
UUID(const UUID &&u)
Move constructor.
Definition uuid.h:102
Array< uint8_t, 16 > DataFormat
Raw 16-byte storage format for a UUID.
Definition uuid.h:37
UUID & operator=(const UUID &&val)
Move assignment operator.
Definition uuid.h:123
static UUID generateV5(const UUID &ns, const String &name)
Generates a version 5 (SHA-1 namespace) UUID.
UUID(const UUID &u)
Copy constructor.
Definition uuid.h:99
UUID(const DataFormat &&val)
Move-constructs a UUID from raw 16-byte data.
Definition uuid.h:108
bool operator==(const UUID &other) const
Returns true if both UUIDs are equal.
Definition uuid.h:141
bool operator<=(const UUID &other) const
Less-than-or-equal comparison for ordering (lexicographic).
Definition uuid.h:161
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19