1 | /* $Id: Guid.h 15051 2008-12-05 17:20:00Z 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-specific
|
---|
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 |
|
---|
68 | Guid (const GUID &that)
|
---|
69 | {
|
---|
70 | AssertCompileSize (GUID, sizeof (RTUUID));
|
---|
71 | ::memcpy (&uuid, &that, sizeof (GUID));
|
---|
72 | }
|
---|
73 |
|
---|
74 | Guid (const char *that)
|
---|
75 | {
|
---|
76 | ::RTUuidClear (&uuid);
|
---|
77 | ::RTUuidFromStr (&uuid, that);
|
---|
78 | }
|
---|
79 |
|
---|
80 | Guid &operator= (const Guid &that)
|
---|
81 | {
|
---|
82 | ::memcpy (&uuid, &that.uuid, sizeof (RTUUID));
|
---|
83 | return *this;
|
---|
84 | }
|
---|
85 | Guid &operator= (const GUID &guid)
|
---|
86 | {
|
---|
87 | ::memcpy (&uuid, &guid, sizeof (GUID));
|
---|
88 | return *this;
|
---|
89 | }
|
---|
90 | Guid &operator= (const RTUUID &guid)
|
---|
91 | {
|
---|
92 | ::memcpy (&uuid, &guid, sizeof (RTUUID));
|
---|
93 | return *this;
|
---|
94 | }
|
---|
95 | Guid &operator= (const char *str)
|
---|
96 | {
|
---|
97 | ::RTUuidFromStr (&uuid, str);
|
---|
98 | return *this;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void create() { ::RTUuidCreate (&uuid); }
|
---|
102 | void clear() { ::RTUuidClear (&uuid); }
|
---|
103 |
|
---|
104 | Utf8Str toString () const
|
---|
105 | {
|
---|
106 | char buf [RTUUID_STR_LENGTH];
|
---|
107 | ::RTUuidToStr (&uuid, buf, RTUUID_STR_LENGTH);
|
---|
108 | return Utf8Str (buf);
|
---|
109 | }
|
---|
110 |
|
---|
111 | bool isEmpty() const { return ::RTUuidIsNull (&uuid); }
|
---|
112 | operator bool() const { return !isEmpty(); }
|
---|
113 |
|
---|
114 | bool operator== (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) == 0; }
|
---|
115 | bool operator== (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) == 0; }
|
---|
116 | bool operator!= (const Guid &that) const { return !operator==(that); }
|
---|
117 | bool operator!= (const GUID &guid) const { return !operator==(guid); }
|
---|
118 | bool operator< (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) < 0; }
|
---|
119 | bool operator< (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) < 0; }
|
---|
120 |
|
---|
121 | /* to pass instances as IN_GUID parameters to interface methods */
|
---|
122 | operator const GUID &() const { return *(GUID *) &uuid; }
|
---|
123 |
|
---|
124 | /* to directly pass instances to RTPrintf("%Vuuid") */
|
---|
125 | PRTUUID ptr() { return &uuid; }
|
---|
126 |
|
---|
127 | /* to pass instances to printf-like functions */
|
---|
128 | PCRTUUID raw() const { return &uuid; }
|
---|
129 |
|
---|
130 | /* to pass instances to RTUuid*() as a constant argument */
|
---|
131 | operator const RTUUID * () const { return &uuid; }
|
---|
132 |
|
---|
133 | #if !defined (VBOX_WITH_XPCOM)
|
---|
134 |
|
---|
135 | /* to assign instances to OUT_GUID parameters from within the
|
---|
136 | * interface method */
|
---|
137 | const Guid &cloneTo (GUID *pguid) const
|
---|
138 | {
|
---|
139 | if (pguid) { ::memcpy (pguid, &uuid, sizeof (GUID)); }
|
---|
140 | return *this;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* to pass instances as OUT_GUID parameters to interface methods */
|
---|
144 | GUID *asOutParam() { return (GUID *) &uuid; }
|
---|
145 |
|
---|
146 | #else
|
---|
147 |
|
---|
148 | /* to assign instances to OUT_GUID parameters from within the
|
---|
149 | * interface method */
|
---|
150 | const Guid &cloneTo (nsID **ppguid) const
|
---|
151 | {
|
---|
152 | if (ppguid) { *ppguid = (nsID *) nsMemory::Clone (&uuid, sizeof (nsID)); }
|
---|
153 | return *this;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /* internal helper class for asOutParam() */
|
---|
157 | class GuidOutParam
|
---|
158 | {
|
---|
159 | GuidOutParam (Guid &guid) : ptr (0), outer (guid) { outer.clear(); }
|
---|
160 | nsID *ptr;
|
---|
161 | Guid &outer;
|
---|
162 | GuidOutParam (const GuidOutParam &that); // disabled
|
---|
163 | GuidOutParam &operator= (const GuidOutParam &that); // disabled
|
---|
164 | public:
|
---|
165 | operator nsID **() { return &ptr; }
|
---|
166 | ~GuidOutParam()
|
---|
167 | {
|
---|
168 | if (ptr && outer.isEmpty()) { outer = *ptr; nsMemory::Free (ptr); }
|
---|
169 | }
|
---|
170 | friend class Guid;
|
---|
171 | };
|
---|
172 |
|
---|
173 | /* to pass instances as OUT_GUID parameters to interface methods */
|
---|
174 | GuidOutParam asOutParam() { return GuidOutParam (*this); }
|
---|
175 |
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | /* to directly test IN_GUID interface method's parameters */
|
---|
179 | static bool isEmpty (const GUID &guid)
|
---|
180 | {
|
---|
181 | return ::RTUuidIsNull ((PRTUUID) &guid);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Static immutable empty object. May be used for comparison purposes.
|
---|
186 | */
|
---|
187 | static const Guid Empty;
|
---|
188 |
|
---|
189 | private:
|
---|
190 |
|
---|
191 | RTUUID uuid;
|
---|
192 | };
|
---|
193 |
|
---|
194 | /* work around error C2593 of the stupid MSVC 7.x ambiguity resolver */
|
---|
195 | WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Guid);
|
---|
196 |
|
---|
197 | } /* namespace com */
|
---|
198 |
|
---|
199 | #endif /* ___VBox_com_Guid_h */
|
---|
200 |
|
---|