A Discrete-Event Network Simulator
API
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 <array>
13 #include <string>
14 #include "ns3/object.h"
15 
16 namespace ns3 {
17 
19 
34 class UUID
35 {
36 public:
38  UUID() : _data() { _data.fill(0); }
40 
43  explicit UUID(const std::string& uuid);
45 
48  explicit UUID(const std::array<uint8_t, 16>& data) : _data(data) {}
49  UUID(const UUID&) = default;
50  UUID(UUID&&) noexcept = default;
51  ~UUID() = default;
52 
53  UUID& operator=(const std::string& uuid)
54  { _data = UUID(uuid).data(); return *this; }
55  UUID& operator=(const std::array<uint8_t, 16>& data)
56  { _data = data; return *this; }
57  UUID& operator=(const UUID&) = default;
58  UUID& operator=(UUID&&) noexcept = default;
59 
60  // UUID comparison
61  friend bool operator==(const UUID& uuid1, const UUID& uuid2)
62  { return uuid1._data == uuid2._data; }
63  friend bool operator!=(const UUID& uuid1, const UUID& uuid2)
64  { return uuid1._data != uuid2._data; }
65  friend bool operator<(const UUID& uuid1, const UUID& uuid2)
66  { return uuid1._data < uuid2._data; }
67  friend bool operator>(const UUID& uuid1, const UUID& uuid2)
68  { return uuid1._data > uuid2._data; }
69  friend bool operator<=(const UUID& uuid1, const UUID& uuid2)
70  { return uuid1._data <= uuid2._data; }
71  friend bool operator>=(const UUID& uuid1, const UUID& uuid2)
72  { return uuid1._data >= uuid2._data; }
73 
75  explicit operator bool() const noexcept { return *this != Nil(); }
76 
78  std::array<uint8_t, 16>& data() noexcept { return _data; }
80  const std::array<uint8_t, 16>& data() const noexcept { return _data; }
81 
83  std::string string() const;
84 
86  static UUID Nil() { return UUID(); }
88  static UUID Sequential();
90  static UUID Random();
91 
93  friend std::ostream& operator<<(std::ostream& os, const UUID& uuid)
94  { os << uuid.string(); return os; }
95 
97  void swap(UUID& uuid) noexcept;
98  friend void swap(UUID& uuid1, UUID& uuid2) noexcept;
99 
100 private:
101  std::array<uint8_t, 16> _data;
102 };
103 
106 } // namespace CppCommon
107 
108 
109 #endif // CPPCOMMON_SYSTEM_UUID_H
Universally unique identifier (UUID)
Definition: uuid.h:35
UUID()
Default constructor.
Definition: uuid.h:38
friend bool operator<(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:65
const std::array< uint8_t, 16 > & data() const noexcept
Get the UUID data buffer.
Definition: uuid.h:80
std::array< uint8_t, 16 > & data() noexcept
Get the UUID data buffer.
Definition: uuid.h:78
friend bool operator>(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:67
UUID(const std::array< uint8_t, 16 > &data)
Initialize UUID with a given 16 bytes data buffer.
Definition: uuid.h:48
friend bool operator<=(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:69
friend std::ostream & operator<<(std::ostream &os, const UUID &uuid)
Output instance into the given output stream.
Definition: uuid.h:93
UUID(const UUID &)=default
void swap(UUID &uuid) noexcept
Swap two instances.
Definition: uuid.cc:205
friend bool operator>=(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:71
UUID & operator=(const std::array< uint8_t, 16 > &data)
Definition: uuid.h:55
static UUID Random()
Generate random UUID4 (randomly or pseudo-randomly generated version)
Definition: uuid.cc:148
static UUID Nil()
Generate nil UUID0 (all bits set to zero)
Definition: uuid.h:86
static UUID Sequential()
Generate sequential UUID1 (time based version)
Definition: uuid.cc:92
friend bool operator!=(const UUID &uuid1, const UUID &uuid2)
Definition: uuid.h:63
std::string string() const
Get string from the current UUID in format "00000000-0000-0000-0000-000000000000".
Definition: uuid.cc:74
UUID & operator=(UUID &&) noexcept=default
std::array< uint8_t, 16 > _data
Definition: uuid.h:101
UUID & operator=(const UUID &)=default
UUID(UUID &&) noexcept=default
Every class exported by the ns3 library is enclosed in the ns3 namespace.