VirtualBox

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

Last change on this file since 89363 was 85929, checked in by vboxsync, 4 years ago

Main: bugref:9224: Main+VBoxManageDisk+doc part

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/* $Id: Guid.h 85929 2020-08-28 14:40:55Z vboxsync $ */
2/** @file
3 * MS COM / XPCOM Abstraction Layer - Guid class declaration.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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 /**
294 * Convert the GUID to a C string.
295 *
296 * @returns See RTUuidToStr.
297 * @param pszUuid The output buffer
298 * @param cbUuid The size of the output buffer. Should be at least
299 * RTUUID_STR_LENGTH in length.
300 */
301 int toString(char *pszUuid, size_t cbUuid) const
302 {
303 return ::RTUuidToStr(mGuidState != GUID_INVALID ? &mUuid : &Empty.mUuid, pszUuid, cbUuid);
304 }
305
306 bool isValid() const
307 {
308 return mGuidState != GUID_INVALID;
309 }
310
311 bool isZero() const
312 {
313 return mGuidState == GUID_ZERO;
314 }
315
316 bool operator==(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) == 0; }
317 bool operator==(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) == 0; }
318 bool operator==(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) == 0; }
319 bool operator!=(const Guid &that) const { return !operator==(that); }
320 bool operator!=(const GUID &guid) const { return !operator==(guid); }
321 bool operator!=(const RTUUID &guid) const { return !operator==(guid); }
322 bool operator<(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid) < 0; }
323 bool operator<(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) < 0; }
324 bool operator<(const RTUUID &guid) const { return ::RTUuidCompare(&mUuid, &guid) < 0; }
325
326 /** Compare with a UUID string representation.
327 * @note Not an operator as that could lead to confusion. */
328 bool equalsString(const char *pszUuid2) const { return ::RTUuidCompareStr(&mUuid, pszUuid2) == 0; }
329
330 /**
331 * To directly copy the contents to a GUID, or for passing it as an input
332 * parameter of type (const GUID *), the compiler converts. */
333 const GUID &ref() const
334 {
335 return *(const GUID *)&mUuid;
336 }
337
338 /**
339 * To pass instances to printf-like functions.
340 */
341 PCRTUUID raw() const
342 {
343 return (PCRTUUID)&mUuid;
344 }
345
346#if !defined(VBOX_WITH_XPCOM)
347
348 /** To assign instances to OUT_GUID parameters from within the interface
349 * method. */
350 const Guid &cloneTo(GUID *pguid) const
351 {
352 if (pguid)
353 ::memcpy(pguid, &mUuid, sizeof(GUID));
354 return *this;
355 }
356
357 /** To pass instances as OUT_GUID parameters to interface methods. */
358 GUID *asOutParam()
359 {
360 return (GUID *)&mUuid;
361 }
362
363#else
364
365 /** To assign instances to OUT_GUID parameters from within the
366 * interface method */
367 const Guid &cloneTo(nsID **ppGuid) const
368 {
369 if (ppGuid)
370 *ppGuid = (nsID *)nsMemory::Clone(&mUuid, sizeof(nsID));
371
372 return *this;
373 }
374
375 /**
376 * Internal helper class for asOutParam().
377 *
378 * This takes a GUID reference in the constructor and copies the mUuid from
379 * the method to that instance in its destructor.
380 */
381 class GuidOutParam
382 {
383 GuidOutParam(Guid &guid)
384 : ptr(0),
385 outer(guid)
386 {
387 outer.clear();
388 }
389
390 nsID *ptr;
391 Guid &outer;
392 GuidOutParam(const GuidOutParam &that); // disabled
393 GuidOutParam &operator=(const GuidOutParam &that); // disabled
394 public:
395 operator nsID**() { return &ptr; }
396 ~GuidOutParam()
397 {
398 if (ptr && outer.isZero())
399 {
400 outer = *ptr;
401 outer.dbg_refresh();
402 nsMemory::Free(ptr);
403 }
404 }
405 friend class Guid;
406 };
407
408 /** to pass instances as OUT_GUID parameters to interface methods */
409 GuidOutParam asOutParam() { return GuidOutParam(*this); }
410
411#endif
412
413 /**
414 * Static immutable empty (zero) object. May be used for comparison purposes.
415 */
416 static const Guid Empty;
417
418private:
419 void makeClear()
420 {
421 ::RTUuidClear(&mUuid);
422 mGuidState = GUID_ZERO;
423 }
424
425 void makeInvalid()
426 {
427 ::RTUuidClear(&mUuid);
428 mGuidState = GUID_INVALID;
429 }
430
431 void updateState()
432 {
433 if (::RTUuidIsNull(&mUuid))
434 mGuidState = GUID_ZERO;
435 else
436 mGuidState = GUID_NORMAL;
437 }
438
439 void initString(const char *that)
440 {
441 if (!that || !*that)
442 {
443 makeClear();
444 }
445 else
446 {
447 int rc = ::RTUuidFromStr(&mUuid, that);
448 if (RT_SUCCESS(rc))
449 updateState();
450 else
451 makeInvalid();
452 }
453 dbg_refresh();
454 }
455
456 void initBSTR(CBSTR that)
457 {
458 if (!that || !*that)
459 {
460 makeClear();
461 }
462 else
463 {
464 int rc = ::RTUuidFromUtf16(&mUuid, that);
465 if (RT_SUCCESS(rc))
466 updateState();
467 else
468 makeInvalid();
469 }
470 dbg_refresh();
471 }
472
473 /**
474 * Refresh the debug-only UUID string.
475 *
476 * In debug code, refresh the UUID string representatino for debugging;
477 * must be called every time the internal uuid changes; compiles to nothing
478 * in release code.
479 */
480 inline void dbg_refresh()
481 {
482#ifdef DEBUG
483 switch (mGuidState)
484 {
485 case GUID_ZERO:
486 case GUID_NORMAL:
487 ::RTUuidToStr(&mUuid, mszUuid, RTUUID_STR_LENGTH);
488 break;
489 default:
490 ::memset(mszUuid, '\0', sizeof(mszUuid));
491 ::RTStrCopy(mszUuid, sizeof(mszUuid), "INVALID");
492 break;
493 }
494 m_pcszUUID = mszUuid;
495#endif
496 }
497
498 /** The UUID. */
499 RTUUID mUuid;
500
501 GuidState_t mGuidState;
502
503#ifdef DEBUG
504 /** String representation of mUuid for printing in the debugger. */
505 char mszUuid[RTUUID_STR_LENGTH];
506 /** Another string variant for the debugger, points to szUUID. */
507 const char *m_pcszUUID;
508#endif
509};
510
511} /* namespace com */
512
513/** @} */
514
515#endif /* !VBOX_INCLUDED_com_Guid_h */
516
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