libutil++  1.9.3
 All Classes Functions Variables
Operators.cpp
1 /*
2 ** libutil++
3 ** $Id: Operators.cpp 1892 2017-06-23 17:23:36Z 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: Operators.cpp 1892 2017-06-23 17:23:36Z sella $";
10 
11 #include "Variant.h"
12 #include "../util/CommonMacro.h"
13 
14 using namespace sella::variant;
15 
16 bool Variant::operator!() throw (UnsupportedOperatorException) {
17  switch (getType()) {
18  case TypeBoolean:
19  return !get<Boolean>();
20 
21  case TypeSigned:
22  return !get<Signed>();
23 
24  case TypeUnsigned:
25  return !get<Unsigned>();
26 
27  case TypeDouble:
28  return !get<Double>();
29 
30  default:
31  THROW(UnsupportedOperatorException, "Unable to perform operator! on %s value", getTypeName());
32  }
33 }
34 
35 Variant& Variant::operator++() throw (UnsupportedOperatorException) {
36  switch (getType()) {
37  case TypeBoolean:
38  get<Boolean>() = !get<Boolean>();
39 
40  break;
41 
42  case TypeSigned:
43  ++get<Signed>();
44 
45  break;
46 
47  case TypeUnsigned:
48  ++get<Unsigned>();
49 
50  break;
51 
52  case TypeDouble:
53  ++get<Double>();
54 
55  break;
56 
57  default:
58  THROW(UnsupportedOperatorException, "Unable to perform operator++ on %s value", getTypeName());
59  }
60 
61  return *this;
62 }
63 
64 Variant Variant::operator++(int) throw (UnsupportedOperatorException) {
65  Variant tmp = *this;
66 
67  switch (getType()) { /* Use of ++T is correct here, since we're not returning that value. */
68  case TypeBoolean:
69  get<Boolean>() = !get<Boolean>();
70 
71  break;
72 
73  case TypeSigned:
74  ++get<Signed>();
75 
76  break;
77 
78  case TypeUnsigned:
79  ++get<Unsigned>();
80 
81  break;
82 
83  case TypeDouble:
84  ++get<Double>();
85 
86  break;
87 
88  default:
89  THROW(UnsupportedOperatorException, "Unable to perform operator++ on %s value", getTypeName());
90  }
91 
92  return tmp;
93 }
94 
95 const Variant& Variant::operator[](const char* key) const throw (UnsupportedOperatorException, RangeException) {
96  return this->operator[](std::string(key));
97 }
98 
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");
102  }
103 
104  const auto &o = get<Object>().get();
105  const auto &i = o.find(key);
106 
107  if (i == o.end()) {
108  THROW(RangeException, "key '%s' not found", key.c_str());
109  }
110 
111  return i->second;
112 }
113 
114 Variant& Variant::operator[](const char* key) throw () {
115  return this->operator[](std::move(std::string(key)));
116 }
117 
118 Variant& Variant::operator[](const std::string &key) throw () {
119  if (getType() != TypeObject) {
120  this->value = std::make_shared<Object>();
121  this->type = TypeObject;
122  }
123 
124  return get<Object>()[key];
125 }
126 
127 Variant& Variant::operator[](std::string &&key) throw () {
128  if (getType() != TypeObject) {
129  this->value = std::make_shared<Object>();
130  this->type = TypeObject;
131  }
132 
133  return get<Object>()[std::move(key)];
134 }
135 
136 const Variant& Variant::operator[](int index) const throw (UnsupportedOperatorException, RangeException) {
137  return operator[]((size_t) index);
138 }
139 
140 const Variant& Variant::operator[](size_t index) const throw (UnsupportedOperatorException, RangeException) {
141  if (UNLIKELY(getType() != TypeVector)) {
142  THROW(UnsupportedOperatorException, "type must be vector");
143  }
144 
145  const auto &v = get<Vector>().get();
146 
147  if (UNLIKELY(index > v.size())) {
148  THROW(RangeException, "index out of bounds");
149  }
150 
151  return v.at(index);
152 }
153 
154 Variant& Variant::operator[](int index) throw () {
155  return operator[]((size_t) index);
156 }
157 
158 Variant& Variant::operator[](size_t index) throw () {
159  if (getType() != TypeVector) {
160  this->value = std::make_shared<Vector>();
161  this->type = TypeVector;
162  }
163 
164  return get<Vector>()[index];
165 }
166 
167 Variant& Variant::operator=(const Variant &other) throw () {
168  if (LIKELY(this != &other)) {
169  bool same = (this->type == other.type);
170 
171  switch (other.type) {
172  case TypeNull:
173  if (!same) {
174  //this->value = std::make_shared<Null>();
175  this->value = null();
176  }
177 
178  break;
179 
180  case TypeBoolean:
181  if (same) {
182  this->get<Boolean>() = other.get<Boolean>();
183  } else {
184  this->value = std::make_shared<Boolean>(other.get<Boolean>());
185  }
186 
187  break;
188 
189  case TypeSigned:
190  if (same) {
191  this->get<Signed>() = other.get<Signed>();
192  } else {
193  this->value = std::make_shared<Signed>(other.get<Signed>());
194  }
195 
196  break;
197 
198  case TypeUnsigned:
199  if (same) {
200  this->get<Unsigned>() = other.get<Unsigned>();
201  } else {
202  this->value = std::make_shared<Unsigned>(other.get<Unsigned>());
203  }
204 
205  break;
206 
207  case TypeDouble:
208  if (same) {
209  this->get<Double>() = other.get<Double>();
210  } else {
211  this->value = std::make_shared<Double>(other.get<Double>());
212  }
213 
214  break;
215 
216  case TypeString:
217  if (same) {
218  this->get<String>() = other.get<String>();
219  } else {
220  this->value = std::make_shared<String>(other.get<String>());
221  }
222 
223  break;
224 
225  case TypeUUID:
226  if (same) {
227  this->get<UUID>() = other.get<UUID>();
228  } else {
229  this->value = std::make_shared<UUID>(other.get<UUID>());
230  }
231 
232  break;
233 
234  case TypeBinary:
235  if (same) {
236  this->get<Binary>() = other.get<Binary>();
237  } else {
238  this->value = std::make_shared<Binary>(other.get<Binary>());
239  }
240 
241  break;
242 
243  case TypePointer:
244  if (same) {
245  this->get<Pointer>() = other.get<Pointer>();
246  } else {
247  this->value = std::make_shared<Pointer>(other.get<Pointer>());
248  }
249 
250  break;
251 
252  case TypeIPAddress:
253  if (same) {
254  this->get<IPAddress>() = other.get<IPAddress>();
255  } else {
256  this->value = std::make_shared<IPAddress>(other.get<IPAddress>());
257  }
258 
259  break;
260 
261  case TypeIPPrefix:
262  if (same) {
263  this->get<IPPrefix>() = other.get<IPPrefix>();
264  } else {
265  this->value = std::make_shared<IPPrefix>(other.get<IPPrefix>());
266  }
267 
268  break;
269 
270  case TypeInterval:
271  if (same) {
272  this->get<Interval>() = other.get<Interval>();
273  } else {
274  this->value = std::make_shared<Interval>(other.get<Interval>());
275  }
276 
277  break;
278 
279  case TypeTime:
280  if (same) {
281  this->get<Time>() = other.get<Time>();
282  } else {
283  this->value = std::make_shared<Time>(other.get<Time>());
284  }
285 
286  break;
287 
288  case TypeDate:
289  if (same) {
290  this->get<Date>() = other.get<Date>();
291  } else {
292  this->value = std::make_shared<Date>(other.get<Date>());
293  }
294 
295  break;
296 
297  case TypeTimeStamp:
298  if (same) {
299  this->get<TimeStamp>() = other.get<TimeStamp>();
300  } else {
301  this->value = std::make_shared<TimeStamp>(other.get<TimeStamp>());
302  }
303 
304  break;
305 
306  case TypeVector:
307  if (same) {
308  this->get<Vector>() = other.get<Vector>();
309  } else {
310  this->value = std::make_shared<Vector>(other.get<Vector>());
311  }
312 
313  break;
314 
315  case TypeObject:
316  if (same) {
317  this->get<Object>() = other.get<Object>();
318  } else {
319  this->value = std::make_shared<Object>(other.get<Object>());
320  }
321 
322  break;
323 
324  default:
325  fprintf(stderr, "DEBUG: Missing Type in switch statements.\n");
326  abort();
327  }
328 
329  this->type = other.type;
330  this->cache = other.cache;
331  }
332 
333  return *this;
334 }
335 
336 Variant& Variant::operator=(Variant &&other) throw () {
337  if (LIKELY(this != &other)) {
338  this->swap(other);
339  }
340 
341  return *this;
342 }
343 
344 Variant& Variant::operator=(const std::string &value) throw () {
345  if (this->type == TypeString) {
346  this->get<String>() = value;
347  } else {
348  this->value = std::make_shared<String>(value);
349  }
350  this->type = TypeString;
351  this->cache.clear();
352 
353  return *this;
354 }
355 
356 Variant& Variant::operator=(const std::shared_ptr<Nullable> &value) throw () {
357  this->value = value;
358  this->type = value->getType();
359  this->cache.clear();
360 
361  return *this;
362 }
363 
364 Variant& Variant::operator=(std::shared_ptr<Nullable> &&value) throw () {
365  this->type = value->getType();
366  this->value.swap(value);
367  this->cache.clear();
368 
369  return *this;
370 }
371 
372 Variant& Variant::operator=(std::string &&value) throw () {
373  if (this->type == TypeString) {
374  this->get<String>() = std::move(value);
375  } else {
376  this->value = std::make_shared<String>(std::move(value));
377  }
378  this->type = TypeString;
379  this->cache.clear();
380 
381  return *this;
382 }
383 
384 Variant& Variant::operator=(const char *value) throw () {
385  if (this->type == TypeString) {
386  this->get<String>() = value;
387  } else {
388  this->value = std::make_shared<String>(value);
389  }
390  this->type = TypeString;
391  this->cache.clear();
392 
393  return *this;
394 }
395 
396 Variant& Variant::operator=(const sella::util::UUID &value) throw () {
397  if (this->type == TypeUUID) {
398  this->get<UUID>() = value;
399  } else {
400  this->value = std::make_shared<UUID>(value);
401  }
402  this->type = TypeUUID;
403 
404  return *this;
405 }
406 
407 Variant& Variant::operator=(sella::util::UUID &&value) throw () {
408  if (this->type == TypeUUID) {
409  this->get<UUID>() = std::move(value);
410  } else {
411  this->value = std::make_shared<UUID>(std::move(value));
412  }
413  this->type = TypeUUID;
414  this->cache.clear();
415 
416  return *this;
417 }
418 
419 Variant& Variant::operator=(uuid_t &value) throw () {
420  this->value = std::make_shared<UUID>(value);
421  this->type = TypeUUID;
422  this->cache.clear();
423 
424  return *this;
425 }
426 
427 Variant& Variant::operator=(void *ptr) throw () {
428  if (this->type == TypePointer) {
429  this->get<Pointer>() = ptr;
430  } else {
431  this->value = std::make_shared<Pointer>(ptr);
432  }
433  this->type = TypePointer;
434  this->cache.clear();
435 
436  return *this;
437 }
438 
439 Variant& Variant::operator=(const sella::net::IPAddress &value) throw () {
440  if (this->type == TypeIPAddress) {
441  this->get<IPAddress>() = value;
442  } else {
443  this->value = std::make_shared<IPAddress>(value);
444  }
445  this->type = TypeIPAddress;
446  this->cache.clear();
447 
448  return *this;
449 }
450 
451 Variant& Variant::operator=(sella::net::IPAddress &&value) throw () {
452  if (this->type == TypeIPAddress) {
453  this->get<IPAddress>() = std::move(value);
454  } else {
455  this->value = std::make_shared<IPAddress>(std::move(value));
456  }
457  this->type = TypeIPAddress;
458  this->cache.clear();
459 
460  return *this;
461 }
462 
463 Variant& Variant::operator=(const sella::net::IPPrefix &value) throw () {
464  if (this->type == TypeIPPrefix) {
465  this->get<IPPrefix>() = value;
466  } else {
467  this->value = std::make_shared<IPPrefix>(value);
468  }
469  this->type = TypeIPPrefix;
470  this->cache.clear();
471 
472  return *this;
473 }
474 
475 Variant& Variant::operator=(sella::net::IPPrefix &&value) throw () {
476  if (this->type == TypeIPPrefix) {
477  this->get<IPPrefix>() = std::move(value);
478  } else {
479  this->value = std::make_shared<IPPrefix>(std::move(value));
480  }
481  this->type = TypeIPPrefix;
482  this->cache.clear();
483 
484  return *this;
485 }
486 
487 Variant& Variant::operator=(bool value) throw () {
488  if (this->type == TypeBoolean) {
489  this->get<Boolean>() = value;
490  } else {
491  this->value = std::make_shared<Boolean>(value);
492  }
493  this->type = TypeBoolean;
494  this->cache.clear();
495 
496  return *this;
497 }
498 
499 Variant& Variant::operator=(uint8_t value) throw () {
500  return operator=((uint64_t) value);
501 }
502 
503 Variant& Variant::operator=(uint16_t value) throw () {
504  return operator=((uint64_t) value);
505 }
506 
507 Variant& Variant::operator=(unsigned int value) throw () {
508  return operator=((uint64_t) value);
509 }
510 
511 Variant& Variant::operator=(uint64_t value) throw () {
512  if (this->type == TypeUnsigned) {
513  this->get<Unsigned>() = value;
514  } else {
515  this->value = std::make_shared<Unsigned>(value);
516  }
517  this->type = TypeUnsigned;
518  this->cache.clear();
519 
520  return *this;
521 }
522 
523 Variant& Variant::operator=(int8_t value) throw () {
524  return operator=((int64_t) value);
525 }
526 
527 Variant& Variant::operator=(int16_t value) throw () {
528  return operator=((int64_t) value);
529 }
530 
531 Variant& Variant::operator=(int value) throw () {
532  return operator=((int64_t) value);
533 }
534 
535 Variant& Variant::operator=(int64_t value) throw () {
536  if (this->type == TypeSigned) {
537  this->get<Signed>() = value;
538  } else {
539  this->value = std::make_shared<Signed>(value);
540  }
541  this->type = TypeSigned;
542  this->cache.clear();
543 
544  return *this;
545 }
546 
547 Variant& Variant::operator=(float value) throw () {
548  return operator=((double) value);
549 }
550 
551 Variant& Variant::operator=(double value) throw () {
552  if (this->type == TypeDouble) {
553  this->get<Double>() = value;
554  } else {
555  this->value = std::make_shared<Double>(value);
556  }
557  this->type = TypeDouble;
558  this->cache.clear();
559 
560  return *this;
561 }
562 
563 #ifdef __LP64__
564 Variant& Variant::operator=(unsigned long long value) throw () {
565  return operator=((uint64_t) value);
566 }
567 
568 Variant& Variant::operator=(long long value) throw () {
569  return operator=((int64_t) value);
570 }
571 #else
572 Variant& Variant::operator=(unsigned long value) throw () {
573  return operator=((uint64_t) value);
574 }
575 
576 Variant& Variant::operator=(long value) throw () {
577  return operator=((int64_t) value);
578 }
579 #endif
580 
581 Variant::operator const char*() const throw (TypeCastException) {
582  return this->cast<const char*>();
583 }
584 
585 Variant::operator const std::string&() const throw (TypeCastException) {
586  return this->cast<const std::string&>();
587 }
588 
589 Variant::operator const sella::util::UUID&() const throw (TypeCastException) {
590  return this->cast<const sella::util::UUID&>();
591 }
592 
593 Variant::operator const sella::net::IPAddress&() const throw (TypeCastException) {
594  return this->cast<const sella::net::IPAddress&>();
595 }
596 
597 Variant::operator const sella::net::IPPrefix&() const throw (TypeCastException) {
598  return this->cast<const sella::net::IPPrefix&>();
599 }
600 
601 Variant::operator bool() const throw (TypeCastException) {
602  return this->cast<bool>();
603 }
604 
605 Variant::operator uint8_t() const throw (TypeCastException) {
606  return this->cast<uint8_t>();
607 }
608 
609 Variant::operator uint16_t() const throw (TypeCastException) {
610  return this->cast<uint16_t>();
611 }
612 
613 Variant::operator uint32_t() const throw (TypeCastException) {
614  return this->cast<uint32_t>();
615 }
616 
617 Variant::operator uint64_t() const throw (TypeCastException) {
618  return this->cast<uint64_t>();
619 }
620 
621 Variant::operator int8_t() const throw (TypeCastException) {
622  return this->cast<int8_t>();
623 }
624 
625 Variant::operator int16_t() const throw (TypeCastException) {
626  return this->cast<int16_t>();
627 }
628 
629 Variant::operator int32_t() const throw (TypeCastException) {
630  return this->cast<int32_t>();
631 }
632 
633 Variant::operator int64_t() const throw (TypeCastException) {
634  return this->cast<int64_t>();
635 }
636 
637 Variant::operator float() const throw (TypeCastException) {
638  return this->cast<float>();
639 }
640 
641 Variant::operator double() const throw (TypeCastException) {
642  return this->cast<double>();
643 }
644 
645 #ifdef __LP64__
646 Variant::operator unsigned long long() const throw (TypeCastException) {
647  return this->cast<uint64_t>();
648 }
649 
650 Variant::operator long long() const throw (TypeCastException) {
651  return this->cast<int64_t>();
652 }
653 #else
654 Variant::operator unsigned long() const throw (TypeCastException) {
655  return this->cast<uint64_t>();
656 }
657 
658 Variant::operator long() const throw (TypeCastException) {
659  return this->cast<int64_t>();
660 }
661 #endif
662 
663 namespace sella { /* friend namespace */
664  namespace variant {
665  bool operator==(const Variant &a, const Variant &b) throw () {
666  if (a.getType() != b.getType()) {
667  return false;
668  } else if (a.isNull()) {
669  return true;
670  }
671 
672  switch (a.getType()) {
673  case Variant::TypeBoolean:
674  return (a.get<Boolean>() == b.get<Boolean>());
675 
676  case Variant::TypeSigned:
677  return (a.get<Signed>() == b.get<Signed>());
678 
679  case Variant::TypeUnsigned:
680  return (a.get<Unsigned>() == b.get<Unsigned>());
681 
682  case Variant::TypeDouble:
683  return (a.get<Double>() == b.get<Double>());
684 
685  case Variant::TypeString:
686  return (a.get<String>() == b.get<String>());
687 
688  case Variant::TypeUUID:
689  return (a.get<UUID>() == b.get<UUID>());
690 
691  case Variant::TypeBinary:
692  return (a.get<Binary>() == b.get<Binary>());
693 
694  case Variant::TypePointer:
695  return (a.get<Pointer>() == b.get<Pointer>());
696 
697  case Variant::TypeIPAddress:
698  return (a.get<IPAddress>() == b.get<IPAddress>());
699 
700  case Variant::TypeIPPrefix:
701  return (a.get<IPPrefix>() == b.get<IPPrefix>());
702 
703  case Variant::TypeInterval:
704  return (a.get<Interval>() == b.get<Interval>());
705 
706  case Variant::TypeTime:
707  return (a.get<Time>() == b.get<Time>());
708 
709  case Variant::TypeDate:
710  return (a.get<Date>() == b.get<Date>());
711 
712  case Variant::TypeTimeStamp:
713  return (a.get<TimeStamp>() == b.get<TimeStamp>());
714 
715  case Variant::TypeVector:
716  return (a.get<Vector>() == b.get<Vector>());
717 
718  case Variant::TypeObject:
719  return (a.get<Object>() == b.get<Object>());
720  }
721 
722  fprintf(stderr, "DEBUG: Missing Type in switch statements.\n");
723  abort();
724  }
725 
726  bool operator!=(const Variant &a, const Variant &b) throw () {
727  return !(a == b);
728  }
729 
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()) {
734  return false;
735  }
736 
737  switch (a.getType()) {
738  case Variant::TypeBoolean:
739  return (a.get<Boolean>() > b.get<Boolean>());
740 
741  case Variant::TypeSigned:
742  return (a.get<Signed>() > b.get<Signed>());
743 
744  case Variant::TypeUnsigned:
745  return (a.get<Unsigned>() > b.get<Unsigned>());
746 
747  case Variant::TypeDouble:
748  return (a.get<Double>() > b.get<Double>());
749 
750  case Variant::TypeString:
751  return (a.get<String>() > b.get<String>());
752 
753  case Variant::TypeUUID:
754  return (a.get<UUID>() > b.get<UUID>());
755 
756  case Variant::TypeBinary:
757  THROW(UnsupportedOperatorException, "Unable to perform operator> on %s value", a.getTypeName());
758 
759  case Variant::TypePointer:
760  return (a.get<Pointer>() > b.get<Pointer>());
761 
762  case Variant::TypeIPAddress:
763  return (a.get<IPAddress>() > b.get<IPAddress>());
764 
765  case Variant::TypeIPPrefix:
766  return (a.get<IPPrefix>() > b.get<IPPrefix>());
767 
768  case Variant::TypeInterval:
769  return (a.get<Interval>() > b.get<Interval>());
770 
771  case Variant::TypeTime:
772  return (a.get<Time>() > b.get<Time>());
773 
774  case Variant::TypeDate:
775  return (a.get<Date>() > b.get<Date>());
776 
777  case Variant::TypeTimeStamp:
778  return (a.get<TimeStamp>() > b.get<TimeStamp>());
779 
780  case Variant::TypeVector:
781  THROW(UnsupportedOperatorException, "Unable to perform operator> on %s value", a.getTypeName());
782 
783  case Variant::TypeObject:
784  THROW(UnsupportedOperatorException, "Unable to perform operator> on %s value", a.getTypeName());
785  }
786 
787  fprintf(stderr, "DEBUG: Missing Type in switch statements.\n");
788  abort();
789  }
790 
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()) {
795  return false;
796  }
797 
798  switch (a.getType()) {
799  case Variant::TypeBoolean:
800  return (a.get<Boolean>() < b.get<Boolean>());
801 
802  case Variant::TypeSigned:
803  return (a.get<Signed>() < b.get<Signed>());
804 
805  case Variant::TypeUnsigned:
806  return (a.get<Unsigned>() < b.get<Unsigned>());
807 
808  case Variant::TypeDouble:
809  return (a.get<Double>() < b.get<Double>());
810 
811  case Variant::TypeString:
812  return (a.get<String>() < b.get<String>());
813 
814  case Variant::TypeUUID:
815  return (a.get<UUID>() < b.get<UUID>());
816 
817  case Variant::TypeBinary:
818  THROW(UnsupportedOperatorException, "Unable to perform operator> on %s value", a.getTypeName());
819 
820  case Variant::TypePointer:
821  return (a.get<Pointer>() < b.get<Pointer>());
822 
823  case Variant::TypeIPAddress:
824  return (a.get<IPAddress>() < b.get<IPAddress>());
825 
826  case Variant::TypeIPPrefix:
827  return (a.get<IPPrefix>() < b.get<IPPrefix>());
828 
829  case Variant::TypeInterval:
830  return (a.get<Interval>() < b.get<Interval>());
831 
832  case Variant::TypeTime:
833  return (a.get<Time>() < b.get<Time>());
834 
835  case Variant::TypeDate:
836  return (a.get<Date>() < b.get<Date>());
837 
838  case Variant::TypeTimeStamp:
839  return (a.get<TimeStamp>() < b.get<TimeStamp>());
840 
841  case Variant::TypeVector:
842  THROW(UnsupportedOperatorException, "Unable to perform operator> on %s value", a.getTypeName());
843 
844  case Variant::TypeObject:
845  THROW(UnsupportedOperatorException, "Unable to perform operator> on %s value", a.getTypeName());
846  }
847 
848  fprintf(stderr, "DEBUG: Missing Type in switch statements.\n");
849  abort();
850  }
851 
852  bool operator>=(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
853  if (a == b) {
854  return true;
855  }
856 
857  return (a > b);
858  }
859 
860  bool operator<=(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
861  if (a == b) {
862  return true;
863  }
864 
865  return (a < b);
866  }
867 
868  Variant& operator+=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
869  try {
870  switch (a.getType()) {
871  case Variant::TypeBoolean:
872  a.get<Boolean>() += (bool) b;
873 
874  break;
875 
876  case Variant::TypeSigned:
877  a.get<Signed>() += (int64_t) b;
878 
879  break;
880 
881  case Variant::TypeUnsigned:
882  a.get<Unsigned>() += (uint64_t) b;
883 
884  break;
885 
886  case Variant::TypeDouble:
887  a.get<Double>() += (double) b;
888 
889  break;
890 
891  case Variant::TypeString:
892  a.get<String>() += (const std::string&) b;
893 
894  break;
895 
896  case Variant::TypeInterval:
897  a.get<Interval>() += (int64_t) b;
898 
899  break;
900 
901  case Variant::TypeTime:
902  a.get<Time>() += (int64_t) b;
903 
904  break;
905 
906  case Variant::TypeDate:
907  a.get<Date>() += (int64_t) b;
908 
909  break;
910 
911  case Variant::TypeTimeStamp:
912  a.get<TimeStamp>() += (int64_t) b;
913 
914  break;
915 
916  case Variant::TypeVector:
917  a.get<Vector>() += b; /* Appends to existing Vector. */
918 
919  break;
920 
921  case Variant::TypeObject:
922  if (b.getType() == Variant::TypeObject) {
923  a.get<Object>() += b; /* Appends to existing Object. */
924 
925  break;
926  } /* Intentional fall thru */
927 
928  default:
929  THROW(UnsupportedOperatorException, "Unable to perform operator+= on %s value for source %s value", a.getTypeName(), b.getTypeName());
930  }
931  } catch (TypeCastException &e) {
932  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
933  }
934 
935  return a;
936  }
937 
938  Variant& operator+=(Variant &a, const std::string &b) throw (UnsupportedOperatorException) {
939  if (a.getType() != Variant::TypeString) {
940  return a += std::move(Variant(b));
941  }
942 
943  a.get<String>() += b;
944 
945  return a;
946  }
947 
948  Variant& operator+=(Variant &a, int64_t b) throw (UnsupportedOperatorException) {
949  if (a.getType() != Variant::TypeSigned) {
950  return a += std::move(Variant(b));
951  }
952 
953  a.get<Signed>() += b;
954 
955  return a;
956  }
957 
958  Variant& operator+=(Variant &a, uint64_t b) throw (UnsupportedOperatorException) {
959  if (a.getType() != Variant::TypeUnsigned) {
960  return a += std::move(Variant(b));
961  }
962 
963  a.get<Unsigned>() += b;
964 
965  return a;
966  }
967 
968  Variant& operator+=(Variant &a, double b) throw (UnsupportedOperatorException) {
969  if (a.getType() != Variant::TypeDouble) {
970  return a += std::move(Variant(b));
971  }
972 
973  a.get<Double>() += b;
974 
975  return a;
976  }
977 
978  Variant& operator+=(Variant &a, Variant &&b) throw (UnsupportedOperatorException) {
979  try {
980  switch (a.getType()) {
981  case Variant::TypeBoolean:
982  a.get<Boolean>() += (bool) b;
983 
984  break;
985 
986  case Variant::TypeSigned:
987  a.get<Signed>() += (int64_t) b;
988 
989  break;
990 
991  case Variant::TypeUnsigned:
992  a.get<Unsigned>() += (uint64_t) b;
993 
994  break;
995 
996  case Variant::TypeDouble:
997  a.get<Double>() += (double) b;
998 
999  break;
1000 
1001  case Variant::TypeString:
1002  a.get<String>() += (const std::string&) b;
1003 
1004  break;
1005 
1006  case Variant::TypeInterval:
1007  a.get<Interval>() += (int64_t) b;
1008 
1009  break;
1010 
1011  case Variant::TypeTime:
1012  a.get<Time>() += (int64_t) b;
1013 
1014  break;
1015 
1016  case Variant::TypeDate:
1017  a.get<Date>() += (int64_t) b;
1018 
1019  break;
1020 
1021  case Variant::TypeTimeStamp:
1022  a.get<TimeStamp>() += (int64_t) b;
1023 
1024  break;
1025 
1026  case Variant::TypeVector: { /* Appends to existing Vector. */
1027 #if 0
1028  std::vector<Variant> &aa = a.get<Vector>().get();
1029 
1030  if (b.getType() == Variant::TypeVector) {
1031  std::vector<Variant> &bb = b.get<Vector>().get();
1032 
1033  std::move(bb.begin(), bb.end(), std::back_inserter(aa));
1034  } else {
1035  aa.push_back(std::move(b));
1036  }
1037 #else
1038  a.get<Vector>() += std::move(b); /* Appends to existing Vector. */
1039 #endif
1040 
1041  break;
1042  }
1043 
1044  case Variant::TypeObject:
1045  if (b.getType() == Variant::TypeObject) {
1046 #if 0
1047  std::map<std::string, Variant> &aa = a.get<Object>().get();
1048  std::map<std::string, Variant> &bb = b.get<Object>().get();
1049 
1050  std::move(bb.begin(), bb.end(), std::inserter(aa, aa.begin()));
1051 #else
1052  a.get<Object>() += std::move(b);
1053 #endif
1054 
1055  break;
1056  } /* Intentional fall thru */
1057 
1058  default:
1059  THROW(UnsupportedOperatorException, "Unable to perform operator+= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1060  }
1061  } catch (TypeCastException &e) {
1062  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1063  }
1064 
1065  return a;
1066  }
1067 
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)));
1071  }
1072 
1073  a.get<String>() += b;
1074 
1075  return a;
1076  }
1077 
1078  Variant& operator-=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1079  try {
1080  switch (a.getType()) {
1081  case Variant::TypeBoolean:
1082  a.get<Boolean>() -= (bool) b;
1083 
1084  break;
1085 
1086  case Variant::TypeSigned:
1087  a.get<Signed>() -= (int64_t) b;
1088 
1089  break;
1090 
1091  case Variant::TypeUnsigned:
1092  a.get<Unsigned>() -= (uint64_t) b;
1093 
1094  break;
1095 
1096  case Variant::TypeDouble:
1097  a.get<Double>() -= (double) b;
1098 
1099  break;
1100 
1101  case Variant::TypeInterval:
1102  a.get<Interval>() -= (int64_t) b;
1103 
1104  break;
1105 
1106  case Variant::TypeTime:
1107  a.get<Time>() -= (int64_t) b;
1108 
1109  break;
1110 
1111  case Variant::TypeDate:
1112  a.get<Date>() -= (int64_t) b;
1113 
1114  break;
1115 
1116  case Variant::TypeTimeStamp:
1117  a.get<TimeStamp>() -= (int64_t) b;
1118 
1119  break;
1120 
1121  default:
1122  THROW(UnsupportedOperatorException, "Unable to perform operator-= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1123  }
1124  } catch (TypeCastException &e) {
1125  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1126  }
1127 
1128  return a;
1129  }
1130 
1131  Variant& operator-=(Variant &a, int64_t b) throw (UnsupportedOperatorException) {
1132  if (a.getType() != Variant::TypeSigned) {
1133  return a -= std::move(Variant(b));
1134  }
1135 
1136  a.get<Signed>() -= b;
1137 
1138  return a;
1139  }
1140 
1141  Variant& operator-=(Variant &a, uint64_t b) throw (UnsupportedOperatorException) {
1142  if (a.getType() != Variant::TypeUnsigned) {
1143  return a -= std::move(Variant(b));
1144  }
1145 
1146  a.get<Unsigned>() -= b;
1147 
1148  return a;
1149  }
1150 
1151  Variant& operator-=(Variant &a, double b) throw (UnsupportedOperatorException) {
1152  if (a.getType() != Variant::TypeDouble) {
1153  return a -= std::move(Variant(b));
1154  }
1155 
1156  a.get<Double>() -= b;
1157 
1158  return a;
1159  }
1160 
1161  Variant& operator*=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1162  try {
1163  switch (a.getType()) {
1164  case Variant::TypeBoolean:
1165  a.get<Boolean>() *= (bool) b;
1166 
1167  break;
1168 
1169  case Variant::TypeSigned:
1170  a.get<Signed>() *= (int64_t) b;
1171 
1172  break;
1173 
1174  case Variant::TypeUnsigned:
1175  a.get<Unsigned>() *= (uint64_t) b;
1176 
1177  break;
1178 
1179  case Variant::TypeDouble:
1180  a.get<Double>() *= (double) b;
1181 
1182  break;
1183 
1184  case Variant::TypeInterval:
1185  a.get<Interval>() *= (int64_t) b;
1186 
1187  break;
1188 
1189  case Variant::TypeTime:
1190  a.get<Time>() *= (int64_t) b;
1191 
1192  break;
1193 
1194  case Variant::TypeDate:
1195  a.get<Date>() *= (int64_t) b;
1196 
1197  break;
1198 
1199  case Variant::TypeTimeStamp:
1200  a.get<TimeStamp>() *= (int64_t) b;
1201 
1202  break;
1203 
1204  default:
1205  THROW(UnsupportedOperatorException, "Unable to perform operator*= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1206  }
1207  } catch (TypeCastException &e) {
1208  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1209  }
1210 
1211  return a;
1212  }
1213 
1214  Variant& operator/=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1215  try {
1216  switch (a.getType()) {
1217  case Variant::TypeBoolean:
1218  a.get<Boolean>() /= (bool) b;
1219 
1220  break;
1221 
1222  case Variant::TypeSigned:
1223  a.get<Signed>() /= (int64_t) b;
1224 
1225  break;
1226 
1227  case Variant::TypeUnsigned:
1228  a.get<Unsigned>() /= (uint64_t) b;
1229 
1230  break;
1231 
1232  case Variant::TypeDouble:
1233  a.get<Double>() /= (double) b;
1234 
1235  break;
1236 
1237  case Variant::TypeInterval:
1238  a.get<Interval>() /= (int64_t) b;
1239 
1240  break;
1241 
1242  case Variant::TypeTime:
1243  a.get<Time>() /= (int64_t) b;
1244 
1245  break;
1246 
1247  case Variant::TypeDate:
1248  a.get<Date>() /= (int64_t) b;
1249 
1250  break;
1251 
1252  case Variant::TypeTimeStamp:
1253  a.get<TimeStamp>() /= (int64_t) b;
1254 
1255  break;
1256 
1257  default:
1258  THROW(UnsupportedOperatorException, "Unable to perform operator/= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1259  }
1260  } catch (TypeCastException &e) {
1261  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1262  }
1263 
1264  return a;
1265  }
1266 
1267  Variant& operator^=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1268  try {
1269  switch (a.getType()) {
1270  case Variant::TypeBoolean:
1271  a.get<Boolean>() ^= (bool) b;
1272 
1273  break;
1274 
1275  case Variant::TypeSigned:
1276  a.get<Signed>() ^= (int64_t) b;
1277 
1278  break;
1279 
1280  case Variant::TypeUnsigned:
1281  a.get<Unsigned>() ^= (uint64_t) b;
1282 
1283  break;
1284 
1285  case Variant::TypeDouble:
1286  a.get<Double>() ^= (double) b;
1287 
1288  break;
1289 
1290  case Variant::TypeInterval:
1291  a.get<Interval>() ^= (int64_t) b;
1292 
1293  break;
1294 
1295  case Variant::TypeTime:
1296  a.get<Time>() ^= (int64_t) b;
1297 
1298  break;
1299 
1300  case Variant::TypeDate:
1301  a.get<Date>() ^= (int64_t) b;
1302 
1303  break;
1304 
1305  case Variant::TypeTimeStamp:
1306  a.get<TimeStamp>() ^= (int64_t) b;
1307 
1308  break;
1309 
1310  default:
1311  THROW(UnsupportedOperatorException, "Unable to perform operator^= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1312  }
1313  } catch (TypeCastException &e) {
1314  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1315  }
1316 
1317  return a;
1318  }
1319 
1320  Variant& operator%=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1321  try {
1322  switch (a.getType()) {
1323  case Variant::TypeBoolean:
1324  a.get<Boolean>() %= (bool) b;
1325 
1326  break;
1327 
1328  case Variant::TypeSigned:
1329  a.get<Signed>() %= (int64_t) b;
1330 
1331  break;
1332 
1333  case Variant::TypeUnsigned:
1334  a.get<Unsigned>() %= (uint64_t) b;
1335 
1336  break;
1337 
1338  case Variant::TypeInterval:
1339  a.get<Interval>() %= (int64_t) b;
1340 
1341  break;
1342 
1343  case Variant::TypeTime:
1344  a.get<Time>() %= (int64_t) b;
1345 
1346  break;
1347 
1348  case Variant::TypeDate:
1349  a.get<Date>() %= (int64_t) b;
1350 
1351  break;
1352 
1353  case Variant::TypeTimeStamp:
1354  a.get<TimeStamp>() %= (int64_t) b;
1355 
1356  break;
1357 
1358  default:
1359  THROW(UnsupportedOperatorException, "Unable to perform operator%= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1360  }
1361  } catch (TypeCastException &e) {
1362  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1363  }
1364 
1365  return a;
1366  }
1367 
1368  Variant& operator&=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1369  try {
1370  switch (a.getType()) {
1371  case Variant::TypeBoolean:
1372  a.get<Boolean>() &= (bool) b;
1373 
1374  break;
1375 
1376  case Variant::TypeSigned:
1377  a.get<Signed>() &= (int64_t) b;
1378 
1379  break;
1380 
1381  case Variant::TypeUnsigned:
1382  a.get<Unsigned>() &= (uint64_t) b;
1383 
1384  break;
1385 
1386  case Variant::TypeInterval:
1387  a.get<Interval>() &= (int64_t) b;
1388 
1389  break;
1390 
1391  case Variant::TypeTime:
1392  a.get<Time>() &= (int64_t) b;
1393 
1394  break;
1395 
1396  case Variant::TypeDate:
1397  a.get<Date>() &= (int64_t) b;
1398 
1399  break;
1400 
1401  case Variant::TypeTimeStamp:
1402  a.get<TimeStamp>() &= (int64_t) b;
1403 
1404  break;
1405 
1406  default:
1407  THROW(UnsupportedOperatorException, "Unable to perform operator&= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1408  }
1409  } catch (TypeCastException &e) {
1410  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1411  }
1412 
1413  return a;
1414  }
1415 
1416  Variant& operator|=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1417  try {
1418  switch (a.getType()) {
1419  case Variant::TypeBoolean:
1420  a.get<Boolean>() |= (bool) b;
1421 
1422  break;
1423 
1424  case Variant::TypeSigned:
1425  a.get<Signed>() |= (int64_t) b;
1426 
1427  break;
1428 
1429  case Variant::TypeUnsigned:
1430  a.get<Unsigned>() |= (uint64_t) b;
1431 
1432  break;
1433 
1434  case Variant::TypeInterval:
1435  a.get<Interval>() |= (int64_t) b;
1436 
1437  break;
1438 
1439  case Variant::TypeTime:
1440  a.get<Time>() |= (int64_t) b;
1441 
1442  break;
1443 
1444  case Variant::TypeDate:
1445  a.get<Date>() |= (int64_t) b;
1446 
1447  break;
1448 
1449  case Variant::TypeTimeStamp:
1450  a.get<TimeStamp>() |= (int64_t) b;
1451 
1452  break;
1453 
1454  default:
1455  THROW(UnsupportedOperatorException, "Unable to perform operator|= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1456  }
1457  } catch (TypeCastException &e) {
1458  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1459  }
1460 
1461  return a;
1462  }
1463 
1464  Variant& operator<<=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1465  try {
1466  switch (a.getType()) {
1467  case Variant::TypeBoolean:
1468  a.get<Boolean>() <<= (int) b;
1469 
1470  break;
1471 
1472  case Variant::TypeSigned:
1473  a.get<Signed>() <<= (int) b;
1474 
1475  break;
1476 
1477  case Variant::TypeUnsigned:
1478  a.get<Unsigned>() <<= (int) b;
1479 
1480  break;
1481 
1482  default:
1483  THROW(UnsupportedOperatorException, "Unable to perform operator<<= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1484  }
1485  } catch (TypeCastException &e) {
1486  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1487  }
1488 
1489  return a;
1490  }
1491 
1492  Variant& operator>>=(Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1493  try {
1494  switch (a.getType()) {
1495  case Variant::TypeBoolean:
1496  a.get<Boolean>() >>= (int) b;
1497 
1498  break;
1499 
1500  case Variant::TypeSigned:
1501  a.get<Signed>() >>= (int) b;
1502 
1503  break;
1504 
1505  case Variant::TypeUnsigned:
1506  a.get<Unsigned>() >>= (int) b;
1507 
1508  break;
1509 
1510  default:
1511  THROW(UnsupportedOperatorException, "Unable to perform operator>>= on %s value for source %s value", a.getTypeName(), b.getTypeName());
1512  }
1513  } catch (TypeCastException &e) {
1514  THROW(UnsupportedOperatorException, "Unsupported operation: %s", e.what());
1515  }
1516 
1517  return a;
1518  }
1519 
1520  Variant operator+(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1521  Variant v = a;
1522 
1523  v += b;
1524 
1525  return v;
1526  }
1527 
1528  Variant operator-(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1529  Variant v = a;
1530 
1531  v -= b;
1532 
1533  return v;
1534  }
1535 
1536  Variant operator*(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1537  Variant v = a;
1538 
1539  v *= b;
1540 
1541  return v;
1542  }
1543 
1544  Variant operator/(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1545  Variant v = a;
1546 
1547  v /= b;
1548 
1549  return v;
1550  }
1551 
1552  Variant operator^(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1553  Variant v = a;
1554 
1555  v ^= b;
1556 
1557  return v;
1558  }
1559 
1560  Variant operator%(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1561  Variant v = a;
1562 
1563  v %= b;
1564 
1565  return v;
1566  }
1567 
1568  Variant operator&(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1569  Variant v = a;
1570 
1571  v &= b;
1572 
1573  return v;
1574  }
1575 
1576  Variant operator|(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1577  Variant v = a;
1578 
1579  v |= b;
1580 
1581  return v;
1582  }
1583 
1584  Variant operator<<(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1585  Variant v = a;
1586 
1587  v <<= b;
1588 
1589  return v;
1590  }
1591 
1592  Variant operator>>(const Variant &a, const Variant &b) throw (UnsupportedOperatorException) {
1593  Variant v = a;
1594 
1595  v >>= b;
1596 
1597  return v;
1598  }
1599 
1600  std::istream& operator>>(std::istream &is, Variant &value) throw (TypeCastException) {
1601  std::string buf;
1602 
1603  is >> buf;
1604  value.parse(buf, Variant::TypeAuto);
1605 
1606  return is;
1607  }
1608 
1609  std::ostream& operator<<(std::ostream &os, const Variant &value) throw (TypeCastException) {
1610  os << value.c_str();
1611 
1612  return os;
1613  }
1614  } /* End namespace */
1615 }
1616 
1617 /*
1618 ** vim: noet ts=3 sw=3
1619 */