HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_SGuid.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_SGuid.h
7  *
8  * COMMENTS:
9  *
10  * A small guid implementation. A "small" guid uses 64 bits instead of 128 and
11  * is not truly globally unique. A "small" guid is meant to be used in cases
12  * where uniqueness is necessary but space is equally as important. If true
13  * "guaranteed" uniqueness is required then UT_Guid should be used instead
14  * at the cost of space.
15  *
16  * reference: https://devblogs.microsoft.com/oldnewthing/20080627-00/?p=21823
17  */
18 
19 #ifndef __UT_SGUID_H__
20 #define __UT_SGUID_H__
21 
22 #include "UT_API.h"
23 
24 #include <SYS/SYS_Hash.h>
25 #include <SYS/SYS_Math.h>
26 
27 class UT_StringHolder;
28 class UT_IStream;
29 class UT_WorkBuffer;
30 class UT_StringRef;
31 
33 {
34 public:
35  UT_SGuid(bool generate=true);
36  UT_SGuid(const UT_StringRef& src);
37 
38  UT_SGuid(const UT_SGuid& other);
39  UT_SGuid& operator=(const UT_SGuid& other);
40 
41  UT_SGuid(UT_SGuid&&) = default;
42  UT_SGuid& operator=(UT_SGuid&&) = default;
43 
44  bool setString(const UT_StringRef& src);
45  UT_StringHolder toString() const;
46  void getString(UT_StringHolder& str) const;
47  void getString(UT_WorkBuffer& str) const;
48 
49  void writeBinary(std::ostream& os) const;
50  bool readBinary(UT_IStream& is);
51  bool readAscii(UT_IStream& is);
52 
53  /// Set the machine id if you need to identify the specific machine within
54  /// a cluster.
55  static void setMachineId(uint32 machine_id);
56 
57  size_t hash() const
58  {
59  generateIfEmpty_();
60 
61  size_t h = SYSwang_inthash(myHigh);
62  SYShashCombine(h, SYSwang_inthash(myLow));
63  return h;
64  }
65 
66  bool operator==(const UT_SGuid& sguid) const;
67  bool operator!=(const UT_SGuid& sguid) const { return !(*this == sguid); }
68 
69  friend UT_API std::ostream& operator<<(
70  std::ostream& os,
71  const UT_SGuid& guid);
72 
73 private:
74  /// Generate the sguid for the object
75  void generate_();
76  /// Check if this object has a value already
77  bool isEmpty_() const { return myHigh == 0 && myLow == 0; }
78  /// Generate the id for the object if its currently empty. This allows users
79  /// to lazy load the id. In cases where the sguid is being loaded in this
80  /// allows the caller to generate an ID once instead of twice.
81  void generateIfEmpty_() const
82  {
83  if (isEmpty_())
84  {
85  const_cast<UT_SGuid*>(this)->generate_();
86  }
87  }
88 
89  uint32 myHigh;
90  uint32 myLow;
91 
92  static uint32 theMachineId;
93 };
94 
95 inline size_t
96 hash_value(const UT_SGuid& guid)
97 {
98  return guid.hash();
99 }
100 
101 #endif // __UT_SGUID_H__
102 
bool operator!=(const UT_SGuid &sguid) const
Definition: UT_SGuid.h:67
#define UT_API
Definition: UT_API.h:14
std::ostream & operator<<(std::ostream &ostr, const DataType &a)
Definition: DataType.h:133
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
LeafData & operator=(const LeafData &)=delete
unsigned int uint32
Definition: SYS_Types.h:40
size_t hash() const
Definition: UT_SGuid.h:57
size_t hash_value(const UT_SGuid &guid)
Definition: UT_SGuid.h:96
GLenum src
Definition: glcorearb.h:1793