9 static const char rcsid[] __attribute__((used)) =
"$Id: Signed.cpp 1702 2016-08-13 23:26:06Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 Signed::Signed() throw () :
Nullable(std::make_shared<int64_t>(0)) { }
18 Signed::Signed(
const std::string &value)
throw (TypeCastException) :
Nullable() {
20 this->value = std::make_shared<int64_t>(std::stoll(value));
21 }
catch (std::invalid_argument &e) {
22 THROW(TypeCastException,
"unable to cast '%s' to a signed value (invalid string)", value.c_str());
23 }
catch (std::out_of_range &e) {
24 THROW(TypeCastException,
"unable to cast '%s' to a signed value (overflow/underflow)", value.c_str());
28 Signed::Signed(int64_t value)
throw () :
Nullable(std::make_shared<int64_t>(value)) { }
31 if (LIKELY(
this != &other)) {
32 if (LIKELY(!other.isNull())) {
33 if (UNLIKELY(isNull())) {
34 this->value = std::make_shared<int64_t>(other.get());
36 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
44 Signed::~Signed() throw () { }
46 bool Signed::operator==(
const Signed &other)
const throw () {
47 return (*std::static_pointer_cast<int64_t>(this->value) == *std::static_pointer_cast<int64_t>(other.value));
50 bool Signed::operator!=(
const Signed &other)
const throw () {
51 return !(*
this == other);
54 bool Signed::operator>(
const Signed &other)
const throw () {
55 return (*std::static_pointer_cast<int64_t>(this->value) > *std::static_pointer_cast<int64_t>(other.value));
58 bool Signed::operator<(
const Signed &other)
const throw () {
59 return (*std::static_pointer_cast<int64_t>(this->value) < *std::static_pointer_cast<int64_t>(other.value));
62 bool Signed::operator!() throw () {
63 return !(*std::static_pointer_cast<int64_t>(this->value));
66 Signed& Signed::operator=(
const Signed &other)
throw () {
67 if (LIKELY(
this != &other)) {
68 if (LIKELY(!other.isNull())) {
69 if (UNLIKELY(isNull())) {
70 this->value = std::make_shared<int64_t>(other.get());
72 *std::static_pointer_cast<int64_t>(this->value) = *std::static_pointer_cast<int64_t>(other.value);
82 Signed& Signed::operator=(int64_t other)
throw () {
83 if (UNLIKELY(isNull())) {
84 this->value = std::make_shared<int64_t>(other);
86 *std::static_pointer_cast<int64_t>(this->value) = other;
92 Signed& Signed::operator++() throw () {
93 ++(*std::static_pointer_cast<int64_t>(this->value));
98 Signed& Signed::operator--() throw () {
99 --(*std::static_pointer_cast<int64_t>(this->value));
104 Signed& Signed::operator+=(int64_t value)
throw () {
105 (*std::static_pointer_cast<int64_t>(this->value)) += value;
110 Signed& Signed::operator-=(int64_t value)
throw () {
111 (*std::static_pointer_cast<int64_t>(this->value)) -= value;
116 Signed& Signed::operator*=(int64_t value)
throw () {
117 (*std::static_pointer_cast<int64_t>(this->value)) *= value;
122 Signed& Signed::operator/=(int64_t value)
throw () {
123 (*std::static_pointer_cast<int64_t>(this->value)) /= value;
128 Signed& Signed::operator^=(int64_t value)
throw () {
129 (*std::static_pointer_cast<int64_t>(this->value)) = pow((*std::static_pointer_cast<int64_t>(this->value)), value);
134 Signed& Signed::operator%=(int64_t value)
throw () {
135 (*std::static_pointer_cast<int64_t>(this->value)) %= value;
140 Signed& Signed::operator&=(int64_t value)
throw () {
141 (*std::static_pointer_cast<int64_t>(this->value)) &= value;
146 Signed& Signed::operator|=(int64_t value)
throw () {
147 (*std::static_pointer_cast<int64_t>(this->value)) |= value;
152 Signed& Signed::operator<<=(
int bits)
throw () {
153 (*std::static_pointer_cast<int64_t>(this->value)) <<= bits;
158 Signed& Signed::operator>>=(
int bits)
throw () {
159 (*std::static_pointer_cast<int64_t>(this->value)) >>= bits;
164 int64_t Signed::get()
const throw (UnsupportedOperatorException) {
165 if (UNLIKELY(isNull())) {
166 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
169 return *std::static_pointer_cast<int64_t>(this->value);
175 if (UNLIKELY(isNull())) {
176 c = mpack.append(offset);
178 c = mpack.append(offset, *std::static_pointer_cast<int64_t>(this->value));
183 if (UNLIKELY(c == 0)) {
184 THROW(TypeCastException,
"Failed to append signed '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
188 void Signed::msgpackBufferSize(
size_t &size)
const throw () {
189 if (UNLIKELY(isNull())) {
198 std::string Signed::str(
int type)
const throw () {
199 if (UNLIKELY(isNull())) {
203 return std::to_string(*std::static_pointer_cast<int64_t>(this->value));
206 bool Signed::isType(
const char *value,
size_t size)
throw () {
207 for (
size_t i = (value[0] ==
'+' || value[0] ==
'-') ? 1 : 0; i < size; i++) {
208 if (!::isdigit(value[i]) != 0) {