VirtualBox

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

Last change on this file since 8155 was 8155, checked in by vboxsync, 16 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/* $Id: Guid.h 8155 2008-04-18 15:16:47Z 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#if defined (VBOX_WITH_XPCOM)
37#include <nsMemory.h>
38#endif
39
40#include "VBox/com/string.h"
41
42#include <iprt/cpputils.h>
43#include <iprt/uuid.h>
44
45namespace com
46{
47
48/**
49 * Helper class that represents the UUID type and hides platform-siecific
50 * implementation details.
51 */
52class Guid
53{
54public:
55
56 Guid () { ::RTUuidClear (&uuid); }
57 Guid (const Guid &that) { uuid = that.uuid; }
58 Guid (const RTUUID &that) { uuid = that; }
59 Guid (const GUID &that) { ::memcpy (&uuid, &that, sizeof (GUID)); }
60 Guid (const char *that)
61 {
62 ::RTUuidClear (&uuid);
63 ::RTUuidFromStr (&uuid, that);
64 }
65
66 Guid &operator= (const Guid &that)
67 {
68 ::memcpy (&uuid, &that.uuid, sizeof (RTUUID));
69 return *this;
70 }
71 Guid &operator= (const GUID &guid)
72 {
73 ::memcpy (&uuid, &guid, sizeof (GUID));
74 return *this;
75 }
76 Guid &operator= (const RTUUID &guid)
77 {
78 ::memcpy (&uuid, &guid, sizeof (RTUUID));
79 return *this;
80 }
81 Guid &operator= (const char *str)
82 {
83 ::RTUuidFromStr (&uuid, str);
84 return *this;
85 }
86
87 void create() { ::RTUuidCreate (&uuid); }
88 void clear() { ::RTUuidClear (&uuid); }
89
90 Utf8Str toString () const
91 {
92 char buf [RTUUID_STR_LENGTH];
93 ::RTUuidToStr (&uuid, buf, RTUUID_STR_LENGTH);
94 return Utf8Str (buf);
95 }
96
97 bool isEmpty() const { return ::RTUuidIsNull (&uuid) != 0; }
98 operator bool() const { return !isEmpty(); }
99
100 bool operator== (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) == 0; }
101 bool operator== (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) == 0; }
102 bool operator!= (const Guid &that) const { return !operator==(that); }
103 bool operator!= (const GUID &guid) const { return !operator==(guid); }
104 bool operator< (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) < 0; }
105 bool operator< (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) < 0; }
106
107 /* to pass instances as GUIDPARAM parameters to interface methods */
108 operator const GUID &() const { return *(GUID *) &uuid; }
109
110 /* to directly pass instances to RTPrintf("%Vuuid") */
111 PRTUUID ptr() { return &uuid; }
112
113 /* to pass instances to printf-like functions */
114 PCRTUUID raw() const { return &uuid; }
115
116 /* to pass instances to RTUuid*() as a constant argument */
117 operator const RTUUID * () const { return &uuid; }
118
119#if !defined (VBOX_WITH_XPCOM)
120
121 /* to assign instances to GUIDPARAMOUT parameters from within the
122 * interface method */
123 const Guid &cloneTo (GUID *pguid) const
124 {
125 if (pguid) { ::memcpy (pguid, &uuid, sizeof (GUID)); }
126 return *this;
127 }
128
129 /* to pass instances as GUIDPARAMOUT parameters to interface methods */
130 GUID *asOutParam() { return (GUID *) &uuid; }
131
132#else
133
134 /* to assign instances to GUIDPARAMOUT parameters from within the
135 * interface method */
136 const Guid &cloneTo (nsID **ppguid) const
137 {
138 if (ppguid) { *ppguid = (nsID *) nsMemory::Clone (&uuid, sizeof (nsID)); }
139 return *this;
140 }
141
142 /* internal helper class for asOutParam() */
143 class GuidOutParam
144 {
145 GuidOutParam (Guid &guid) : ptr (0), outer (guid) { outer.clear(); }
146 nsID *ptr;
147 Guid &outer;
148 GuidOutParam (const GuidOutParam &that); // disabled
149 GuidOutParam &operator= (const GuidOutParam &that); // disabled
150 public:
151 operator nsID **() { return &ptr; }
152 ~GuidOutParam()
153 {
154 if (ptr && outer.isEmpty()) { outer = *ptr; nsMemory::Free (ptr); }
155 }
156 friend class Guid;
157 };
158
159 /* to pass instances as GUIDPARAMOUT parameters to interface methods */
160 GuidOutParam asOutParam() { return GuidOutParam (*this); }
161
162#endif
163
164 /* to directly test GUIDPARAM interface method's parameters */
165 static bool isEmpty (const GUID &guid)
166 {
167 return ::RTUuidIsNull ((PRTUUID) &guid) != 0;
168 }
169
170 /**
171 * Static immutable empty object. May be used for comparison purposes.
172 */
173 static const Guid Empty;
174
175private:
176
177 RTUUID uuid;
178};
179
180/* work around error C2593 of the stupid MSVC 7.x ambiguity resolver */
181WORKAROUND_MSVC7_ERROR_C2593_FOR_BOOL_OP (Guid);
182
183} /* namespace com */
184
185#endif /* ___VBox_com_Guid_h */
186
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use