TrueReality  v0.1.1912
UniqueId.h
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 #pragma once
23 
24 #include "Export.h"
25 
26 #include <trBase/SmrtClass.h>
27 #include <trUtil/RefStr.h>
28 #include <trUtil/Hash.h>
29 
30 #include <string>
31 #include <sstream>
32 #include <iosfwd>
33 
34 namespace trBase
35 {
36  // Forward declarations of the pimpl class
37  class implId;
38 
43  {
44 
45  public:
46 
48 
49  const static trUtil::RefStr CLASS_TYPE;
50 
54  explicit UniqueId(bool createNewId = true);
55 
59  UniqueId(const UniqueId& toCopy);
60 
65  explicit UniqueId(const std::string& toCopy);
66 
70  virtual ~UniqueId();
71 
75  virtual const std::string& GetType() const override;
76 
80  const std::string ToString() const;
81 
87  void FromString(const std::string& idString);
88 
92  bool IsNull() const;
93 
99  UniqueId& operator=(const UniqueId& id);
100 
106  UniqueId& operator=(const UniqueId* id);
107 
113  UniqueId& operator=(const std::string& id);
114 
124  bool operator==(const UniqueId& id) const;
125 
135  bool operator!=(const UniqueId& id) const;
136 
146  bool operator< (const UniqueId& id) const;
147 
157  bool operator> (const UniqueId& id) const;
158 
159  protected:
160  implId* mGUIDPtr = nullptr;
161  };
162 
166  TR_BASE_EXPORT std::ostream& operator << (std::ostream& o, const UniqueId& id);
167 
168  TR_BASE_EXPORT std::istream& operator >> (std::istream& i, UniqueId& id);
169 }
170 
171 namespace trUtil
172 {
176  template<> struct hash<trBase::UniqueId>
177  {
178  size_t operator()(const trBase::UniqueId& id) const
179  {
180  return __hash_string(id.ToString().c_str());
181  }
182  };
183 
187  template<> struct hash<const trBase::UniqueId>
188  {
189  size_t operator()(const trBase::UniqueId& id) const
190  {
191  return __hash_string(id.ToString().c_str());
192  }
193  };
194 }
#define TR_BASE_EXPORT
Definition: trBase/Export.h:34
size_t __hash_string(const char *__s)
Hash string.
Definition: Hash.h:71
size_t operator()(const trBase::UniqueId &id) const
Definition: UniqueId.h:178
bool operator==(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:187
This class creates a GUID, or a Unique ID that is used through out TR to identify and distinguish one...
Definition: UniqueId.h:42
bool operator!=(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:199
TR_BASE_EXPORT std::ostream & operator<<(std::ostream &ios, const Matrixd &q)
Stream insertion operator.
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
static const trUtil::RefStr CLASS_TYPE
Adds an easy and swappable access to the base class.
Definition: UniqueId.h:49
size_t operator()(const trBase::UniqueId &id) const
Definition: UniqueId.h:189
This class is part of the internal garbage collection system.
Definition: SmrtClass.h:38
std::string ToString(const T &t, int precision=-1)
A utility function to convert a basic type into a string.
Definition: StringUtils.h:334
An implementation class for GUID.
Definition: UniqueId.cpp:42
This Hash class and its implementations are taken from the libstdc++ hash_fun.h.
Definition: Hash.h:45
T FromString(const std::string &u)
Converts a string to a specified type.
Definition: StringUtils.h:362
TR_BASE_EXPORT std::istream & operator>>(std::istream &i, UniqueId &id)
Definition: UniqueId.cpp:364
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
osg::Referenced BaseClass
Definition: SmrtClass.h:42