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