HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_Guid.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_Guid.h ( UT Library, C++)
7  *
8  * COMMENTS: This class is a globally unique identifier. Simply creating
9  * one of these objects assigns a new unique value. These values
10  * are keyed to the host machine, process id, time, and are
11  * incremented on each request.
12  *
13  * ****************** IMPORTANT NOTE: *********************
14  * This implementation isn't guaranteed to produce unique values
15  * because we use the IP address rather than a MAC address. It
16  * has a privacy hole because the IP address is hashed in a way
17  * that allows the original address to be recovered (which
18  * would be more of a concern if we actualy used the MAC
19  * address). And it is not thread safe. The unique counter
20  * value used as the final int should be made a SYS_AtomicInt
21  * to fix this last issue.
22  *
23  * Current usage of this class is in the SIM library only,
24  * which doesn't require true global uniqueness or thread
25  * safety. But future uses of this class should beware of
26  * these limitations and fix these problems as required.
27  */
28 
29 #ifndef __UT_Guid__
30 #define __UT_Guid__
31 
32 #include "UT_API.h"
33 #include <iosfwd>
34 #include "UT_Hash.h"
35 
36 class UT_String;
37 class UT_StringHolder;
38 class UT_WorkBuffer;
39 class UT_IStream;
40 class UT_Lock;
41 
43 {
44 public:
45  UT_Guid();
46  UT_Guid(const UT_Guid &src);
47  ~UT_Guid();
48 
49  enum { theStringSize = 43 };
50  enum { theBinarySize = 16 };
51  bool setString(const char *str);
52  void getString(UT_String &str) const;
53  void getString(UT_StringHolder &str) const;
54  void getString(UT_WorkBuffer &str) const;
55 
56  bool operator==(const UT_Guid &cmp) const;
57  bool operator!=(const UT_Guid &cmp) const;
58  const UT_Guid &operator=(const UT_Guid &src);
59 
60  void writeBinary(std::ostream &os) const;
61  bool readBinary(UT_IStream &is);
62  bool readAscii(UT_IStream &is);
63 
64  size_t hash() const { return SYSwang_inthash(myUniqueCount); }
65 
66  friend UT_API std::ostream &operator<<(std::ostream &os,
67  const UT_Guid &guid);
68 
69 private:
70  static void initializeValues();
71 
72  int myRandom;
73  int myPid;
74  int myTime;
75  int myUniqueCount;
76 
77  static int theRandomBase;
78  static int thePid;
79  static int theStartTime;
80  static bool theValuesInitialized;
81  static UT_Lock theValuesInitializeLock;
82 
83  friend class UT_GuidHash;
84 };
85 
86 inline size_t hash_value(const UT_Guid &guid)
87 {
88  return guid.hash();
89 }
90 
92 
93 class SYS_DEPRECATED_REPLACE(16.0, "SYShash<UT_Guid>") UT_API UT_GuidHash : public UT_Hash
94 {
95 public:
96  UT_GuidHash(const UT_Guid &uniqueid)
97  : myGuid(uniqueid)
98  { }
99  virtual ~UT_GuidHash()
100  { }
101 
102  virtual int compare(const UT_Hash &a) const
103  {
104  const UT_GuidHash &b = (const UT_GuidHash &)a;
105  int d;
106 
107  d = myGuid.myUniqueCount - b.myGuid.myUniqueCount;
108  if( d ) return ((d < 0) ? -1 : 1);
109  d = myGuid.myTime - b.myGuid.myTime;
110  if( d ) return ((d < 0) ? -1 : 1);
111  d = myGuid.myPid - b.myGuid.myPid;
112  if( d ) return ((d < 0) ? -1 : 1);
113  d = myGuid.myRandom - b.myGuid.myRandom;
114  if( d ) return ((d < 0) ? -1 : 1);
115 
116  return 0;
117  }
118  virtual void copy(const UT_Hash &a)
119  {
120  myGuid = static_cast<const UT_GuidHash&>(a).myGuid;
121  }
122  virtual unsigned hash() const
123  {
124  return SYSwang_inthash(myGuid.myUniqueCount);
125  }
126  virtual UT_Hash *copy() const
127  {
128  return new UT_GuidHash(myGuid);
129  }
130  virtual int64 getMemoryUsage(bool inclusive) const
131  {
132  return inclusive ? sizeof(*this) : 0;
133  }
134 
135 private:
136  UT_Guid myGuid;
137 };
138 
140 
141 #endif
142 
virtual UT_Hash * copy() const
Definition: UT_Guid.h:126
#define SYS_DEPRECATED_PUSH_DISABLE()
#define SYS_DEPRECATED_POP_DISABLE()
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
virtual void copy(const UT_Hash &a)
Definition: UT_Guid.h:118
#define UT_API
Definition: UT_API.h:12
#define SYS_DEPRECATED_REPLACE(__V__, __R__)
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
long long int64
Definition: SYS_Types.h:100
size_t hash_value(const UT_Guid &guid)
Definition: UT_Guid.h:86
virtual int64 getMemoryUsage(bool inclusive) const
Definition: UT_Guid.h:130
size_t hash() const
Definition: UT_Guid.h:64
virtual ~UT_GuidHash()
Definition: UT_Guid.h:99
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
UT_GuidHash(const UT_Guid &uniqueid)
Definition: UT_Guid.h:96
int cmp(T a, T b)
Definition: ImathFun.h:119
virtual unsigned hash() const
Definition: UT_Guid.h:122
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
virtual int compare(const UT_Hash &a) const
Definition: UT_Guid.h:102
GLenum src
Definition: glcorearb.h:1792