libutil++  1.9.3
 All Classes Functions Variables
Unsigned.cpp
1 /*
2 ** libutil++
3 ** $Id: Unsigned.cpp 1702 2016-08-13 23:26:06Z 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: Unsigned.cpp 1702 2016-08-13 23:26:06Z sella $";
10 
11 #include "Unsigned.h"
12 #include "../util/CommonMacro.h"
13 
14 using namespace sella::variant;
15 
16 Unsigned::Unsigned() throw () : Nullable(std::make_shared<uint64_t>(0)) { }
17 
18 Unsigned::Unsigned(const std::string &value) throw (TypeCastException) : Nullable() {
19  try {
20  this->value = std::make_shared<uint64_t>(std::stoull(value));
21  } catch (std::invalid_argument &e) {
22  THROW(TypeCastException, "unable to cast '%s' to an unsigned value (invalid string)", value.c_str());
23  } catch (std::out_of_range &e) {
24  THROW(TypeCastException, "unable to cast '%s' to an unsigned value (overflow/underflow)", value.c_str());
25  }
26 }
27 
28 Unsigned::Unsigned(uint64_t value) throw () : Nullable(std::make_shared<uint64_t>(value)) { }
29 
30 Unsigned::Unsigned(const Unsigned &other) throw () : Nullable() {
31  if (LIKELY(this != &other)) {
32  if (LIKELY(!other.isNull())) {
33  if (UNLIKELY(isNull())) {
34  this->value = std::make_shared<uint64_t>(other.get());
35  } else {
36  *std::static_pointer_cast<uint64_t>(this->value) = *std::static_pointer_cast<uint64_t>(other.value);
37  }
38  } else {
39  this->value.reset();
40  }
41  }
42 }
43 
44 Unsigned::~Unsigned() throw () { }
45 
46 bool Unsigned::operator==(const Unsigned &other) const throw () {
47  return (*std::static_pointer_cast<uint64_t>(this->value) == *std::static_pointer_cast<uint64_t>(other.value));
48 }
49 
50 bool Unsigned::operator!=(const Unsigned &other) const throw () {
51  return !(*this == other);
52 }
53 
54 bool Unsigned::operator>(const Unsigned &other) const throw () {
55  return (*std::static_pointer_cast<uint64_t>(this->value) > *std::static_pointer_cast<uint64_t>(other.value));
56 }
57 
58 bool Unsigned::operator<(const Unsigned &other) const throw () {
59  return (*std::static_pointer_cast<uint64_t>(this->value) < *std::static_pointer_cast<uint64_t>(other.value));
60 }
61 
62 bool Unsigned::operator!() throw () {
63  return !(*std::static_pointer_cast<uint64_t>(this->value));
64 }
65 
66 Unsigned& Unsigned::operator=(const Unsigned &other) throw () {
67  if (LIKELY(this != &other)) {
68  if (LIKELY(!other.isNull())) {
69  if (UNLIKELY(isNull())) {
70  this->value = std::make_shared<uint64_t>(other.get());
71  } else {
72  *std::static_pointer_cast<uint64_t>(this->value) = *std::static_pointer_cast<uint64_t>(other.value);
73  }
74  } else {
75  this->value.reset();
76  }
77  }
78 
79  return *this;
80 }
81 
82 Unsigned& Unsigned::operator=(uint64_t other) throw () {
83  if (UNLIKELY(isNull())) {
84  this->value = std::make_shared<uint64_t>(other);
85  } else {
86  *std::static_pointer_cast<uint64_t>(this->value) = other;
87  }
88 
89  return *this;
90 }
91 
92 Unsigned& Unsigned::operator++() throw () {
93  ++(*std::static_pointer_cast<uint64_t>(this->value));
94 
95  return *this;
96 }
97 
98 Unsigned& Unsigned::operator--() throw () {
99  --(*std::static_pointer_cast<uint64_t>(this->value));
100 
101  return *this;
102 }
103 
104 Unsigned& Unsigned::operator+=(uint64_t value) throw () {
105  (*std::static_pointer_cast<uint64_t>(this->value)) += value;
106 
107  return *this;
108 }
109 
110 Unsigned& Unsigned::operator-=(uint64_t value) throw () {
111  (*std::static_pointer_cast<uint64_t>(this->value)) -= value;
112 
113  return *this;
114 }
115 
116 Unsigned& Unsigned::operator*=(uint64_t value) throw () {
117  (*std::static_pointer_cast<uint64_t>(this->value)) *= value;
118 
119  return *this;
120 }
121 
122 Unsigned& Unsigned::operator/=(uint64_t value) throw () {
123  (*std::static_pointer_cast<uint64_t>(this->value)) /= value;
124 
125  return *this;
126 }
127 
128 Unsigned& Unsigned::operator^=(uint64_t value) throw () {
129  (*std::static_pointer_cast<uint64_t>(this->value)) = pow((*std::static_pointer_cast<uint64_t>(this->value)), value);
130 
131  return *this;
132 }
133 
134 Unsigned& Unsigned::operator%=(uint64_t value) throw () {
135  (*std::static_pointer_cast<uint64_t>(this->value)) %= value;
136 
137  return *this;
138 }
139 
140 Unsigned& Unsigned::operator&=(uint64_t value) throw () {
141  (*std::static_pointer_cast<uint64_t>(this->value)) &= value;
142 
143  return *this;
144 }
145 
146 Unsigned& Unsigned::operator|=(uint64_t value) throw () {
147  (*std::static_pointer_cast<uint64_t>(this->value)) |= value;
148 
149  return *this;
150 }
151 
152 Unsigned& Unsigned::operator<<=(int bits) throw () {
153  (*std::static_pointer_cast<uint64_t>(this->value)) <<= bits;
154 
155  return *this;
156 }
157 
158 Unsigned& Unsigned::operator>>=(int bits) throw () {
159  (*std::static_pointer_cast<uint64_t>(this->value)) >>= bits;
160 
161  return *this;
162 }
163 
164 uint64_t Unsigned::get() const throw (UnsupportedOperatorException) {
165  if (UNLIKELY(isNull())) {
166  THROW(UnsupportedOperatorException, "unable to take reference of null value");
167  }
168 
169  return *std::static_pointer_cast<uint64_t>(this->value);
170 }
171 
172 void Unsigned::msgpack(sella::util::MessagePack &mpack, uint32_t &offset) const throw (TypeCastException) {
173  uint32_t c;
174 
175  if (UNLIKELY(isNull())) {
176  c = mpack.append(offset);
177  } else {
178  c = mpack.append(offset, *std::static_pointer_cast<uint64_t>(this->value));
179  }
180 
181  offset += c;
182 
183  if (UNLIKELY(c == 0)) {
184  THROW(TypeCastException, "Failed to append unsigned '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
185  }
186 }
187 
188 void Unsigned::msgpackBufferSize(size_t &size) const throw () {
189  if (UNLIKELY(isNull())) {
190  size += 1;
191 
192  return;
193  }
194 
195  size += 9;
196 }
197 
198 std::string Unsigned::str(int type) const throw () {
199  if (UNLIKELY(isNull())) {
200  return "null";
201  }
202 
203  return std::to_string(*std::static_pointer_cast<uint64_t>(this->value));
204 }
205 
206 bool Unsigned::isType(const char *value, size_t size) throw () {
207  for (size_t i = (value[0] == '+') ? 1 : 0; i < size; i++) {
208  if (!::isdigit(value[i]) != 0) {
209  return false;
210  }
211  }
212 
213  return true;
214 }
215 
216 /*
217 ** vim: noet ts=3 sw=3
218 */