9 #ifndef CPPCOMMON_SYSTEM_UUID_H
10 #define CPPCOMMON_SYSTEM_UUID_H
39 constexpr
UUID() : _data() { _data.fill(0); }
45 explicit constexpr
UUID(
const char(&uuid)[N]) :
UUID(uuid, N) {}
51 explicit constexpr
UUID(
const char* uuid,
size_t size);
56 explicit UUID(
const std::string& uuid) :
UUID(uuid.
data(), uuid.size()) {}
61 explicit UUID(
const std::array<uint8_t, 16>&
data) : _data(
data) {}
66 UUID& operator=(const std::
string& uuid)
67 { _data =
UUID(uuid).data();
return *
this; }
69 { _data =
data;
return *
this; }
74 friend
bool operator==(const
UUID& uuid1, const
UUID& uuid2)
75 {
return uuid1._data == uuid2._data; }
77 {
return uuid1._data != uuid2._data; }
79 {
return uuid1._data < uuid2._data; }
81 {
return uuid1._data > uuid2._data; }
83 {
return uuid1._data <= uuid2._data; }
85 {
return uuid1._data >= uuid2._data; }
88 explicit operator bool() const noexcept {
return *
this !=
Nil(); }
91 std::array<uint8_t, 16>&
data() noexcept {
return _data; }
93 const std::array<uint8_t, 16>&
data() const noexcept {
return _data; }
96 std::string
string()
const;
109 { os << uuid.
string();
return os; }
116 std::array<uint8_t, 16> _data;
Universally unique identifier (UUID)
UUID(const UUID &)=default
friend bool operator<(const UUID &uuid1, const UUID &uuid2)
static UUID Nil()
Generate nil UUID0 (all bits set to zero)
UUID & operator=(const UUID &)=default
UUID(UUID &&) noexcept=default
friend bool operator>(const UUID &uuid1, const UUID &uuid2)
friend bool operator<=(const UUID &uuid1, const UUID &uuid2)
friend std::ostream & operator<<(std::ostream &os, const UUID &uuid)
Output instance into the given output stream.
std::array< uint8_t, 16 > & data() noexcept
Get the UUID data buffer.
friend bool operator>=(const UUID &uuid1, const UUID &uuid2)
UUID & operator=(UUID &&) noexcept=default
std::string string() const
Get string from the current UUID in format "00000000-0000-0000-0000-000000000000".
static UUID Random()
Generate random UUID4 (randomly or pseudo-randomly generated version)
UUID(const std::string &uuid)
Initialize UUID with a given string.
UUID & operator=(const std::array< uint8_t, 16 > &data)
UUID(const std::array< uint8_t, 16 > &data)
Initialize UUID with a given 16 bytes data buffer.
void swap(UUID &uuid) noexcept
Swap two instances.
static UUID Sequential()
Generate sequential UUID1 (time based version)
const std::array< uint8_t, 16 > & data() const noexcept
Get the UUID data buffer.
friend bool operator!=(const UUID &uuid1, const UUID &uuid2)
static UUID Secure()
Generate secure UUID4 (secure generated version)
constexpr UUID(const char(&uuid)[N])
Initialize UUID with a given string literal.
constexpr UUID()
Default constructor.
C++ Common project definitions.
Universally unique identifier (UUID) inline implementation.