VirtualBox

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

Last change on this file since 76585 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1/* $Id: Guid.h 76585 2019-01-01 06:31:29Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - Guid class declaration.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef VBOX_INCLUDED_com_Guid_h
28#define VBOX_INCLUDED_com_Guid_h
29#ifndef RT_WITHOUT_PRAGMA_ONCE
30# pragma once
31#endif
32
33/* Make sure all the stdint.h macros are included - must come first! */
34#ifndef __STDC_LIMIT_MACROS
35# define __STDC_LIMIT_MACROS
36#endif
37#ifndef __STDC_CONSTANT_MACROS
38# define __STDC_CONSTANT_MACROS
39#endif
40
41#include "VBox/com/string.h"
42
43#include <iprt/uuid.h>
44
45
46/** @defgroup grp_com_guid GUID Class
47 * @ingroup grp_com
48 * @{
49 */
50
51namespace com
52{
53
54typedef enum GuidState_t
55{
56 GUID_ZERO,
57 GUID_NORMAL,
58 GUID_INVALID
59} GuidState_t;
60
61/**
62 * Helper class that represents the UUID type and hides platform-specific
63 * implementation details.
64 */
65class Guid
66{
67public:
68
69 Guid()
70 {
71 clear();
72 }
73
74 Guid(const Guid &that)
75 {
76 mUuid = that.mUuid;
77 mGuidState = that.mGuidState;
78 dbg_refresh();
79 }
80
81 Guid(const RTUUID &that)
82 {
83 mUuid = that;
84 updateState();
85 dbg_refresh();
86 }
87
88 Guid(const GUID &that)
89 {
90 AssertCompileSize(GUID, sizeof(RTUUID));
91 ::memcpy(&mUuid, &that, sizeof(GUID));
92 updateState();
93 dbg_refresh();
94 }
95
96 /**
97 * Construct a GUID from a string.
98 *
99 * @param that The UUID string. Can be with or without the curly
100 * brackets. Empty strings are translated to a zero
101 * GUID, and strings which are not confirming to
102 * valid GUID string representations are marked as
103 * invalid.
104 */
105 Guid(const char *that)
106 {
107 initString(that);
108 }
109
110 /**
111 * Construct a GUID from a BSTR.
112 *
113 * @param that The UUID BSTR. Can be with or without the curly
114 * brackets. Empty strings are translated to a zero
115 * GUID, and strings which are not confirming to
116 * valid GUID string representations are marked as
117 * invalid.
118 */
119 Guid(CBSTR that)
120 {
121 initBSTR(that);
122 }
123
124 /**
125 * Construct a GUID from a Utf8Str.
126 *
127 * @param that The UUID Utf8Str. Can be with or without the curly
128 * brackets. Empty strings are translated to a zero
129 * GUID, and strings which are not confirming to
130 * valid GUID string representations are marked as
131 */
132 Guid(const Utf8Str &that)
133 {
134 initString(that.c_str());
135 }
136
137 /**
138 * Construct a GUID from a RTCString.
139 *
140 * @param that The UUID RTCString. Can be with or without the curly
141 * brackets. Empty strings are translated to a zero
142 * GUID, and strings which are not confirming to
143 * valid GUID string representations are marked as
144 */
145 Guid(const RTCString &that)
146 {
147 initString(that.c_str());
148 }
149
150 /**
151 * Construct a GUID from a Bstr.
152 *
153 * @param that The UUID Bstr. Can be with or without the curly
154 * brackets. Empty strings are translated to a zero
155 * GUID, and strings which are not confirming to
156 * valid GUID string representations are marked as
157 */
158 Guid(const Bstr &that)
159 {
160 initBSTR(that.raw());
161 }
162
163 Guid& operator=(const Guid &that)
164 {
165 mUuid = that.mUuid;
166 mGuidState = that.mGuidState;
167 dbg_refresh();
168 return *this;
169 }
170
171 Guid& operator=(const RTUUID &guid)
172 {
173 mUuid = guid;
174 updateState();
175 dbg_refresh();
176 return *this;
177 }
178
179 Guid& operator=(const GUID &guid)
180 {
181 AssertCompileSize(GUID, sizeof(RTUUID));
182 ::memcpy(&mUuid, &guid, sizeof(GUID));
183 updateState();
184 dbg_refresh();
185 return *this;
186 }
187
188 Guid& operator=(const char *str)
189 {
190 initString(str);
191 return *this;
192 }
193
194 Guid& operator=(CBSTR str)
195 {
196 initBSTR(str);
197 return *this;
198 }
199
200 Guid& operator=(const Utf8Str &str)
201 {
202 return operator=(str.c_str());
203 }
204
205 Guid& operator=(const RTCString &str)
206 {
207 return operator=(str.c_str());
208 }
209
210 Guid& operator=(const Bstr &str)
211 {
212 return operator=(str.raw());
213 }
214
215 void create()
216 {
217 ::RTUuidCreate(&mUuid);
218 mGuidState = GUID_NORMAL;
219 dbg_refresh();
220 }
221
222 void clear()
223 {
224 makeClear();
225 dbg_refresh();
226 }
227
228 /**
229 * Convert the GUID to a string.
230 *
231 * @returns String object containing the formatted GUID.
232 * @throws std::bad_alloc
233 */
234 Utf8Str toString() const
235 {
236 if (mGuidState == GUID_INVALID)
237 {
238 /* What to return in case of wrong Guid */
239 return Utf8Str("00000000-0000-0000-0000-00000000000");
240 }
241
242 char buf[RTUUID_STR_LENGTH];
243 ::memset(buf, '\0', sizeof(buf));
244 ::RTUuidToStr(&mUuid, buf, sizeof(buf));
245
246 return Utf8Str(buf);
247 }
248
249 /**
250 * Like toString, but encloses the returned string in curly brackets.
251 *
252 * @returns String object containing the formatted GUID in curly brackets.
253 * @throws std::bad_alloc
254 */
255 Utf8Str toStringCurly() const
256 {
257 if (mGuidState == GUID_INVALID)
258 {
259 /* What to return in case of wrong Guid */
260 return Utf8Str("{00000000-0000-0000-0000-00000000000}");
261 }
262
263 char buf[RTUUID_STR_LENGTH + 2];
264 ::memset(buf, '\0', sizeof(buf));
265 ::RTUuidToStr(&mUuid, buf + 1, sizeof(buf) - 2);
266 buf[0] = '{';
267 buf[sizeof(buf) - 2] = '}';
268
269 return Utf8Str(buf);
270 }
271
272 /**
273 * Convert the GUID to a string.
274 *
275 * @returns Bstr object containing the formatted GUID.
276 * @throws std::bad_alloc
277 */
278 Bstr toUtf16() const
279 {
280 if (mGuidState == GUID_INVALID)
281 {
282 /* What to return in case of wrong Guid */
283 return Bstr("00000000-0000-0000-0000-00000000000");
284 }
285
286 RTUTF16 buf[RTUUID_STR_LENGTH];
287 ::memset(buf, '\0', sizeof(buf));
288 ::RTUuidToUtf16(&mUuid, buf, RT_ELEMENTS(buf));
289
290 return Bstr(buf);
291 }
292
293 bool isValid() const
294 {
295 return mGuidState != GUID_INVALID;
296 }
297
298 bool isZero() const
299 {
300 return mGuidState == GUID_ZERO;
301 }
302
303 bool operator==(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) == 0; }
304 bool operator==(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) == 0; }
305 bool operator==(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) == 0; }
306 bool operator!=(const Guid &that) const { return !operator==(that); }
307 bool operator!=(const GUID &guid) const { return !operator==(guid); }
308 bool operator!=(const RTUUID &guid) const { return !operator==(guid); }
309 bool operator<(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) < 0; }
310 bool operator<(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) < 0; }
311 bool operator<(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) < 0; }
312
313 /**
314 * To directly copy the contents to a GUID, or for passing it as an input
315 * parameter of type (const GUID *), the compiler converts. */
316 const GUID &ref() const
317 {
318 return *(const GUID *)&mUuid;
319 }
320
321 /**
322 * To pass instances to printf-like functions.
323 */
324 PCRTUUID raw() const
325 {
326 return (PCRTUUID)&mUuid;
327 }
328
329#if !defined(VBOX_WITH_XPCOM)
330
331 /** To assign instances to OUT_GUID parameters from within the interface
332 * method. */
333 const Guid &cloneTo(GUID *pguid) const
334 {
335 if (pguid)
336 ::memcpy(pguid, &mUuid, sizeof(GUID));
337 return *this;
338 }
339
340 /** To pass instances as OUT_GUID parameters to interface methods. */
341 GUID *asOutParam()
342 {
343 return (GUID *)&mUuid;
344 }
345
346#else
347
348 /** To assign instances to OUT_GUID parameters from within the
349 * interface method */
350 const Guid &cloneTo(nsID **ppGuid) const
351 {
352 if (ppGuid)
353 *ppGuid = (nsID *)nsMemory::Clone(&mUuid, sizeof(nsID));
354
355 return *this;
356 }
357
358 /**
359 * Internal helper class for asOutParam().
360 *
361 * This takes a GUID reference in the constructor and copies the mUuid from
362 * the method to that instance in its destructor.
363 */
364 class GuidOutParam
365 {
366 GuidOutParam(Guid &guid)
367 : ptr(0),
368 outer(guid)
369 {
370 outer.clear();
371 }
372
373 nsID *ptr;
374 Guid &outer;
375 GuidOutParam(const GuidOutParam &that); // disabled
376 GuidOutParam &operator=(const GuidOutParam &that); // disabled
377 public:
378 operator nsID**() { return &ptr; }
379 ~GuidOutParam()
380 {
381 if (ptr && outer.isZero())
382 {
383 outer = *ptr;
384 outer.dbg_refresh();
385 nsMemory::Free(ptr);
386 }
387 }
388 friend class Guid;
389 };
390
391 /** to pass instances as OUT_GUID parameters to interface methods */
392 GuidOutParam asOutParam() { return GuidOutParam(*this); }
393
394#endif
395
396 /**
397 * Static immutable empty (zero) object. May be used for comparison purposes.
398 */
399 static const Guid Empty;
400
401private:
402 void makeClear()
403 {
404 ::RTUuidClear(&mUuid);
405 mGuidState = GUID_ZERO;
406 }
407
408 void makeInvalid()
409 {
410 ::RTUuidClear(&mUuid);
411 mGuidState = GUID_INVALID;
412 }
413
414 void updateState()
415 {
416 if (::RTUuidIsNull(&mUuid))
417 mGuidState = GUID_ZERO;
418 else
419 mGuidState = GUID_NORMAL;
420 }
421
422 void initString(const char *that)
423 {
424 if (!that || !*that)
425 {
426 makeClear();
427 }
428 else
429 {
430 int rc = ::RTUuidFromStr(&mUuid, that);
431 if (RT_SUCCESS(rc))
432 updateState();
433 else
434 makeInvalid();
435 }
436 dbg_refresh();
437 }
438
439 void initBSTR(CBSTR that)
440 {
441 if (!that || !*that)
442 {
443 makeClear();
444 }
445 else
446 {
447 int rc = ::RTUuidFromUtf16(&mUuid, that);
448 if (RT_SUCCESS(rc))
449 updateState();
450 else
451 makeInvalid();
452 }
453 dbg_refresh();
454 }
455
456 /**
457 * Refresh the debug-only UUID string.
458 *
459 * In debug code, refresh the UUID string representatino for debugging;
460 * must be called every time the internal uuid changes; compiles to nothing
461 * in release code.
462 */
463 inline void dbg_refresh()
464 {
465#ifdef DEBUG
466 switch (mGuidState)
467 {
468 case GUID_ZERO:
469 case GUID_NORMAL:
470 ::RTUuidToStr(&mUuid, mszUuid, RTUUID_STR_LENGTH);
471 break;
472 default:
473 ::memset(mszUuid, '\0', sizeof(mszUuid));
474 ::RTStrCopy(mszUuid, sizeof(mszUuid), "INVALID");
475 break;
476 }
477 m_pcszUUID = mszUuid;
478#endif
479 }
480
481 /** The UUID. */
482 RTUUID mUuid;
483
484 GuidState_t mGuidState;
485
486#ifdef DEBUG
487 /** String representation of mUuid for printing in the debugger. */
488 char mszUuid[RTUUID_STR_LENGTH];
489 /** Another string variant for the debugger, points to szUUID. */
490 const char *m_pcszUUID;
491#endif
492};
493
494} /* namespace com */
495
496/** @} */
497
498#endif /* !VBOX_INCLUDED_com_Guid_h */
499
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