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