VirtualBox

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

Last change on this file since 7581 was 6935, checked in by vboxsync, 17 years ago

Main: Changed 'defined (RT_OS_WINDOWS)' => '!defined (VBOX_WITH_XPCOM)' in relevant places, for clarity (not XPCOM is possible only on Windows so far).

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

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