1 | /* $Id: Guid.h 19239 2009-04-28 13:19:14Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * MS COM / XPCOM Abstraction Layer:
|
---|
5 | * Guid class declaration
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | *
|
---|
28 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
29 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
30 | * additional information or have any questions.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #ifndef ___VBox_com_Guid_h
|
---|
34 | #define ___VBox_com_Guid_h
|
---|
35 |
|
---|
36 | /* Make sure all the stdint.h macros are included - must come first! */
|
---|
37 | #ifndef __STDC_LIMIT_MACROS
|
---|
38 | # define __STDC_LIMIT_MACROS
|
---|
39 | #endif
|
---|
40 | #ifndef __STDC_CONSTANT_MACROS
|
---|
41 | # define __STDC_CONSTANT_MACROS
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #if defined (VBOX_WITH_XPCOM)
|
---|
45 | #include <nsMemory.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include "VBox/com/string.h"
|
---|
49 |
|
---|
50 | #include <iprt/cpputils.h>
|
---|
51 | #include <iprt/uuid.h>
|
---|
52 |
|
---|
53 | namespace com
|
---|
54 | {
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Helper class that represents the UUID type and hides platform-specific
|
---|
58 | * implementation details.
|
---|
59 | */
|
---|
60 | class Guid
|
---|
61 | {
|
---|
62 | public:
|
---|
63 |
|
---|
64 | Guid () { ::RTUuidClear (&uuid); }
|
---|
65 | Guid (const Guid &that) { uuid = that.uuid; }
|
---|
66 | Guid (const RTUUID &that) { uuid = that; }
|
---|
67 |
|
---|
68 | Guid (const GUID &that)
|
---|
69 | {
|
---|
70 | AssertCompileSize (GUID, sizeof (RTUUID));
|
---|
71 | ::memcpy (&uuid, &that, sizeof (GUID));
|
---|
72 | }
|
---|
73 |
|
---|
74 | Guid (const char *that)
|
---|
75 | {
|
---|
76 | ::RTUuidClear (&uuid);
|
---|
77 | ::RTUuidFromStr (&uuid, that);
|
---|
78 | }
|
---|
79 |
|
---|
80 | Guid (const Bstr &that)
|
---|
81 | {
|
---|
82 | ::RTUuidClear (&uuid);
|
---|
83 | if (!that.isNull())
|
---|
84 | ::RTUuidFromUtf16(&uuid, that.raw());
|
---|
85 | }
|
---|
86 |
|
---|
87 | Guid &operator= (const Guid &that)
|
---|
88 | {
|
---|
89 | ::memcpy (&uuid, &that.uuid, sizeof (RTUUID));
|
---|
90 | return *this;
|
---|
91 | }
|
---|
92 | Guid &operator= (const GUID &guid)
|
---|
93 | {
|
---|
94 | ::memcpy (&uuid, &guid, sizeof (GUID));
|
---|
95 | return *this;
|
---|
96 | }
|
---|
97 | Guid &operator= (const RTUUID &guid)
|
---|
98 | {
|
---|
99 | ::memcpy (&uuid, &guid, sizeof (RTUUID));
|
---|
100 | return *this;
|
---|
101 | }
|
---|
102 | Guid &operator= (const char *str)
|
---|
103 | {
|
---|
104 | ::RTUuidFromStr (&uuid, str);
|
---|
105 | return *this;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void create() { ::RTUuidCreate (&uuid); }
|
---|
109 | void clear() { ::RTUuidClear (&uuid); }
|
---|
110 |
|
---|
111 | Utf8Str toString () const
|
---|
112 | {
|
---|
113 | char buf [RTUUID_STR_LENGTH];
|
---|
114 | ::RTUuidToStr (&uuid, buf, RTUUID_STR_LENGTH);
|
---|
115 | return Utf8Str (buf);
|
---|
116 | }
|
---|
117 |
|
---|
118 | Bstr toUtf16 () const
|
---|
119 | {
|
---|
120 | if (isEmpty())
|
---|
121 | return Bstr();
|
---|
122 |
|
---|
123 | RTUTF16 buf [RTUUID_STR_LENGTH];
|
---|
124 | ::RTUuidToUtf16 (&uuid, buf, RTUUID_STR_LENGTH);
|
---|
125 | return Bstr (buf);
|
---|
126 | }
|
---|
127 |
|
---|
128 | bool isEmpty() const { return ::RTUuidIsNull (&uuid); }
|
---|
129 | operator bool() const { return !isEmpty(); }
|
---|
130 |
|
---|
131 | bool operator== (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) == 0; }
|
---|
132 | bool operator== (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) == 0; }
|
---|
133 | bool operator!= (const Guid &that) const { return !operator==(that); }
|
---|
134 | bool operator!= (const GUID &guid) const { return !operator==(guid); }
|
---|
135 | bool operator< (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) < 0; }
|
---|
136 | bool operator< (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) < 0; }
|
---|
137 |
|
---|
138 | /* to pass instances as IN_GUID parameters to interface methods */
|
---|
139 | operator const GUID &() const { return *(GUID *) &uuid; }
|
---|
140 |
|
---|
141 | /* to directly pass instances to RTPrintf("%Vuuid") */
|
---|
142 | PRTUUID ptr() { return &uuid; }
|
---|
143 |
|
---|
144 | /* to pass instances to printf-like functions */
|
---|
145 | PCRTUUID raw() const { return &uuid; }
|
---|
146 |
|
---|
147 | /* to pass instances to RTUuid*() as a constant argument */
|
---|
148 | operator const RTUUID * () const { return &uuid; }
|
---|
149 |
|
---|
150 | #if !defined (VBOX_WITH_XPCOM)
|
---|
151 |
|
---|
152 | /* to assign instances to OUT_GUID parameters from within the
|
---|
153 | * interface method */
|
---|
154 | const Guid &cloneTo (GUID *pguid) const
|
---|
155 | {
|
---|
156 | if (pguid) { ::memcpy (pguid, &uuid, sizeof (GUID)); }
|
---|
157 | return *this;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* to pass instances as OUT_GUID parameters to interface methods */
|
---|
161 | GUID *asOutParam() { return (GUID *) &uuid; }
|
---|
162 |
|
---|
163 | #else
|
---|
164 |
|
---|
165 | /* to assign instances to OUT_GUID parameters from within the
|
---|
166 | * interface method */
|
---|
167 | const Guid &cloneTo (nsID **ppguid) const
|
---|
168 | {
|
---|
169 | if (ppguid) { *ppguid = (nsID *) nsMemory::Clone (&uuid, sizeof (nsID)); }
|
---|
170 | return *this;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* internal helper class for asOutParam() */
|
---|
174 | class GuidOutParam
|
---|
175 | {
|
---|
176 | GuidOutParam (Guid &guid) : ptr (0), outer (guid) { outer.clear(); }
|
---|
177 | nsID *ptr;
|
---|
178 | Guid &outer;
|
---|
179 | GuidOutParam (const GuidOutParam &that); // disabled
|
---|
180 | GuidOutParam &operator= (const GuidOutParam &that); // disabled
|
---|
181 | public:
|
---|
182 | operator nsID **() { return &ptr; }
|
---|
183 | ~GuidOutParam()
|
---|
184 | {
|
---|
185 | if (ptr && outer.isEmpty()) { outer = *ptr; nsMemory::Free (ptr); }
|
---|
186 | }
|
---|
187 | friend class Guid;
|
---|
188 | };
|
---|
189 |
|
---|
190 | /* to pass instances as OUT_GUID parameters to interface methods */
|
---|
191 | GuidOutParam asOutParam() { return GuidOutParam (*this); }
|
---|
192 |
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | /* to directly test IN_GUID interface method's parameters */
|
---|
196 | static bool isEmpty (const GUID &guid)
|
---|
197 | {
|
---|
198 | return ::RTUuidIsNull ((PRTUUID) &guid);
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Static immutable empty object. May be used for comparison purposes.
|
---|
203 | */
|
---|
204 | static const Guid Empty;
|
---|
205 |
|
---|
206 | private:
|
---|
207 |
|
---|
208 | RTUUID uuid;
|
---|
209 | };
|
---|
210 |
|
---|
211 | inline Bstr asGuidStr(const Bstr& str)
|
---|
212 | {
|
---|
213 | Guid guid(str);
|
---|
214 | return guid.isEmpty() ? Bstr() : guid.toUtf16();
|
---|
215 | }
|
---|
216 |
|
---|
217 | inline bool isValidGuid(const Bstr& str)
|
---|
218 | {
|
---|
219 | Guid guid(str);
|
---|
220 | return !guid.isEmpty();
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /* work around error C2593 of the stupid MSVC 7.x ambiguity resolver */
|
---|
225 | WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Guid)
|
---|
226 |
|
---|
227 | } /* namespace com */
|
---|
228 |
|
---|
229 | #endif /* ___VBox_com_Guid_h */
|
---|
230 |
|
---|