9 static const char rcsid[] __attribute__((used)) =
"$Id: UUID.cpp 1703 2016-08-16 00:16:40Z sella $";
16 #define BUFFER_SIZE 64
17 #define HASH_ALGO_DJB2
19 using namespace sella::util;
21 UUID::UUID() throw () : scache(), bcache(), bcache_std(false), hcache(0) {
22 uuid_generate(this->uuid);
25 UUID::UUID(uuid_t &uuid)
throw () : scache(), bcache(), bcache_std(
false), hcache(0) {
26 uuid_copy(this->uuid, uuid);
29 UUID::UUID(
const std::string &uuid)
throw (UUIDException) : scache(), bcache(), bcache_std(
false), hcache(0) {
30 if (uuid_parse(uuid.c_str(), this->uuid) != 0) {
31 THROW(UUIDException,
"Improperly formatted UUID '%s' provided", uuid.c_str());
35 UUID::UUID(
const char *uuid)
throw (UUIDException) : scache(), bcache(), bcache_std(
false), hcache(0) {
36 if (uuid_parse(uuid, this->uuid) != 0) {
37 THROW(UUIDException,
"Improperly formatted UUID '%s' provided", uuid);
41 UUID::UUID(
const UUID &other) :
44 bcache_std(other.bcache_std),
47 uuid_copy(this->uuid, other.uuid);
50 UUID::UUID(
const UUID &&other) :
51 scache(std::move(other.scache)),
52 bcache(std::move(other.bcache)),
53 bcache_std(other.bcache_std),
56 uuid_copy(this->uuid, other.uuid);
63 UUID& UUID::operator=(
const UUID &other)
throw () {
65 uuid_copy(this->uuid, other.uuid);
66 this->scache = other.scache;
67 this->bcache = other.bcache;
68 this->bcache_std = other.bcache_std;
69 this->hcache = other.hcache;
75 UUID& UUID::operator=(
const UUID &&other) throw () {
77 uuid_copy(this->uuid, other.uuid);
78 this->scache = std::move(other.scache);
79 this->bcache = std::move(other.bcache);
80 this->bcache_std = other.bcache_std;
81 this->hcache = other.hcache;
87 UUID& UUID::operator=(
const std::string &uuid)
throw (UUIDException) {
90 this->bcache_std =
false;
93 if (uuid_parse(uuid.c_str(), this->uuid) != 0) {
94 THROW(UUIDException,
"Improperly formatted UUID '%s' provided", uuid.c_str());
100 UUID& UUID::operator=(
const char *uuid)
throw (UUIDException) {
101 this->scache.clear();
102 this->bcache.clear();
103 this->bcache_std =
false;
106 if (uuid_parse(uuid, this->uuid) != 0) {
107 THROW(UUIDException,
"Improperly formatted UUID '%s' provided", uuid);
113 bool UUID::operator==(
const UUID &other)
const throw () {
114 return (uuid_compare(this->uuid, other.uuid) == 0);
117 bool UUID::operator!=(
const UUID &other)
const throw () {
118 return !(*
this == other);
121 bool UUID::operator>=(
const UUID &other)
const throw () {
122 if (*
this == other) {
126 return (*
this > other);
129 bool UUID::operator<=(
const UUID &other)
const throw () {
130 if (*
this == other) {
134 return (*
this < other);
137 bool UUID::operator>(
const UUID &other)
const throw () {
138 return uuid_compare(this->uuid, other.uuid) > 0;
141 bool UUID::operator<(
const UUID &other)
const throw () {
142 return uuid_compare(this->uuid, other.uuid) < 0;
145 void UUID::set(uuid_t &uuid)
throw () {
151 uuid_copy(this->uuid, uuid);
154 const uuid_t& UUID::get(
void)
const throw () {
158 void UUID::generate(
void) throw () {
159 this->scache.clear();
160 this->bcache.clear();
161 this->bcache_std =
false;
164 uuid_generate(this->uuid);
167 void UUID::clear(
void) throw () {
168 this->scache.clear();
169 this->bcache.clear();
170 this->bcache_std =
false;
173 uuid_clear(this->uuid);
176 bool UUID::isNull(
void)
const throw () {
177 return uuid_is_null(this->uuid);
180 void UUID::shrink_to_fit(
void) throw () {
181 std::string(scache.begin(), scache.end()).swap(scache);
182 std::string(bcache.begin(), bcache.end()).swap(bcache);
185 const std::string& UUID::str(
void)
const throw () {
186 if (scache.empty()) {
187 char buffer[BUFFER_SIZE];
189 uuid_unparse(this->uuid, buffer);
197 const std::string& UUID::base64(
bool standard)
const throw () {
198 if (bcache.empty() || bcache_std != standard) {
199 bcache = String::base64_encode((
unsigned char const*) this->uuid,
sizeof(this->uuid), standard);
200 bcache_std = standard;
206 const char* UUID::c_str(
void)
const throw () {
207 return str().c_str();
210 const char* UUID::c_base64(
bool standard)
const throw () {
211 return base64(standard).c_str();
214 const UUID::shared UUID::shared_ptr(
void) throw () {
215 return std::make_shared<UUID>();
218 const UUID::shared UUID::shared_ptr(uuid_t &uuid)
throw () {
219 return std::make_shared<UUID>(uuid);
222 uint32_t UUID::getU32(
void)
const throw () {
223 const uint32_t *a = (
const uint32_t *) this->uuid;
225 return (a[0] ^ a[1] ^ a[2] ^ a[3]);
228 uint32_t UUID::getHash(
void)
const throw () {
233 #if defined HASH_ALGO_INT1
234 uint32_t k = getU32();
236 hcache = ((k & 0xF0F0F0F0) >> 4) | ((k & 0x0F0F0F0F) << 4);
237 #elif defined HASH_ALGO_INT2
238 uint32_t k = getU32();
240 hcache = (k<<16)^(k>>16)^k;
241 #elif defined HASH_ALGO_INT3
243 #elif defined HASH_ALGO_DJB2
247 for (i = 0; i <
sizeof(uuid_t); ++i) {
248 hcache = ((hcache << 5) + hcache) + uuid[i];
250 #elif defined HASH_ALGO_SDBM
254 for (i = 0; i <
sizeof(uuid_t); ++i) {
255 hcache = uuid[i] + (hcache << 6) + (hcache << 16) - hcache;
258 #error No hash algo selected.