9 static const char rcsid[] __attribute__((used)) =
"$Id: Double.cpp 1702 2016-08-13 23:26:06Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 Double::Double() throw () :
Nullable(std::make_shared<
double>()) { }
18 Double::Double(
const std::string &value)
throw (TypeCastException) :
Nullable() {
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)");
28 Double::Double(
double value)
throw () :
Nullable(std::make_shared<double>(value)) { }
32 if (!other.isNull()) {
34 this->value = std::make_shared<double>(other.get());
36 *std::static_pointer_cast<
double>(this->value) = *std::static_pointer_cast<double>(other.value);
44 Double::~Double() throw () { }
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));
50 bool Double::operator!=(
const Double &other)
const throw () {
51 return !(*
this == other);
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));
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));
62 bool Double::operator!() throw () {
63 return !(*std::static_pointer_cast<
double>(this->value));
66 Double& Double::operator=(
const Double &other)
throw () {
68 if (!other.isNull()) {
70 this->value = std::make_shared<double>(other.get());
72 *std::static_pointer_cast<
double>(this->value) = *std::static_pointer_cast<double>(other.value);
82 Double& Double::operator=(
double other)
throw () {
84 this->value = std::make_shared<double>(other);
86 *std::static_pointer_cast<
double>(this->value) = other;
92 Double& Double::operator++() throw () {
93 ++(*std::static_pointer_cast<
double>(this->value));
98 Double& Double::operator--() throw () {
99 --(*std::static_pointer_cast<
double>(this->value));
104 Double& Double::operator+=(
double value)
throw () {
105 (*std::static_pointer_cast<
double>(this->value)) += value;
110 Double& Double::operator-=(
double value)
throw () {
111 (*std::static_pointer_cast<
double>(this->value)) -= value;
116 Double& Double::operator*=(
double value)
throw () {
117 (*std::static_pointer_cast<
double>(this->value)) *= value;
122 Double& Double::operator/=(
double value)
throw () {
123 (*std::static_pointer_cast<
double>(this->value)) /= value;
128 Double& Double::operator^=(
double value)
throw () {
129 (*std::static_pointer_cast<
double>(this->value)) = pow((*std::static_pointer_cast<double>(this->value)), value);
134 double Double::get()
const throw (UnsupportedOperatorException) {
136 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
139 return *std::static_pointer_cast<
double>(this->value);
145 if (UNLIKELY(this->isNull())) {
146 c = mpack.append(offset);
148 c = mpack.append(offset, *std::static_pointer_cast<double>(this->value));
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());
158 void Double::msgpackBufferSize(
size_t &size)
const throw () {
159 if (UNLIKELY(isNull())) {
168 std::string Double::str(
int type)
const throw () {
169 if (this->isNull()) {
173 return std::to_string(*std::static_pointer_cast<double>(this->value));
176 bool Double::isType(
const char *value,
size_t size)
throw () {
179 for (
size_t i = (value[0] ==
'-') ? 1 : 0; i < size; i++) {
180 if (!::isdigit(value[i]) != 0) {
181 if (dot ==
false && value[i] ==
'.') {
189 return (size > 0 && ::isdigit(value[size - 1]));