libutil++  1.9.3
 All Classes Functions Variables
UUID.cpp
1 /*
2 ** libutil++
3 ** $Id: UUID.cpp 1911 2017-09-02 17:37:35Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: UUID.cpp 1911 2017-09-02 17:37:35Z sella $";
10 
11 #include "UUID.h"
12 #include "../util/CommonMacro.h"
13 
14 using namespace sella::variant;
15 
16 UUID::UUID() throw () : Nullable(std::make_shared<sella::util::UUID>()) { }
17 
18 UUID::UUID(const std::string &value) throw (TypeCastException) : Nullable() {
19  if (value.empty()) {
20  this->value = std::make_shared<sella::util::UUID>();
21  } else {
22  if (LIKELY(boost::regex_match(value.c_str(), UUID::Pattern()))) {
23  try {
24  this->value = std::make_shared<sella::util::UUID>(value);
25  } catch (sella::util::UUIDException &e) {
26  THROW(TypeCastException, "unable to cast '%s' to a UUID value: %s", value.c_str(), e.what());
27  }
28  } else {
29  THROW(TypeCastException, "unable to cast '%s' to a UUID value", value.c_str());
30  }
31  }
32 }
33 
34 UUID::UUID(std::string &&value) throw (TypeCastException) : Nullable() {
35  if (value.empty()) {
36  this->value = std::make_shared<sella::util::UUID>();
37  } else {
38  if (LIKELY(boost::regex_match(value.c_str(), UUID::Pattern()))) {
39  try {
40  this->value = std::make_shared<sella::util::UUID>(std::move(value));
41  } catch (sella::util::UUIDException &e) {
42  THROW(TypeCastException, "unable to cast '%s' to a UUID value: %s", value.c_str(), e.what());
43  }
44  } else {
45  THROW(TypeCastException, "unable to cast '%s' to a UUID value", value.c_str());
46  }
47  }
48 }
49 
50 UUID::UUID(const char *value) throw (TypeCastException) : Nullable() {
51  if (value == NULL) {
52  this->value = std::make_shared<sella::util::UUID>();
53  } else {
54  if (LIKELY(boost::regex_match(value, UUID::Pattern()))) {
55  this->value = std::make_shared<sella::util::UUID>(value);
56  } else {
57  THROW(TypeCastException, "unable to cast '%s' to a UUID value", value);
58  }
59  }
60 }
61 
62 UUID::UUID(uuid_t &value) throw () : Nullable() {
63  this->value = std::make_shared<sella::util::UUID>(value);
64 }
65 
66 UUID::UUID(const sella::util::UUID &value) throw () : Nullable(std::make_shared<sella::util::UUID>(value)) { }
67 
68 UUID::UUID(sella::util::UUID &&value) throw () : Nullable(std::make_shared<sella::util::UUID>(std::move(value))) { }
69 
70 UUID::UUID(const UUID &other) throw () : Nullable() {
71  if (LIKELY(this != &other)) {
72  if (LIKELY(!other.isNull())) {
73  if (UNLIKELY(isNull())) {
74  this->value = std::make_shared<sella::util::UUID>(other.get());
75  } else {
76  *std::static_pointer_cast<sella::util::UUID>(this->value) = *std::static_pointer_cast<sella::util::UUID>(other.value);
77  }
78  } else {
79  this->value.reset();
80  }
81  }
82 }
83 
84 UUID::UUID(UUID &&other) throw () : Nullable() {
85  if (LIKELY(!other.isNull())) {
86  this->value = std::move(other.value);
87  }
88 }
89 
90 UUID::~UUID() throw () { }
91 
92 bool UUID::operator==(const UUID &other) const throw () {
93  return (*std::static_pointer_cast<sella::util::UUID>(this->value) == *std::static_pointer_cast<sella::util::UUID>(other.value));
94 }
95 
96 bool UUID::operator!=(const UUID &other) const throw () {
97  return !(*this == other);
98 }
99 
100 bool UUID::operator>(const UUID &other) const throw () {
101  return (*std::static_pointer_cast<sella::util::UUID>(this->value) > *std::static_pointer_cast<sella::util::UUID>(other.value));
102 }
103 
104 bool UUID::operator<(const UUID &other) const throw () {
105  return (*std::static_pointer_cast<sella::util::UUID>(this->value) < *std::static_pointer_cast<sella::util::UUID>(other.value));
106 }
107 
108 UUID& UUID::operator=(const UUID &other) throw () {
109  if (LIKELY((this != &other))) {
110  if (LIKELY(!other.isNull())) {
111  if (UNLIKELY(isNull())) {
112  this->value = std::make_shared<sella::util::UUID>(other.get());
113  } else {
114  *std::static_pointer_cast<sella::util::UUID>(this->value) = *std::static_pointer_cast<sella::util::UUID>(other.value);
115  }
116  } else {
117  this->value.reset();
118  }
119  }
120 
121  return *this;
122 }
123 
124 UUID& UUID::operator=(const sella::util::UUID &other) throw () {
125  if (UNLIKELY(isNull())) {
126  this->value = std::make_shared<sella::util::UUID>(other);
127  } else {
128  *std::static_pointer_cast<sella::util::UUID>(this->value) = other;
129  }
130 
131  return *this;
132 }
133 
134 const sella::util::UUID& UUID::get() const throw () {
135  return *std::static_pointer_cast<sella::util::UUID>(this->value);
136 }
137 
138 sella::util::UUID& UUID::get() throw () {
139  return *std::static_pointer_cast<sella::util::UUID>(this->value);
140 }
141 
142 void UUID::clear() throw() {
143  if (UNLIKELY(isNull())) {
144  this->value = std::make_shared<sella::util::UUID>();
145  } else {
146  std::static_pointer_cast<sella::util::UUID>(this->value)->clear();
147  }
148 }
149 
150 void UUID::shrink_to_fit() throw () {
151  auto &u = *std::static_pointer_cast<sella::util::UUID>(this->value);
152 
153  u.shrink_to_fit();
154 }
155 
156 void UUID::msgpack(sella::util::MessagePack &mpack, uint32_t &offset) const throw (TypeCastException) {
157  uint32_t c;
158 
159  if (UNLIKELY(isNull())) {
160  c = mpack.append(offset);
161  } else {
162  c = mpack.append(offset, std::static_pointer_cast<sella::util::UUID>(this->value)->str());
163  }
164 
165  offset += c;
166 
167  if (UNLIKELY(c == 0)) {
168  THROW(TypeCastException, "Failed to append uuid '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
169  }
170 }
171 
172 void UUID::msgpackBufferSize(size_t &size) const throw () {
173  if (UNLIKELY(isNull())) {
174  size += 1;
175 
176  return;
177  }
178 
179  size += 5 + 36;
180 }
181 
182 std::string UUID::str(int type) const throw () {
183  if (UNLIKELY(isNull())) {
184  return "null";
185  }
186 
187  return this->get().str();
188 }
189 
190 bool UUID::isType(const char *value, size_t size) throw () {
191  return (size == 36 && boost::regex_match(std::string(value, size), Pattern()));
192 }
193 
194 /*
195 ** vim: noet ts=3 sw=3
196 */