join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_VALUE_HPP__
26#define __JOIN_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 <std::string, Value>;
55
57 using Object = std::vector <Member>;
58
62 class Value : public Variant <std::nullptr_t, bool, int32_t, uint32_t, int64_t, uint64_t, double, std::string, Array, Object>
63 {
64 public:
68 enum Index : size_t
69 {
70 Null = 0,
71 Boolean = 1,
72 Integer = 2,
76 Real = 6,
77 String = 7,
80 };
81
83 using Ptr = Value*;
84
86 using Variant::Variant;
87
89 using Variant::operator=;
90
94 constexpr Value () = default;
95
100 constexpr Value (const char* other)
101 : Variant (in_place_index_t <String> {}, other)
102 {
103 }
104
110 Value& operator= (const char* other)
111 {
112 Variant::operator= (Value (in_place_index_t <String> {}, other));
113 return *this;
114 }
115
120 constexpr Value (const Value& other) = default;
121
127 Value& operator= (const Value& other) = default;
128
133 constexpr Value (Value&& other) = default;
134
140 Value& operator= (Value&& other) = default;
141
145 virtual ~Value () = default;
146
151 constexpr bool isNull () const
152 {
153 return index () == Null;
154 }
155
160 constexpr bool isBool () const
161 {
162 return index () == Boolean;
163 }
164
170 constexpr bool getBool () const
171 {
172 switch (index ())
173 {
174 case Null:
175 return false;
176
177 case Boolean:
178 return get <Boolean> ();
179
180 case Integer:
181 return get <Integer> ();
182
183 case Unsigned:
184 return get <Unsigned> ();
185
186 case Integer64:
187 return get <Integer64> ();
188
189 case Unsigned64:
190 return get <Unsigned64> ();
191
192 case Real:
193 return get <Real> ();
194
195 default:
196 break;
197 }
198
199 throw std::bad_cast ();
200 }
201
207 explicit operator bool () const
208 {
209 return getBool ();
210 }
211
217 constexpr bool isTrue () const
218 {
219 return getBool ();
220 }
221
227 constexpr bool isFalse () const
228 {
229 return !getBool ();
230 }
231
236 constexpr bool isNumber () const
237 {
238 return index () == Integer || index () == Unsigned || index () == Integer64 || index () == Unsigned64 || index () == Real;
239 }
240
245 constexpr bool isInt8 () const
246 {
247 switch (index ())
248 {
249 case Integer:
250 return get <Integer> () >= static_cast <int32_t> (std::numeric_limits <int8_t>::min ()) &&
251 get <Integer> () <= static_cast <int32_t> (std::numeric_limits <int8_t>::max ());
252
253 case Unsigned:
254 return get <Unsigned> () <= static_cast <uint32_t> (std::numeric_limits <int8_t>::max ());
255
256 case Integer64:
257 return get <Integer64> () >= static_cast <int64_t> (std::numeric_limits <int8_t>::min ()) &&
258 get <Integer64> () <= static_cast <int64_t> (std::numeric_limits <int8_t>::max ());
259
260 case Unsigned64:
261 return get <Unsigned64> () <= static_cast <uint64_t> (std::numeric_limits <int8_t>::max ());
262
263 case Real:
264 return std::trunc (get <Real> ()) == get <Real> () &&
265 get <Real> () >= static_cast <double> (std::numeric_limits <int8_t>::min ()) &&
266 get <Real> () <= static_cast <double> (std::numeric_limits <int8_t>::max ());
267
268 default:
269 break;
270 }
271
272 return false;
273 }
274
280 constexpr int8_t getInt8 () const
281 {
282 if (isInt8 ())
283 {
284 switch (index ())
285 {
286 case Integer:
287 return static_cast <int8_t> (get <Integer> ());
288
289 case Unsigned:
290 return static_cast <int8_t> (get <Unsigned> ());
291
292 case Integer64:
293 return static_cast <int8_t> (get <Integer64> ());
294
295 case Unsigned64:
296 return static_cast <int8_t> (get <Unsigned64> ());
297
298 default:
299 return static_cast <int8_t> (get <Real> ());
300 }
301 }
302
303 throw std::bad_cast ();
304 }
305
311 explicit operator int8_t () const
312 {
313 return getInt8 ();
314 }
315
320 constexpr bool isUint8 () const
321 {
322 switch (index ())
323 {
324 case Integer:
325 return get <Integer> () >= 0 &&
326 get <Integer> () <= static_cast <int32_t> (std::numeric_limits <uint8_t>::max ());
327
328 case Unsigned:
329 return get <Unsigned> () <= static_cast <uint32_t> (std::numeric_limits <uint8_t>::max ());
330
331 case Integer64:
332 return get <Integer64> () >= 0 &&
333 get <Integer64> () <= static_cast <int64_t> (std::numeric_limits <uint8_t>::max ());
334
335 case Unsigned64:
336 return get <Unsigned64> () <= static_cast <uint64_t> (std::numeric_limits <uint8_t>::max ());
337
338 case Real:
339 return std::trunc (get <Real> ()) == get <Real> () &&
340 get <Real> () >= 0 &&
341 get <Real> () <= static_cast <double> (std::numeric_limits <uint8_t>::max ());
342
343 default:
344 break;
345 }
346
347 return false;
348 }
349
355 constexpr uint8_t getUint8 () const
356 {
357 if (isUint8 ())
358 {
359 switch (index ())
360 {
361 case Integer:
362 return static_cast <uint8_t> (get <Integer> ());
363
364 case Unsigned:
365 return static_cast <uint8_t> (get <Unsigned> ());
366
367 case Integer64:
368 return static_cast <uint8_t> (get <Integer64> ());
369
370 case Unsigned64:
371 return static_cast <uint8_t> (get <Unsigned64> ());
372
373 default:
374 return static_cast <uint8_t> (get <Real> ());
375 }
376 }
377
378 throw std::bad_cast ();
379 }
380
386 explicit operator uint8_t () const
387 {
388 return getUint8 ();
389 }
390
395 constexpr bool isInt16 () const
396 {
397 switch (index ())
398 {
399 case Integer:
400 return get <Integer> () >= static_cast <int32_t> (std::numeric_limits <int16_t>::min ()) &&
401 get <Integer> () <= static_cast <int32_t> (std::numeric_limits <int16_t>::max ());
402
403 case Unsigned:
404 return get <Unsigned> () <= static_cast <uint32_t> (std::numeric_limits <int16_t>::max ());
405
406 case Integer64:
407 return get <Integer64> () >= static_cast <int64_t> (std::numeric_limits <int16_t>::min ()) &&
408 get <Integer64> () <= static_cast <int64_t> (std::numeric_limits <int16_t>::max ());
409
410 case Unsigned64:
411 return get <Unsigned64> () <= static_cast <uint64_t> (std::numeric_limits <int16_t>::max ());
412
413 case Real:
414 return std::trunc (get <Real> ()) == get <Real> () &&
415 get <Real> () >= static_cast <double> (std::numeric_limits <int16_t>::min ()) &&
416 get <Real> () <= static_cast <double> (std::numeric_limits <int16_t>::max ());
417
418 default:
419 break;
420 }
421
422 return false;
423 }
424
430 constexpr int16_t getInt16 () const
431 {
432 if (isInt16 ())
433 {
434 switch (index ())
435 {
436 case Integer:
437 return static_cast <int16_t> (get <Integer> ());
438
439 case Unsigned:
440 return static_cast <int16_t> (get <Unsigned> ());
441
442 case Integer64:
443 return static_cast <int16_t> (get <Integer64> ());
444
445 case Unsigned64:
446 return static_cast <int16_t> (get <Unsigned64> ());
447
448 default:
449 return static_cast <int16_t> (get <Real> ());
450 }
451 }
452
453 throw std::bad_cast ();
454 }
455
461 explicit operator int16_t () const
462 {
463 return getInt16 ();
464 }
465
470 constexpr bool isUint16 () const
471 {
472 switch (index ())
473 {
474 case Integer:
475 return get <Integer> () >= 0 &&
476 get <Integer> () <= static_cast <int32_t> (std::numeric_limits <uint16_t>::max ());
477
478 case Unsigned:
479 return get <Unsigned> () <= static_cast <uint32_t> (std::numeric_limits <uint16_t>::max ());
480
481 case Integer64:
482 return get <Integer64> () >= 0 &&
483 get <Integer64> () <= static_cast <int64_t> (std::numeric_limits <uint16_t>::max ());
484
485 case Unsigned64:
486 return get <Unsigned64> () <= static_cast <uint64_t> (std::numeric_limits <uint16_t>::max ());
487
488 case Real:
489 return std::trunc (get <Real> ()) == get <Real> () &&
490 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> () &&
638 get <Real> () >= 0 &&
639 get <Real> () <= static_cast <double> (std::numeric_limits <uint32_t>::max ());
640
641 default:
642 break;
643 }
644
645 return false;
646 }
647
653 constexpr uint32_t getUint () const
654 {
655 if (isUint ())
656 {
657 switch (index ())
658 {
659 case Integer:
660 return static_cast <uint32_t> (get <Integer> ());
661
662 case Unsigned:
663 return get <Unsigned> ();
664
665 case Integer64:
666 return static_cast <uint32_t> (get <Integer64> ());
667
668 case Unsigned64:
669 return static_cast <uint32_t> (get <Unsigned64> ());
670
671 default:
672 return static_cast <uint32_t> (get <Real> ());
673 }
674 }
675
676 throw std::bad_cast ();
677 }
678
684 explicit operator uint32_t () const
685 {
686 return getUint ();
687 }
688
693 constexpr bool isInt64 () const
694 {
695 switch (index ())
696 {
697 case Integer:
698 case Unsigned:
699 case Integer64:
700 return true;
701
702 case Unsigned64:
703 return get <Unsigned64> () <= static_cast <uint64_t> (std::numeric_limits <int64_t>::max ());
704
705 case Real:
706 return std::trunc (get <Real> ()) == get <Real> () &&
707 get <Real> () >= static_cast <double> (std::numeric_limits <int64_t>::min ()) &&
708 get <Real> () < static_cast <double> (std::numeric_limits <int64_t>::max ());
709
710 default:
711 break;
712 }
713
714 return false;
715 }
716
722 constexpr int64_t getInt64 () const
723 {
724 if (isInt64 ())
725 {
726 switch (index ())
727 {
728 case Integer:
729 return static_cast <int64_t> (get <Integer> ());
730
731 case Unsigned:
732 return static_cast <int64_t> (get <Unsigned> ());
733
734 case Integer64:
735 return get <Integer64> ();
736
737 case Unsigned64:
738 return static_cast <int64_t> (get <Unsigned64> ());
739
740 default:
741 return static_cast <int64_t> (get <Real> ());
742 }
743 }
744
745 throw std::bad_cast ();
746 }
747
753 explicit operator int64_t () const
754 {
755 return getInt64 ();
756 }
757
762 constexpr bool isUint64 () const
763 {
764 switch (index ())
765 {
766 case Integer:
767 return get <Integer> () >= 0;
768
769 case Unsigned:
770 return true;
771
772 case Integer64:
773 return get <Integer64> () >= 0;
774
775 case Unsigned64:
776 return true;
777
778 case Real:
779 return std::trunc (get <Real> ()) == get <Real> () &&
780 get <Real> () >= 0 &&
781 get <Real> () < static_cast <double> (std::numeric_limits <uint64_t>::max ());
782
783 default:
784 break;
785 }
786
787 return false;
788 }
789
795 constexpr uint64_t getUint64 () const
796 {
797 if (isUint64 ())
798 {
799 switch (index ())
800 {
801 case Integer:
802 return static_cast <uint64_t> (get <Integer> ());
803
804 case Unsigned:
805 return static_cast <uint64_t> (get <Unsigned> ());
806
807 case Integer64:
808 return static_cast <uint64_t> (get <Integer64> ());
809
810 case Unsigned64:
811 return get <Unsigned64> ();
812
813 default:
814 return static_cast <uint64_t> (get <Real> ());
815 }
816 }
817
818 throw std::bad_cast ();
819 }
820
826 explicit operator uint64_t () const
827 {
828 return getUint64 ();
829 }
830
835 constexpr bool isFloat () const
836 {
837 return isNumber ();
838 }
839
845 constexpr float getFloat () const
846 {
847 switch (index ())
848 {
849 case Integer:
850 return static_cast <float> (get <Integer> ());
851
852 case Unsigned:
853 return static_cast <float> (get <Unsigned> ());
854
855 case Integer64:
856 return static_cast <float> (get <Integer64> ());
857
858 case Unsigned64:
859 return static_cast <float> (get <Unsigned64> ());
860
861 case Real:
862 return static_cast <float> (get <Real> ());
863
864 default:
865 break;
866 }
867
868 throw std::bad_cast ();
869 }
870
876 explicit operator float () const
877 {
878 return getFloat ();
879 }
880
885 constexpr bool isDouble () const
886 {
887 return isNumber ();
888 }
889
895 constexpr double getDouble () const
896 {
897 switch (index ())
898 {
899 case Integer:
900 return static_cast <double> (get <Integer> ());
901
902 case Unsigned:
903 return static_cast <double> (get <Unsigned> ());
904
905 case Integer64:
906 return static_cast <double> (get <Integer64> ());
907
908 case Unsigned64:
909 return static_cast <double> (get <Unsigned64> ());
910
911 case Real:
912 return get <Real> ();
913
914 default:
915 break;
916 }
917
918 throw std::bad_cast ();
919 }
920
926 explicit operator double () const
927 {
928 return getDouble ();
929 }
930
935 constexpr bool isString () const
936 {
937 return index () == String;
938 }
939
945 constexpr const std::string& getString () const
946 {
947 return get <String> ();
948 }
949
955 constexpr std::string& getString ()
956 {
957 return get <String> ();
958 }
959
965 explicit operator const char * () const
966 {
967 return getString ().c_str ();
968 }
969
974 constexpr bool isArray () const
975 {
976 return index () == ArrayValue;
977 }
978
984 constexpr const Array& getArray () const
985 {
986 return get <ArrayValue> ();
987 }
988
994 constexpr Array& getArray ()
995 {
996 return get <ArrayValue> ();
997 }
998
1003 constexpr bool isObject () const
1004 {
1005 return index () == ObjectValue;
1006 }
1007
1013 constexpr const Object& getObject () const
1014 {
1015 return get <ObjectValue> ();
1016 }
1017
1023 constexpr Object& getObject ()
1024 {
1025 return get <ObjectValue> ();
1026 }
1027
1034 Value& at (size_t pos)
1035 {
1036 return get <ArrayValue> ().at (pos);
1037 }
1038
1045 const Value& at (size_t pos) const
1046 {
1047 return get <ArrayValue> ().at (pos);
1048 }
1049
1056 Value& at (const std::string& key)
1057 {
1058 for (auto& member : get <ObjectValue> ())
1059 {
1060 if (member.first == key)
1061 {
1062 return member.second;
1063 }
1064 }
1065
1066 throw std::out_of_range ("invalid key");
1067 }
1068
1075 const Value& at (const std::string& key) const
1076 {
1077 for (auto const& member : get <ObjectValue> ())
1078 {
1079 if (member.first == key)
1080 {
1081 return member.second;
1082 }
1083 }
1084
1085 throw std::out_of_range ("invalid key");
1086 }
1087
1094 Value& operator[] (size_t pos)
1095 {
1096 return get <ArrayValue> ()[pos];
1097 }
1098
1105 Value& operator[] (const std::string& key)
1106 {
1107 if (index () == Null)
1108 {
1110 }
1111
1112 for (auto& member : get <ObjectValue> ())
1113 {
1114 if (member.first == key)
1115 {
1116 return member.second;
1117 }
1118 }
1119
1120 get <ObjectValue> ().emplace_back (key, nullptr);
1121
1122 return get <ObjectValue> ().back ().second;
1123 }
1124
1130 bool empty () const
1131 {
1132 switch (index ())
1133 {
1134 case String:
1135 return get <String> ().empty ();
1136
1137 case ArrayValue:
1138 return get <ArrayValue> ().empty ();
1139
1140 case ObjectValue:
1141 return get <ObjectValue> ().empty ();
1142
1143 default:
1144 break;
1145 }
1146
1147 throw std::bad_cast ();
1148 }
1149
1155 size_t size () const
1156 {
1157 switch (index ())
1158 {
1159 case String:
1160 return get <String> ().size ();
1161
1162 case ArrayValue:
1163 return get <ArrayValue> ().size ();
1164
1165 case ObjectValue:
1166 return get <ObjectValue> ().size ();
1167
1168 default:
1169 break;
1170 }
1171
1172 throw std::bad_cast ();
1173 }
1174
1180 void reserve (size_t cap)
1181 {
1182 switch (index ())
1183 {
1184 case String:
1185 get <String> ().reserve (cap);
1186 return;
1187
1188 case ArrayValue:
1189 get <ArrayValue> ().reserve (cap);
1190 return;
1191
1192 case ObjectValue:
1193 get <ObjectValue> ().reserve (cap);
1194 return;
1195
1196 default:
1197 break;
1198 }
1199
1200 throw std::bad_cast ();
1201 }
1202
1207 void clear ()
1208 {
1209 switch (index ())
1210 {
1211 case String:
1212 get <String> ().clear ();
1213 return;
1214
1215 case ArrayValue:
1216 get <ArrayValue> ().clear ();
1217 return;
1218
1219 case ObjectValue:
1220 get <ObjectValue> ().clear ();
1221 return;
1222
1223 default:
1224 break;
1225 }
1226
1227 throw std::bad_cast ();
1228 }
1229
1236 Value& insert (const Member& member)
1237 {
1238 get <ObjectValue> ().push_back (member);
1239 return get <ObjectValue> ().back ().second;
1240 }
1241
1248 Value& insert (Member&& member)
1249 {
1250 get <ObjectValue> ().emplace_back (std::move (member));
1251 return get <ObjectValue> ().back ().second;
1252 }
1253
1260 size_t erase (const std::string& key)
1261 {
1262 auto beg = get <ObjectValue> ().begin ();
1263 auto end = get <ObjectValue> ().end ();
1264
1265 for (auto it = beg; it != end; ++it)
1266 {
1267 if (it->first == key)
1268 {
1269 get <ObjectValue> ().erase (it);
1270 return 1;
1271 }
1272 }
1273
1274 return 0;
1275 }
1276
1283 template <typename... Args>
1284 Value& emplaceBack (Args&&... args)
1285 {
1286 if (index () == Null)
1287 {
1289 }
1290 return get <ArrayValue> ().emplace_back (std::forward <Args> (args)...);
1291 }
1292
1299 Value& pushBack (const Value& value)
1300 {
1301 if (index () == Null)
1302 {
1304 }
1305 get <ArrayValue> ().push_back (value);
1306 return get <ArrayValue> ().back ();
1307 }
1308
1316 {
1317 if (index () == Null)
1318 {
1320 }
1321 get <ArrayValue> ().emplace_back (std::move (value));
1322 return get <ArrayValue> ().back ();
1323 }
1324
1329 void popBack ()
1330 {
1331 get <ArrayValue> ().pop_back ();
1332 }
1333
1339 bool contains (size_t pos) const
1340 {
1341 return get <ArrayValue> ().size () > pos;
1342 }
1343
1349 bool contains (const std::string& key) const
1350 {
1351 for (auto& member : get <ObjectValue> ())
1352 {
1353 if (member.first == key)
1354 {
1355 return true;
1356 }
1357 }
1358
1359 return false;
1360 }
1361
1366 void swap (Value& other)
1367 {
1368 Value temp (std::move (*this));
1369 *this = std::move (other);
1370 other = std::move (temp);
1371 }
1372
1379 template <typename Reader>
1380 int deserialize (const char* document, size_t length)
1381 {
1382 Reader reader (*this);
1383 return reader.deserialize (document, length);
1384 }
1385
1392 template <typename Reader>
1393 int deserialize (const char* first, const char* last)
1394 {
1395 Reader reader (*this);
1396 return reader.deserialize (first, last);
1397 }
1398
1404 template <typename Reader>
1405 int deserialize (const std::string& document)
1406 {
1407 Reader reader (*this);
1408 return reader.deserialize (document);
1409 }
1410
1416 template <typename Reader>
1417 int deserialize (std::stringstream& document)
1418 {
1419 Reader reader (*this);
1420 return reader.deserialize (document);
1421 }
1422
1428 template <typename Reader>
1429 int deserialize (std::istringstream& document)
1430 {
1431 Reader reader (*this);
1432 return reader.deserialize (document);
1433 }
1434
1440 template <typename Reader>
1441 int deserialize (std::fstream& document)
1442 {
1443 Reader reader (*this);
1444 return reader.deserialize (document);
1445 }
1446
1452 template <typename Reader>
1453 int deserialize (std::ifstream& document)
1454 {
1455 Reader reader (*this);
1456 return reader.deserialize (document);
1457 }
1458
1464 template <typename Reader>
1465 int deserialize (std::iostream& document)
1466 {
1467 Reader reader (*this);
1468 return reader.deserialize (document);
1469 }
1470
1476 template <typename Reader>
1477 int deserialize (std::istream& document)
1478 {
1479 Reader reader (*this);
1480 return reader.deserialize (document);
1481 }
1482
1488 template <typename Writer>
1489 int serialize (std::ostream& document) const
1490 {
1491 Writer writer (document);
1492 return writer.serialize (*this);
1493 }
1494
1501 int jsonRead (const char* document, size_t length);
1502
1509 int jsonRead (const char* first, const char* last);
1510
1516 int jsonRead (const std::string& document);
1517
1523 int jsonRead (std::stringstream& document);
1524
1530 int jsonRead (std::istringstream& document);
1531
1537 int jsonRead (std::fstream& document);
1538
1544 int jsonRead (std::ifstream& document);
1545
1551 int jsonRead (std::iostream& document);
1552
1558 int jsonRead (std::istream& document);
1559
1565 int jsonWrite (std::ostream& document, size_t indentation = 0) const;
1566
1572 int jsonCanonicalize (std::ostream& document);
1573
1580 int packRead (const char* document, size_t length);
1581
1588 int packRead (const char* first, const char* last);
1589
1595 int packRead (const std::string& document);
1596
1602 int packRead (std::stringstream& document);
1603
1609 int packRead (std::istringstream& document);
1610
1616 int packRead (std::fstream& document);
1617
1623 int packRead (std::ifstream& document);
1624
1630 int packRead (std::iostream& document);
1631
1637 int packRead (std::istream& document);
1638
1643 int packWrite (std::ostream& document) const;
1644
1645 // friendship with equal operator.
1646 friend constexpr bool operator== (const Value& lhs, const Value& rhs);
1647
1648 // friendship with lower operator.
1649 friend constexpr bool operator< (const Value& lhs, const Value& rhs);
1650 };
1651
1658 constexpr bool operator== (const Value& lhs, const Value& rhs)
1659 {
1660 if (lhs.isNumber () && rhs.isNumber () && (lhs.index () != rhs.index ()))
1661 {
1662 if (lhs.isInt64 () && rhs.isUint64 ())
1663 {
1664 return lhs.getInt64 () >= 0 && uint64_t (lhs.getInt64 ()) == rhs.getUint64 ();
1665 }
1666 else if (lhs.isUint64 () && rhs.isInt64 ())
1667 {
1668 return rhs.getInt64 () >= 0 && uint64_t (rhs.getInt64 ()) == lhs.getUint64 () ;
1669 }
1670 else
1671 {
1672 return lhs.getDouble () == rhs.getDouble ();
1673 }
1674 }
1675
1676 return lhs.equal (rhs);
1677 }
1678
1685 constexpr bool operator!= (const Value& lhs, const Value& rhs)
1686 {
1687 return !(lhs == rhs);
1688 }
1689
1696 constexpr bool operator< (const Value& lhs, const Value& rhs)
1697 {
1698 if (lhs.isNumber () && rhs.isNumber () && (lhs.index () != rhs.index ()))
1699 {
1700 if (lhs.isInt64 () && rhs.isUint64 ())
1701 {
1702 return lhs.getInt64 () < 0 || uint64_t (lhs.getInt64 ()) < rhs.getUint64 ();
1703 }
1704 else if (lhs.isUint64 () && rhs.isInt64 ())
1705 {
1706 return rhs.getInt64 () >= 0 && lhs.getUint64 () < uint64_t (rhs.getInt64 ());
1707 }
1708 else
1709 {
1710 return lhs.getDouble () < rhs.getDouble ();
1711 }
1712 }
1713
1714 return lhs.lower (rhs);
1715 }
1716
1723 constexpr bool operator> (const Value& lhs, const Value& rhs)
1724 {
1725 return rhs < lhs;
1726 }
1727
1734 constexpr bool operator<= (const Value& lhs, const Value& rhs)
1735 {
1736 return !(rhs < lhs);
1737 }
1738
1745 constexpr bool operator>= (const Value& lhs, const Value& rhs)
1746 {
1747 return !(lhs < rhs);
1748 }
1749}
1750
1751#endif
value class.
Definition value.hpp:63
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:835
size_t size() const
returns the number of elements in the nested container.
Definition value.hpp:1155
void clear()
erases all elements in the nested container.
Definition value.hpp:1207
constexpr std::string & getString()
get variable held by value as a string value.
Definition value.hpp:955
void swap(Value &other)
exchanges the contents of the value with those of other.
Definition value.hpp:1366
constexpr bool isFalse() const
check if the content of the value is false.
Definition value.hpp:227
const Value & at(size_t pos) const
returns a reference to the element at specified location pos.
Definition value.hpp:1045
int deserialize(std::fstream &document)
deserialize a document.
Definition value.hpp:1441
int deserialize(std::iostream &document)
deserialize a document.
Definition value.hpp:1465
int serialize(std::ostream &document) const
serialize data.
Definition value.hpp:1489
constexpr int16_t getInt16() const
get variable held by value as a 16 bits integer.
Definition value.hpp:430
constexpr bool isInt() const
check if the variable held by value is a 32 bits integer value.
Definition value.hpp:545
size_t erase(const std::string &key)
removes member with the key equivalent to key.
Definition value.hpp:1260
friend constexpr bool operator<(const Value &lhs, const Value &rhs)
compare if lower than.
Definition value.hpp:1696
friend constexpr bool operator==(const Value &lhs, const Value &rhs)
compare if equal.
Definition value.hpp:1658
constexpr bool isInt64() const
check if the variable held by value is a 64 bits integer value.
Definition value.hpp:693
constexpr bool isUint16() const
check if the variable held by value is a 16 bits unsigned integer value.
Definition value.hpp:470
void popBack()
removes the last element of the nested container.
Definition value.hpp:1329
constexpr bool isTrue() const
check if the content of the value is true.
Definition value.hpp:217
constexpr const Array & getArray() const
get variable held by value as an array.
Definition value.hpp:984
Value & insert(Member &&member)
insert element in the nested container.
Definition value.hpp:1248
constexpr bool isArray() const
check if the variable held by value is an array.
Definition value.hpp:974
constexpr Value(const char *other)
constructs the value with the copy of the contents of a C-style string.
Definition value.hpp:100
constexpr const std::string & getString() const
get variable held by value as a string value.
Definition value.hpp:945
int deserialize(std::istringstream &document)
deserialize a document.
Definition value.hpp:1429
constexpr bool isUint64() const
check if the variable held by value is a 64 bits unsigned integer value.
Definition value.hpp:762
int deserialize(std::stringstream &document)
deserialize a document.
Definition value.hpp:1417
Value & pushBack(Value &&value)
appends element at the end of the nested container.
Definition value.hpp:1315
constexpr Object & getObject()
get variable held by value as an object.
Definition value.hpp:1023
int deserialize(std::ifstream &document)
deserialize a document.
Definition value.hpp:1453
constexpr bool isObject() const
check if the variable held by value is an object.
Definition value.hpp:1003
constexpr const Object & getObject() const
get variable held by value as an object.
Definition value.hpp:1013
void reserve(size_t cap)
increase the capacity of the nested container.
Definition value.hpp:1180
constexpr uint8_t getUint8() const
get variable held by value as a 8 bits unsigned integer.
Definition value.hpp:355
Value & operator[](size_t pos)
returns a reference to the element at specified location pos.
Definition value.hpp:1094
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:110
constexpr bool isInt16() const
check if the variable held by value is a 16 bits integer value.
Definition value.hpp:395
int deserialize(const std::string &document)
deserialize a document.
Definition value.hpp:1405
const Value & at(const std::string &key) const
returns a reference to the value that is mapped to a key.
Definition value.hpp:1075
constexpr bool getBool() const
get variable held by value as a boolean value.
Definition value.hpp:170
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:1393
Index
nested value type index.
Definition value.hpp:69
@ Integer64
Definition value.hpp:74
@ Unsigned64
Definition value.hpp:75
@ Real
Definition value.hpp:76
@ Boolean
Definition value.hpp:71
@ String
Definition value.hpp:77
@ ArrayValue
Definition value.hpp:78
@ Integer
Definition value.hpp:72
@ Null
Definition value.hpp:70
@ Unsigned
Definition value.hpp:73
@ ObjectValue
Definition value.hpp:79
Value & pushBack(const Value &value)
appends element at the end of the nested container.
Definition value.hpp:1299
virtual ~Value()=default
destroy the Value instance.
int deserialize(const char *document, size_t length)
deserialize a document.
Definition value.hpp:1380
Value & at(size_t pos)
returns a reference to the element at specified location pos.
Definition value.hpp:1034
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:280
bool contains(size_t pos) const
checks if the nested container contains an element at position pos.
Definition value.hpp:1339
constexpr double getDouble() const
get variable held by value as a double value.
Definition value.hpp:895
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:653
constexpr bool isBool() const
check if the variable held by value is a boolean value.
Definition value.hpp:160
Value & at(const std::string &key)
returns a reference to the value that is mapped to a key.
Definition value.hpp:1056
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:935
constexpr float getFloat() const
get variable held by value as a float value.
Definition value.hpp:845
constexpr bool isDouble() const
check if the variable held by value is a double value.
Definition value.hpp:885
Value & insert(const Member &member)
insert element in the nested container.
Definition value.hpp:1236
Value & emplaceBack(Args &&... args)
appends element at the end of the nested container.
Definition value.hpp:1284
constexpr uint64_t getUint64() const
get variable held by value as a 64 bits unsigned integer value.
Definition value.hpp:795
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:1130
constexpr bool isNumber() const
check if the variable held by value is a number value.
Definition value.hpp:236
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:151
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:320
bool contains(const std::string &key) const
checks if the nested container contains an element that is mapped to key.
Definition value.hpp:1349
constexpr int64_t getInt64() const
get variable held by value as a 64 bits integer value.
Definition value.hpp:722
int packWrite(std::ostream &document) const
serialize msgpack data.
Definition value.cpp:223
int deserialize(std::istream &document)
deserialize a document.
Definition value.hpp:1477
constexpr bool isInt8() const
check if the variable held by value is a 8 bits integer value.
Definition value.hpp:245
constexpr Array & getArray()
get variable held by value as an array.
Definition value.hpp:994
variant class.
Definition variant.hpp:375
constexpr bool lower(const Variant &rhs) const
check if lower than.
Definition variant.hpp:729
constexpr std::enable_if_t< is_unique< T, Ts... >::value, T & > get()
Definition variant.hpp:584
constexpr Variant()=default
default constructor.
constexpr Variant & operator=(const Variant &other)=default
copy assignment.
constexpr std::enable_if_t< is_unique< T, Ts... >::value &&std::is_constructible< T, Args &&... >::value, T & > set(Args &&...args)
Definition variant.hpp:532
constexpr bool equal(const Variant &rhs) const
check if equal.
Definition variant.hpp:715
const std::string key(65, 'a')
key.
Definition acceptor.hpp:32
std::pair< std::string, Value > Member
object member.
Definition value.hpp:54
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::vector< Value > Array
array.
Definition value.hpp:51
std::vector< Member > Object
object.
Definition value.hpp:57
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183
disambiguation tag to indicate that the contained object should be constructed in-place.
Definition traits.hpp:57