9 static const char rcsid[] __attribute__((used)) =
"$Id: Boolean.cpp 1702 2016-08-13 23:26:06Z sella $";
12 #include "../util/CommonMacro.h"
16 using namespace sella::variant;
18 Boolean::Boolean() throw () :
Nullable(std::make_shared<
bool>(false)) { }
20 Boolean::Boolean(
const std::string &value)
throw (TypeCastException) :
Nullable() {
21 if (LIKELY(boost::regex_match(value, Boolean::Pattern()))) {
22 this->value = std::make_shared<bool>(tolower(value[0]) ==
't');
24 THROW(TypeCastException,
"unable to cast '%s' to a boolean value", value.c_str());
28 Boolean::Boolean(
bool value)
throw () :
Nullable(std::make_shared<bool>(value)) { }
31 if (LIKELY(
this != &other)) {
32 if (LIKELY(!other.isNull())) {
33 if (UNLIKELY(isNull())) {
34 this->value = std::make_shared<bool>(other.get());
36 *std::static_pointer_cast<
bool>(this->value) = *std::static_pointer_cast<bool>(other.value);
44 Boolean::~Boolean() throw () { }
46 bool Boolean::operator==(
const Boolean &other)
const throw () {
47 return (*std::static_pointer_cast<bool>(this->value) == *std::static_pointer_cast<bool>(other.value));
50 bool Boolean::operator!=(
const Boolean &other)
const throw () {
51 return !(*
this == other);
54 bool Boolean::operator>(
const Boolean &other)
const throw () {
55 return (*std::static_pointer_cast<bool>(this->value) > *std::static_pointer_cast<bool>(other.value));
58 bool Boolean::operator<(
const Boolean &other)
const throw () {
59 return (*std::static_pointer_cast<bool>(this->value) < *std::static_pointer_cast<bool>(other.value));
62 bool Boolean::operator!() throw () {
63 return !(*std::static_pointer_cast<
bool>(this->value));
67 if (LIKELY(
this != &other)) {
68 if (LIKELY(!other.isNull())) {
69 if (UNLIKELY(isNull())) {
70 this->value = std::make_shared<bool>(other.get());
72 *std::static_pointer_cast<
bool>(this->value) = *std::static_pointer_cast<bool>(other.value);
82 Boolean& Boolean::operator=(
bool other)
throw () {
83 if (UNLIKELY(isNull())) {
84 this->value = std::make_shared<bool>(other);
86 *std::static_pointer_cast<
bool>(this->value) = other;
92 Boolean& Boolean::operator+=(
bool value)
throw () {
93 (*std::static_pointer_cast<
bool>(this->value)) += value;
98 Boolean& Boolean::operator-=(
bool value)
throw () {
99 (*std::static_pointer_cast<
bool>(this->value)) -= value;
104 Boolean& Boolean::operator*=(
bool value)
throw () {
105 (*std::static_pointer_cast<
bool>(this->value)) *= value;
110 Boolean& Boolean::operator/=(
bool value)
throw () {
111 (*std::static_pointer_cast<
bool>(this->value)) /= value;
116 Boolean& Boolean::operator^=(
bool value)
throw () {
117 (*std::static_pointer_cast<
bool>(this->value)) = pow((*std::static_pointer_cast<bool>(this->value)), value);
122 Boolean& Boolean::operator%=(
bool value)
throw () {
123 (*std::static_pointer_cast<
bool>(this->value)) %= value;
128 Boolean& Boolean::operator&=(
bool value)
throw () {
129 (*std::static_pointer_cast<
bool>(this->value)) &= value;
134 Boolean& Boolean::operator|=(
bool value)
throw () {
135 (*std::static_pointer_cast<
bool>(this->value)) |= value;
140 Boolean& Boolean::operator<<=(
int bits)
throw () {
141 (*std::static_pointer_cast<
bool>(this->value)) <<= bits;
146 Boolean& Boolean::operator>>=(
int bits)
throw () {
147 (*std::static_pointer_cast<
bool>(this->value)) >>= bits;
152 bool Boolean::get()
const throw (UnsupportedOperatorException) {
153 if (UNLIKELY(isNull())) {
154 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
157 return *std::static_pointer_cast<
bool>(this->value);
163 if (UNLIKELY(isNull())) {
164 c = mpack.append(offset);
166 c = mpack.append(offset, *std::static_pointer_cast<bool>(this->value));
171 if (UNLIKELY(c == 0)) {
172 THROW(TypeCastException,
"Failed to append boolean '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
176 void Boolean::msgpackBufferSize(
size_t &size)
const throw () {
180 std::string Boolean::str(
int type)
const throw () {
181 if (UNLIKELY(isNull())) {
186 return std::string(
"true");
189 return std::string(
"false");
192 bool Boolean::isType(
const char *value,
size_t size)
throw () {
194 return ((value[0] ==
't' || value[0] ==
'T') && (value[1] ==
'r' || value[1] ==
'R') && (value[2] ==
'u' || value[2] ==
'U') && (value[3] ==
'e' || value[3] ==
'E'));
195 }
else if (size == 5) {
196 return ((value[0] ==
'f' || value[0] ==
'F') && (value[1] ==
'a' || value[1] ==
'A') && (value[2] ==
'l' || value[2] ==
'L') && (value[3] ==
's' || value[3] ==
'S') && (value[4] ==
'e' || value[4] ==
'E'));