Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef UT_UUID_H
00027 #define UT_UUID_H
00028
00029
00030
00031
00032
00033 #ifndef UT_TYPES_H
00034 #include "ut_types.h"
00035 #endif
00036 #include <time.h>
00037
00038 #include <memory>
00039 #include <string>
00040
00041 #include "ut_option.h"
00042
00043
00044 enum UT_UUIDVariant
00045 {
00046 UT_UUID_VARIANT_NCS = 0,
00047 UT_UUID_VARIANT_DCE = 1,
00048 UT_UUID_VARIANT_MICROSOFT = 2,
00049 UT_UUID_VARIANT_OTHER = 3,
00050 UT_UUID_VARIANT_ERROR = 0xffffffff
00051 };
00052
00053 struct uuid
00054 {
00055 UT_uint32 time_low;
00056 UT_uint16 time_mid;
00057 UT_uint16 time_high_and_version;
00058 UT_uint16 clock_seq;
00059 UT_Byte node[6];
00060 };
00061
00062
00063 class UT_UUIDGenerator;
00064
00065
00078 class ABI_EXPORT UT_UUID
00079 {
00080 public:
00081 friend void UT_UUIDGenerator__test(UT_UUIDGenerator*);
00082 public:
00083
00084
00085
00086
00087
00088
00089 virtual ~UT_UUID (){};
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 bool makeUUID();
00100 bool makeUUID(std::string & out);
00101
00102
00103 bool setUUID(const std::string &s);
00104 bool setUUID(const char *s);
00105 bool setUUID(const struct uuid &u);
00106
00107
00108
00109 UT_Option<std::string> toString() const;
00110
00111
00112 bool toBinary(struct uuid &u) const;
00113
00114
00115 static bool toStringFromBinary(char * s, UT_uint32 len, const struct uuid &u);
00116
00117
00118
00119 UT_uint32 hash32() const;
00120 UT_uint64 hash64() const;
00121
00122
00123
00124 static const UT_UUID & getNull() {return s_Null;}
00125
00126
00127
00128 time_t getTime() const;
00129 UT_sint32 getType() const;
00130 UT_UUIDVariant getVariant() const;
00131
00132 bool resetTime();
00133
00134
00135
00136
00137 bool operator ==(const UT_UUID &u) const;
00138 bool operator !=(const UT_UUID &u) const;
00139 bool operator < (const UT_UUID &u) const;
00140 bool operator > (const UT_UUID &u) const;
00141
00142 UT_UUID & operator = (const UT_UUID &u);
00143
00144
00145 bool isOlder(const UT_UUID &u) const;
00146 bool isYounger(const UT_UUID &u) const;
00147 bool isOfSameAge(const UT_UUID &u) const;
00148
00149 bool isValid() const {return m_bIsValid;}
00150 bool isNull() const;
00151
00152
00153 void clear();
00154
00155 protected:
00156 friend class UT_UUIDGenerator;
00157
00158
00159
00160 UT_UUID();
00161 UT_UUID(const std::string &s);
00162 UT_UUID(const char *s);
00163 UT_UUID(const struct uuid&u);
00164 UT_UUID(const UT_UUID &u);
00165
00166
00167
00168
00169 virtual bool _getRandomBytes(void *buf, int nbytes) const;
00170
00171 private:
00172 bool _parse(const char * in, struct uuid &u) const;
00173
00174 bool _makeUUID(struct uuid & u);
00175 std::string _toString(const struct uuid &uu) const;
00176
00177
00178
00179
00180 static time_t _getTime(const struct uuid & uu);
00181 static UT_sint32 _getType(const struct uuid &uu);
00182 static UT_UUIDVariant _getVariant(const struct uuid &uu);
00183
00184 bool _getClock(UT_uint32 &iHigh, UT_uint32 &iLow, UT_uint16 &iSeq) const;
00185
00186 private:
00187 uuid m_uuid;
00188 bool m_bIsValid;
00189 static bool s_bInitDone;
00190 static unsigned char s_node[6];
00191 static UT_UUID s_Null;
00192 };
00193
00194 typedef std::unique_ptr<UT_UUID> UT_UUIDPtr;
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 class ABI_EXPORT UT_UUIDGenerator
00206 {
00207 public:
00208 UT_UUIDGenerator()
00209 :m_pUUID(NULL)
00210 {
00211 };
00212
00213 virtual ~UT_UUIDGenerator(){if(m_pUUID) delete m_pUUID;};
00214
00215
00216
00217 virtual UT_UUID * createUUID(){UT_UUID *p = new UT_UUID(); if(p)p->makeUUID(); return p;}
00218
00219 virtual UT_UUID * createUUID(const std::string &s){return new UT_UUID(s);}
00220 virtual UT_UUID * createUUID(const char *s){return new UT_UUID(s);}
00221 virtual UT_UUID * createUUID(const UT_UUID &u){return new UT_UUID(u);}
00222 virtual UT_UUID * createUUID(const struct uuid &u){return new UT_UUID(u);}
00223
00224 UT_uint32 getNewUUID32();
00225 UT_uint64 getNewUUID64();
00226
00227 public:
00228 friend void UT_UUIDGenerator__test(UT_UUIDGenerator*);
00229
00230 private:
00231
00232 UT_UUID * m_pUUID;
00233 };
00234
00235 #endif