-
Notifications
You must be signed in to change notification settings - Fork 34
/
Guid.h
111 lines (102 loc) · 4.53 KB
/
Guid.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Guid.h
*
* Created on: Feb 29, 2020
* Author: dzhou
*/
#ifndef GUID_H_
#define GUID_H_
#include <string>
#include <string.h>
using std::string;
class Guid {
public:
Guid(bool newGuid = false);
Guid(unsigned char* guid){
memcpy(uuid_, guid, 16);
}
Guid(const string& guid);
Guid(const char* guid, int len);
Guid(const Guid& copy){
memcpy(uuid_, copy.uuid_, 16);
}
Guid(unsigned long long high, unsigned long long low){
#ifndef BIGENDIANNESS
memcpy((char*)uuid_, (char*)&low, 8);
memcpy((char*)uuid_ + 8, (char*)&high, 8);
#else
memcpy((char*)uuid_, (char*)&high, 8);
memcpy((char*)uuid_ + 8, (char*)&low, 8);
#endif
}
inline bool operator==(const Guid &other) const {
const unsigned char* a = (const unsigned char*)uuid_;
const unsigned char* b = (const unsigned char*)other.uuid_;
return (*(long long*)a) == (*(long long*)b) && (*(long long*)(a+8)) == (*(long long*)(b+8));
}
inline bool operator!=(const Guid &other) const {
const unsigned char* a = (const unsigned char*)uuid_;
const unsigned char* b = (const unsigned char*)other.uuid_;
return (*(long long*)a) != (*(long long*)b) || (*(long long*)(a+8)) != (*(long long*)(b+8));
}
inline bool operator<(const Guid &other) const {
const unsigned char* a = (const unsigned char*)uuid_;
const unsigned char* b = (const unsigned char*)other.uuid_;
#ifndef BIGENDIANNESS
return (*(unsigned long long*)(a+8)) < (*(unsigned long long*)(b+8)) || ((*(unsigned long long*)(a+8)) == (*(unsigned long long*)(b+8)) && (*(unsigned long long*)a) < (*(unsigned long long*)b));
#else
return (*(unsigned long long*)a) < (*(unsigned long long*)b) || ((*(unsigned long long*)a) == (*(unsigned long long*)b) && (*(unsigned long long*)(a+8)) < (*(unsigned long long*)(b+8)));
#endif
}
inline bool operator>(const Guid &other) const {
const unsigned char* a = (const unsigned char*)uuid_;
const unsigned char* b = (const unsigned char*)other.uuid_;
#ifndef BIGENDIANNESS
return (*(unsigned long long*)(a+8)) > (*(unsigned long long*)(b+8)) || ((*(unsigned long long*)(a+8)) == (*(unsigned long long*)(b+8)) && (*(unsigned long long*)a) > (*(unsigned long long*)b));
#else
return (*(unsigned long long*)a) > (*(unsigned long long*)b) || ((*(unsigned long long*)a) == (*(unsigned long long*)b) && (*(unsigned long long*)(a+8)) > (*(unsigned long long*)(b+8)));
#endif
}
inline bool operator<=(const Guid &other) const {
const unsigned char* a = (const unsigned char*)uuid_;
const unsigned char* b = (const unsigned char*)other.uuid_;
#ifndef BIGENDIANNESS
return (*(unsigned long long*)(a+8)) < (*(unsigned long long*)(b+8)) || ((*(unsigned long long*)(a+8)) == (*(unsigned long long*)(b+8)) && (*(unsigned long long*)a) <= (*(unsigned long long*)b));
#else
return (*(unsigned long long*)a) < (*(unsigned long long*)b) || ((*(unsigned long long*)a) == (*(unsigned long long*)b) && (*(unsigned long long*)(a+8)) <= (*(unsigned long long*)(b+8)));
#endif
}
inline bool operator>=(const Guid &other) const {
const unsigned char* a = (const unsigned char*)uuid_;
const unsigned char* b = (const unsigned char*)other.uuid_;
#ifndef BIGENDIANNESS
return (*(unsigned long long*)(a+8)) > (*(unsigned long long*)(b+8)) || ((*(unsigned long long*)(a+8)) == (*(unsigned long long*)(b+8)) && (*(unsigned long long*)a) >= (*(unsigned long long*)b));
#else
return (*(unsigned long long*)a) > (*(unsigned long long*)b) || ((*(unsigned long long*)a) == (*(unsigned long long*)b) && (*(unsigned long long*)(a+8)) >= (*(unsigned long long*)(b+8)));
#endif
}
inline int compare(const Guid &other) const { return (*this < other) ? -1 : (*this > other ? 1 : 0);}
inline unsigned char operator[](int i) const { return uuid_[i];}
inline bool isZero() const {
const unsigned char* a = (const unsigned char*)uuid_;
return (*(long long*)a) == 0 && (*(long long*)(a+8)) == 0;
}
inline bool isNull() const {
const unsigned char* a = (const unsigned char*)uuid_;
return (*(long long*)a) == 0 && (*(long long*)(a+8)) == 0;
}
inline bool isValid() const {
const unsigned char* a = (const unsigned char*)uuid_;
return (*(long long*)a) != 0 || (*(long long*)(a+8)) != 0;
}
string getString() const { return getString(uuid_);}
inline const unsigned char* bytes() const { return uuid_;}
static void toGuid(const unsigned char*, char* str);
static bool fromGuid(const char* str, unsigned char* data);
static string getString(const unsigned char* guid);
static Guid ZERO;
friend std::ostream& operator<<(std::ostream &os, const Guid &guid);
private:
unsigned char uuid_[16];
};
#endif /* GUID_H_ */