A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uuid.h
Go to the documentation of this file.
1/*!
2 \file uuid.h
3 \brief Universally unique identifier(UUID) definition
4 \author Ivan Shynkarenka
5 \date 18.08.2016
6 \copyright MIT License
7*/
8
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
16namespace ns3 {
17
18//! Universally unique identifier(UUID)
19/*!
20 A universally unique identifier(UUID) is an identifier standard used
21 in software construction. This implementation generates the following
22 UUID types:
23 - Nil UUID0(all bits set to zero)
24 - Sequential UUID1(time based version)
25 - Random UUID4(randomly or pseudo-randomly generated version)
26
27 A UUID is simply a 128-bit value: "123e4567-e89b-12d3-a456-426655440000"
28
29 Not thread-safe.
30
31 https://en.wikipedia.org/wiki/Universally_unique_identifier
32 https://www.ietf.org/rfc/rfc4122.txt
33*/
34class UUID
35{
36public:
37 //! Default constructor
38 UUID() : _data() { _data.fill(0); }
39 //! Initialize UUID with a given string
40 /*!
41 \param uuid - UUID string
42 */
43 explicit UUID(const std::string& uuid);
44 //! Initialize UUID with a given 16 bytes data buffer
45 /*!
46 \param data - UUID 16 bytes data buffer
47 */
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
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
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
74 //! Check if the UUID is nil UUID0(all bits set to zero)
75 explicit operator bool() const noexcept { return *this != Nil(); }
76
77 //! Get the UUID data buffer
78 std::array<uint8_t, 16>& data() noexcept { return _data; }
79 //! Get the UUID data buffer
80 const std::array<uint8_t, 16>& data() const noexcept { return _data; }
81
82 //! Get string from the current UUID in format "00000000-0000-0000-0000-000000000000"
83 std::string string() const;
84
85 //! Generate nil UUID0(all bits set to zero)
86 static UUID Nil() { return UUID(); }
87 //! Generate sequential UUID1(time based version)
88 static UUID Sequential();
89 //! Generate random UUID4(randomly or pseudo-randomly generated version)
90 static UUID Random();
91
92 //! Output instance into the given output stream
93 friend std::ostream& operator<<(std::ostream& os, const UUID& uuid)
94 { os << uuid.string(); return os; }
95
96 //! Swap two instances
97 void swap(UUID& uuid) noexcept;
98 friend void swap(UUID& uuid1, UUID& uuid2) noexcept;
99
100private:
101 std::array<uint8_t, 16> _data;
102};
103
104/*! \example system_uuid.cpp Universally unique identifier(UUID) example */
105
106} // namespace CppCommon
107
108
109#endif // CPPCOMMON_SYSTEM_UUID_H
Universally unique identifier(UUID)
Definition uuid.h:35
UUID & operator=(const std::array< uint8_t, 16 > &data)
Definition uuid.h:55
UUID()
Default constructor.
Definition uuid.h:38
friend bool operator<(const UUID &uuid1, const UUID &uuid2)
Definition uuid.h:65
UUID & operator=(const UUID &)=default
friend void swap(UUID &uuid1, UUID &uuid2) noexcept
Definition uuid.cc:212
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
const std::array< uint8_t, 16 > & data() const noexcept
Get the UUID data buffer.
Definition uuid.h:80
friend bool operator<=(const UUID &uuid1, const UUID &uuid2)
Definition uuid.h:69
UUID(const UUID &)=default
friend bool operator>=(const UUID &uuid1, const UUID &uuid2)
Definition uuid.h:71
static UUID Random()
Generate random UUID4(randomly or pseudo-randomly generated version)
Definition uuid.cc:148
friend std::ostream & operator<<(std::ostream &os, const UUID &uuid)
Output instance into the given output stream.
Definition uuid.h:93
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
std::array< uint8_t, 16 > & data() noexcept
Get the UUID data buffer.
Definition uuid.h:78
UUID(UUID &&) noexcept=default
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
STL namespace.