libutil++  1.9.3
 All Classes Functions Variables
Boolean.cpp
1 /*
2 ** libutil++
3 ** $Id: Boolean.cpp 1702 2016-08-13 23:26:06Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: Boolean.cpp 1702 2016-08-13 23:26:06Z sella $";
10 
11 #include "Boolean.h"
12 #include "../util/CommonMacro.h"
13 
14 #include <ctype.h>
15 
16 using namespace sella::variant;
17 
18 Boolean::Boolean() throw () : Nullable(std::make_shared<bool>(false)) { }
19 
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');
23  } else {
24  THROW(TypeCastException, "unable to cast '%s' to a boolean value", value.c_str());
25  }
26 }
27 
28 Boolean::Boolean(bool value) throw () : Nullable(std::make_shared<bool>(value)) { }
29 
30 Boolean::Boolean(const Boolean &other) throw () : Nullable() {
31  if (LIKELY(this != &other)) {
32  if (LIKELY(!other.isNull())) {
33  if (UNLIKELY(isNull())) {
34  this->value = std::make_shared<bool>(other.get());
35  } else {
36  *std::static_pointer_cast<bool>(this->value) = *std::static_pointer_cast<bool>(other.value);
37  }
38  } else {
39  this->value.reset();
40  }
41  }
42 }
43 
44 Boolean::~Boolean() throw () { }
45 
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));
48 }
49 
50 bool Boolean::operator!=(const Boolean &other) const throw () {
51  return !(*this == other);
52 }
53 
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));
56 }
57 
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));
60 }
61 
62 bool Boolean::operator!() throw () {
63  return !(*std::static_pointer_cast<bool>(this->value));
64 }
65 
66 Boolean& Boolean::operator=(const Boolean &other) throw () {
67  if (LIKELY(this != &other)) {
68  if (LIKELY(!other.isNull())) {
69  if (UNLIKELY(isNull())) {
70  this->value = std::make_shared<bool>(other.get());
71  } else {
72  *std::static_pointer_cast<bool>(this->value) = *std::static_pointer_cast<bool>(other.value);
73  }
74  } else {
75  this->value.reset();
76  }
77  }
78 
79  return *this;
80 }
81 
82 Boolean& Boolean::operator=(bool other) throw () {
83  if (UNLIKELY(isNull())) {
84  this->value = std::make_shared<bool>(other);
85  } else {
86  *std::static_pointer_cast<bool>(this->value) = other;
87  }
88 
89  return *this;
90 }
91 
92 Boolean& Boolean::operator+=(bool value) throw () {
93  (*std::static_pointer_cast<bool>(this->value)) += value;
94 
95  return *this;
96 }
97 
98 Boolean& Boolean::operator-=(bool value) throw () {
99  (*std::static_pointer_cast<bool>(this->value)) -= value;
100 
101  return *this;
102 }
103 
104 Boolean& Boolean::operator*=(bool value) throw () {
105  (*std::static_pointer_cast<bool>(this->value)) *= value;
106 
107  return *this;
108 }
109 
110 Boolean& Boolean::operator/=(bool value) throw () {
111  (*std::static_pointer_cast<bool>(this->value)) /= value;
112 
113  return *this;
114 }
115 
116 Boolean& Boolean::operator^=(bool value) throw () {
117  (*std::static_pointer_cast<bool>(this->value)) = pow((*std::static_pointer_cast<bool>(this->value)), value);
118 
119  return *this;
120 }
121 
122 Boolean& Boolean::operator%=(bool value) throw () {
123  (*std::static_pointer_cast<bool>(this->value)) %= value;
124 
125  return *this;
126 }
127 
128 Boolean& Boolean::operator&=(bool value) throw () {
129  (*std::static_pointer_cast<bool>(this->value)) &= value;
130 
131  return *this;
132 }
133 
134 Boolean& Boolean::operator|=(bool value) throw () {
135  (*std::static_pointer_cast<bool>(this->value)) |= value;
136 
137  return *this;
138 }
139 
140 Boolean& Boolean::operator<<=(int bits) throw () {
141  (*std::static_pointer_cast<bool>(this->value)) <<= bits;
142 
143  return *this;
144 }
145 
146 Boolean& Boolean::operator>>=(int bits) throw () {
147  (*std::static_pointer_cast<bool>(this->value)) >>= bits;
148 
149  return *this;
150 }
151 
152 bool Boolean::get() const throw (UnsupportedOperatorException) {
153  if (UNLIKELY(isNull())) {
154  THROW(UnsupportedOperatorException, "unable to take reference of null value");
155  }
156 
157  return *std::static_pointer_cast<bool>(this->value);
158 }
159 
160 void Boolean::msgpack(sella::util::MessagePack &mpack, uint32_t &offset) const throw (TypeCastException) {
161  uint32_t c;
162 
163  if (UNLIKELY(isNull())) {
164  c = mpack.append(offset);
165  } else {
166  c = mpack.append(offset, *std::static_pointer_cast<bool>(this->value));
167  }
168 
169  offset += c;
170 
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());
173  }
174 }
175 
176 void Boolean::msgpackBufferSize(size_t &size) const throw () {
177  size += 1;
178 }
179 
180 std::string Boolean::str(int type) const throw () {
181  if (UNLIKELY(isNull())) {
182  return "null";
183  }
184 
185  if (get()) {
186  return std::string("true");
187  }
188 
189  return std::string("false");
190 }
191 
192 bool Boolean::isType(const char *value, size_t size) throw () {
193  if (size == 4) {
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'));
197  }
198 
199  return false;
200 }
201 
202 /*
203 ** vim: noet ts=3 sw=3
204 */