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 "UT_ORMColumnType.h"
25 
26 #include <SYS/SYS_Hash.h>
27 #include <SYS/SYS_Math.h>
28 
29 class UT_StringHolder;
30 class UT_IStream;
31 class UT_WorkBuffer;
32 class UT_StringRef;
33 class UT_SqlStatement;
34 
36 {
37 public:
38  UT_SGuid(bool generate=true);
39  explicit UT_SGuid(const UT_StringRef& src);
40 
41  UT_SGuid(const UT_SGuid& other);
42  UT_SGuid& operator=(const UT_SGuid& other);
43 
44  UT_SGuid(UT_SGuid&&) = default;
45  UT_SGuid& operator=(UT_SGuid&&) = default;
46 
47  UT_SGuid& operator=(const UT_StringHolder& str);
48 
49  bool setString(const UT_StringRef& src);
50  UT_StringHolder toString() const;
51  void getString(UT_StringHolder& str) const;
52  void getString(UT_WorkBuffer& str) const;
53  void setFromInt(uint64 value);
54 
55  void writeBinary(std::ostream& os) const;
56  bool readBinary(UT_IStream& is);
57  bool readAscii(UT_IStream& is);
58 
59  /// Set the machine id if you need to identify the specific machine within
60  /// a cluster.
61  static void setMachineId(uint32 machine_id);
62 
63  size_t hash() const
64  {
65  generateIfEmpty_();
66 
67  size_t h = SYSwang_inthash(myHigh);
68  SYShashCombine(h, SYSwang_inthash(myLow));
69  return h;
70  }
71 
72  bool operator==(const UT_SGuid& sguid) const;
73  bool operator!=(const UT_SGuid& sguid) const { return !(*this == sguid); }
74 
75  friend UT_API std::ostream& operator<<(
76  std::ostream& os,
77  const UT_SGuid& guid);
78 
80  {
81  return (uint64)myHigh << 32 | myLow;
82  }
83 
84 private:
85  /// Generate the sguid for the object
86  void generate_();
87  /// Check if this object has a value already
88  bool isEmpty_() const { return myHigh == 0 && myLow == 0; }
89  /// Generate the id for the object if its currently empty. This allows users
90  /// to lazy load the id. In cases where the sguid is being loaded in this
91  /// allows the caller to generate an ID once instead of twice.
92  void generateIfEmpty_() const
93  {
94  if (isEmpty_())
95  {
96  const_cast<UT_SGuid*>(this)->generate_();
97  }
98  }
99 
100  uint32 myHigh;
101  uint32 myLow;
102 
103  static uint32 theMachineId;
104 };
105 
106 inline size_t
107 hash_value(const UT_SGuid& guid)
108 {
109  return guid.hash();
110 }
111 
112 UT_API bool UTsqlBind(UT_SqlStatement& stmt, int idx, const UT_SGuid& sguid);
113 UT_API bool UTsqlGet(const UT_SqlStatement& stmt, int idx, UT_SGuid& sguid);
114 
115 template <>
116 inline UT_ORMColumnType
118 {
120 }
121 
122 #endif // __UT_SGUID_H__
123 
SYS_NO_DISCARD_RESULT uint64 toInt64() const
Definition: UT_SGuid.h:79
bool operator!=(const UT_SGuid &sguid) const
Definition: UT_SGuid.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
UT_ORMColumnType
#define UT_API
Definition: UT_API.h:14
unsigned long long uint64
Definition: SYS_Types.h:117
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
UT_ORMColumnType UTsqlOrmColumnType< UT_SGuid >()
Definition: UT_SGuid.h:117
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
UT_API bool UTsqlBind(UT_SqlStatement &stmt, int idx, const UT_SGuid &sguid)
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:63
UT_API bool UTsqlGet(const UT_SqlStatement &stmt, int idx, UT_SGuid &sguid)
size_t hash_value(const UT_SGuid &guid)
Definition: UT_SGuid.h:107
GLenum src
Definition: glcorearb.h:1793