VirtualBox

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

Last change on this file since 79731 was 79731, checked in by vboxsync, 5 years ago

VBox/com/Guid.h: Added a pure RTUuidToStr wrapper, i.e. takes a C string buffer.

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