TrueReality  v0.1.1912
UniqueId.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #include <trBase/UniqueId.h>
23 
24 #include <trUtil/PlatformMacros.h>
25 #include <trUtil/Logging/Log.h>
26 
27 #include <bID/uuid/random_generator.hpp>
28 #include <bID/uuid/string_generator.hpp>
29 #include <bID/uuid/nil_generator.hpp>
30 #include <bID/uuid/uuid.hpp>
31 #include <bID/uuid/uuid_io.hpp>
32 
33 #include <iostream>
34 
35 namespace trBase
36 {
42  class implId
43  {
44  public:
45 
53  explicit implId(bool createNewId)
54  : mGUID(bID::uuids::random_generator()()) //Create a random GUID
55  {
56  if (!createNewId)
57  {
58  //Create a NULL GUID
59  mGUID = bID::uuids::nil_uuid();
60  }
61  }
62 
70  implId(const implId& toCopy)
71  : mGUID(toCopy.mGUID)
72  {
73  }
74 
82  explicit implId(const std::string& toCopy)
83  : mGUID(bID::uuids::string_generator()(toCopy))
84  {
85  }
86 
94  const std::string ToString() const
95  {
96  return bID::uuids::to_string(mGUID);
97  }
98 
106  void FromString(const std::string& idString)
107  {
108  mGUID = bID::uuids::string_generator()(idString);
109  }
110 
118  bool IsNull() const
119  {
120  return mGUID.is_nil();
121  }
122 
130  uint8_t* GetData()
131  {
132  return mGUID.data;
133  }
134 
144  implId& operator=(const implId& id)
145  {
146  if (this == &id)
147  {
148  return *this;
149  }
150 
151  mGUID = id.mGUID;
152  return *this;
153  }
154 
164  implId& operator=(const implId* id)
165  {
166  if (this == id)
167  {
168  return *this;
169  }
170 
171  mGUID = id->mGUID;
172  return *this;
173  }
174 
184  implId& operator=(const std::string& id)
185  {
186  mGUID = bID::uuids::string_generator()(id);
187  return *this;
188  }
189 
199  bool operator==(const implId & id) const
200  {
201  return mGUID == id.mGUID;
202  }
203 
213  bool operator!=(const implId & id) const
214  {
215  return mGUID != id.mGUID;
216  }
217 
227  bool operator<(const implId & id) const
228  {
229  return mGUID < id.mGUID;
230  }
231 
241  bool operator>(const implId & id) const
242  {
243  return mGUID > id.mGUID;
244  }
245 
246  private:
247  bID::uuids::uuid mGUID;
248  };
249 
251  const trUtil::RefStr UniqueId::CLASS_TYPE = trUtil::RefStr("trBase::UniqueId");
252 
254  UniqueId::UniqueId(bool createNewId)
255  {
256  mGUIDPtr = new implId(createNewId);
257  }
258 
261  {
262  // Create a NULL ID then copy the passed in IDs bits.
263  mGUIDPtr = new implId(false);
264  for (int i = 0; i < 16; ++i)
265  {
266  mGUIDPtr->GetData()[i] = toCopy.mGUIDPtr->GetData()[i];
267  }
268 
269  }
270 
272  UniqueId::UniqueId(const std::string& toCopy)
273  {
274  mGUIDPtr = new implId(toCopy);
275  }
276 
279  {
280  if (mGUIDPtr)
281  {
282  delete mGUIDPtr;
283  mGUIDPtr = nullptr;
284  }
285  }
286 
288  const std::string& UniqueId::GetType() const
289  {
290  return CLASS_TYPE;
291  }
292 
294  const std::string UniqueId::ToString() const
295  {
296  return mGUIDPtr->ToString();
297  }
298 
300  void UniqueId::FromString(const std::string& idString)
301  {
302  mGUIDPtr->FromString(idString);
303  }
304 
306  bool UniqueId::IsNull() const
307  {
308  return mGUIDPtr->IsNull();
309  }
310 
313  {
314  mGUIDPtr->operator= (*id.mGUIDPtr);
315  return *this;
316  }
317 
320  {
321  mGUIDPtr->operator= (id->mGUIDPtr);
322  return *this;
323  }
324 
326  UniqueId& UniqueId::operator=(const std::string& id)
327  {
328  mGUIDPtr->operator=(id);
329  return *this;
330  }
331 
333  bool UniqueId::operator==(const UniqueId & id) const
334  {
335  return mGUIDPtr->operator== (*id.mGUIDPtr);
336  }
337 
339  bool UniqueId::operator!=(const UniqueId & id) const
340  {
341  return mGUIDPtr->operator!= (*id.mGUIDPtr);
342  }
343 
345  bool UniqueId::operator<(const UniqueId & id) const
346  {
347  return mGUIDPtr->operator< (*id.mGUIDPtr);
348  }
349 
351  bool UniqueId::operator>(const UniqueId & id) const
352  {
353  return mGUIDPtr->operator> (*id.mGUIDPtr);
354  }
355 
357  std::ostream& operator << (std::ostream& o, const UniqueId& id)
358  {
359  o << id.ToString();
360  return o;
361  }
362 
364  std::istream& operator >> (std::istream& i, UniqueId& id)
365  {
366  std::string value;
367  i >> value;
368  id = value;
369  return i;
370  }
371 }
bool operator<(const UniqueId &id) const
Less-than comparison operator.
Definition: UniqueId.cpp:345
const std::string ToString() const
Convert this object into a string representation.
Definition: UniqueId.cpp:94
This class creates a GUID, or a Unique ID that is used through out TR to identify and distinguish one...
Definition: UniqueId.h:42
UniqueId & operator=(const UniqueId &id)
Copy the GUID from another unique id.
Definition: UniqueId.cpp:312
UniqueId(bool createNewId=true)
Holds the class type name for efficient comparisons.
Definition: UniqueId.cpp:254
virtual ~UniqueId()
dtor
Definition: UniqueId.cpp:278
implId(const implId &toCopy)
Makes a copy of the passed in Unique ID GUID.
Definition: UniqueId.cpp:70
TR_BASE_EXPORT std::ostream & operator<<(std::ostream &ios, const Matrixd &q)
Stream insertion operator.
implId & operator=(const implId &id)
Assignment operator.
Definition: UniqueId.cpp:144
A string wrapper that will make sure that all of the strings with the same value will point to the sa...
Definition: RefStr.h:50
implId & operator=(const implId *id)
Assignment operator.
Definition: UniqueId.cpp:164
static const trUtil::RefStr CLASS_TYPE
Adds an easy and swappable access to the base class.
Definition: UniqueId.h:49
bool IsNull() const
Returns true if the GUID is equal to 00000000-0000-0000-0000-000000000000.
Definition: UniqueId.cpp:118
bool operator!=(const implId &id) const
Inequality operator.
Definition: UniqueId.cpp:213
virtual const std::string & GetType() const override
Returns the class type.
Definition: UniqueId.cpp:288
void FromString(const std::string &idString)
Initializes this object from the given string.
Definition: UniqueId.cpp:106
An implementation class for GUID.
Definition: UniqueId.cpp:42
void FromString(const std::string &idString)
Assign the GUID value to this instance from a string.
Definition: UniqueId.cpp:300
bool operator>(const UniqueId &id) const
Greater-than comparison operator.
Definition: UniqueId.cpp:351
implId(const std::string &toCopy)
Constructor.
Definition: UniqueId.cpp:82
implId * mGUIDPtr
Definition: UniqueId.h:160
bool operator<(const implId &id) const
Less-than comparison operator.
Definition: UniqueId.cpp:227
uint8_t * GetData()
Gets the internal data array that holds the GUID bits.
Definition: UniqueId.cpp:130
bID::uuids::uuid mGUID
Definition: UniqueId.cpp:247
bool IsNull() const
Returns true if the GUID is equal to 00000000-0000-0000-0000-000000000000.
Definition: UniqueId.cpp:306
TR_BASE_EXPORT std::istream & operator>>(std::istream &i, UniqueId &id)
Definition: UniqueId.cpp:364
implId & operator=(const std::string &id)
Assignment operator.
Definition: UniqueId.cpp:184
bool operator==(const UniqueId &id) const
Equality operator.
Definition: UniqueId.cpp:333
bool operator==(const implId &id) const
Equality operator.
Definition: UniqueId.cpp:199
bool operator>(const implId &id) const
Greater-than comparison operator.
Definition: UniqueId.cpp:241
implId(bool createNewId)
Constructor.
Definition: UniqueId.cpp:53
bool operator!=(const UniqueId &id) const
Inequality operator.
Definition: UniqueId.cpp:339
const std::string ToString() const
Convert the current GUID into a string.
Definition: UniqueId.cpp:294
osg::Referenced BaseClass
Definition: SmrtClass.h:42