-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUID.cpp
101 lines (79 loc) · 1.79 KB
/
GUID.cpp
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
#include <Rpc.h>
#pragma comment(lib, "Rpcrt4.lib")
namespace Utility {
class GUID {
#if defined(UNICODE) || defined(_UNICODE)
using RPC_STR = ::RPC_WSTR;
#else
using RPC_STR = ::RPC_CSTR;
#endif // defined(UNICODE) || defined(_UNICODE)
public:
GUID()
: mStatus(::UuidCreate(&mUUID)),
mIsGlobal(mStatus != RPC_S_UUID_LOCAL_ONLY),
mIsGood(mStatus != RPC_S_UUID_NO_ADDRESS) {
}
~GUID() {
Uninitialize();
}
GUID(const GUID& guid) {
*this = guid;
}
GUID& operator=(const GUID& right) {
Uninitialize();
mUUID = right.mUUID;
mStatus = RPC_S_OK;
mIsGood = right.mIsGood;
mIsGlobal = right.mIsGlobal;
return *this;
}
bool operator==(const GUID& right) {
return mUUID == right.mUUID;
}
bool IsGlobal() const {
return mIsGlobal;
}
bool IsGood() const {
return mIsGood;
}
void SetUUID(const ::UUID& uuid) {
Uninitialize();
mUUID = uuid;
mStatus = RPC_S_OK;
mIsGood = true;
mIsGlobal = false;
}
void SetUUID(RPC_CSTR uuidStr) {
Uninitialize();
mStatus = ::UuidFromString(uuidStr, &mUUID);
mIsGood = mStatus == RPC_S_OK;
mIsGlobal = false;
}
const RPC_STR& GetStr() {
if (!mUUIDString) {
mStatus = ::UuidToString(&mUUID, &mUUIDString);
}
return mStatus == RPC_S_OK ? mUUIDString : mUUIDStringDefault;
}
const ::UUID& GetUUID() const {
return mIsGood ? mUUID : mUUIDDefault;
}
::RPC_STATUS GetStatus() const {
return mStatus;
}
private:
void Uninitialize() {
if (mUUIDString) {
::RpcStringFree(&mUUIDString);
mUUIDString = nullptr;
}
}
::UUID mUUID = GUID_NULL;
static inline ::UUID mUUIDDefault {};
RPC_STR mUUIDString {};
static inline RPC_STR mUUIDStringDefault = (RPC_STR)TEXT("");
::RPC_STATUS mStatus {};
bool mIsGood {};
bool mIsGlobal {};
};
}