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