00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: Double.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "Double.h"
00012
00013 using namespace sella::variant;
00014
00015 const boost::regex Double::Pattern("^-?[0-9]*\\.[0-9]+$");
00016
00017 Double::Double() throw () : Nullable() { }
00018
00019 Double::Double(const std::string &value) throw (TypeCastException) : Nullable() {
00020 try {
00021 this->value = std::make_shared<double>(std::stod(value));
00022 } catch (std::invalid_argument &e) {
00023 THROW(TypeCastException, "unable to cast from string to a decimal value (invalid string)");
00024 } catch (std::out_of_range &e) {
00025 THROW(TypeCastException, "unable to cast from string to a decimal value (overflow/underflow)");
00026 }
00027 }
00028
00029 Double::Double(double value) throw () : Nullable(std::make_shared<double>(value)) { }
00030
00031 Double::Double(const Double &other) throw () : Nullable(other) {
00032 if (!other.isNull()) {
00033 this->value = std::make_shared<double>(*other.getDouble());
00034 }
00035 }
00036
00037 Double::~Double() throw () { }
00038
00039 bool Double::operator==(const Double &other) const throw () {
00040 return (*std::static_pointer_cast<double>(this->value) == *std::static_pointer_cast<double>(other.value));
00041 }
00042
00043 bool Double::operator!=(const Double &other) const throw () {
00044 return !(*this == other);
00045 }
00046
00047 bool Double::operator>(const Double &other) const throw () {
00048 return (*std::static_pointer_cast<double>(this->value) > *std::static_pointer_cast<double>(other.value));
00049 }
00050
00051 bool Double::operator<(const Double &other) const throw () {
00052 return (*std::static_pointer_cast<double>(this->value) < *std::static_pointer_cast<double>(other.value));
00053 }
00054
00055 bool Double::operator!() throw () {
00056 return !(*std::static_pointer_cast<double>(this->value));
00057 }
00058
00059 Double& Double::operator=(const Double &other) throw () {
00060 if (this != &other) {
00061 Nullable::operator=(other);
00062 if (!other.isNull()) {
00063 this->value = std::make_shared<double>(*other.getDouble());
00064 }
00065 }
00066
00067 return *this;
00068 }
00069
00070 Double& Double::operator++() throw () {
00071 cache.clear();
00072 ++(*std::static_pointer_cast<double>(this->value));
00073
00074 return *this;
00075 }
00076
00077 Double& Double::operator--() throw () {
00078 cache.clear();
00079 --(*std::static_pointer_cast<double>(this->value));
00080
00081 return *this;
00082 }
00083
00084 Double& Double::operator+=(double value) throw () {
00085 cache.clear();
00086 (*std::static_pointer_cast<double>(this->value)) += value;
00087
00088 return *this;
00089 }
00090
00091 Double& Double::operator-=(double value) throw () {
00092 cache.clear();
00093 (*std::static_pointer_cast<double>(this->value)) -= value;
00094
00095 return *this;
00096 }
00097
00098 Double& Double::operator*=(double value) throw () {
00099 cache.clear();
00100 (*std::static_pointer_cast<double>(this->value)) *= value;
00101
00102 return *this;
00103 }
00104
00105 Double& Double::operator/=(double value) throw () {
00106 cache.clear();
00107 (*std::static_pointer_cast<double>(this->value)) /= value;
00108
00109 return *this;
00110 }
00111
00112 Double& Double::operator^=(double value) throw () {
00113 cache.clear();
00114 (*std::static_pointer_cast<double>(this->value)) = pow((*std::static_pointer_cast<double>(this->value)), value);
00115
00116 return *this;
00117 }
00118
00119 const double* Double::getDouble() const throw (UnsupportedOperatorException) {
00120 if (isNull()) {
00121 THROW(UnsupportedOperatorException, "unable to take reference of null value");
00122 }
00123
00124 return &(*std::static_pointer_cast<double>(this->value));
00125 }
00126
00127 const std::string& Double::str() const throw () {
00128 if (cache.empty()) {
00129 if (this->isNull()) {
00130 cache = "null";
00131 } else {
00132 char buffer[4096];
00133
00134 snprintf(buffer, sizeof(buffer), "%lf", *(this->getDouble()));
00135 cache = buffer;
00136 }
00137 }
00138
00139 return cache;
00140 }
00141
00142
00143
00144