From 87fa2298eb73bf6213ec836937c236df25e2a4a7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 26 Jun 2012 14:34:55 +0000 Subject: Changed the Bu::Uuid generate interface. It's now one function that takes a type parameter. I think it's much nicer overall, the old gen function is stil there for now, but marked deprecated. --- src/unstable/uuid.cpp | 58 ++++++++++++++++++++++++++++++++------------------- src/unstable/uuid.h | 20 ++++++++++++------ 2 files changed, 51 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/unstable/uuid.cpp b/src/unstable/uuid.cpp index 41a35c2..0257270 100644 --- a/src/unstable/uuid.cpp +++ b/src/unstable/uuid.cpp @@ -80,33 +80,43 @@ void Bu::Uuid::clear() data[7] = msb(0); } -Bu::Uuid Bu::Uuid::gen() +Bu::Uuid Bu::Uuid::generate( Bu::Uuid::Type eType ) { + switch( eType ) + { + case Version1: + case Version2: + case Version3: + case Version4: + case Version5: + default: + case System: #if defined(linux) - Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); - char dat[36]; - fIn.read( dat, 36 ); - Uuid id; - id.set( dat ); - return id; + Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); + char dat[36]; + fIn.read( dat, 36 ); + Uuid id; + id.set( dat ); + return id; #elif defined(WIN32) - UUID uuid; - UuidCreate( &uuid ); - Uuid id; - id.data[0] = ((unsigned char *)&uuid.Data1)[3]; - id.data[1] = ((unsigned char *)&uuid.Data1)[2]; - id.data[2] = ((unsigned char *)&uuid.Data1)[1]; - id.data[3] = ((unsigned char *)&uuid.Data1)[0]; - id.data[4] = ((unsigned char *)&uuid.Data2)[1]; - id.data[5] = ((unsigned char *)&uuid.Data2)[0]; - id.data[6] = ((unsigned char *)&uuid.Data3)[1]; - id.data[7] = ((unsigned char *)&uuid.Data3)[0]; - memcpy( id.data+8, uuid.Data4, 8 ); - - return id; + UUID uuid; + UuidCreate( &uuid ); + Uuid id; + id.data[0] = ((unsigned char *)&uuid.Data1)[3]; + id.data[1] = ((unsigned char *)&uuid.Data1)[2]; + id.data[2] = ((unsigned char *)&uuid.Data1)[1]; + id.data[3] = ((unsigned char *)&uuid.Data1)[0]; + id.data[4] = ((unsigned char *)&uuid.Data2)[1]; + id.data[5] = ((unsigned char *)&uuid.Data2)[0]; + id.data[6] = ((unsigned char *)&uuid.Data3)[1]; + id.data[7] = ((unsigned char *)&uuid.Data3)[0]; + memcpy( id.data+8, uuid.Data4, 8 ); + + return id; #else # error We should be using one of the other fallbacks, but your platform is not supported yet. Sorry. #endif + } } void Bu::Uuid::set( const Bu::String &sSrc ) @@ -129,6 +139,12 @@ bool Bu::Uuid::operator==( const Uuid &rhs ) const return memcmp( data, rhs.data, 16 ) == 0; } +Bu::Uuid &Bu::Uuid::operator=( const Uuid &rhs ) +{ + memcpy( data, rhs.data, 16 ); + return *this; +} + template<> uint32_t Bu::__calcHashCode( const Bu::Uuid &k ) { return __calcHashCode( k.toRawString() ); diff --git a/src/unstable/uuid.h b/src/unstable/uuid.h index 73b34bc..de20d4c 100644 --- a/src/unstable/uuid.h +++ b/src/unstable/uuid.h @@ -8,6 +8,7 @@ #ifndef BU_UUID_H #define BU_UUID_H +#include "bu/util.h" #include "bu/string.h" namespace Bu @@ -26,20 +27,27 @@ namespace Bu Bu::String toString() const; Bu::String toUrn() const; + enum Type + { + System, + Version1, + Version2, + Version3, + Version4, + Version5, + }; + int getVersion(); - static Uuid gen(); - static Uuid genV1(); - static Uuid genV2(); - static Uuid genV3(); - static Uuid genV4(); - static Uuid genV5(); + static Uuid generate( Type eType = System ); + DEPRECATED static Uuid gen() { return generate(); } void clear(); void set( const Bu::String &sSrc ); bool operator==( const Uuid &rhs ) const; Uuid &operator=( const Bu::String &rhs ) { set( rhs ); return *this; } + Uuid &operator=( const Uuid &rhs ); private: unsigned char data[16]; -- cgit v1.2.3