Ocean
UniqueId.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_UNIQUE_ID_H
9 #define META_OCEAN_PLATFORM_UNIQUE_ID_H
10 
12 
13 #include "ocean/base/String.h"
14 
15 #ifndef _WINDOWS
16  #include <uuid/uuid.h>
17 #endif
18 
19 namespace Ocean
20 {
21 
22 namespace Platform
23 {
24 
25 /**
26  * This class provides a 128 bit id which is unique.
27  * @ingroup platform
28  */
29 class UniqueId
30 {
31  public:
32 
33  /**
34  * Creates a new invalid id.
35  */
36  inline UniqueId();
37 
38  /**
39  * Creates a new unique id by two 64 bit values.
40  * @param a First 64 bit value
41  * @param b Second 64 bit value
42  */
43  inline UniqueId(unsigned long long a, unsigned long long b);
44 
45  /**
46  * Creates a new unique id.
47  * @param createUniqueId True, to initialize this object with a valid unique id; False, to create in invalid id
48  */
49  inline explicit UniqueId(const bool createUniqueId);
50 
51  /**
52  * Modifies this id and sets a new unique id value.
53  */
54  inline void newUniqueId();
55 
56  /**
57  * Returns whether this object holds a valid unique id.
58  * @return True, if so
59  */
60  inline bool isValid() const;
61 
62  /**
63  * Returns this unique id as string.
64  * @return The id as string
65  */
66  inline std::string string() const;
67 
68  /**
69  * Returns a very short part of this unique id as string.
70  * The short string contains four characters of the normal string (representing the first two bytes of information).<br>
71  * Beware: The short string should be used for userfriendly output information only because this string is not unique anymore!
72  * @return The id as string
73  */
74  inline std::string shortString() const;
75 
76  /**
77  * Returns a pointer to two 64 bit values defining the id.
78  * @return The 64 bit values
79  */
80  inline const unsigned long long* operator()() const;
81 
82  /**
83  * Compares two id objects and returns whether the left one has a smaller id than the right one.
84  * @param uniqueId The right unique id object
85  * @return True, if so
86  */
87  inline bool operator<(const UniqueId& uniqueId) const;
88 
89  /**
90  * Returns whether two id objects have the same id value.
91  * @param uniqueId The second unique id object
92  * @return True, if so
93  */
94  inline bool operator==(const UniqueId& uniqueId) const;
95 
96  /**
97  * Returns whether two id objects have not the same id value.
98  * @param uniqueId The second unique id object
99  * @return True, if so
100  */
101  inline bool operator!=(const UniqueId& uniqueId) const;
102 
103  protected:
104 
105  /// The two 64 bit values defining the id value.
106  unsigned long long value[2];
107 };
108 
110 {
111  value[0] = 0ull;
112  value[1] = 0ull;
113 
114  ocean_assert(!isValid());
115 }
116 
117 inline UniqueId::UniqueId(unsigned long long a, unsigned long long b)
118 {
119  value[0] = a;
120  value[1] = b;
121 
122  ocean_assert(isValid());
123 }
124 
125 inline UniqueId::UniqueId(const bool createUniqueId)
126 {
127  if (createUniqueId)
128  {
129  newUniqueId();
130  ocean_assert(isValid());
131  }
132  else
133  {
134  value[0] = 0ull;
135  value[1] = 0ull;
136 
137  ocean_assert(!isValid());
138  }
139 }
140 
142 {
143 #ifdef _WINDOWS
144  static_assert(sizeof(UUID) == sizeof(unsigned long long) * 2, "Invalid UUID size!");
145  UuidCreate((UUID*)value);
146 #else
147  static_assert(sizeof(unsigned long long) == 8, "Invalid data type");
148  static_assert(sizeof(uuid_t) == sizeof(unsigned long long) * 2, "Invalid data type");
149 
150  uuid_generate(*((uuid_t*)&value));
151 #endif
152 }
153 
154 inline const unsigned long long* UniqueId::operator()() const
155 {
156  return value;
157 }
158 
159 inline bool UniqueId::isValid() const
160 {
161  return value[0] != 0ull || value[1] != 0ull;
162 }
163 
164 inline std::string UniqueId::string() const
165 {
167 }
168 
169 inline std::string UniqueId::shortString() const
170 {
171  return String::toAStringHexReverse((unsigned char*)value, 2u);
172 }
173 
174 inline bool UniqueId::operator<(const UniqueId& uniqueId) const
175 {
176  return value[0] < uniqueId.value[0] || (value[0] == uniqueId.value[1] && value[1] < uniqueId.value[1]);
177 }
178 
179 inline bool UniqueId::operator==(const UniqueId& uniqueId) const
180 {
181  return value[0] == uniqueId.value[0] && value[1] == uniqueId.value[1];
182 }
183 
184 inline bool UniqueId::operator!=(const UniqueId& uniqueId) const
185 {
186  return !(*this == uniqueId);
187 }
188 
189 }
190 
191 }
192 
193 #endif // META_OCEAN_PLATFORM_UNIQUE_ID_H
This class provides a 128 bit id which is unique.
Definition: UniqueId.h:30
void newUniqueId()
Modifies this id and sets a new unique id value.
Definition: UniqueId.h:141
bool isValid() const
Returns whether this object holds a valid unique id.
Definition: UniqueId.h:159
unsigned long long value[2]
The two 64 bit values defining the id value.
Definition: UniqueId.h:106
const unsigned long long * operator()() const
Returns a pointer to two 64 bit values defining the id.
Definition: UniqueId.h:154
bool operator<(const UniqueId &uniqueId) const
Compares two id objects and returns whether the left one has a smaller id than the right one.
Definition: UniqueId.h:174
UniqueId()
Creates a new invalid id.
Definition: UniqueId.h:109
std::string string() const
Returns this unique id as string.
Definition: UniqueId.h:164
std::string shortString() const
Returns a very short part of this unique id as string.
Definition: UniqueId.h:169
bool operator!=(const UniqueId &uniqueId) const
Returns whether two id objects have not the same id value.
Definition: UniqueId.h:184
bool operator==(const UniqueId &uniqueId) const
Returns whether two id objects have the same id value.
Definition: UniqueId.h:179
static std::string toAStringHex(const char value, const bool upperCases=true)
Converts a value to a string with 8bit character as hexadecimal notation.
static std::string toAStringHexReverse(const uint8_t *data, const size_t size, const bool upperCases=true)
Converts memory to a string with hexadecimal notation in a reverse order.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15