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