9 static const char rcsid[] __attribute__((used)) =
"$Id: Operators.cpp 1892 2017-06-23 17:23:36Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 bool Variant::operator!() throw (UnsupportedOperatorException) {
19 return !get<Boolean>();
22 return !get<Signed>();
25 return !get<Unsigned>();
28 return !get<Double>();
31 THROW(UnsupportedOperatorException,
"Unable to perform operator! on %s value", getTypeName());
35 Variant& Variant::operator++() throw (UnsupportedOperatorException) {
38 get<Boolean>() = !get<Boolean>();
58 THROW(UnsupportedOperatorException,
"Unable to perform operator++ on %s value", getTypeName());
64 Variant Variant::operator++(
int) throw (UnsupportedOperatorException) {
69 get<Boolean>() = !get<Boolean>();
89 THROW(UnsupportedOperatorException,
"Unable to perform operator++ on %s value", getTypeName());
95 const Variant& Variant::operator[](
const char* key)
const throw (UnsupportedOperatorException, RangeException) {
96 return this->operator[](std::string(key));
99 const Variant& Variant::operator[](
const std::string &key)
const throw (UnsupportedOperatorException, RangeException) {
100 if (UNLIKELY(getType() != TypeObject)) {
101 THROW(UnsupportedOperatorException,
"type must be object");
104 const auto &o = get<Object>().
get();
105 const auto &i = o.find(key);
108 THROW(RangeException,
"key '%s' not found", key.c_str());
114 Variant& Variant::operator[](
const char* key)
throw () {
115 return this->operator[](std::move(std::string(key)));
118 Variant& Variant::operator[](
const std::string &key)
throw () {
119 if (getType() != TypeObject) {
120 this->value = std::make_shared<Object>();
121 this->type = TypeObject;
124 return get<Object>()[key];
127 Variant& Variant::operator[](std::string &&key) throw () {
128 if (getType() != TypeObject) {
129 this->value = std::make_shared<Object>();
130 this->type = TypeObject;
133 return get<Object>()[std::move(key)];
136 const Variant& Variant::operator[](
int index)
const throw (UnsupportedOperatorException, RangeException) {
137 return operator[]((
size_t) index);
140 const Variant& Variant::operator[](
size_t index)
const throw (UnsupportedOperatorException, RangeException) {
141 if (UNLIKELY(getType() != TypeVector)) {
142 THROW(UnsupportedOperatorException,
"type must be vector");
145 const auto &v = get<Vector>().
get();
147 if (UNLIKELY(index > v.size())) {
148 THROW(RangeException,
"index out of bounds");
154 Variant& Variant::operator[](
int index)
throw () {
155 return operator[]((
size_t) index);
158 Variant& Variant::operator[](
size_t index)
throw () {
159 if (getType() != TypeVector) {
160 this->value = std::make_shared<Vector>();
161 this->type = TypeVector;
164 return get<Vector>()[index];
168 if (LIKELY(
this != &other)) {
169 bool same = (this->type == other.type);
171 switch (other.type) {
175 this->value = null();
182 this->get<Boolean>() = other.get<
Boolean>();
184 this->value = std::make_shared<Boolean>(other.get<
Boolean>());
191 this->get<Signed>() = other.get<
Signed>();
193 this->value = std::make_shared<Signed>(other.get<
Signed>());
200 this->get<Unsigned>() = other.get<
Unsigned>();
202 this->value = std::make_shared<Unsigned>(other.get<
Unsigned>());
209 this->get<Double>() = other.get<
Double>();
211 this->value = std::make_shared<Double>(other.get<
Double>());
218 this->get<String>() = other.get<
String>();
220 this->value = std::make_shared<String>(other.get<
String>());
227 this->get<UUID>() = other.get<
UUID>();
229 this->value = std::make_shared<UUID>(other.get<
UUID>());
236 this->get<Binary>() = other.get<
Binary>();
238 this->value = std::make_shared<Binary>(other.get<
Binary>());
245 this->get<Pointer>() = other.get<
Pointer>();
247 this->value = std::make_shared<Pointer>(other.get<
Pointer>());
254 this->get<IPAddress>() = other.get<
IPAddress>();
256 this->value = std::make_shared<IPAddress>(other.get<
IPAddress>());
263 this->get<IPPrefix>() = other.get<
IPPrefix>();
265 this->value = std::make_shared<IPPrefix>(other.get<
IPPrefix>());
272 this->get<Interval>() = other.get<
Interval>();
274 this->value = std::make_shared<Interval>(other.get<
Interval>());
281 this->get<Time>() = other.get<
Time>();
283 this->value = std::make_shared<Time>(other.get<
Time>());
290 this->get<Date>() = other.get<
Date>();
292 this->value = std::make_shared<Date>(other.get<
Date>());
299 this->get<TimeStamp>() = other.get<
TimeStamp>();
301 this->value = std::make_shared<TimeStamp>(other.get<
TimeStamp>());
308 this->get<Vector>() = other.get<
Vector>();
310 this->value = std::make_shared<Vector>(other.get<
Vector>());
317 this->get<Object>() = other.get<
Object>();
319 this->value = std::make_shared<Object>(other.get<
Object>());
325 fprintf(stderr,
"DEBUG: Missing Type in switch statements.\n");
329 this->type = other.type;
330 this->cache = other.cache;
337 if (LIKELY(
this != &other)) {
344 Variant& Variant::operator=(
const std::string &value)
throw () {
345 if (this->type == TypeString) {
346 this->get<String>() = value;
348 this->value = std::make_shared<String>(value);
350 this->type = TypeString;
356 Variant& Variant::operator=(
const std::shared_ptr<Nullable> &value)
throw () {
358 this->type = value->getType();
364 Variant& Variant::operator=(std::shared_ptr<Nullable> &&value) throw () {
365 this->type = value->getType();
366 this->value.swap(value);
372 Variant& Variant::operator=(std::string &&value) throw () {
373 if (this->type == TypeString) {
374 this->get<String>() = std::move(value);
376 this->value = std::make_shared<String>(std::move(value));
378 this->type = TypeString;
384 Variant& Variant::operator=(
const char *value)
throw () {
385 if (this->type == TypeString) {
386 this->get<String>() = value;
388 this->value = std::make_shared<String>(value);
390 this->type = TypeString;
397 if (this->type == TypeUUID) {
398 this->get<UUID>() = value;
400 this->value = std::make_shared<UUID>(value);
402 this->type = TypeUUID;
408 if (this->type == TypeUUID) {
409 this->get<UUID>() = std::move(value);
411 this->value = std::make_shared<UUID>(std::move(value));
413 this->type = TypeUUID;
419 Variant& Variant::operator=(uuid_t &value)
throw () {
420 this->value = std::make_shared<UUID>(value);
421 this->type = TypeUUID;
427 Variant& Variant::operator=(
void *ptr)
throw () {
428 if (this->type == TypePointer) {
429 this->get<Pointer>() = ptr;
431 this->value = std::make_shared<Pointer>(ptr);
433 this->type = TypePointer;
440 if (this->type == TypeIPAddress) {
441 this->get<IPAddress>() = value;
443 this->value = std::make_shared<IPAddress>(value);
445 this->type = TypeIPAddress;
452 if (this->type == TypeIPAddress) {
453 this->get<IPAddress>() = std::move(value);
455 this->value = std::make_shared<IPAddress>(std::move(value));
457 this->type = TypeIPAddress;
464 if (this->type == TypeIPPrefix) {
465 this->get<IPPrefix>() = value;
467 this->value = std::make_shared<IPPrefix>(value);
469 this->type = TypeIPPrefix;
476 if (this->type == TypeIPPrefix) {
477 this->get<IPPrefix>() = std::move(value);
479 this->value = std::make_shared<IPPrefix>(std::move(value));
481 this->type = TypeIPPrefix;
487 Variant& Variant::operator=(
bool value)
throw () {
488 if (this->type == TypeBoolean) {
489 this->get<Boolean>() = value;
491 this->value = std::make_shared<Boolean>(value);
493 this->type = TypeBoolean;
499 Variant& Variant::operator=(uint8_t value)
throw () {
500 return operator=((uint64_t) value);
503 Variant& Variant::operator=(uint16_t value)
throw () {
504 return operator=((uint64_t) value);
507 Variant& Variant::operator=(
unsigned int value)
throw () {
508 return operator=((uint64_t) value);
511 Variant& Variant::operator=(uint64_t value)
throw () {
512 if (this->type == TypeUnsigned) {
513 this->get<Unsigned>() = value;
515 this->value = std::make_shared<Unsigned>(value);
517 this->type = TypeUnsigned;
523 Variant& Variant::operator=(int8_t value)
throw () {
524 return operator=((int64_t) value);
527 Variant& Variant::operator=(int16_t value)
throw () {
528 return operator=((int64_t) value);
531 Variant& Variant::operator=(
int value)
throw () {
532 return operator=((int64_t) value);
535 Variant& Variant::operator=(int64_t value)
throw () {
536 if (this->type == TypeSigned) {
537 this->get<Signed>() = value;
539 this->value = std::make_shared<Signed>(value);
541 this->type = TypeSigned;
547 Variant& Variant::operator=(
float value)
throw () {
548 return operator=((
double) value);
551 Variant& Variant::operator=(
double value)
throw () {
552 if (this->type == TypeDouble) {
553 this->get<Double>() = value;
555 this->value = std::make_shared<Double>(value);
557 this->type = TypeDouble;
564 Variant& Variant::operator=(
unsigned long long value)
throw () {
565 return operator=((uint64_t) value);
568 Variant& Variant::operator=(
long long value)
throw () {
569 return operator=((int64_t) value);
572 Variant& Variant::operator=(
unsigned long value)
throw () {
573 return operator=((uint64_t) value);
576 Variant& Variant::operator=(
long value)
throw () {
577 return operator=((int64_t) value);
581 Variant::operator
const char*()
const throw (TypeCastException) {
582 return this->cast<const char*>();
585 Variant::operator
const std::string&()
const throw (TypeCastException) {
586 return this->cast<const std::string&>();
590 return this->cast<const sella::util::UUID&>();
594 return this->cast<const sella::net::IPAddress&>();
598 return this->cast<const sella::net::IPPrefix&>();
601 Variant::operator bool()
const throw (TypeCastException) {
602 return this->cast<bool>();
605 Variant::operator uint8_t()
const throw (TypeCastException) {
606 return this->cast<uint8_t>();
609 Variant::operator uint16_t()
const throw (TypeCastException) {
610 return this->cast<uint16_t>();
613 Variant::operator uint32_t()
const throw (TypeCastException) {
614 return this->cast<uint32_t>();
617 Variant::operator uint64_t()
const throw (TypeCastException) {
618 return this->cast<uint64_t>();
621 Variant::operator int8_t()
const throw (TypeCastException) {
622 return this->cast<int8_t>();
625 Variant::operator int16_t()
const throw (TypeCastException) {
626 return this->cast<int16_t>();
629 Variant::operator int32_t()
const throw (TypeCastException) {
630 return this->cast<int32_t>();
633 Variant::operator int64_t()
const throw (TypeCastException) {
634 return this->cast<int64_t>();
637 Variant::operator float()
const throw (TypeCastException) {
638 return this->cast<float>();
641 Variant::operator double()
const throw (TypeCastException) {
642 return this->cast<double>();
646 Variant::operator
unsigned long long()
const throw (TypeCastException) {
647 return this->cast<uint64_t>();
650 Variant::operator
long long()
const throw (TypeCastException) {
651 return this->cast<int64_t>();
654 Variant::operator
unsigned long()
const throw (TypeCastException) {
655 return this->cast<uint64_t>();
658 Variant::operator long()
const throw (TypeCastException) {
659 return this->cast<int64_t>();
666 if (a.getType() != b.getType()) {
668 }
else if (a.isNull()) {
672 switch (a.getType()) {
673 case Variant::TypeBoolean:
676 case Variant::TypeSigned:
679 case Variant::TypeUnsigned:
682 case Variant::TypeDouble:
685 case Variant::TypeString:
688 case Variant::TypeUUID:
689 return (a.get<
UUID>() == b.get<
UUID>());
691 case Variant::TypeBinary:
694 case Variant::TypePointer:
697 case Variant::TypeIPAddress:
700 case Variant::TypeIPPrefix:
703 case Variant::TypeInterval:
706 case Variant::TypeTime:
707 return (a.get<
Time>() == b.get<
Time>());
709 case Variant::TypeDate:
710 return (a.get<
Date>() == b.get<
Date>());
712 case Variant::TypeTimeStamp:
715 case Variant::TypeVector:
718 case Variant::TypeObject:
722 fprintf(stderr,
"DEBUG: Missing Type in switch statements.\n");
730 bool operator>(
const Variant &a,
const Variant &b)
throw (UnsupportedOperatorException) {
731 if (a.getType() != b.getType()) {
732 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value for source %s value", a.getTypeName(), b.getTypeName());
733 }
else if (a.isNull()) {
737 switch (a.getType()) {
738 case Variant::TypeBoolean:
741 case Variant::TypeSigned:
744 case Variant::TypeUnsigned:
747 case Variant::TypeDouble:
750 case Variant::TypeString:
753 case Variant::TypeUUID:
754 return (a.get<
UUID>() > b.get<
UUID>());
756 case Variant::TypeBinary:
757 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value", a.getTypeName());
759 case Variant::TypePointer:
762 case Variant::TypeIPAddress:
765 case Variant::TypeIPPrefix:
768 case Variant::TypeInterval:
771 case Variant::TypeTime:
772 return (a.get<
Time>() > b.get<
Time>());
774 case Variant::TypeDate:
775 return (a.get<
Date>() > b.get<
Date>());
777 case Variant::TypeTimeStamp:
780 case Variant::TypeVector:
781 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value", a.getTypeName());
783 case Variant::TypeObject:
784 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value", a.getTypeName());
787 fprintf(stderr,
"DEBUG: Missing Type in switch statements.\n");
791 bool operator<(
const Variant &a,
const Variant &b)
throw (UnsupportedOperatorException) {
792 if (a.getType() != b.getType()) {
793 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value for source %s value", a.getTypeName(), b.getTypeName());
794 }
else if (a.isNull()) {
798 switch (a.getType()) {
799 case Variant::TypeBoolean:
802 case Variant::TypeSigned:
805 case Variant::TypeUnsigned:
808 case Variant::TypeDouble:
811 case Variant::TypeString:
814 case Variant::TypeUUID:
815 return (a.get<
UUID>() < b.get<
UUID>());
817 case Variant::TypeBinary:
818 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value", a.getTypeName());
820 case Variant::TypePointer:
823 case Variant::TypeIPAddress:
826 case Variant::TypeIPPrefix:
829 case Variant::TypeInterval:
832 case Variant::TypeTime:
833 return (a.get<
Time>() < b.get<
Time>());
835 case Variant::TypeDate:
836 return (a.get<
Date>() < b.get<
Date>());
838 case Variant::TypeTimeStamp:
841 case Variant::TypeVector:
842 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value", a.getTypeName());
844 case Variant::TypeObject:
845 THROW(UnsupportedOperatorException,
"Unable to perform operator> on %s value", a.getTypeName());
848 fprintf(stderr,
"DEBUG: Missing Type in switch statements.\n");
852 bool operator>=(
const Variant &a,
const Variant &b)
throw (UnsupportedOperatorException) {
860 bool operator<=(
const Variant &a,
const Variant &b)
throw (UnsupportedOperatorException) {
870 switch (a.getType()) {
871 case Variant::TypeBoolean:
877 a.get<
Signed>() += (int64_t) b;
887 a.get<
Double>() += (
double) b;
892 a.get<
String>() += (const std::
string&) b;
902 a.get<
Time>() += (int64_t) b;
907 a.get<
Date>() += (int64_t) b;
922 if (b.getType() ==
Variant::TypeObject) {
929 THROW(UnsupportedOperatorException,
"Unable to perform operator+= on %s value for source %s value", a.getTypeName(), b.getTypeName());
931 }
catch (TypeCastException &e) {
932 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
938 Variant& operator+=(
Variant &a,
const std::string &b)
throw (UnsupportedOperatorException) {
939 if (a.getType() != Variant::TypeString) {
940 return a += std::move(
Variant(b));
948 Variant& operator+=(
Variant &a, int64_t b)
throw (UnsupportedOperatorException) {
949 if (a.getType() != Variant::TypeSigned) {
950 return a += std::move(
Variant(b));
958 Variant& operator+=(
Variant &a, uint64_t b)
throw (UnsupportedOperatorException) {
959 if (a.getType() != Variant::TypeUnsigned) {
960 return a += std::move(
Variant(b));
968 Variant& operator+=(
Variant &a,
double b)
throw (UnsupportedOperatorException) {
969 if (a.getType() != Variant::TypeDouble) {
970 return a += std::move(
Variant(b));
980 switch (a.getType()) {
981 case Variant::TypeBoolean:
987 a.get<
Signed>() += (int64_t) b;
997 a.get<
Double>() += (
double) b;
1002 a.get<
String>() += (const std::
string&) b;
1012 a.get<
Time>() += (int64_t) b;
1017 a.get<
Date>() += (int64_t) b;
1028 std::vector<Variant> &aa = a.get<
Vector>().
get();
1030 if (b.getType() == Variant::TypeVector) {
1031 std::vector<Variant> &bb = b.get<
Vector>().
get();
1033 std::move(bb.begin(), bb.end(), std::back_inserter(aa));
1035 aa.push_back(std::move(b));
1038 a.get<
Vector>() += std::move(b);
1044 case Variant::TypeObject:
1045 if (b.getType() == Variant::TypeObject) {
1047 std::map<std::string, Variant> &aa = a.get<
Object>().
get();
1048 std::map<std::string, Variant> &bb = b.get<
Object>().
get();
1050 std::move(bb.begin(), bb.end(), std::inserter(aa, aa.begin()));
1052 a.get<
Object>() += std::move(b);
1059 THROW(UnsupportedOperatorException,
"Unable to perform operator+= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1061 }
catch (TypeCastException &e) {
1062 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1068 Variant& operator+=(
Variant &a, std::string &&b) throw (UnsupportedOperatorException) {
1069 if (a.getType() != Variant::TypeString) {
1070 return a += std::move(
Variant(std::move(b)));
1080 switch (a.getType()) {
1081 case Variant::TypeBoolean:
1087 a.get<
Signed>() -= (int64_t) b;
1097 a.get<
Double>() -= (
double) b;
1107 a.get<
Time>() -= (int64_t) b;
1112 a.get<
Date>() -= (int64_t) b;
1122 THROW(UnsupportedOperatorException, "Unable to perform operator-= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1124 } catch (TypeCastException &e) {
1125 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1131 Variant& operator-=(
Variant &a, int64_t b)
throw (UnsupportedOperatorException) {
1132 if (a.getType() != Variant::TypeSigned) {
1133 return a -= std::move(
Variant(b));
1141 Variant& operator-=(
Variant &a, uint64_t b)
throw (UnsupportedOperatorException) {
1142 if (a.getType() != Variant::TypeUnsigned) {
1143 return a -= std::move(
Variant(b));
1151 Variant& operator-=(
Variant &a,
double b)
throw (UnsupportedOperatorException) {
1152 if (a.getType() != Variant::TypeDouble) {
1153 return a -= std::move(
Variant(b));
1163 switch (a.getType()) {
1164 case Variant::TypeBoolean:
1170 a.get<
Signed>() *= (int64_t) b;
1180 a.get<
Double>() *= (
double) b;
1190 a.get<
Time>() *= (int64_t) b;
1195 a.get<
Date>() *= (int64_t) b;
1205 THROW(UnsupportedOperatorException, "Unable to perform operator*= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1207 } catch (TypeCastException &e) {
1208 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1216 switch (a.getType()) {
1217 case Variant::TypeBoolean:
1223 a.get<
Signed>() /= (int64_t) b;
1233 a.get<
Double>() /= (
double) b;
1243 a.get<
Time>() /= (int64_t) b;
1248 a.get<
Date>() /= (int64_t) b;
1258 THROW(UnsupportedOperatorException, "Unable to perform operator/= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1260 } catch (TypeCastException &e) {
1261 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1269 switch (a.getType()) {
1270 case Variant::TypeBoolean:
1276 a.get<
Signed>() ^= (int64_t) b;
1286 a.get<
Double>() ^= (
double) b;
1296 a.get<
Time>() ^= (int64_t) b;
1301 a.get<
Date>() ^= (int64_t) b;
1311 THROW(UnsupportedOperatorException, "Unable to perform operator^= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1313 } catch (TypeCastException &e) {
1314 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1322 switch (a.getType()) {
1323 case Variant::TypeBoolean:
1329 a.get<
Signed>() %= (int64_t) b;
1344 a.get<
Time>() %= (int64_t) b;
1349 a.get<
Date>() %= (int64_t) b;
1359 THROW(UnsupportedOperatorException, "Unable to perform operator%= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1361 } catch (TypeCastException &e) {
1362 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1370 switch (a.getType()) {
1371 case Variant::TypeBoolean:
1377 a.get<
Signed>() &= (int64_t) b;
1392 a.get<
Time>() &= (int64_t) b;
1397 a.get<
Date>() &= (int64_t) b;
1407 THROW(UnsupportedOperatorException, "Unable to perform operator&= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1409 } catch (TypeCastException &e) {
1410 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1418 switch (a.getType()) {
1419 case Variant::TypeBoolean:
1425 a.get<
Signed>() |= (int64_t) b;
1440 a.get<
Time>() |= (int64_t) b;
1445 a.get<
Date>() |= (int64_t) b;
1455 THROW(UnsupportedOperatorException, "Unable to perform operator|= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1457 } catch (TypeCastException &e) {
1458 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1466 switch (a.getType()) {
1467 case Variant::TypeBoolean:
1473 a.get<
Signed>() <<= (
int) b;
1483 THROW(UnsupportedOperatorException, "Unable to perform operator<<= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1485 } catch (TypeCastException &e) {
1486 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1494 switch (a.getType()) {
1495 case Variant::TypeBoolean:
1501 a.get<
Signed>() >>= (
int) b;
1511 THROW(UnsupportedOperatorException, "Unable to perform operator>>= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1513 } catch (TypeCastException &e) {
1514 THROW(UnsupportedOperatorException,
"Unsupported operation: %s", e.what());
1600 std::istream& operator>>(std::istream &is,
Variant &value)
throw (TypeCastException) {
1604 value.parse(buf, Variant::TypeAuto);
1609 std::ostream& operator<<(std::ostream &os,
const Variant &value)
throw (TypeCastException) {
1610 os << value.c_str();