VirtualBox

Changeset 34071 in vbox for trunk/include/VBox/com


Ignore:
Timestamp:
Nov 15, 2010 3:35:10 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
67759
Message:

com::Guid: Don't depend on RTUuitFromStr/Utf16 to not touch the output buffer. Cleanup. Added isNotEmpty. Provide compile targets for the VBox/com/*.h files as well.

Location:
trunk/include/VBox/com
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/com/Guid.h

    r32781 r34071  
    11/* $Id$ */
    2 
    32/** @file
    4  * MS COM / XPCOM Abstraction Layer:
    5  * Guid class declaration
     3 * MS COM / XPCOM Abstraction Layer - Guid class declaration.
    64 */
    75
     
    3836#endif
    3937
    40 #if defined (VBOX_WITH_XPCOM)
    41 #include <nsMemory.h>
     38#if defined(VBOX_WITH_XPCOM)
     39# include <nsMemory.h>
    4240#endif
    4341
     
    4543
    4644#include <iprt/uuid.h>
     45#include <iprt/err.h>
    4746
    4847namespace com
     
    5958    Guid()
    6059    {
    61         ::RTUuidClear(&uuid);
     60        ::RTUuidClear(&mUuid);
    6261        refresh();
    6362    }
     
    6564    Guid(const Guid &that)
    6665    {
    67         uuid = that.uuid;
     66        mUuid = that.mUuid;
    6867        refresh();
    6968    }
     
    7170    Guid(const RTUUID &that)
    7271    {
    73         uuid = that;
     72        mUuid = that;
    7473        refresh();
    7574    }
     
    7877    {
    7978        AssertCompileSize(GUID, sizeof(RTUUID));
    80         ::memcpy(&uuid, &that, sizeof(GUID));
    81         refresh();
    82     }
    83 
     79        ::memcpy(&mUuid, &that, sizeof(GUID));
     80        refresh();
     81    }
     82
     83    /**
     84     * Construct a GUID from a string.
     85     *
     86     * Should the string be invalid, the object will be set to the null GUID
     87     * (isEmpty() == true).
     88     *
     89     * @param   that        The UUID string.  We feed this to RTUuidFromStr(),
     90     *                      so check it out for the exact format.
     91     */
    8492    Guid(const char *that)
    8593    {
    86         ::RTUuidClear(&uuid);
    87         ::RTUuidFromStr(&uuid, that);
    88         refresh();
    89     }
    90 
     94        int rc = ::RTUuidFromStr(&mUuid, that);
     95        if (RT_FAILURE(rc))
     96            ::RTUuidClear(&mUuid);
     97        refresh();
     98    }
     99
     100    /**
     101     * Construct a GUID from a BSTR.
     102     *
     103     * Should the string be empty or invalid, the object will be set to the
     104     * null GUID (isEmpty() == true).
     105     *
     106     * @param   that        The UUID BSTR.  We feed this to RTUuidFromUtf16(),
     107     *                      so check it out for the exact format.
     108     */
    91109    Guid(const Bstr &that)
    92110    {
    93         ::RTUuidClear(&uuid);
    94         if (!that.isEmpty())
    95            ::RTUuidFromUtf16(&uuid, that.raw());
     111        int rc = !that.isEmpty()
     112               ? ::RTUuidFromUtf16(&mUuid, that.raw())
     113               : VERR_INVALID_UUID_FORMAT;
     114        if (RT_FAILURE(rc))
     115            ::RTUuidClear(&mUuid);
    96116        refresh();
    97117    }
     
    99119    Guid& operator=(const Guid &that)
    100120    {
    101         ::memcpy(&uuid, &that.uuid, sizeof (RTUUID));
     121        ::memcpy(&mUuid, &that.mUuid, sizeof (RTUUID));
    102122        refresh();
    103123        return *this;
     
    105125    Guid& operator=(const GUID &guid)
    106126    {
    107         ::memcpy(&uuid, &guid, sizeof (GUID));
     127        ::memcpy(&mUuid, &guid, sizeof (GUID));
    108128        refresh();
    109129        return *this;
     
    111131    Guid& operator=(const RTUUID &guid)
    112132    {
    113         ::memcpy(&uuid, &guid, sizeof (RTUUID));
     133        ::memcpy(&mUuid, &guid, sizeof (RTUUID));
    114134        refresh();
    115135        return *this;
     
    117137    Guid& operator=(const char *str)
    118138    {
    119         ::RTUuidFromStr(&uuid, str);
     139        int rc = ::RTUuidFromStr(&mUuid, str);
     140        if (RT_FAILURE(rc))
     141            ::RTUuidClear(&mUuid);
    120142        refresh();
    121143        return *this;
     
    124146    void create()
    125147    {
    126         ::RTUuidCreate(&uuid);
     148        ::RTUuidCreate(&mUuid);
    127149        refresh();
    128150    }
    129151    void clear()
    130152    {
    131         ::RTUuidClear(&uuid);
    132         refresh();
    133     }
    134 
     153        ::RTUuidClear(&mUuid);
     154        refresh();
     155    }
     156
     157    /**
     158     * Convert the GUID to a string.
     159     *
     160     * @returns String object containing the formatted GUID.
     161     * @throws  std::bad_alloc
     162     */
    135163    Utf8Str toString() const
    136164    {
    137165        char buf[RTUUID_STR_LENGTH];
    138         ::RTUuidToStr(&uuid, buf, RTUUID_STR_LENGTH);
     166        ::RTUuidToStr(&mUuid, buf, RTUUID_STR_LENGTH);
    139167        return Utf8Str(buf);
    140168    }
     
    142170    /**
    143171     * Like toString, but encloses the returned string in curly brackets.
    144      * @return
     172     *
     173     * @returns String object containing the formatted GUID in curly brackets.
     174     * @throws  std::bad_alloc
    145175     */
    146176    Utf8Str toStringCurly() const
    147177    {
    148178        char buf[RTUUID_STR_LENGTH + 2] = "{";
    149         ::RTUuidToStr(&uuid, buf + 1, RTUUID_STR_LENGTH);
     179        ::RTUuidToStr(&mUuid, buf + 1, RTUUID_STR_LENGTH);
    150180        buf[sizeof(buf) - 2] = '}';
    151181        buf[sizeof(buf) - 1] = '\0';
     
    153183    }
    154184
     185    /**
     186     * Convert the GUID to a string.
     187     *
     188     * @returns Bstr object containing the formatted GUID.
     189     * @throws  std::bad_alloc
     190     */
    155191    Bstr toUtf16() const
    156192    {
     
    159195
    160196        RTUTF16 buf[RTUUID_STR_LENGTH];
    161         ::RTUuidToUtf16(&uuid, buf, RTUUID_STR_LENGTH);
     197        ::RTUuidToUtf16(&mUuid, buf, RTUUID_STR_LENGTH);
    162198        return Bstr(buf);
    163199    }
     
    165201    bool isEmpty() const
    166202    {
    167         return ::RTUuidIsNull (&uuid);
    168     }
    169 
    170     bool operator==(const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) == 0; }
    171     bool operator==(const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) == 0; }
     203        return ::RTUuidIsNull(&mUuid);
     204    }
     205
     206    bool isNotEmpty() const
     207    {
     208        return !::RTUuidIsNull(&mUuid);
     209    }
     210
     211    bool operator==(const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid)    == 0; }
     212    bool operator==(const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) == 0; }
    172213    bool operator!=(const Guid &that) const { return !operator==(that); }
    173214    bool operator!=(const GUID &guid) const { return !operator==(guid); }
    174     bool operator<(const Guid &that) const { return ::RTUuidCompare (&uuid, &that.uuid) < 0; }
    175     bool operator<(const GUID &guid) const { return ::RTUuidCompare (&uuid, (PRTUUID) &guid) < 0; }
    176 
    177     /* to directly copy the contents to a GUID, or for passing it as
    178      * an input parameter of type (const GUID *), the compiler converts */
     215    bool operator<( const Guid &that) const { return ::RTUuidCompare(&mUuid, &that.mUuid)    < 0; }
     216    bool operator<( const GUID &guid) const { return ::RTUuidCompare(&mUuid, (PRTUUID)&guid) < 0; }
     217
     218    /**
     219     * To directly copy the contents to a GUID, or for passing it as an input
     220     * parameter of type (const GUID *), the compiler converts. */
    179221    const GUID &ref() const
    180222    {
    181         return *(const GUID *)&uuid;
    182     }
    183 
    184     /* to pass instances to printf-like functions */
     223        return *(const GUID *)&mUuid;
     224    }
     225
     226    /**
     227     * To pass instances to printf-like functions.
     228     */
    185229    PCRTUUID raw() const
    186230    {
    187         return (PCRTUUID)&uuid;
    188     }
    189 
    190 #if !defined (VBOX_WITH_XPCOM)
    191 
    192     /* to assign instances to OUT_GUID parameters from within the
    193      *  interface method */
    194     const Guid &cloneTo (GUID *pguid) const
     231        return (PCRTUUID)&mUuid;
     232    }
     233
     234#if !defined(VBOX_WITH_XPCOM)
     235
     236    /** To assign instances to OUT_GUID parameters from within the interface
     237     * method. */
     238    const Guid &cloneTo(GUID *pguid) const
    195239    {
    196240        if (pguid)
    197             ::memcpy(pguid, &uuid, sizeof(GUID));
    198         return *this;
    199     }
    200 
    201     /* to pass instances as OUT_GUID parameters to interface methods */
     241            ::memcpy(pguid, &mUuid, sizeof(GUID));
     242        return *this;
     243    }
     244
     245    /** To pass instances as OUT_GUID parameters to interface methods. */
    202246    GUID *asOutParam()
    203247    {
    204         return (GUID*)&uuid;
     248        return (GUID *)&mUuid;
    205249    }
    206250
    207251#else
    208252
    209     /* to assign instances to OUT_GUID parameters from within the
     253    /** To assign instances to OUT_GUID parameters from within the
    210254     * interface method */
    211     const Guid &cloneTo (nsID **ppguid) const
    212     {
    213         if (ppguid) { *ppguid = (nsID *) nsMemory::Clone (&uuid, sizeof (nsID)); }
    214         return *this;
    215     }
    216 
    217     // internal helper class for asOutParam(); this takes a GUID refrence
    218     // in the constructor and copies the uuid from the method to that instance
    219     // in its destructor
     255    const Guid &cloneTo(nsID **ppGuid) const
     256    {
     257        if (ppGuid)
     258            *ppGuid = (nsID *)nsMemory::Clone(&mUuid, sizeof(nsID));
     259        return *this;
     260    }
     261
     262    /**
     263     * Internal helper class for asOutParam().
     264     *
     265     * This takes a GUID refrence in the constructor and copies the mUuid from
     266     * the method to that instance in its destructor.
     267     */
    220268    class GuidOutParam
    221269    {
     
    245293    };
    246294
    247     /* to pass instances as OUT_GUID parameters to interface methods */
     295    /** to pass instances as OUT_GUID parameters to interface methods */
    248296    GuidOutParam asOutParam() { return GuidOutParam(*this); }
    249297
     
    262310
    263311private:
    264     // in debug code, refresh the UUID string representatino for
    265     // debugging; must be called every time the internal uuid
    266     // changes; compiles to nothing in release code
     312    /**
     313     * Refresh the debug-only UUID string.
     314     *
     315     * In debug code, refresh the UUID string representatino for debugging;
     316     * must be called every time the internal uuid changes; compiles to nothing
     317     * in release code.
     318     */
    267319    inline void refresh()
    268320    {
    269321#ifdef DEBUG
    270         ::RTUuidToStr(&uuid, szUUID, RTUUID_STR_LENGTH);
    271         pcszUUID = szUUID;
    272 #endif
    273     }
    274 
    275     RTUUID uuid;
     322        ::RTUuidToStr(&mUuid, mszUuid, RTUUID_STR_LENGTH);
     323        m_pcszUUID = mszUuid;
     324#endif
     325    }
     326
     327    /** The UUID. */
     328    RTUUID mUuid;
    276329
    277330#ifdef DEBUG
    278     // in debug builds, have a Utf8Str representation of the UUID so we can look
    279     // at it in the debugger more easily
    280     char szUUID[RTUUID_STR_LENGTH];
    281     const char *pcszUUID;
     331    /** String representation of mUuid for printing in the debugger. */
     332    char mszUuid[RTUUID_STR_LENGTH];
     333    /** Another string variant for the debugger, points to szUUID. */
     334    const char *m_pcszUUID;
    282335#endif
    283336};
     
    286339{
    287340   Guid guid(str);
    288    return  guid.isEmpty() ? Bstr() : guid.toUtf16();
     341   return guid.isEmpty() ? Bstr() : guid.toUtf16();
    289342}
    290343
     
    292345{
    293346   Guid guid(str);
    294    return  !guid.isEmpty();
     347   return !guid.isEmpty();
    295348}
    296349
    297350} /* namespace com */
    298351
    299 #endif /* ___VBox_com_Guid_h */
    300 
     352#endif /* !___VBox_com_Guid_h */
     353
Note: See TracChangeset for help on using the changeset viewer.

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