diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/unstable/uuid.cpp | 58 | ||||
-rw-r--r-- | src/unstable/uuid.h | 20 |
2 files changed, 51 insertions, 27 deletions
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() | |||
80 | data[7] = msb(0); | 80 | data[7] = msb(0); |
81 | } | 81 | } |
82 | 82 | ||
83 | Bu::Uuid Bu::Uuid::gen() | 83 | Bu::Uuid Bu::Uuid::generate( Bu::Uuid::Type eType ) |
84 | { | 84 | { |
85 | switch( eType ) | ||
86 | { | ||
87 | case Version1: | ||
88 | case Version2: | ||
89 | case Version3: | ||
90 | case Version4: | ||
91 | case Version5: | ||
92 | default: | ||
93 | case System: | ||
85 | #if defined(linux) | 94 | #if defined(linux) |
86 | Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); | 95 | Bu::File fIn( "/proc/sys/kernel/random/uuid", Bu::File::Read ); |
87 | char dat[36]; | 96 | char dat[36]; |
88 | fIn.read( dat, 36 ); | 97 | fIn.read( dat, 36 ); |
89 | Uuid id; | 98 | Uuid id; |
90 | id.set( dat ); | 99 | id.set( dat ); |
91 | return id; | 100 | return id; |
92 | #elif defined(WIN32) | 101 | #elif defined(WIN32) |
93 | UUID uuid; | 102 | UUID uuid; |
94 | UuidCreate( &uuid ); | 103 | UuidCreate( &uuid ); |
95 | Uuid id; | 104 | Uuid id; |
96 | id.data[0] = ((unsigned char *)&uuid.Data1)[3]; | 105 | id.data[0] = ((unsigned char *)&uuid.Data1)[3]; |
97 | id.data[1] = ((unsigned char *)&uuid.Data1)[2]; | 106 | id.data[1] = ((unsigned char *)&uuid.Data1)[2]; |
98 | id.data[2] = ((unsigned char *)&uuid.Data1)[1]; | 107 | id.data[2] = ((unsigned char *)&uuid.Data1)[1]; |
99 | id.data[3] = ((unsigned char *)&uuid.Data1)[0]; | 108 | id.data[3] = ((unsigned char *)&uuid.Data1)[0]; |
100 | id.data[4] = ((unsigned char *)&uuid.Data2)[1]; | 109 | id.data[4] = ((unsigned char *)&uuid.Data2)[1]; |
101 | id.data[5] = ((unsigned char *)&uuid.Data2)[0]; | 110 | id.data[5] = ((unsigned char *)&uuid.Data2)[0]; |
102 | id.data[6] = ((unsigned char *)&uuid.Data3)[1]; | 111 | id.data[6] = ((unsigned char *)&uuid.Data3)[1]; |
103 | id.data[7] = ((unsigned char *)&uuid.Data3)[0]; | 112 | id.data[7] = ((unsigned char *)&uuid.Data3)[0]; |
104 | memcpy( id.data+8, uuid.Data4, 8 ); | 113 | memcpy( id.data+8, uuid.Data4, 8 ); |
105 | 114 | ||
106 | return id; | 115 | return id; |
107 | #else | 116 | #else |
108 | # error We should be using one of the other fallbacks, but your platform is not supported yet. Sorry. | 117 | # error We should be using one of the other fallbacks, but your platform is not supported yet. Sorry. |
109 | #endif | 118 | #endif |
119 | } | ||
110 | } | 120 | } |
111 | 121 | ||
112 | void Bu::Uuid::set( const Bu::String &sSrc ) | 122 | void Bu::Uuid::set( const Bu::String &sSrc ) |
@@ -129,6 +139,12 @@ bool Bu::Uuid::operator==( const Uuid &rhs ) const | |||
129 | return memcmp( data, rhs.data, 16 ) == 0; | 139 | return memcmp( data, rhs.data, 16 ) == 0; |
130 | } | 140 | } |
131 | 141 | ||
142 | Bu::Uuid &Bu::Uuid::operator=( const Uuid &rhs ) | ||
143 | { | ||
144 | memcpy( data, rhs.data, 16 ); | ||
145 | return *this; | ||
146 | } | ||
147 | |||
132 | template<> uint32_t Bu::__calcHashCode<Bu::Uuid>( const Bu::Uuid &k ) | 148 | template<> uint32_t Bu::__calcHashCode<Bu::Uuid>( const Bu::Uuid &k ) |
133 | { | 149 | { |
134 | return __calcHashCode<String>( k.toRawString() ); | 150 | return __calcHashCode<String>( 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 @@ | |||
8 | #ifndef BU_UUID_H | 8 | #ifndef BU_UUID_H |
9 | #define BU_UUID_H | 9 | #define BU_UUID_H |
10 | 10 | ||
11 | #include "bu/util.h" | ||
11 | #include "bu/string.h" | 12 | #include "bu/string.h" |
12 | 13 | ||
13 | namespace Bu | 14 | namespace Bu |
@@ -26,20 +27,27 @@ namespace Bu | |||
26 | Bu::String toString() const; | 27 | Bu::String toString() const; |
27 | Bu::String toUrn() const; | 28 | Bu::String toUrn() const; |
28 | 29 | ||
30 | enum Type | ||
31 | { | ||
32 | System, | ||
33 | Version1, | ||
34 | Version2, | ||
35 | Version3, | ||
36 | Version4, | ||
37 | Version5, | ||
38 | }; | ||
39 | |||
29 | int getVersion(); | 40 | int getVersion(); |
30 | 41 | ||
31 | static Uuid gen(); | 42 | static Uuid generate( Type eType = System ); |
32 | static Uuid genV1(); | 43 | DEPRECATED static Uuid gen() { return generate(); } |
33 | static Uuid genV2(); | ||
34 | static Uuid genV3(); | ||
35 | static Uuid genV4(); | ||
36 | static Uuid genV5(); | ||
37 | 44 | ||
38 | void clear(); | 45 | void clear(); |
39 | void set( const Bu::String &sSrc ); | 46 | void set( const Bu::String &sSrc ); |
40 | 47 | ||
41 | bool operator==( const Uuid &rhs ) const; | 48 | bool operator==( const Uuid &rhs ) const; |
42 | Uuid &operator=( const Bu::String &rhs ) { set( rhs ); return *this; } | 49 | Uuid &operator=( const Bu::String &rhs ) { set( rhs ); return *this; } |
50 | Uuid &operator=( const Uuid &rhs ); | ||
43 | 51 | ||
44 | private: | 52 | private: |
45 | unsigned char data[16]; | 53 | unsigned char data[16]; |