VirtualBox

source: vbox/trunk/include/VBox/com/Guid.h@ 27023

Last change on this file since 27023 was 26753, checked in by vboxsync, 15 years ago

Main: Bstr makeover (third attempt) -- make Bstr(NULL) and Bstr() behave the same; resulting cleanup; make some more internal methods use Utf8Str instead of Bstr; fix a lot of CheckComArgNotNull??() usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/* $Id: Guid.h 26753 2010-02-24 16:24:33Z 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/cpp/utils.h>
51#include <iprt/uuid.h>
52
53namespace com
54{
55
56/**
57 * Helper class that represents the UUID type and hides platform-specific
58 * implementation details.
59 */
60class Guid
61{
62public:
63
64 Guid()
65 {
66 ::RTUuidClear(&uuid);
67 refresh();
68 }
69
70 Guid(const Guid &that)
71 {
72 uuid = that.uuid;
73 refresh();
74 }
75
76 Guid(const RTUUID &that)
77 {
78 uuid = that;
79 refresh();
80 }
81
82 Guid(const GUID &that)
83 {
84 AssertCompileSize(GUID, sizeof(RTUUID));
85 ::memcpy(&uuid, &that, sizeof(GUID));
86 refresh();
87 }
88
89 Guid(const char *that)
90 {
91 ::RTUuidClear(&uuid);
92 ::RTUuidFromStr(&uuid, that);
93 refresh();
94 }
95
96 Guid(const Bstr &that)
97 {
98 ::RTUuidClear(&uuid);
99 if (!that.isEmpty())
100 ::RTUuidFromUtf16(&uuid, that.raw());
101 refresh();
102 }
103
104 Guid& operator=(const Guid &that)
105 {
106 ::memcpy(&uuid, &that.uuid, sizeof (RTUUID));
107 refresh();
108 return *this;
109 }
110 Guid& operator=(const GUID &guid)
111 {
112 ::memcpy(&uuid, &guid, sizeof (GUID));
113 refresh();
114 return *this;
115 }
116 Guid& operator=(const RTUUID &guid)
117 {
118 ::memcpy(&uuid, &guid, sizeof (RTUUID));
119 refresh();
120 return *this;
121 }
122 Guid& operator=(const char *str)
123 {
124 ::RTUuidFromStr(&uuid, str);
125 refresh();
126 return *this;
127 }
128
129 void create()
130 {
131 ::RTUuidCreate(&uuid);
132 refresh();
133 }
134 void clear()
135 {
136 ::RTUuidClear(&uuid);
137 refresh();
138 }
139
140 Utf8Str toString() const
141 {
142 char buf[RTUUID_STR_LENGTH];
143 ::RTUuidToStr(&uuid, buf, RTUUID_STR_LENGTH);
144 return Utf8Str(buf);
145 }
146
147 Bstr toUtf16() const
148 {
149 if (isEmpty())
150 return Bstr();
151
152 RTUTF16 buf[RTUUID_STR_LENGTH];
153 ::RTUuidToUtf16(&uuid, buf, RTUUID_STR_LENGTH);
154 return Bstr(buf);
155 }
156
157 bool isEmpty() const
158 {
159 return ::RTUuidIsNull (&uuid);
160 }
161
162 operator bool() const
163 {
164 return !isEmpty();
165 }
166
167 bool operator==(const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) == 0; }
168 bool operator==(const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) == 0; }
169 bool operator!=(const Guid &that) const { return !operator==(that); }
170 bool operator!=(const GUID &guid) const { return !operator==(guid); }
171 bool operator<(const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) < 0; }
172 bool operator<(const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) < 0; }
173
174 /* to pass instances as IN_GUID parameters to interface methods */
175 operator const GUID&() const
176 {
177 return *(GUID *) &uuid;
178 }
179
180 /* to directly pass instances to RTPrintf("%RTuuid") */
181 PRTUUID ptr()
182 {
183 return &uuid;
184 }
185
186 /* to pass instances to printf-like functions */
187 PCRTUUID raw() const
188 {
189 return &uuid;
190 }
191
192 /* to pass instances to RTUuid*() as a constant argument */
193 operator const RTUUID*() const
194 {
195 return &uuid;
196 }
197
198#if !defined (VBOX_WITH_XPCOM)
199
200 /* to assign instances to OUT_GUID parameters from within the
201 * interface method */
202 const Guid &cloneTo (GUID *pguid) const
203 {
204 if (pguid)
205 ::memcpy(pguid, &uuid, sizeof(GUID));
206 return *this;
207 }
208
209 /* to pass instances as OUT_GUID parameters to interface methods */
210 GUID *asOutParam()
211 {
212 return (GUID*)&uuid;
213 }
214
215#else
216
217 /* to assign instances to OUT_GUID parameters from within the
218 * interface method */
219 const Guid &cloneTo (nsID **ppguid) const
220 {
221 if (ppguid) { *ppguid = (nsID *) nsMemory::Clone (&uuid, sizeof (nsID)); }
222 return *this;
223 }
224
225 // internal helper class for asOutParam(); this takes a GUID refrence
226 // in the constructor and copies the uuid from the method to that instance
227 // in its destructor
228 class GuidOutParam
229 {
230 GuidOutParam(Guid &guid)
231 : ptr(0),
232 outer(guid)
233 {
234 outer.clear();
235 }
236
237 nsID *ptr;
238 Guid &outer;
239 GuidOutParam(const GuidOutParam &that); // disabled
240 GuidOutParam &operator=(const GuidOutParam &that); // disabled
241 public:
242 operator nsID**() { return &ptr; }
243 ~GuidOutParam()
244 {
245 if (ptr && outer.isEmpty())
246 {
247 outer = *ptr;
248 outer.refresh();
249 nsMemory::Free(ptr);
250 }
251 }
252 friend class Guid;
253 };
254
255 /* to pass instances as OUT_GUID parameters to interface methods */
256 GuidOutParam asOutParam() { return GuidOutParam(*this); }
257
258#endif
259
260 /* to directly test IN_GUID interface method's parameters */
261 static bool isEmpty(const GUID &guid)
262 {
263 return ::RTUuidIsNull((PRTUUID)&guid);
264 }
265
266 /**
267 * Static immutable empty object. May be used for comparison purposes.
268 */
269 static const Guid Empty;
270
271private:
272 // in debug code, refresh the UUID string representatino for
273 // debugging; must be called every time the internal uuid
274 // changes; compiles to nothing in release code
275 inline void refresh()
276 {
277#ifdef DEBUG
278 ::RTUuidToStr(&uuid, szUUID, RTUUID_STR_LENGTH);
279 pcszUUID = szUUID;
280#endif
281 }
282
283 RTUUID uuid;
284
285#ifdef DEBUG
286 // in debug builds, have a Utf8Str representation of the UUID so we can look
287 // at it in the debugger more easily
288 char szUUID[RTUUID_STR_LENGTH];
289 const char *pcszUUID;
290#endif
291};
292
293inline Bstr asGuidStr(const Bstr& str)
294{
295 Guid guid(str);
296 return guid.isEmpty() ? Bstr() : guid.toUtf16();
297}
298
299inline bool isValidGuid(const Bstr& str)
300{
301 Guid guid(str);
302 return !guid.isEmpty();
303}
304
305
306/* work around error C2593 of the stupid MSVC 7.x ambiguity resolver */
307WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Guid)
308
309} /* namespace com */
310
311#endif /* ___VBox_com_Guid_h */
312
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette