CppCommon  1.0.4.1
C++ Common Library
uuid.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_SYSTEM_UUID_H
10 #define CPPCOMMON_SYSTEM_UUID_H
11 
12 #include "errors/exceptions.h"
13 
14 #include <array>
15 #include <string>
16 
17 namespace CppCommon {
18 
20 
35 class UUID
36 {
37 public:
39  constexpr UUID() : _data() { _data.fill(0); }
41 
44  template<size_t N>
45  explicit constexpr UUID(const char(&uuid)[N]) : UUID(uuid, N) {}
47 
51  explicit constexpr UUID(const char* uuid, size_t size);
53 
56  explicit UUID(const std::string& uuid) : UUID(uuid.data(), uuid.size()) {}
58 
61  explicit UUID(const std::array<uint8_t, 16>& data) : _data(data) {}
62  UUID(const UUID&) = default;
63  UUID(UUID&&) noexcept = default;
64  ~UUID() = default;
65 
66  UUID& operator=(const std::string& uuid)
67  { _data = UUID(uuid).data(); return *this; }
68  UUID& operator=(const std::array<uint8_t, 16>& data)
69  { _data = data; return *this; }
70  UUID& operator=(const UUID&) = default;
71  UUID& operator=(UUID&&) noexcept = default;
72 
73  // UUID comparison
74  friend bool operator==(const UUID& uuid1, const UUID& uuid2)
75  { return uuid1._data == uuid2._data; }
76  friend bool operator!=(const UUID& uuid1, const UUID& uuid2)
77  { return uuid1._data != uuid2._data; }
78  friend bool operator<(const UUID& uuid1, const UUID& uuid2)
79  { return uuid1._data < uuid2._data; }
80  friend bool operator>(const UUID& uuid1, const UUID& uuid2)
81  { return uuid1._data > uuid2._data; }
82  friend bool operator<=(const UUID& uuid1, const UUID& uuid2)
83  { return uuid1._data <= uuid2._data; }
84  friend bool operator>=(const UUID& uuid1, const UUID& uuid2)
85  { return uuid1._data >= uuid2._data; }
86 
88  explicit operator bool() const noexcept { return *this != Nil(); }
89 
91  std::array<uint8_t, 16>& data() noexcept { return _data; }
93  const std::array<uint8_t, 16>& data() const noexcept { return _data; }
94 
96  std::string string() const;
97 
99  static UUID Nil() { return UUID(); }
101  static UUID Sequential();
103  static UUID Random();
105  static UUID Secure();
106 
108  friend std::ostream& operator<<(std::ostream& os, const UUID& uuid)
109  { os << uuid.string(); return os; }
110 
112  void swap(UUID& uuid) noexcept;
113  friend void swap(UUID& uuid1, UUID& uuid2) noexcept;
114 
115 private:
116  std::array<uint8_t, 16> _data;
117 };
118 
121 } // namespace CppCommon
122 
124 
128 constexpr CppCommon::UUID operator ""_uuid(const char* uuid, size_t size)
129 {
130  return CppCommon::UUID(uuid, size);
131 }
132 
133 #include "uuid.inl"
134 
135 #endif // CPPCOMMON_SYSTEM_UUID_H
Universally unique identifier (UUID)
Definition: uuid.h:36
UUID(const UUID &)=default
friend bool operator<(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:78
static UUID Nil()
Generate nil UUID0 (all bits set to zero)
Definition: uuid.h:99
UUID & operator=(const UUID &)=default
UUID(UUID &&) noexcept=default
friend bool operator>(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:80
friend bool operator<=(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:82
friend std::ostream & operator<<(std::ostream &os, const UUID &uuid)
Output instance into the given output stream.
Definition: uuid.h:108
std::array< uint8_t, 16 > & data() noexcept
Get the UUID data buffer.
Definition: uuid.h:91
friend bool operator>=(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:84
UUID & operator=(UUID &&) noexcept=default
std::string string() const
Get string from the current UUID in format "00000000-0000-0000-0000-000000000000".
Definition: uuid.cpp:22
static UUID Random()
Generate random UUID4 (randomly or pseudo-randomly generated version)
Definition: uuid.cpp:96
UUID(const std::string &uuid)
Initialize UUID with a given string.
Definition: uuid.h:56
UUID & operator=(const std::array< uint8_t, 16 > &data)
Definition: uuid.h:68
UUID(const std::array< uint8_t, 16 > &data)
Initialize UUID with a given 16 bytes data buffer.
Definition: uuid.h:61
void swap(UUID &uuid) noexcept
Swap two instances.
Definition: uuid.inl:67
static UUID Sequential()
Generate sequential UUID1 (time based version)
Definition: uuid.cpp:40
const std::array< uint8_t, 16 > & data() const noexcept
Get the UUID data buffer.
Definition: uuid.h:93
friend bool operator!=(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:76
static UUID Secure()
Generate secure UUID4 (secure generated version)
Definition: uuid.cpp:152
constexpr UUID(const char(&uuid)[N])
Initialize UUID with a given string literal.
Definition: uuid.h:45
constexpr UUID()
Default constructor.
Definition: uuid.h:39
Exceptions definition.
C++ Common project definitions.
Definition: token_bucket.h:15
Universally unique identifier (UUID) inline implementation.