1 | /* $Id: Guid.h 9738 2008-06-16 22:38:49Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * MS COM / XPCOM Abstraction Layer:
|
---|
5 | * Guid class declaration
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | *
|
---|
28 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
29 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
30 | * additional information or have any questions.
|
---|
31 | */
|
---|
32 |
|
---|
33 | #ifndef ___VBox_com_Guid_h
|
---|
34 | #define ___VBox_com_Guid_h
|
---|
35 |
|
---|
36 | /* Make sure all the stdint.h macros are included - must come first! */
|
---|
37 | #ifndef __STDC_LIMIT_MACROS
|
---|
38 | # define __STDC_LIMIT_MACROS
|
---|
39 | #endif
|
---|
40 | #ifndef __STDC_CONSTANT_MACROS
|
---|
41 | # define __STDC_CONSTANT_MACROS
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #if defined (VBOX_WITH_XPCOM)
|
---|
45 | #include <nsMemory.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include "VBox/com/string.h"
|
---|
49 |
|
---|
50 | #include <iprt/cpputils.h>
|
---|
51 | #include <iprt/uuid.h>
|
---|
52 |
|
---|
53 | namespace com
|
---|
54 | {
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Helper class that represents the UUID type and hides platform-siecific
|
---|
58 | * implementation details.
|
---|
59 | */
|
---|
60 | class Guid
|
---|
61 | {
|
---|
62 | public:
|
---|
63 |
|
---|
64 | Guid () { ::RTUuidClear (&uuid); }
|
---|
65 | Guid (const Guid &that) { uuid = that.uuid; }
|
---|
66 | Guid (const RTUUID &that) { uuid = that; }
|
---|
67 | Guid (const GUID &that) { ::memcpy (&uuid, &that, sizeof (GUID)); }
|
---|
68 | Guid (const char *that)
|
---|
69 | {
|
---|
70 | ::RTUuidClear (&uuid);
|
---|
71 | ::RTUuidFromStr (&uuid, that);
|
---|
72 | }
|
---|
73 |
|
---|
74 | Guid &operator= (const Guid &that)
|
---|
75 | {
|
---|
76 | ::memcpy (&uuid, &that.uuid, sizeof (RTUUID));
|
---|
77 | return *this;
|
---|
78 | }
|
---|
79 | Guid &operator= (const GUID &guid)
|
---|
80 | {
|
---|
81 | ::memcpy (&uuid, &guid, sizeof (GUID));
|
---|
82 | return *this;
|
---|
83 | }
|
---|
84 | Guid &operator= (const RTUUID &guid)
|
---|
85 | {
|
---|
86 | ::memcpy (&uuid, &guid, sizeof (RTUUID));
|
---|
87 | return *this;
|
---|
88 | }
|
---|
89 | Guid &operator= (const char *str)
|
---|
90 | {
|
---|
91 | ::RTUuidFromStr (&uuid, str);
|
---|
92 | return *this;
|
---|
93 | }
|
---|
94 |
|
---|
95 | void create() { ::RTUuidCreate (&uuid); }
|
---|
96 | void clear() { ::RTUuidClear (&uuid); }
|
---|
97 |
|
---|
98 | Utf8Str toString () const
|
---|
99 | {
|
---|
100 | char buf [RTUUID_STR_LENGTH];
|
---|
101 | ::RTUuidToStr (&uuid, buf, RTUUID_STR_LENGTH);
|
---|
102 | return Utf8Str (buf);
|
---|
103 | }
|
---|
104 |
|
---|
105 | bool isEmpty() const { return ::RTUuidIsNull (&uuid); }
|
---|
106 | operator bool() const { return !isEmpty(); }
|
---|
107 |
|
---|
108 | bool operator== (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) == 0; }
|
---|
109 | bool operator== (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) == 0; }
|
---|
110 | bool operator!= (const Guid &that) const { return !operator==(that); }
|
---|
111 | bool operator!= (const GUID &guid) const { return !operator==(guid); }
|
---|
112 | bool operator< (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) < 0; }
|
---|
113 | bool operator< (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) < 0; }
|
---|
114 |
|
---|
115 | /* to pass instances as GUIDPARAM parameters to interface methods */
|
---|
116 | operator const GUID &() const { return *(GUID *) &uuid; }
|
---|
117 |
|
---|
118 | /* to directly pass instances to RTPrintf("%Vuuid") */
|
---|
119 | PRTUUID ptr() { return &uuid; }
|
---|
120 |
|
---|
121 | /* to pass instances to printf-like functions */
|
---|
122 | PCRTUUID raw() const { return &uuid; }
|
---|
123 |
|
---|
124 | /* to pass instances to RTUuid*() as a constant argument */
|
---|
125 | operator const RTUUID * () const { return &uuid; }
|
---|
126 |
|
---|
127 | #if !defined (VBOX_WITH_XPCOM)
|
---|
128 |
|
---|
129 | /* to assign instances to GUIDPARAMOUT parameters from within the
|
---|
130 | * interface method */
|
---|
131 | const Guid &cloneTo (GUID *pguid) const
|
---|
132 | {
|
---|
133 | if (pguid) { ::memcpy (pguid, &uuid, sizeof (GUID)); }
|
---|
134 | return *this;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* to pass instances as GUIDPARAMOUT parameters to interface methods */
|
---|
138 | GUID *asOutParam() { return (GUID *) &uuid; }
|
---|
139 |
|
---|
140 | #else
|
---|
141 |
|
---|
142 | /* to assign instances to GUIDPARAMOUT parameters from within the
|
---|
143 | * interface method */
|
---|
144 | const Guid &cloneTo (nsID **ppguid) const
|
---|
145 | {
|
---|
146 | if (ppguid) { *ppguid = (nsID *) nsMemory::Clone (&uuid, sizeof (nsID)); }
|
---|
147 | return *this;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* internal helper class for asOutParam() */
|
---|
151 | class GuidOutParam
|
---|
152 | {
|
---|
153 | GuidOutParam (Guid &guid) : ptr (0), outer (guid) { outer.clear(); }
|
---|
154 | nsID *ptr;
|
---|
155 | Guid &outer;
|
---|
156 | GuidOutParam (const GuidOutParam &that); // disabled
|
---|
157 | GuidOutParam &operator= (const GuidOutParam &that); // disabled
|
---|
158 | public:
|
---|
159 | operator nsID **() { return &ptr; }
|
---|
160 | ~GuidOutParam()
|
---|
161 | {
|
---|
162 | if (ptr && outer.isEmpty()) { outer = *ptr; nsMemory::Free (ptr); }
|
---|
163 | }
|
---|
164 | friend class Guid;
|
---|
165 | };
|
---|
166 |
|
---|
167 | /* to pass instances as GUIDPARAMOUT parameters to interface methods */
|
---|
168 | GuidOutParam asOutParam() { return GuidOutParam (*this); }
|
---|
169 |
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | /* to directly test GUIDPARAM interface method's parameters */
|
---|
173 | static bool isEmpty (const GUID &guid)
|
---|
174 | {
|
---|
175 | return ::RTUuidIsNull ((PRTUUID) &guid);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Static immutable empty object. May be used for comparison purposes.
|
---|
180 | */
|
---|
181 | static const Guid Empty;
|
---|
182 |
|
---|
183 | private:
|
---|
184 |
|
---|
185 | RTUUID uuid;
|
---|
186 | };
|
---|
187 |
|
---|
188 | /* work around error C2593 of the stupid MSVC 7.x ambiguity resolver */
|
---|
189 | WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Guid);
|
---|
190 |
|
---|
191 | } /* namespace com */
|
---|
192 |
|
---|
193 | #endif /* ___VBox_com_Guid_h */
|
---|
194 |
|
---|