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