VirtualBox

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

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

com::Guid: use initString/initBSTR in the corresponding operator=
instead of duplicating the code.

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

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