VirtualBox

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

Last change on this file since 4878 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Guid class declaration
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ___VBox_com_Guid_h
19#define ___VBox_com_Guid_h
20
21#if !defined (RT_OS_WINDOWS)
22#include <nsMemory.h>
23#endif
24
25#include "VBox/com/string.h"
26
27#include <iprt/uuid.h>
28
29namespace com
30{
31
32/**
33 * Helper class that represents the UUID type and hides platform-siecific
34 * implementation details.
35 */
36class Guid
37{
38public:
39
40 Guid () { ::RTUuidClear (&uuid); }
41 Guid (const Guid &that) { uuid = that.uuid; }
42 Guid (const RTUUID &that) { uuid = that; }
43 Guid (const GUID &that) { ::memcpy (&uuid, &that, sizeof (GUID)); }
44 Guid (const char *that) {
45 ::RTUuidClear (&uuid);
46 ::RTUuidFromStr (&uuid, that);
47 }
48
49 Guid &operator= (const Guid &that) {
50 ::memcpy (&uuid, &that.uuid, sizeof (RTUUID));
51 return *this;
52 }
53 Guid &operator= (const GUID &guid) {
54 ::memcpy (&uuid, &guid, sizeof (GUID));
55 return *this;
56 }
57 Guid &operator= (const RTUUID &guid) {
58 ::memcpy (&uuid, &guid, sizeof (RTUUID));
59 return *this;
60 }
61 Guid &operator= (const char *str) {
62 ::RTUuidFromStr (&uuid, str);
63 return *this;
64 }
65
66 void create() { ::RTUuidCreate (&uuid); }
67 void clear() { ::RTUuidClear (&uuid); }
68
69 Utf8Str toString () const {
70 char buf [RTUUID_STR_LENGTH];
71 ::RTUuidToStr (&uuid, buf, RTUUID_STR_LENGTH);
72 return Utf8Str (buf);
73 }
74
75 bool isEmpty() const { return ::RTUuidIsNull (&uuid) != 0; }
76 operator bool() const { return !isEmpty(); }
77
78 bool operator== (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) == 0; }
79 bool operator== (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) == 0; }
80 bool operator!= (const Guid &that) const { return !operator==(that); }
81 bool operator!= (const GUID &guid) const { return !operator==(guid); }
82 bool operator< (const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) < 0; }
83 bool operator< (const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) < 0; }
84
85 // to pass instances as GUIDPARAM parameters to interface methods
86 operator const GUID &() const { return *(GUID *) &uuid; }
87
88 // to directly pass instances to CFGLDRQueryUUID()
89 PRTUUID ptr() { return &uuid; }
90
91 // to pass instances to printf-like functions
92 PCRTUUID raw() const { return &uuid; }
93
94 // to pass instances to RTUuid*() as a constant argument
95 operator const RTUUID * const () const { return &uuid; }
96
97#if defined(RT_OS_WINDOWS)
98
99 // to assign instances to GUIDPARAMOUT parameters from within the interface method
100 const Guid &cloneTo (GUID *pguid) const {
101 if (pguid) { ::memcpy (pguid, &uuid, sizeof (GUID)); }
102 return *this;
103 }
104
105 // to pass instances as GUIDPARAMOUT parameters to interface methods
106 GUID *asOutParam() { return (GUID *) &uuid; }
107
108#else
109
110 // to assign instances to GUIDPARAMOUT parameters from within the interface method
111 const Guid &cloneTo (nsID **ppguid) const {
112 if (ppguid) { *ppguid = (nsID *) nsMemory::Clone (&uuid, sizeof (nsID)); }
113 return *this;
114 }
115
116 // internal helper class for asOutParam()
117 class GuidOutParam {
118 GuidOutParam (Guid &guid) : ptr (0), outer (guid) { outer.clear(); }
119 nsID *ptr;
120 Guid &outer;
121 GuidOutParam (const GuidOutParam &that); // disabled
122 GuidOutParam &operator= (const GuidOutParam &that); // disabled
123 public:
124 operator nsID **() { return &ptr; }
125 ~GuidOutParam() {
126 if (ptr && outer.isEmpty()) { outer = *ptr; nsMemory::Free (ptr); }
127 }
128 friend class Guid;
129 };
130
131 // to pass instances as GUIDPARAMOUT parameters to interface methods
132 GuidOutParam asOutParam() { return GuidOutParam (*this); }
133
134#endif
135
136 // to directly test GUIDPARAM interface method's parameters
137 static BOOL isEmpty (const GUID &guid) { return ::RTUuidIsNull ((PRTUUID) &guid); }
138
139private:
140
141 RTUUID uuid;
142};
143
144#if defined (_MSC_VER)
145
146// work around error C2593 of the stupid MSVC 7.x ambiguity resolver
147inline bool operator! (const Guid& guid) { return !bool (guid); }
148inline bool operator&& (const Guid& guid, bool b) { return bool (guid) && b; }
149inline bool operator|| (const Guid& guid, bool b) { return bool (guid) || b; }
150inline bool operator&& (bool b, const Guid& guid) { return b && bool (guid); }
151inline bool operator|| (bool b, const Guid& guid) { return b || bool (guid); }
152
153#endif
154
155}; // namespace com
156
157#endif
158
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