AGX Dynamics  2.35.0.1
Uuid.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_UUID_H
18 #define AGX_UUID_H
19 
20 #include <agx/agxCore_export.h>
21 #include <agx/Integer.h>
22 #include <agx/String.h>
23 #include <agxData/Type.h>
24 #include <random>
25 
26 #ifdef _MSC_VER
27 #pragma warning( push )
28 #pragma warning( disable : 4251 ) // class 'x' needs to have dll-interface to be used by clients of class 'y'
29 #endif
30 
31 
32 namespace agx
33 {
34 
42  {
43  public:
44 
46  Uuid();
47 
52  explicit Uuid(const agx::String& uuidString);
53 
54  inline bool operator==(const Uuid& rhs) const
55  {
56  return m_data.ab == rhs.m_data.ab && m_data.cd == rhs.m_data.cd;
57  }
58 
59  inline bool operator!=(const Uuid& rhs) const
60  {
61  return !(m_data.ab == rhs.m_data.ab && m_data.cd == rhs.m_data.cd);
62  }
63 
64  inline bool operator<(const Uuid& rhs) const
65  {
66  if (m_data.ab < rhs.m_data.ab) return true;
67  if (m_data.ab > rhs.m_data.ab) return false;
68  if (m_data.cd < rhs.m_data.cd) return true;
69  return false;
70  }
71 
72  inline bool operator>(const Uuid& rhs) const
73  {
74  if (m_data.ab > rhs.m_data.ab) return true;
75  if (m_data.ab < rhs.m_data.ab) return false;
76  if (m_data.cd > rhs.m_data.cd) return true;
77  return false;
78  }
79 
84  inline bool isNil() const
85  {
86  return (m_data.ab == m_data.cd) && (m_data.ab == 0);
87  }
88 
90  static size_t size() { return 2 * sizeof(uint64_t); }
91 
93  const agx::UInt8 *data() const;
94 
97 
98  friend class UuidGenerator;
99 
100  friend AGXCORE_EXPORT std::ostream& operator <<(std::ostream& stream, const Uuid& uuid);
101 
102 #ifndef SWIG
104  template<class T>
105  struct Less {
106  // functor for operator<
107  bool operator()(const T& _Left, const T& _Right) const {
108  // apply operator< to operands
109  return (_Left->getUuid() < _Right->getUuid());
110  }
111  };
112 #endif
113 
114 
123  static Uuid generateFromString(const agx::String& seedString);
124 
126  agx::String str() const;
127 
128  AGX_FORCE_INLINE UInt32 hash() const;
129 
130  protected:
131  friend class UuidGenerator2;
132 
133  struct Data {
134  Data(uint64_t xab, uint64_t xcd) : ab(xab), cd(xcd) {}
135  Data() : ab(0), cd(0) {}
136 
137  uint64_t ab;
138  uint64_t cd;
139  };
140 
142 
143 
144  };
145 
146 
147  AGXCORE_EXPORT std::ostream& operator <<(std::ostream& stream, const Uuid& uuid);
148 
149 
150  template <typename ch, typename char_traits>
151  std::basic_istream<ch, char_traits>& operator>>(std::basic_istream<ch, char_traits> &is, Uuid &u)
152  {
153  const typename std::basic_istream<ch, char_traits>::sentry ok(is);
154  if (ok) {
155  unsigned char data[16] = {0};
156 
157  typedef std::ctype<ch> ctype_t;
158  ctype_t const& ctype = std::use_facet<ctype_t>(is.getloc());
159 
160  ch xdigits[16];
161  {
162  char szdigits[] = "0123456789ABCDEF";
163  ctype.widen(szdigits, szdigits + 16, xdigits);
164  }
165  ch*const xdigits_end = xdigits + 16;
166 
167  ch c;
168  for (std::size_t i = 0; i<u.size() && is; ++i) {
169  is >> c;
170  c = ctype.toupper(c);
171 
172  ch* f = std::find(xdigits, xdigits_end, c);
173  if (f == xdigits_end) {
174  is.setstate(std::ios_base::failbit);
175  break;
176  }
177 
178  unsigned char byte = static_cast<unsigned char>(std::distance(&xdigits[0], f));
179 
180  is >> c;
181  c = ctype.toupper(c);
182  f = std::find(xdigits, xdigits_end, c);
183  if (f == xdigits_end) {
184  is.setstate(std::ios_base::failbit);
185  break;
186  }
187 
188  byte = static_cast<unsigned char>(byte << 4);
189  byte = static_cast<unsigned char>(byte | static_cast<unsigned char>(std::distance(&xdigits[0], f)));
190 
191  data[i] = byte;
192 
193  if (is) {
194  if (i == 3 || i == 5 || i == 7 || i == 9) {
195  is >> c;
196  if (c != is.widen('-')) is.setstate(std::ios_base::failbit);
197  }
198  }
199  }
200 
201  if (is) {
202  memcpy(u.data(), data, 16);
203  //std::copy(data, data + 16, u.data());
204  }
205  }
206  return is;
207  }
208 
209 #ifdef _MSC_VER
210 #pragma warning( push )
211 #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant
212 #endif
214  {
215  if (sizeof(size_t) > 4) {
216  return agx::hash(size_t(m_data.ab ^ m_data.cd));
217  }
218  else {
219  uint64_t hash64 = m_data.ab ^ m_data.cd;
220  return agx::hash(size_t(uint32_t(hash64 >> 32) ^ uint32_t(hash64)));
221  }
222  }
223 #ifdef _MSC_VER
224 #pragma warning( pop )
225 #endif
226 
232  {
233  public:
236 
239 
242 
243  private:
244  std::uniform_int_distribution<uint64_t> m_dist;
245 
246  };
247 
248 
249 } //namespace agx
250 
251 #ifndef SWIG
253 
254 #endif
255 
256 
257 #ifdef _MSC_VER
258 #pragma warning( pop )
259 #endif
260 
261 #endif
#define AGX_TYPE_BINDING(_Type, _Name)
Definition: Type.h:179
#define AGXCORE_EXPORT
Generator of UUID values based on V4 http://en.wikipedia.org/wiki/Universally_unique_identifier.
Definition: Uuid.h:232
UuidGenerator()
Constructor.
~UuidGenerator()
Destructor.
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:42
UInt32 hash() const
Definition: Uuid.h:213
agx::String str() const
bool operator<(const Uuid &rhs) const
Definition: Uuid.h:64
bool operator>(const Uuid &rhs) const
Definition: Uuid.h:72
bool isNil() const
If this method returns true, the str() method will return a string of zeros.
Definition: Uuid.h:84
Uuid(const agx::String &uuidString)
Constructor, will construct a uuid based on the given Uuis formatted string.
bool operator==(const Uuid &rhs) const
Definition: Uuid.h:54
agx::UInt8 * data()
bool operator!=(const Uuid &rhs) const
Definition: Uuid.h:59
const agx::UInt8 * data() const
Uuid()
Constructor.
static size_t size()
Definition: Uuid.h:90
Data m_data
Definition: Uuid.h:141
static Uuid generateFromString(const agx::String &seedString)
Generate a valid UUID string from a specified input string.
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
AGXCORE_EXPORT UInt32 hash(const T &key)
Definition: HashFunction.h:65
uint8_t UInt8
Definition: Integer.h:38
std::ostream & operator<<(std::ostream &stream, const AtomicValueT< T > &value)
Definition: AtomicValue.h:109
std::istream & operator>>(std::istream &input, Vec2 &vec)
Definition: io_utils.h:37
Data(uint64_t xab, uint64_t xcd)
Definition: Uuid.h:134
uint64_t ab
Definition: Uuid.h:137
uint64_t cd
Definition: Uuid.h:138
Sorting functor of an uuid.
Definition: Uuid.h:105
bool operator()(const T &_Left, const T &_Right) const
Definition: Uuid.h:107