join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_DATA_VALUE_HPP
26#define JOIN_DATA_VALUE_HPP
27
28// libjoin.
29#include <join/variant.hpp>
30#include <join/error.hpp>
31
32// C++.
33#include <functional>
34#include <iostream>
35#include <sstream>
36#include <fstream>
37#include <string>
38#include <vector>
39#include <limits>
40
41// C.
42#include <cstdint>
43#include <cmath>
44
45namespace join
46{
48 class Value;
49
51 using Array = std::vector<Value>;
52
54 using Member = std::pair<Value, Value>;
55
57 using Object = std::vector<Member>;
58
62 class Value
63 : public Variant<std::nullptr_t, bool, int32_t, uint32_t, int64_t, uint64_t, double, std::string, Array, Object>
64 {
65 public:
69 enum Index : size_t
70 {
71 Null = 0,
72 Boolean = 1,
73 Integer = 2,
77 Real = 6,
78 String = 7,
81 };
82
84 using Ptr = Value*;
85
87 using Variant::Variant;
88
90 using Variant::operator=;
91
95 constexpr Value () = default;
96
101 constexpr Value (const char* other)
102 : Variant (in_place_index_t<String>{}, other)
103 {
104 }
105
111 Value& operator= (const char* other)
112 {
114 return *this;
115 }
116
121 constexpr Value (const Value& other) = default;
122
128 Value& operator= (const Value& other) = default;
129
134 constexpr Value (Value&& other) = default;
135
141 Value& operator= (Value&& other) = default;
142
146 virtual ~Value () = default;
147
152 constexpr bool isNull () const
153 {
154 return index () == Null;
155 }
156
161 constexpr bool isBool () const
162 {
163 return index () == Boolean;
164 }
165
171 constexpr bool getBool () const
172 {
173 switch (index ())
174 {
175 case Null:
176 return false;
177
178 case Boolean:
179 return get<Boolean> ();
180
181 case Integer:
182 return get<Integer> ();
183
184 case Unsigned:
185 return get<Unsigned> ();
186
187 case Integer64:
188 return get<Integer64> ();
189
190 case Unsigned64:
191 return get<Unsigned64> ();
192
193 case Real:
194 return get<Real> ();
195
196 default:
197 break;
198 }
199
200 throw std::bad_cast ();
201 }
202
208 explicit operator bool () const
209 {
210 return getBool ();
211 }
212
218 constexpr bool isTrue () const
219 {
220 return getBool ();
221 }
222
228 constexpr bool isFalse () const
229 {
230 return !getBool ();
231 }
232
237 constexpr bool isNumber () const
238 {
239 return index () == Integer || index () == Unsigned || index () == Integer64 || index () == Unsigned64 ||
240 index () == Real;
241 }
242
247 constexpr bool isInt8 () const
248 {
249 switch (index ())
250 {
251 case Integer:
252 return get<Integer> () >= static_cast<int32_t> (std::numeric_limits<int8_t>::min ()) &&
253 get<Integer> () <= static_cast<int32_t> (std::numeric_limits<int8_t>::max ());
254
255 case Unsigned:
256 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<int8_t>::max ());
257
258 case Integer64:
259 return get<Integer64> () >= static_cast<int64_t> (std::numeric_limits<int8_t>::min ()) &&
260 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<int8_t>::max ());
261
262 case Unsigned64:
263 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<int8_t>::max ());
264
265 case Real:
266 return std::trunc (get<Real> ()) == get<Real> () &&
267 get<Real> () >= static_cast<double> (std::numeric_limits<int8_t>::min ()) &&
268 get<Real> () <= static_cast<double> (std::numeric_limits<int8_t>::max ());
269
270 default:
271 break;
272 }
273
274 return false;
275 }
276
282 constexpr int8_t getInt8 () const
283 {
284 if (isInt8 ())
285 {
286 switch (index ())
287 {
288 case Integer:
289 return static_cast<int8_t> (get<Integer> ());
290
291 case Unsigned:
292 return static_cast<int8_t> (get<Unsigned> ());
293
294 case Integer64:
295 return static_cast<int8_t> (get<Integer64> ());
296
297 case Unsigned64:
298 return static_cast<int8_t> (get<Unsigned64> ());
299
300 default:
301 return static_cast<int8_t> (get<Real> ());
302 }
303 }
304
305 throw std::bad_cast ();
306 }
307
313 explicit operator int8_t () const
314 {
315 return getInt8 ();
316 }
317
322 constexpr bool isUint8 () const
323 {
324 switch (index ())
325 {
326 case Integer:
327 return get<Integer> () >= 0 &&
328 get<Integer> () <= static_cast<int32_t> (std::numeric_limits<uint8_t>::max ());
329
330 case Unsigned:
331 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<uint8_t>::max ());
332
333 case Integer64:
334 return get<Integer64> () >= 0 &&
335 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<uint8_t>::max ());
336
337 case Unsigned64:
338 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<uint8_t>::max ());
339
340 case Real:
341 return std::trunc (get<Real> ()) == get<Real> () && get<Real> () >= 0 &&
342 get<Real> () <= static_cast<double> (std::numeric_limits<uint8_t>::max ());
343
344 default:
345 break;
346 }
347
348 return false;
349 }
350
356 constexpr uint8_t getUint8 () const
357 {
358 if (isUint8 ())
359 {
360 switch (index ())
361 {
362 case Integer:
363 return static_cast<uint8_t> (get<Integer> ());
364
365 case Unsigned:
366 return static_cast<uint8_t> (get<Unsigned> ());
367
368 case Integer64:
369 return static_cast<uint8_t> (get<Integer64> ());
370
371 case Unsigned64:
372 return static_cast<uint8_t> (get<Unsigned64> ());
373
374 default:
375 return static_cast<uint8_t> (get<Real> ());
376 }
377 }
378
379 throw std::bad_cast ();
380 }
381
387 explicit operator uint8_t () const
388 {
389 return getUint8 ();
390 }
391
396 constexpr bool isInt16 () const
397 {
398 switch (index ())
399 {
400 case Integer:
401 return get<Integer> () >= static_cast<int32_t> (std::numeric_limits<int16_t>::min ()) &&
402 get<Integer> () <= static_cast<int32_t> (std::numeric_limits<int16_t>::max ());
403
404 case Unsigned:
405 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<int16_t>::max ());
406
407 case Integer64:
408 return get<Integer64> () >= static_cast<int64_t> (std::numeric_limits<int16_t>::min ()) &&
409 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<int16_t>::max ());
410
411 case Unsigned64:
412 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<int16_t>::max ());
413
414 case Real:
415 return std::trunc (get<Real> ()) == get<Real> () &&
416 get<Real> () >= static_cast<double> (std::numeric_limits<int16_t>::min ()) &&
417 get<Real> () <= static_cast<double> (std::numeric_limits<int16_t>::max ());
418
419 default:
420 break;
421 }
422
423 return false;
424 }
425
431 constexpr int16_t getInt16 () const
432 {
433 if (isInt16 ())
434 {
435 switch (index ())
436 {
437 case Integer:
438 return static_cast<int16_t> (get<Integer> ());
439
440 case Unsigned:
441 return static_cast<int16_t> (get<Unsigned> ());
442
443 case Integer64:
444 return static_cast<int16_t> (get<Integer64> ());
445
446 case Unsigned64:
447 return static_cast<int16_t> (get<Unsigned64> ());
448
449 default:
450 return static_cast<int16_t> (get<Real> ());
451 }
452 }
453
454 throw std::bad_cast ();
455 }
456
462 explicit operator int16_t () const
463 {
464 return getInt16 ();
465 }
466
471 constexpr bool isUint16 () const
472 {
473 switch (index ())
474 {
475 case Integer:
476 return get<Integer> () >= 0 &&
477 get<Integer> () <= static_cast<int32_t> (std::numeric_limits<uint16_t>::max ());
478
479 case Unsigned:
480 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<uint16_t>::max ());
481
482 case Integer64:
483 return get<Integer64> () >= 0 &&
484 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<uint16_t>::max ());
485
486 case Unsigned64:
487 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<uint16_t>::max ());
488
489 case Real:
490 return std::trunc (get<Real> ()) == get<Real> () && get<Real> () >= 0 &&
491 get<Real> () <= static_cast<double> (std::numeric_limits<uint16_t>::max ());
492
493 default:
494 break;
495 }
496
497 return false;
498 }
499
505 constexpr uint16_t getUint16 () const
506 {
507 if (isUint16 ())
508 {
509 switch (index ())
510 {
511 case Integer:
512 return static_cast<uint16_t> (get<Integer> ());
513
514 case Unsigned:
515 return static_cast<uint16_t> (get<Unsigned> ());
516
517 case Integer64:
518 return static_cast<uint16_t> (get<Integer64> ());
519
520 case Unsigned64:
521 return static_cast<uint16_t> (get<Unsigned64> ());
522
523 default:
524 return static_cast<uint16_t> (get<Real> ());
525 }
526 }
527
528 throw std::bad_cast ();
529 }
530
536 explicit operator uint16_t () const
537 {
538 return getUint16 ();
539 }
540
545 constexpr bool isInt () const
546 {
547 switch (index ())
548 {
549 case Integer:
550 return true;
551
552 case Unsigned:
553 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<int32_t>::max ());
554
555 case Integer64:
556 return get<Integer64> () >= static_cast<int64_t> (std::numeric_limits<int32_t>::min ()) &&
557 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<int32_t>::max ());
558
559 case Unsigned64:
560 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<int32_t>::max ());
561
562 case Real:
563 return std::trunc (get<Real> ()) == get<Real> () &&
564 get<Real> () >= static_cast<double> (std::numeric_limits<int32_t>::min ()) &&
565 get<Real> () <= static_cast<double> (std::numeric_limits<int32_t>::max ());
566
567 default:
568 break;
569 }
570
571 return false;
572 }
573
579 constexpr int32_t getInt () const
580 {
581 if (isInt ())
582 {
583 switch (index ())
584 {
585 case Integer:
586 return get<Integer> ();
587
588 case Unsigned:
589 return static_cast<int32_t> (get<Unsigned> ());
590
591 case Integer64:
592 return static_cast<int32_t> (get<Integer64> ());
593
594 case Unsigned64:
595 return static_cast<int32_t> (get<Unsigned64> ());
596
597 default:
598 return static_cast<int32_t> (get<Real> ());
599 }
600 }
601
602 throw std::bad_cast ();
603 }
604
610 explicit operator int32_t () const
611 {
612 return getInt ();
613 }
614
619 constexpr bool isUint () const
620 {
621 switch (index ())
622 {
623 case Integer:
624 return get<Integer> () >= 0;
625
626 case Unsigned:
627 return true;
628
629 case Integer64:
630 return get<Integer64> () >= 0 &&
631 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<uint32_t>::max ());
632
633 case Unsigned64:
634 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<uint32_t>::max ());
635
636 case Real:
637 return std::trunc (get<Real> ()) == get<Real> () && get<Real> () >= 0 &&
638 get<Real> () <= static_cast<double> (std::numeric_limits<uint32_t>::max ());
639
640 default:
641 break;
642 }
643
644 return false;
645 }
646
652 constexpr uint32_t getUint () const
653 {
654 if (isUint ())
655 {
656 switch (index ())
657 {
658 case Integer:
659 return static_cast<uint32_t> (get<Integer> ());
660
661 case Unsigned:
662 return get<Unsigned> ();
663
664 case Integer64:
665 return static_cast<uint32_t> (get<Integer64> ());
666
667 case Unsigned64:
668 return static_cast<uint32_t> (get<Unsigned64> ());
669
670 default:
671 return static_cast<uint32_t> (get<Real> ());
672 }
673 }
674
675 throw std::bad_cast ();
676 }
677
683 explicit operator uint32_t () const
684 {
685 return getUint ();
686 }
687
692 constexpr bool isInt64 () const
693 {
694 switch (index ())
695 {
696 case Integer:
697 case Unsigned:
698 case Integer64:
699 return true;
700
701 case Unsigned64:
702 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<int64_t>::max ());
703
704 case Real:
705 return std::trunc (get<Real> ()) == get<Real> () &&
706 get<Real> () >= static_cast<double> (std::numeric_limits<int64_t>::min ()) &&
707 get<Real> () < static_cast<double> (std::numeric_limits<int64_t>::max ());
708
709 default:
710 break;
711 }
712
713 return false;
714 }
715
721 constexpr int64_t getInt64 () const
722 {
723 if (isInt64 ())
724 {
725 switch (index ())
726 {
727 case Integer:
728 return static_cast<int64_t> (get<Integer> ());
729
730 case Unsigned:
731 return static_cast<int64_t> (get<Unsigned> ());
732
733 case Integer64:
734 return get<Integer64> ();
735
736 case Unsigned64:
737 return static_cast<int64_t> (get<Unsigned64> ());
738
739 default:
740 return static_cast<int64_t> (get<Real> ());
741 }
742 }
743
744 throw std::bad_cast ();
745 }
746
752 explicit operator int64_t () const
753 {
754 return getInt64 ();
755 }
756
761 constexpr bool isUint64 () const
762 {
763 switch (index ())
764 {
765 case Integer:
766 return get<Integer> () >= 0;
767
768 case Unsigned:
769 return true;
770
771 case Integer64:
772 return get<Integer64> () >= 0;
773
774 case Unsigned64:
775 return true;
776
777 case Real:
778 return std::trunc (get<Real> ()) == get<Real> () && get<Real> () >= 0 &&
779 get<Real> () < static_cast<double> (std::numeric_limits<uint64_t>::max ());
780
781 default:
782 break;
783 }
784
785 return false;
786 }
787
793 constexpr uint64_t getUint64 () const
794 {
795 if (isUint64 ())
796 {
797 switch (index ())
798 {
799 case Integer:
800 return static_cast<uint64_t> (get<Integer> ());
801
802 case Unsigned:
803 return static_cast<uint64_t> (get<Unsigned> ());
804
805 case Integer64:
806 return static_cast<uint64_t> (get<Integer64> ());
807
808 case Unsigned64:
809 return get<Unsigned64> ();
810
811 default:
812 return static_cast<uint64_t> (get<Real> ());
813 }
814 }
815
816 throw std::bad_cast ();
817 }
818
824 explicit operator uint64_t () const
825 {
826 return getUint64 ();
827 }
828
833 constexpr bool isFloat () const
834 {
835 return isNumber ();
836 }
837
843 constexpr float getFloat () const
844 {
845 switch (index ())
846 {
847 case Integer:
848 return static_cast<float> (get<Integer> ());
849
850 case Unsigned:
851 return static_cast<float> (get<Unsigned> ());
852
853 case Integer64:
854 return static_cast<float> (get<Integer64> ());
855
856 case Unsigned64:
857 return static_cast<float> (get<Unsigned64> ());
858
859 case Real:
860 return static_cast<float> (get<Real> ());
861
862 default:
863 break;
864 }
865
866 throw std::bad_cast ();
867 }
868
874 explicit operator float () const
875 {
876 return getFloat ();
877 }
878
883 constexpr bool isDouble () const
884 {
885 return isNumber ();
886 }
887
893 constexpr double getDouble () const
894 {
895 switch (index ())
896 {
897 case Integer:
898 return static_cast<double> (get<Integer> ());
899
900 case Unsigned:
901 return static_cast<double> (get<Unsigned> ());
902
903 case Integer64:
904 return static_cast<double> (get<Integer64> ());
905
906 case Unsigned64:
907 return static_cast<double> (get<Unsigned64> ());
908
909 case Real:
910 return get<Real> ();
911
912 default:
913 break;
914 }
915
916 throw std::bad_cast ();
917 }
918
924 explicit operator double () const
925 {
926 return getDouble ();
927 }
928
933 constexpr bool isString () const
934 {
935 return index () == String;
936 }
937
943 constexpr const std::string& getString () const
944 {
945 return get<String> ();
946 }
947
953 constexpr std::string& getString ()
954 {
955 return get<String> ();
956 }
957
963 explicit operator const char* () const
964 {
965 return getString ().c_str ();
966 }
967
972 constexpr bool isArray () const
973 {
974 return index () == ArrayValue;
975 }
976
982 constexpr const Array& getArray () const
983 {
984 return get<ArrayValue> ();
985 }
986
992 constexpr Array& getArray ()
993 {
994 return get<ArrayValue> ();
995 }
996
1001 constexpr bool isObject () const
1002 {
1003 return index () == ObjectValue;
1004 }
1005
1011 constexpr const Object& getObject () const
1012 {
1013 return get<ObjectValue> ();
1014 }
1015
1021 constexpr Object& getObject ()
1022 {
1023 return get<ObjectValue> ();
1024 }
1025
1032 Value& at (const Value& key)
1033 {
1034 if (index () == ArrayValue)
1035 {
1036 return get<ArrayValue> ().at (key.getUint64 ());
1037 }
1038
1039 for (auto& member : get<ObjectValue> ())
1040 {
1041 if (member.first == key)
1042 {
1043 return member.second;
1044 }
1045 }
1046
1047 throw std::out_of_range ("invalid key");
1048 }
1049
1056 const Value& at (const Value& key) const
1057 {
1058 if (index () == ArrayValue)
1059 {
1060 return get<ArrayValue> ().at (key.getUint64 ());
1061 }
1062
1063 for (auto const& member : get<ObjectValue> ())
1064 {
1065 if (member.first == key)
1066 {
1067 return member.second;
1068 }
1069 }
1070
1071 throw std::out_of_range ("invalid key");
1072 }
1073
1081 {
1082 if (index () == ArrayValue)
1083 {
1084 return get<ArrayValue> ()[key.getUint64 ()];
1085 }
1086
1087 if (index () == Null)
1088 {
1090 }
1091
1092 for (auto& member : get<ObjectValue> ())
1093 {
1094 if (member.first == key)
1095 {
1096 return member.second;
1097 }
1098 }
1099
1100 get<ObjectValue> ().emplace_back (key, nullptr);
1101
1102 return get<ObjectValue> ().back ().second;
1103 }
1104
1110 bool empty () const
1111 {
1112 switch (index ())
1113 {
1114 case String:
1115 return get<String> ().empty ();
1116
1117 case ArrayValue:
1118 return get<ArrayValue> ().empty ();
1119
1120 case ObjectValue:
1121 return get<ObjectValue> ().empty ();
1122
1123 default:
1124 break;
1125 }
1126
1127 throw std::bad_cast ();
1128 }
1129
1135 size_t size () const
1136 {
1137 switch (index ())
1138 {
1139 case String:
1140 return get<String> ().size ();
1141
1142 case ArrayValue:
1143 return get<ArrayValue> ().size ();
1144
1145 case ObjectValue:
1146 return get<ObjectValue> ().size ();
1147
1148 default:
1149 break;
1150 }
1151
1152 throw std::bad_cast ();
1153 }
1154
1160 void reserve (size_t cap)
1161 {
1162 switch (index ())
1163 {
1164 case String:
1165 get<String> ().reserve (cap);
1166 return;
1167
1168 case ArrayValue:
1169 get<ArrayValue> ().reserve (cap);
1170 return;
1171
1172 case ObjectValue:
1173 get<ObjectValue> ().reserve (cap);
1174 return;
1175
1176 default:
1177 break;
1178 }
1179
1180 throw std::bad_cast ();
1181 }
1182
1187 void clear ()
1188 {
1189 switch (index ())
1190 {
1191 case String:
1192 get<String> ().clear ();
1193 return;
1194
1195 case ArrayValue:
1196 get<ArrayValue> ().clear ();
1197 return;
1198
1199 case ObjectValue:
1200 get<ObjectValue> ().clear ();
1201 return;
1202
1203 default:
1204 break;
1205 }
1206
1207 throw std::bad_cast ();
1208 }
1209
1216 Value& insert (const Member& member)
1217 {
1218 get<ObjectValue> ().push_back (member);
1219 return get<ObjectValue> ().back ().second;
1220 }
1221
1228 Value& insert (Member&& member)
1229 {
1230 get<ObjectValue> ().emplace_back (std::move (member));
1231 return get<ObjectValue> ().back ().second;
1232 }
1233
1240 size_t erase (const Value& key)
1241 {
1242 auto beg = get<ObjectValue> ().begin ();
1243 auto end = get<ObjectValue> ().end ();
1244
1245 for (auto it = beg; it != end; ++it)
1246 {
1247 if (it->first == key)
1248 {
1249 get<ObjectValue> ().erase (it);
1250 return 1;
1251 }
1252 }
1253
1254 return 0;
1255 }
1256
1263 template <typename... Args>
1264 Value& emplaceBack (Args&&... args)
1265 {
1266 if (index () == Null)
1267 {
1268 set<ArrayValue> ();
1269 }
1270 return get<ArrayValue> ().emplace_back (std::forward<Args> (args)...);
1271 }
1272
1279 Value& pushBack (const Value& value)
1280 {
1281 if (index () == Null)
1282 {
1283 set<ArrayValue> ();
1284 }
1285 get<ArrayValue> ().push_back (value);
1286 return get<ArrayValue> ().back ();
1287 }
1288
1296 {
1297 if (index () == Null)
1298 {
1299 set<ArrayValue> ();
1300 }
1301 get<ArrayValue> ().emplace_back (std::move (value));
1302 return get<ArrayValue> ().back ();
1303 }
1304
1309 void popBack ()
1310 {
1311 get<ArrayValue> ().pop_back ();
1312 }
1313
1319 bool contains (const Value& key) const
1320 {
1321 if (index () == ArrayValue)
1322 {
1323 return get<ArrayValue> ().size () > key.getUint64 ();
1324 }
1325
1326 for (auto const& member : get<ObjectValue> ())
1327 {
1328 if (member.first == key)
1329 {
1330 return true;
1331 }
1332 }
1333
1334 return false;
1335 }
1336
1341 void swap (Value& other)
1342 {
1343 Value temp (std::move (*this));
1344 *this = std::move (other);
1345 other = std::move (temp);
1346 }
1347
1354 template <typename Reader>
1355 int deserialize (const char* document, size_t length)
1356 {
1357 Reader reader (*this);
1358 return reader.deserialize (document, length);
1359 }
1360
1367 template <typename Reader>
1368 int deserialize (const char* first, const char* last)
1369 {
1370 Reader reader (*this);
1371 return reader.deserialize (first, last);
1372 }
1373
1379 template <typename Reader>
1380 int deserialize (const std::string& document)
1381 {
1382 Reader reader (*this);
1383 return reader.deserialize (document);
1384 }
1385
1391 template <typename Reader>
1392 int deserialize (std::stringstream& document)
1393 {
1394 Reader reader (*this);
1395 return reader.deserialize (document);
1396 }
1397
1403 template <typename Reader>
1404 int deserialize (std::istringstream& document)
1405 {
1406 Reader reader (*this);
1407 return reader.deserialize (document);
1408 }
1409
1415 template <typename Reader>
1416 int deserialize (std::fstream& document)
1417 {
1418 Reader reader (*this);
1419 return reader.deserialize (document);
1420 }
1421
1427 template <typename Reader>
1428 int deserialize (std::ifstream& document)
1429 {
1430 Reader reader (*this);
1431 return reader.deserialize (document);
1432 }
1433
1439 template <typename Reader>
1440 int deserialize (std::iostream& document)
1441 {
1442 Reader reader (*this);
1443 return reader.deserialize (document);
1444 }
1445
1451 template <typename Reader>
1452 int deserialize (std::istream& document)
1453 {
1454 Reader reader (*this);
1455 return reader.deserialize (document);
1456 }
1457
1463 template <typename Writer>
1464 int serialize (std::ostream& document) const
1465 {
1466 Writer writer (document);
1467 return writer.serialize (*this);
1468 }
1469
1476 int jsonRead (const char* document, size_t length);
1477
1484 int jsonRead (const char* first, const char* last);
1485
1491 int jsonRead (const std::string& document);
1492
1498 int jsonRead (std::stringstream& document);
1499
1505 int jsonRead (std::istringstream& document);
1506
1512 int jsonRead (std::fstream& document);
1513
1519 int jsonRead (std::ifstream& document);
1520
1526 int jsonRead (std::iostream& document);
1527
1533 int jsonRead (std::istream& document);
1534
1540 int jsonWrite (std::ostream& document, size_t indentation = 0) const;
1541
1547 int jsonCanonicalize (std::ostream& document);
1548
1555 int packRead (const char* document, size_t length);
1556
1563 int packRead (const char* first, const char* last);
1564
1570 int packRead (const std::string& document);
1571
1577 int packRead (std::stringstream& document);
1578
1584 int packRead (std::istringstream& document);
1585
1591 int packRead (std::fstream& document);
1592
1598 int packRead (std::ifstream& document);
1599
1605 int packRead (std::iostream& document);
1606
1612 int packRead (std::istream& document);
1613
1618 int packWrite (std::ostream& document) const;
1619
1620 // friendship with equal operator.
1621 friend constexpr bool operator== (const Value& lhs, const Value& rhs);
1622
1623 // friendship with lower operator.
1624 friend constexpr bool operator< (const Value& lhs, const Value& rhs);
1625 };
1626
1633 constexpr bool operator== (const Value& lhs, const Value& rhs)
1634 {
1635 if (lhs.isNumber () && rhs.isNumber () && (lhs.index () != rhs.index ()))
1636 {
1637 if (lhs.isInt64 () && rhs.isUint64 ())
1638 {
1639 return lhs.getInt64 () >= 0 && uint64_t (lhs.getInt64 ()) == rhs.getUint64 ();
1640 }
1641 else if (lhs.isUint64 () && rhs.isInt64 ())
1642 {
1643 return rhs.getInt64 () >= 0 && uint64_t (rhs.getInt64 ()) == lhs.getUint64 ();
1644 }
1645 else
1646 {
1647 return lhs.getDouble () == rhs.getDouble ();
1648 }
1649 }
1650
1651 return lhs.equal (rhs);
1652 }
1653
1660 constexpr bool operator!= (const Value& lhs, const Value& rhs)
1661 {
1662 return !(lhs == rhs);
1663 }
1664
1671 constexpr bool operator< (const Value& lhs, const Value& rhs)
1672 {
1673 if (lhs.isNumber () && rhs.isNumber () && (lhs.index () != rhs.index ()))
1674 {
1675 if (lhs.isInt64 () && rhs.isUint64 ())
1676 {
1677 return lhs.getInt64 () < 0 || uint64_t (lhs.getInt64 ()) < rhs.getUint64 ();
1678 }
1679 else if (lhs.isUint64 () && rhs.isInt64 ())
1680 {
1681 return rhs.getInt64 () >= 0 && lhs.getUint64 () < uint64_t (rhs.getInt64 ());
1682 }
1683 else
1684 {
1685 return lhs.getDouble () < rhs.getDouble ();
1686 }
1687 }
1688
1689 return lhs.lower (rhs);
1690 }
1691
1698 constexpr bool operator> (const Value& lhs, const Value& rhs)
1699 {
1700 return rhs < lhs;
1701 }
1702
1709 constexpr bool operator<= (const Value& lhs, const Value& rhs)
1710 {
1711 return !(rhs < lhs);
1712 }
1713
1720 constexpr bool operator>= (const Value& lhs, const Value& rhs)
1721 {
1722 return !(lhs < rhs);
1723 }
1724}
1725
1726#endif
value class.
Definition value.hpp:64
constexpr int32_t getInt() const
get variable held by value as a 32 bits integer.
Definition value.hpp:579
constexpr bool isFloat() const
check if the variable held by value is a float value.
Definition value.hpp:833
size_t size() const
returns the number of elements in the nested container.
Definition value.hpp:1135
void clear()
erases all elements in the nested container.
Definition value.hpp:1187
constexpr std::string & getString()
get variable held by value as a string value.
Definition value.hpp:953
void swap(Value &other)
exchanges the contents of the value with those of other.
Definition value.hpp:1341
constexpr bool isFalse() const
check if the content of the value is false.
Definition value.hpp:228
int deserialize(std::fstream &document)
deserialize a document.
Definition value.hpp:1416
const Value & at(const Value &key) const
returns a reference to the value at position pos or mapped to key.
Definition value.hpp:1056
int deserialize(std::iostream &document)
deserialize a document.
Definition value.hpp:1440
int serialize(std::ostream &document) const
serialize data.
Definition value.hpp:1464
constexpr int16_t getInt16() const
get variable held by value as a 16 bits integer.
Definition value.hpp:431
constexpr bool isInt() const
check if the variable held by value is a 32 bits integer value.
Definition value.hpp:545
friend constexpr bool operator<(const Value &lhs, const Value &rhs)
compare if lower than.
Definition value.hpp:1671
friend constexpr bool operator==(const Value &lhs, const Value &rhs)
compare if equal.
Definition value.hpp:1633
constexpr bool isInt64() const
check if the variable held by value is a 64 bits integer value.
Definition value.hpp:692
constexpr bool isUint16() const
check if the variable held by value is a 16 bits unsigned integer value.
Definition value.hpp:471
void popBack()
removes the last element of the nested container.
Definition value.hpp:1309
constexpr bool isTrue() const
check if the content of the value is true.
Definition value.hpp:218
Value & operator[](const Value &key)
returns a reference to the value at position pos or mapped to key.
Definition value.hpp:1080
constexpr const Array & getArray() const
get variable held by value as an array.
Definition value.hpp:982
Value & insert(Member &&member)
insert element in the nested container.
Definition value.hpp:1228
constexpr bool isArray() const
check if the variable held by value is an array.
Definition value.hpp:972
constexpr Value(const char *other)
constructs the value with the copy of the contents of a C-style string.
Definition value.hpp:101
constexpr const std::string & getString() const
get variable held by value as a string value.
Definition value.hpp:943
int deserialize(std::istringstream &document)
deserialize a document.
Definition value.hpp:1404
constexpr bool isUint64() const
check if the variable held by value is a 64 bits unsigned integer value.
Definition value.hpp:761
int deserialize(std::stringstream &document)
deserialize a document.
Definition value.hpp:1392
Value & pushBack(Value &&value)
appends element at the end of the nested container.
Definition value.hpp:1295
constexpr Object & getObject()
get variable held by value as an object.
Definition value.hpp:1021
int deserialize(std::ifstream &document)
deserialize a document.
Definition value.hpp:1428
constexpr bool isObject() const
check if the variable held by value is an object.
Definition value.hpp:1001
constexpr const Object & getObject() const
get variable held by value as an object.
Definition value.hpp:1011
void reserve(size_t cap)
increase the capacity of the nested container.
Definition value.hpp:1160
bool contains(const Value &key) const
checks if the nested container contains an element that is mapped to key.
Definition value.hpp:1319
constexpr uint8_t getUint8() const
get variable held by value as a 8 bits unsigned integer.
Definition value.hpp:356
size_t erase(const Value &key)
removes member with the key equivalent to key.
Definition value.hpp:1240
constexpr bool isUint() const
check if the variable held by value is a 32 bits unsigned integer value.
Definition value.hpp:619
Value & operator=(const char *other)
replaces the contents with a copy of a C-style string.
Definition value.hpp:111
constexpr bool isInt16() const
check if the variable held by value is a 16 bits integer value.
Definition value.hpp:396
int deserialize(const std::string &document)
deserialize a document.
Definition value.hpp:1380
constexpr bool getBool() const
get variable held by value as a boolean value.
Definition value.hpp:171
int jsonCanonicalize(std::ostream &document)
serialize canonicalized json data.
Definition value.cpp:133
int deserialize(const char *first, const char *last)
deserialize a document.
Definition value.hpp:1368
Index
nested value type index.
Definition value.hpp:70
@ Integer64
Definition value.hpp:75
@ Unsigned64
Definition value.hpp:76
@ Real
Definition value.hpp:77
@ Boolean
Definition value.hpp:72
@ String
Definition value.hpp:78
@ ArrayValue
Definition value.hpp:79
@ Integer
Definition value.hpp:73
@ Null
Definition value.hpp:71
@ Unsigned
Definition value.hpp:74
@ ObjectValue
Definition value.hpp:80
Value & pushBack(const Value &value)
appends element at the end of the nested container.
Definition value.hpp:1279
Value & at(const Value &key)
returns a reference to the value at position pos or mapped to key.
Definition value.hpp:1032
virtual ~Value()=default
destroy the Value instance.
int deserialize(const char *document, size_t length)
deserialize a document.
Definition value.hpp:1355
constexpr Value(Value &&other)=default
move constructor.
constexpr int8_t getInt8() const
get variable held by value as a 8 bits integer.
Definition value.hpp:282
constexpr double getDouble() const
get variable held by value as a double value.
Definition value.hpp:893
constexpr Value(const Value &other)=default
copy constructor.
constexpr uint32_t getUint() const
get variable held by value as a 32 bits unsigned integer value.
Definition value.hpp:652
constexpr bool isBool() const
check if the variable held by value is a boolean value.
Definition value.hpp:161
constexpr Value()=default
default constructor.
int jsonRead(const char *document, size_t length)
deserialize a json document.
Definition value.cpp:42
constexpr bool isString() const
check if the variable held by value is a string value.
Definition value.hpp:933
constexpr float getFloat() const
get variable held by value as a float value.
Definition value.hpp:843
constexpr bool isDouble() const
check if the variable held by value is a double value.
Definition value.hpp:883
Value & insert(const Member &member)
insert element in the nested container.
Definition value.hpp:1216
Value & emplaceBack(Args &&... args)
appends element at the end of the nested container.
Definition value.hpp:1264
constexpr uint64_t getUint64() const
get variable held by value as a 64 bits unsigned integer value.
Definition value.hpp:793
int packRead(const char *document, size_t length)
deserialize a msgpack document.
Definition value.cpp:142
bool empty() const
check if the nested container is empty.
Definition value.hpp:1110
constexpr bool isNumber() const
check if the variable held by value is a number value.
Definition value.hpp:237
int jsonWrite(std::ostream &document, size_t indentation=0) const
serialize json data.
Definition value.cpp:123
constexpr bool isNull() const
check if the variable held by value is a null value.
Definition value.hpp:152
constexpr uint16_t getUint16() const
get variable held by value as a 16 bits unsigned integer.
Definition value.hpp:505
constexpr bool isUint8() const
check if the variable held by value is a 8 bits unsigned integer value.
Definition value.hpp:322
constexpr int64_t getInt64() const
get variable held by value as a 64 bits integer value.
Definition value.hpp:721
int packWrite(std::ostream &document) const
serialize msgpack data.
Definition value.cpp:223
int deserialize(std::istream &document)
deserialize a document.
Definition value.hpp:1452
constexpr bool isInt8() const
check if the variable held by value is a 8 bits integer value.
Definition value.hpp:247
constexpr Array & getArray()
get variable held by value as an array.
Definition value.hpp:992
variant class.
Definition variant.hpp:352
constexpr bool lower(const Variant &rhs) const
check if lower than.
Definition variant.hpp:693
constexpr std::enable_if_t< is_unique< T, Ts... >::value, T & > get()
Definition variant.hpp:548
constexpr std::enable_if_t< is_unique< T, Ts... >::value &&std::is_constructible< T, Args &&... >::value, T & > set(Args &&... args)
Definition variant.hpp:496
constexpr Variant()=default
default constructor.
constexpr Variant & operator=(const Variant &other)=default
copy assignment.
constexpr bool equal(const Variant &rhs) const
check if equal.
Definition variant.hpp:679
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
bool operator<(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
bool operator>(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater.
Definition endpoint.hpp:219
bool operator!=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are not equal.
Definition endpoint.hpp:195
bool operator>=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater or equal.
Definition endpoint.hpp:243
bool operator<=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower or equal.
Definition endpoint.hpp:231
std::pair< Value, Value > Member
object member.
Definition value.hpp:54
std::vector< Value > Array
array.
Definition value.hpp:51
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183
std::vector< Member > Object
object.
Definition value.hpp:57
disambiguation tag to indicate that the contained object should be constructed in-place.
Definition traits.hpp:57