VirtualBox

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

最後變更 在這個檔案從76366是 76366,由 vboxsync 提交於 6 年 前

include/VBox/com/Guid.h: Don't include iprt/err.h for no good reason. bugref:9344

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

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