vpr::GUID Class Reference

A cross-platform implementation of globally unique identifiers, also known as GUIDs or UUIDs (universally unique identifiers). More...

#include <vpr/Util/GUID.h>

Inheritance diagram for vpr::GUID:

Inheritance graph
[legend]
Collaboration diagram for vpr::GUID:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 GUID (const GenerateTag &tag)
virtual ~GUID ()
std::string toString () const
 Converts this GUID to its corresponding string representation.
bool operator== (const GUID &guidObj) const
bool operator!= (const GUID &guidObj) const
bool operator< (const GUID &r) const
 GUID ()
 Generates an empty GUID, set equal to vpr::GUID::NullGUID.
 GUID (const struct StdGUID &guid)
 Generates a GUID from the given struct.
 GUID (const std::string &guidString)
 Generates a GUID from the given string representation of the GUID using a std::string.
 GUID (const GUID &nsGuid, const std::string &name)
 Generates a GUID based on the given name that is part of the namespace identified by the given namespace GUID.
 GUID (const GUID &obj)
 Copy constructor.
GUIDoperator= (const GUID &obj)
void generate ()
void generate (const GUID &nsGuid, const std::string &name)
Reader/Writer methods
virtual vpr::ReturnStatus writeObject (vpr::ObjectWriter *writer)
 Template method for writing object this object to the given stream.
virtual vpr::ReturnStatus readObject (vpr::ObjectReader *reader)
 Template method for reading data into this object from the given stream.

Public Attributes

vpr::GUID::_vpr_guid mGuid
 Multi-format GUID/UUID container.

Static Public Attributes

static GenerateTag generateTag
static const vpr::GUID NullGUID

Friends

class vpr::GUIDFactory

Classes

union  _vpr_guid
 Multi-format GUID/UUID container. More...
class  GenerateTag
 Tag to the constructor to force generation:. More...
struct  hash
 Hash trait/functor for vpr::GUID. More...
struct  StdGUID

Detailed Description

A cross-platform implementation of globally unique identifiers, also known as GUIDs or UUIDs (universally unique identifiers).

Definition at line 61 of file GUID.h.


Constructor & Destructor Documentation

vpr::GUID::GUID ( const GenerateTag tag  ) 

Definition at line 98 of file GUID.cpp.

References generate().

00099 {
00100    boost::ignore_unused_variable_warning(tag);
00101    generate();
00102 }

virtual vpr::GUID::~GUID (  )  [inline, virtual]

Definition at line 76 of file GUID.h.

00076 {;}

vpr::GUID::GUID (  ) 

Generates an empty GUID, set equal to vpr::GUID::NullGUID.

Definition at line 152 of file GUID.cpp.

00153    : mGuid(GUID::NullGUID.mGuid)     // Assign a null guid
00154 {
00155    ; /* Do nothing */
00156 }

vpr::GUID::GUID ( const struct StdGUID guid  ) 

Generates a GUID from the given struct.

Definition at line 158 of file GUID.cpp.

References mGuid.

00159 {
00160    memcpy(&mGuid, &guid, sizeof(vpr::GUID::StdGUID));
00161 }

vpr::GUID::GUID ( const std::string &  guidString  )  [inline, explicit]

Generates a GUID from the given string representation of the GUID using a std::string.

Format: "8x-4x-4x-2x2x-2x2x2x2x2x2x"

Parameters:
guidString A string that is used to inialize this GUID.

Definition at line 146 of file GUID.h.

00147    {
00148       fromString(guidString);
00149    }

vpr::GUID::GUID ( const GUID nsGuid,
const std::string &  name 
)

Generates a GUID based on the given name that is part of the namespace identified by the given namespace GUID.

Definition at line 163 of file GUID.cpp.

References generate().

00164 {
00165    generate(nsGuid,name);
00166 }

vpr::GUID::GUID ( const GUID obj  )  [inline]

Copy constructor.

Definition at line 158 of file GUID.h.

References vpr::GUID::_vpr_guid::_packed::l0, vpr::GUID::_vpr_guid::_packed::l1, vpr::GUID::_vpr_guid::_packed::l2, vpr::GUID::_vpr_guid::_packed::l3, mGuid, and vpr::GUID::_vpr_guid::packed.

00158                          : vpr::SerializableObject(obj)
00159    {
00160       mGuid.packed.l0 = obj.mGuid.packed.l0;
00161       mGuid.packed.l1 = obj.mGuid.packed.l1;
00162       mGuid.packed.l2 = obj.mGuid.packed.l2;
00163       mGuid.packed.l3 = obj.mGuid.packed.l3;
00164    }


Member Function Documentation

std::string vpr::GUID::toString (  )  const

Converts this GUID to its corresponding string representation.

Definition at line 104 of file GUID.cpp.

References vpr::GUID::_vpr_guid::_moz::m0, vpr::GUID::_vpr_guid::_moz::m1, vpr::GUID::_vpr_guid::_moz::m2, vpr::GUID::_vpr_guid::_moz::m3, mGuid, and vpr::GUID::_vpr_guid::moz.

Referenced by std::operator<<().

00105 {
00106    std::string guid_str;
00107    char guid_c_str[37];
00108 
00109    // Visual C++ 8.0 deprecates _snprintf() and provides _snprintf_s()
00110    // instead.
00111 #if defined(_MSC_VER) && _MSC_VER >= 1400
00112    _snprintf_s(guid_c_str, 37,
00113    // Visual C++ 7.0 and 7.1 have _snprintf()
00114 #elif defined(_MSC_VER)
00115    _snprintf(guid_c_str, 37,
00116 #elif defined(HAVE_SNPRINTF)
00117    snprintf(guid_c_str, 37,
00118 #else
00119    sprintf(guid_c_str,
00120 #endif
00121             "%08X-%04hX-%04hX-%02X%02X-%02X%02X%02X%02X%02X%02X",
00122             mGuid.moz.m0, mGuid.moz.m1, mGuid.moz.m2, mGuid.moz.m3[0],
00123             (vpr::Uint32) mGuid.moz.m3[1], (vpr::Uint32) mGuid.moz.m3[2],
00124             (vpr::Uint32) mGuid.moz.m3[3], (vpr::Uint32) mGuid.moz.m3[4],
00125             (vpr::Uint32) mGuid.moz.m3[5], (vpr::Uint32) mGuid.moz.m3[6],
00126             (vpr::Uint32) mGuid.moz.m3[7]);
00127    guid_str = guid_c_str;
00128 
00129    return guid_str;
00130 }

bool vpr::GUID::operator== ( const GUID guidObj  )  const

Definition at line 132 of file GUID.cpp.

References vpr::GUID::_vpr_guid::_packed::l0, vpr::GUID::_vpr_guid::_packed::l1, vpr::GUID::_vpr_guid::_packed::l2, vpr::GUID::_vpr_guid::_packed::l3, mGuid, and vpr::GUID::_vpr_guid::packed.

00133 {
00134    /*
00135    bool ret_val0 = (mGuid.packed.l0 == guid.mGuid.packed.l0);
00136    bool ret_val1 = (mGuid.packed.l1 == guid.mGuid.packed.l1);
00137    bool ret_val2 = (mGuid.packed.l2 == guid.mGuid.packed.l2);
00138    bool ret_val3 = (mGuid.packed.l3 == guid.mGuid.packed.l3);
00139    return (ret_val0 && ret_val1 && ret_val2 && ret_val3);
00140    */
00141 
00142    return ( (mGuid.packed.l0 == guid.mGuid.packed.l0) &&
00143             (mGuid.packed.l1 == guid.mGuid.packed.l1) &&
00144             (mGuid.packed.l2 == guid.mGuid.packed.l2) &&
00145             (mGuid.packed.l3 == guid.mGuid.packed.l3) );
00146 }

bool vpr::GUID::operator!= ( const GUID guidObj  )  const [inline]

Definition at line 85 of file GUID.h.

00086    {
00087       return ! (*this == guidObj);
00088    }

bool vpr::GUID::operator< ( const GUID r  )  const [inline]

Definition at line 90 of file GUID.h.

References vpr::GUID::_vpr_guid::_packed::l0, vpr::GUID::_vpr_guid::_packed::l1, vpr::GUID::_vpr_guid::_packed::l2, vpr::GUID::_vpr_guid::_packed::l3, mGuid, and vpr::GUID::_vpr_guid::packed.

00091    {
00092       bool ret_val(false);
00093 
00094       if (mGuid.packed.l0 < r.mGuid.packed.l0)
00095       {  ret_val = true; }
00096       else if(mGuid.packed.l0 == r.mGuid.packed.l0)
00097       {
00098          if(mGuid.packed.l1 < r.mGuid.packed.l1)
00099          {  ret_val = true; }
00100          else if(mGuid.packed.l1 == r.mGuid.packed.l1)
00101          {
00102             if(mGuid.packed.l2 < r.mGuid.packed.l2)
00103             {  ret_val = true; }
00104             else if(mGuid.packed.l2 == r.mGuid.packed.l2)
00105             {
00106                if(mGuid.packed.l3 < r.mGuid.packed.l3)
00107                {
00108                   ret_val = true;
00109                }
00110             }
00111          }
00112       }
00113 
00114       return ret_val;
00115    }

GUID& vpr::GUID::operator= ( const GUID obj  )  [inline]

Definition at line 166 of file GUID.h.

References vpr::GUID::_vpr_guid::_packed::l0, vpr::GUID::_vpr_guid::_packed::l1, vpr::GUID::_vpr_guid::_packed::l2, vpr::GUID::_vpr_guid::_packed::l3, mGuid, and vpr::GUID::_vpr_guid::packed.

00167    {
00168       if(&obj == this) // Check for self
00169       {
00170          return *this;
00171       }
00172 
00173       mGuid.packed.l0 = obj.mGuid.packed.l0;
00174       mGuid.packed.l1 = obj.mGuid.packed.l1;
00175       mGuid.packed.l2 = obj.mGuid.packed.l2;
00176       mGuid.packed.l3 = obj.mGuid.packed.l3;
00177       return *this;
00178    }

void vpr::GUID::generate (  ) 

Definition at line 168 of file GUID.cpp.

References mGuid, and vpr::GUID::_vpr_guid::standard.

Referenced by GUID().

00169 {
00170 // Windows DCE UUID.
00171 #if defined(VPR_OS_Windows)
00172    UuidCreate((UUID*) &mGuid.standard);
00173 // DCE 1.1 UUID.
00174 #elif defined(VPR_USE_DCE_1_1_UUID)
00175    uint32_t status(0);
00176    uuid_create((uuid_t*) &mGuid.standard, &status);
00177 // Linux e2fsprogs libuuid.
00178 #elif defined(VPR_USE_LIBUUID)
00179    uuid_t storage;
00180    uuid_generate(storage);
00181    memcpy((void*) &mGuid.standard, storage, sizeof(mGuid));
00182 // Leach UUID (see juggler/external/leach-uuid).
00183 #else
00184    uuid_create((uuid_t*) &mGuid.standard);
00185 #endif
00186 }

void vpr::GUID::generate ( const GUID nsGuid,
const std::string &  name 
)

Definition at line 188 of file GUID.cpp.

References vpr::SystemPosix::Htonl(), vpr::SystemPosix::Htons(), vpr::GUID::StdGUID::m0, vpr::GUID::StdGUID::m1, vpr::GUID::StdGUID::m2, vpr::GUID::StdGUID::m3, mGuid, vpr::SystemPosix::Ntohl(), vpr::SystemPosix::Ntohs(), and vpr::GUID::_vpr_guid::standard.

00189 {
00190 // Leach UUID (see juggler/external/leach-uuid).
00191 #if defined(VPR_USE_LEACH_UUID)
00192    uuid_t temp_ns_id = *((uuid_t*) &nsGuid.mGuid.standard); // nasty, but works
00193 
00194    uuid_create_from_name((uuid_t*) (&mGuid.standard), temp_ns_id,
00195                          (void*) name.c_str(), name.length());
00196 
00197 // Implementation of the algorithm for creating a name-based UUID.
00198 #else
00199    // Convert to network byte order.
00200    vpr::GUID net_ns_guid = nsGuid;
00201    net_ns_guid.mGuid.standard.m0 = vpr::System::Htonl(net_ns_guid.mGuid.standard.m0);
00202    net_ns_guid.mGuid.standard.m1 = vpr::System::Htons(net_ns_guid.mGuid.standard.m1);
00203    net_ns_guid.mGuid.standard.m2 = vpr::System::Htons(net_ns_guid.mGuid.standard.m2);
00204 
00205    // The following uses the MD5 implementation in juggler/external/md5-c.
00206    MD5_CTX c;
00207    unsigned char hash[16];
00208 
00209    MD5Init(&c);
00210    MD5Update(&c, (unsigned char*) &net_ns_guid.mGuid.standard,
00211              sizeof(net_ns_guid.mGuid.standard));
00212    MD5Update(&c, (unsigned char*) name.c_str(), name.length());
00213    MD5Final(hash, &c);
00214 
00215    // At this point, the hash is in network byte order.
00216    memcpy((void*) &mGuid.standard, hash, sizeof(mGuid.standard));
00217 
00218    // Convert to host byte order.
00219    mGuid.standard.m0 = vpr::System::Ntohl(mGuid.standard.m0);
00220    mGuid.standard.m1 = vpr::System::Ntohs(mGuid.standard.m1);
00221    mGuid.standard.m2 = vpr::System::Ntohs(mGuid.standard.m2);
00222 
00223    // Put in the variant and version bits.  This is based on the Leach UUID
00224    // implementation (the function format_uuid_v3).
00225    mGuid.standard.m2 &= 0x0FFF;
00226 //   mGuid.standard.m2 |= (3 << 12);
00227    mGuid.standard.m2 |= 0x3000; // Version 3 UUID
00228    mGuid.standard.m3 &= 0x3F;
00229    mGuid.standard.m3 |= 0x80;
00230 #endif
00231 }

vpr::ReturnStatus vpr::GUID::writeObject ( vpr::ObjectWriter writer  )  [virtual]

Template method for writing object this object to the given stream.

Postcondition:
All object data is written to the writer.
Parameters:
writer The object writer to which the data for this object will be written to allow serialization of this object.

Implements vpr::WriteableObject.

Definition at line 233 of file GUID.cpp.

References vpr::GUID::StdGUID::m0, vpr::GUID::StdGUID::m1, vpr::GUID::StdGUID::m2, vpr::GUID::StdGUID::m3, vpr::GUID::StdGUID::m4, vpr::GUID::StdGUID::m5, mGuid, vpr::GUID::_vpr_guid::standard, vpr::ReturnStatus::Succeed, vpr::ObjectWriter::writeUint16(), vpr::ObjectWriter::writeUint32(), and vpr::ObjectWriter::writeUint8().

00234 {
00235    writer->writeUint32(mGuid.standard.m0);
00236    writer->writeUint16(mGuid.standard.m1);
00237    writer->writeUint16(mGuid.standard.m2);
00238    writer->writeUint8(mGuid.standard.m3);
00239    writer->writeUint8(mGuid.standard.m4);
00240    writer->writeUint8(mGuid.standard.m5[0]);
00241    writer->writeUint8(mGuid.standard.m5[1]);
00242    writer->writeUint8(mGuid.standard.m5[2]);
00243    writer->writeUint8(mGuid.standard.m5[3]);
00244    writer->writeUint8(mGuid.standard.m5[4]);
00245    writer->writeUint8(mGuid.standard.m5[5]);
00246    return vpr::ReturnStatus::Succeed;
00247 }

vpr::ReturnStatus vpr::GUID::readObject ( vpr::ObjectReader reader  )  [virtual]

Template method for reading data into this object from the given stream.

Postcondition:
All object data is read from the reader.
Parameters:
reader The object reader from which the data for this object can be read to allow de-serialization of this object.

Implements vpr::ReadableObject.

Definition at line 249 of file GUID.cpp.

References vpr::GUID::StdGUID::m0, vpr::GUID::StdGUID::m1, vpr::GUID::StdGUID::m2, vpr::GUID::StdGUID::m3, vpr::GUID::StdGUID::m4, vpr::GUID::StdGUID::m5, mGuid, vpr::ObjectReader::readUint16(), vpr::ObjectReader::readUint32(), vpr::ObjectReader::readUint8(), vpr::GUID::_vpr_guid::standard, and vpr::ReturnStatus::Succeed.

00250 {
00251    mGuid.standard.m0 = reader->readUint32();
00252    mGuid.standard.m1 = reader->readUint16();
00253    mGuid.standard.m2 = reader->readUint16();
00254    mGuid.standard.m3 = reader->readUint8();
00255    mGuid.standard.m4 = reader->readUint8();
00256    mGuid.standard.m5[0] = reader->readUint8();
00257    mGuid.standard.m5[1] = reader->readUint8();
00258    mGuid.standard.m5[2] = reader->readUint8();
00259    mGuid.standard.m5[3] = reader->readUint8();
00260    mGuid.standard.m5[4] = reader->readUint8();
00261    mGuid.standard.m5[5] = reader->readUint8();
00262    return vpr::ReturnStatus::Succeed;
00263 }


Friends And Related Function Documentation

friend class vpr::GUIDFactory [friend]

Definition at line 223 of file GUID.h.


Member Data Documentation

vpr::GUID::GenerateTag vpr::GUID::generateTag [static]

Definition at line 71 of file GUID.h.

union vpr::GUID::_vpr_guid vpr::GUID::mGuid

Multi-format GUID/UUID container.

This will always be 128 bits, but its memory may be accessed differently depending on the needs of the code.

Referenced by generate(), GUID(), vpr::GUID::hash::operator()(), operator<(), operator=(), operator==(), readObject(), toString(), and writeObject().

const vpr::GUID vpr::GUID::NullGUID [static]

Definition at line 225 of file GUID.h.


The documentation for this class was generated from the following files:
Generated on Thu Jan 4 10:56:06 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1