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 <iostream>
34#include <sstream>
35#include <fstream>
36#include <string>
37#include <vector>
38#include <limits>
39
40// C.
41#include <cstdint>
42#include <cmath>
43
44namespace join
45{
47 class Value;
48
50 using Array = std::vector<Value>;
51
53 using Member = std::pair<Value, Value>;
54
56 using Object = std::vector<Member>;
57
61 class Value
62 : 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 {
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 ||
239 index () == Real;
240 }
241
246 constexpr bool isInt8 () const
247 {
248 switch (index ())
249 {
250 case Integer:
251 return get<Integer> () >= static_cast<int32_t> (std::numeric_limits<int8_t>::min ()) &&
252 get<Integer> () <= static_cast<int32_t> (std::numeric_limits<int8_t>::max ());
253
254 case Unsigned:
255 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<int8_t>::max ());
256
257 case Integer64:
258 return get<Integer64> () >= static_cast<int64_t> (std::numeric_limits<int8_t>::min ()) &&
259 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<int8_t>::max ());
260
261 case Unsigned64:
262 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<int8_t>::max ());
263
264 case Real:
265 return std::trunc (get<Real> ()) == get<Real> () &&
266 get<Real> () >= static_cast<double> (std::numeric_limits<int8_t>::min ()) &&
267 get<Real> () <= static_cast<double> (std::numeric_limits<int8_t>::max ());
268
269 default:
270 break;
271 }
272
273 return false;
274 }
275
281 constexpr int8_t getInt8 () const
282 {
283 if (isInt8 ())
284 {
285 switch (index ())
286 {
287 case Integer:
288 return static_cast<int8_t> (get<Integer> ());
289
290 case Unsigned:
291 return static_cast<int8_t> (get<Unsigned> ());
292
293 case Integer64:
294 return static_cast<int8_t> (get<Integer64> ());
295
296 case Unsigned64:
297 return static_cast<int8_t> (get<Unsigned64> ());
298
299 default:
300 return static_cast<int8_t> (get<Real> ());
301 }
302 }
303
304 throw std::bad_cast ();
305 }
306
312 explicit operator int8_t () const
313 {
314 return getInt8 ();
315 }
316
321 constexpr bool isUint8 () const
322 {
323 switch (index ())
324 {
325 case Integer:
326 return get<Integer> () >= 0 &&
327 get<Integer> () <= static_cast<int32_t> (std::numeric_limits<uint8_t>::max ());
328
329 case Unsigned:
330 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<uint8_t>::max ());
331
332 case Integer64:
333 return get<Integer64> () >= 0 &&
334 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<uint8_t>::max ());
335
336 case Unsigned64:
337 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<uint8_t>::max ());
338
339 case Real:
340 return std::trunc (get<Real> ()) == get<Real> () && 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> () && get<Real> () >= 0 &&
490 get<Real> () <= static_cast<double> (std::numeric_limits<uint16_t>::max ());
491
492 default:
493 break;
494 }
495
496 return false;
497 }
498
504 constexpr uint16_t getUint16 () const
505 {
506 if (isUint16 ())
507 {
508 switch (index ())
509 {
510 case Integer:
511 return static_cast<uint16_t> (get<Integer> ());
512
513 case Unsigned:
514 return static_cast<uint16_t> (get<Unsigned> ());
515
516 case Integer64:
517 return static_cast<uint16_t> (get<Integer64> ());
518
519 case Unsigned64:
520 return static_cast<uint16_t> (get<Unsigned64> ());
521
522 default:
523 return static_cast<uint16_t> (get<Real> ());
524 }
525 }
526
527 throw std::bad_cast ();
528 }
529
535 explicit operator uint16_t () const
536 {
537 return getUint16 ();
538 }
539
544 constexpr bool isInt () const
545 {
546 switch (index ())
547 {
548 case Integer:
549 return true;
550
551 case Unsigned:
552 return get<Unsigned> () <= static_cast<uint32_t> (std::numeric_limits<int32_t>::max ());
553
554 case Integer64:
555 return get<Integer64> () >= static_cast<int64_t> (std::numeric_limits<int32_t>::min ()) &&
556 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<int32_t>::max ());
557
558 case Unsigned64:
559 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<int32_t>::max ());
560
561 case Real:
562 return std::trunc (get<Real> ()) == get<Real> () &&
563 get<Real> () >= static_cast<double> (std::numeric_limits<int32_t>::min ()) &&
564 get<Real> () <= static_cast<double> (std::numeric_limits<int32_t>::max ());
565
566 default:
567 break;
568 }
569
570 return false;
571 }
572
578 constexpr int32_t getInt () const
579 {
580 if (isInt ())
581 {
582 switch (index ())
583 {
584 case Integer:
585 return get<Integer> ();
586
587 case Unsigned:
588 return static_cast<int32_t> (get<Unsigned> ());
589
590 case Integer64:
591 return static_cast<int32_t> (get<Integer64> ());
592
593 case Unsigned64:
594 return static_cast<int32_t> (get<Unsigned64> ());
595
596 default:
597 return static_cast<int32_t> (get<Real> ());
598 }
599 }
600
601 throw std::bad_cast ();
602 }
603
609 explicit operator int32_t () const
610 {
611 return getInt ();
612 }
613
618 constexpr bool isUint () const
619 {
620 switch (index ())
621 {
622 case Integer:
623 return get<Integer> () >= 0;
624
625 case Unsigned:
626 return true;
627
628 case Integer64:
629 return get<Integer64> () >= 0 &&
630 get<Integer64> () <= static_cast<int64_t> (std::numeric_limits<uint32_t>::max ());
631
632 case Unsigned64:
633 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<uint32_t>::max ());
634
635 case Real:
636 return std::trunc (get<Real> ()) == get<Real> () && get<Real> () >= 0 &&
637 get<Real> () <= static_cast<double> (std::numeric_limits<uint32_t>::max ());
638
639 default:
640 break;
641 }
642
643 return false;
644 }
645
651 constexpr uint32_t getUint () const
652 {
653 if (isUint ())
654 {
655 switch (index ())
656 {
657 case Integer:
658 return static_cast<uint32_t> (get<Integer> ());
659
660 case Unsigned:
661 return get<Unsigned> ();
662
663 case Integer64:
664 return static_cast<uint32_t> (get<Integer64> ());
665
666 case Unsigned64:
667 return static_cast<uint32_t> (get<Unsigned64> ());
668
669 default:
670 return static_cast<uint32_t> (get<Real> ());
671 }
672 }
673
674 throw std::bad_cast ();
675 }
676
682 explicit operator uint32_t () const
683 {
684 return getUint ();
685 }
686
691 constexpr bool isInt64 () const
692 {
693 switch (index ())
694 {
695 case Integer:
696 case Unsigned:
697 case Integer64:
698 return true;
699
700 case Unsigned64:
701 return get<Unsigned64> () <= static_cast<uint64_t> (std::numeric_limits<int64_t>::max ());
702
703 case Real:
704 return std::trunc (get<Real> ()) == get<Real> () &&
705 get<Real> () >= static_cast<double> (std::numeric_limits<int64_t>::min ()) &&
706 get<Real> () < static_cast<double> (std::numeric_limits<int64_t>::max ());
707
708 default:
709 break;
710 }
711
712 return false;
713 }
714
720 constexpr int64_t getInt64 () const
721 {
722 if (isInt64 ())
723 {
724 switch (index ())
725 {
726 case Integer:
727 return static_cast<int64_t> (get<Integer> ());
728
729 case Unsigned:
730 return static_cast<int64_t> (get<Unsigned> ());
731
732 case Integer64:
733 return get<Integer64> ();
734
735 case Unsigned64:
736 return static_cast<int64_t> (get<Unsigned64> ());
737
738 default:
739 return static_cast<int64_t> (get<Real> ());
740 }
741 }
742
743 throw std::bad_cast ();
744 }
745
751 explicit operator int64_t () const
752 {
753 return getInt64 ();
754 }
755
760 constexpr bool isUint64 () const
761 {
762 switch (index ())
763 {
764 case Integer:
765 return get<Integer> () >= 0;
766
767 case Unsigned:
768 return true;
769
770 case Integer64:
771 return get<Integer64> () >= 0;
772
773 case Unsigned64:
774 return true;
775
776 case Real:
777 return std::trunc (get<Real> ()) == get<Real> () && get<Real> () >= 0 &&
778 get<Real> () < static_cast<double> (std::numeric_limits<uint64_t>::max ());
779
780 default:
781 break;
782 }
783
784 return false;
785 }
786
792 constexpr uint64_t getUint64 () const
793 {
794 if (isUint64 ())
795 {
796 switch (index ())
797 {
798 case Integer:
799 return static_cast<uint64_t> (get<Integer> ());
800
801 case Unsigned:
802 return static_cast<uint64_t> (get<Unsigned> ());
803
804 case Integer64:
805 return static_cast<uint64_t> (get<Integer64> ());
806
807 case Unsigned64:
808 return get<Unsigned64> ();
809
810 default:
811 return static_cast<uint64_t> (get<Real> ());
812 }
813 }
814
815 throw std::bad_cast ();
816 }
817
823 explicit operator uint64_t () const
824 {
825 return getUint64 ();
826 }
827
832 constexpr bool isFloat () const
833 {
834 return isNumber ();
835 }
836
842 constexpr float getFloat () const
843 {
844 switch (index ())
845 {
846 case Integer:
847 return static_cast<float> (get<Integer> ());
848
849 case Unsigned:
850 return static_cast<float> (get<Unsigned> ());
851
852 case Integer64:
853 return static_cast<float> (get<Integer64> ());
854
855 case Unsigned64:
856 return static_cast<float> (get<Unsigned64> ());
857
858 case Real:
859 return static_cast<float> (get<Real> ());
860
861 default:
862 break;
863 }
864
865 throw std::bad_cast ();
866 }
867
873 explicit operator float () const
874 {
875 return getFloat ();
876 }
877
882 constexpr bool isDouble () const
883 {
884 return isNumber ();
885 }
886
892 constexpr double getDouble () const
893 {
894 switch (index ())
895 {
896 case Integer:
897 return static_cast<double> (get<Integer> ());
898
899 case Unsigned:
900 return static_cast<double> (get<Unsigned> ());
901
902 case Integer64:
903 return static_cast<double> (get<Integer64> ());
904
905 case Unsigned64:
906 return static_cast<double> (get<Unsigned64> ());
907
908 case Real:
909 return get<Real> ();
910
911 default:
912 break;
913 }
914
915 throw std::bad_cast ();
916 }
917
923 explicit operator double () const
924 {
925 return getDouble ();
926 }
927
932 constexpr bool isString () const
933 {
934 return index () == String;
935 }
936
942 constexpr const std::string& getString () const
943 {
944 return get<String> ();
945 }
946
952 constexpr std::string& getString ()
953 {
954 return get<String> ();
955 }
956
962 explicit operator const char* () const
963 {
964 return getString ().c_str ();
965 }
966
971 constexpr bool isArray () const
972 {
973 return index () == ArrayValue;
974 }
975
981 constexpr const Array& getArray () const
982 {
983 return get<ArrayValue> ();
984 }
985
991 constexpr Array& getArray ()
992 {
993 return get<ArrayValue> ();
994 }
995
1000 constexpr bool isObject () const
1001 {
1002 return index () == ObjectValue;
1003 }
1004
1010 constexpr const Object& getObject () const
1011 {
1012 return get<ObjectValue> ();
1013 }
1014
1020 constexpr Object& getObject ()
1021 {
1022 return get<ObjectValue> ();
1023 }
1024
1031 Value& at (const Value& key)
1032 {
1033 if (index () == ArrayValue)
1034 {
1035 return get<ArrayValue> ().at (key.getUint64 ());
1036 }
1037
1038 for (auto& member : get<ObjectValue> ())
1039 {
1040 if (member.first == key)
1041 {
1042 return member.second;
1043 }
1044 }
1045
1046 throw std::out_of_range ("invalid key");
1047 }
1048
1055 const Value& at (const Value& key) const
1056 {
1057 if (index () == ArrayValue)
1058 {
1059 return get<ArrayValue> ().at (key.getUint64 ());
1060 }
1061
1062 for (auto const& member : get<ObjectValue> ())
1063 {
1064 if (member.first == key)
1065 {
1066 return member.second;
1067 }
1068 }
1069
1070 throw std::out_of_range ("invalid key");
1071 }
1072
1080 {
1081 if (index () == ArrayValue)
1082 {
1083 return get<ArrayValue> ()[key.getUint64 ()];
1084 }
1085
1086 if (index () == Null)
1087 {
1089 }
1090
1091 for (auto& member : get<ObjectValue> ())
1092 {
1093 if (member.first == key)
1094 {
1095 return member.second;
1096 }
1097 }
1098
1099 get<ObjectValue> ().emplace_back (key, nullptr);
1100
1101 return get<ObjectValue> ().back ().second;
1102 }
1103
1109 bool empty () const
1110 {
1111 switch (index ())
1112 {
1113 case String:
1114 return get<String> ().empty ();
1115
1116 case ArrayValue:
1117 return get<ArrayValue> ().empty ();
1118
1119 case ObjectValue:
1120 return get<ObjectValue> ().empty ();
1121
1122 default:
1123 break;
1124 }
1125
1126 throw std::bad_cast ();
1127 }
1128
1134 size_t size () const
1135 {
1136 switch (index ())
1137 {
1138 case String:
1139 return get<String> ().size ();
1140
1141 case ArrayValue:
1142 return get<ArrayValue> ().size ();
1143
1144 case ObjectValue:
1145 return get<ObjectValue> ().size ();
1146
1147 default:
1148 break;
1149 }
1150
1151 throw std::bad_cast ();
1152 }
1153
1159 void reserve (size_t cap)
1160 {
1161 switch (index ())
1162 {
1163 case String:
1164 get<String> ().reserve (cap);
1165 return;
1166
1167 case ArrayValue:
1168 get<ArrayValue> ().reserve (cap);
1169 return;
1170
1171 case ObjectValue:
1172 get<ObjectValue> ().reserve (cap);
1173 return;
1174
1175 default:
1176 break;
1177 }
1178
1179 throw std::bad_cast ();
1180 }
1181
1186 void clear ()
1187 {
1188 switch (index ())
1189 {
1190 case String:
1191 get<String> ().clear ();
1192 return;
1193
1194 case ArrayValue:
1195 get<ArrayValue> ().clear ();
1196 return;
1197
1198 case ObjectValue:
1199 get<ObjectValue> ().clear ();
1200 return;
1201
1202 default:
1203 break;
1204 }
1205
1206 throw std::bad_cast ();
1207 }
1208
1215 Value& insert (const Member& member)
1216 {
1217 get<ObjectValue> ().push_back (member);
1218 return get<ObjectValue> ().back ().second;
1219 }
1220
1227 Value& insert (Member&& member)
1228 {
1229 get<ObjectValue> ().emplace_back (std::move (member));
1230 return get<ObjectValue> ().back ().second;
1231 }
1232
1239 size_t erase (const Value& key)
1240 {
1241 auto beg = get<ObjectValue> ().begin ();
1242 auto end = get<ObjectValue> ().end ();
1243
1244 for (auto it = beg; it != end; ++it)
1245 {
1246 if (it->first == key)
1247 {
1248 get<ObjectValue> ().erase (it);
1249 return 1;
1250 }
1251 }
1252
1253 return 0;
1254 }
1255
1262 template <typename... Args>
1263 Value& emplaceBack (Args&&... args)
1264 {
1265 if (index () == Null)
1266 {
1267 set<ArrayValue> ();
1268 }
1269 return get<ArrayValue> ().emplace_back (std::forward<Args> (args)...);
1270 }
1271
1278 Value& pushBack (const Value& value)
1279 {
1280 if (index () == Null)
1281 {
1282 set<ArrayValue> ();
1283 }
1284 get<ArrayValue> ().push_back (value);
1285 return get<ArrayValue> ().back ();
1286 }
1287
1295 {
1296 if (index () == Null)
1297 {
1298 set<ArrayValue> ();
1299 }
1300 get<ArrayValue> ().emplace_back (std::move (value));
1301 return get<ArrayValue> ().back ();
1302 }
1303
1308 void popBack ()
1309 {
1310 get<ArrayValue> ().pop_back ();
1311 }
1312
1318 bool contains (const Value& key) const
1319 {
1320 if (index () == ArrayValue)
1321 {
1322 return get<ArrayValue> ().size () > key.getUint64 ();
1323 }
1324
1325 for (auto const& member : get<ObjectValue> ())
1326 {
1327 if (member.first == key)
1328 {
1329 return true;
1330 }
1331 }
1332
1333 return false;
1334 }
1335
1340 void swap (Value& other)
1341 {
1342 Value temp (std::move (*this));
1343 *this = std::move (other);
1344 other = std::move (temp);
1345 }
1346
1353 template <typename Reader>
1354 int deserialize (const char* document, size_t length)
1355 {
1356 Reader reader (*this);
1357 return reader.deserialize (document, length);
1358 }
1359
1366 template <typename Reader>
1367 int deserialize (const char* first, const char* last)
1368 {
1369 Reader reader (*this);
1370 return reader.deserialize (first, last);
1371 }
1372
1378 template <typename Reader>
1379 int deserialize (const std::string& document)
1380 {
1381 Reader reader (*this);
1382 return reader.deserialize (document);
1383 }
1384
1390 template <typename Reader>
1391 int deserialize (std::stringstream& document)
1392 {
1393 Reader reader (*this);
1394 return reader.deserialize (document);
1395 }
1396
1402 template <typename Reader>
1403 int deserialize (std::istringstream& document)
1404 {
1405 Reader reader (*this);
1406 return reader.deserialize (document);
1407 }
1408
1414 template <typename Reader>
1415 int deserialize (std::fstream& document)
1416 {
1417 Reader reader (*this);
1418 return reader.deserialize (document);
1419 }
1420
1426 template <typename Reader>
1427 int deserialize (std::ifstream& document)
1428 {
1429 Reader reader (*this);
1430 return reader.deserialize (document);
1431 }
1432
1438 template <typename Reader>
1439 int deserialize (std::iostream& document)
1440 {
1441 Reader reader (*this);
1442 return reader.deserialize (document);
1443 }
1444
1450 template <typename Reader>
1451 int deserialize (std::istream& document)
1452 {
1453 Reader reader (*this);
1454 return reader.deserialize (document);
1455 }
1456
1462 template <typename Writer>
1463 int serialize (std::ostream& document) const
1464 {
1465 Writer writer (document);
1466 return writer.serialize (*this);
1467 }
1468
1475 int jsonRead (const char* document, size_t length);
1476
1483 int jsonRead (const char* first, const char* last);
1484
1490 int jsonRead (const std::string& document);
1491
1497 int jsonRead (std::stringstream& document);
1498
1504 int jsonRead (std::istringstream& document);
1505
1511 int jsonRead (std::fstream& document);
1512
1518 int jsonRead (std::ifstream& document);
1519
1525 int jsonRead (std::iostream& document);
1526
1532 int jsonRead (std::istream& document);
1533
1539 int jsonWrite (std::ostream& document, size_t indentation = 0) const;
1540
1546 int jsonCanonicalize (std::ostream& document);
1547
1554 int packRead (const char* document, size_t length);
1555
1562 int packRead (const char* first, const char* last);
1563
1569 int packRead (const std::string& document);
1570
1576 int packRead (std::stringstream& document);
1577
1583 int packRead (std::istringstream& document);
1584
1590 int packRead (std::fstream& document);
1591
1597 int packRead (std::ifstream& document);
1598
1604 int packRead (std::iostream& document);
1605
1611 int packRead (std::istream& document);
1612
1617 int packWrite (std::ostream& document) const;
1618
1619 // friendship with equal operator.
1620 friend constexpr bool operator== (const Value& lhs, const Value& rhs);
1621
1622 // friendship with lower operator.
1623 friend constexpr bool operator< (const Value& lhs, const Value& rhs);
1624 };
1625
1632 constexpr bool operator== (const Value& lhs, const Value& rhs)
1633 {
1634 if (lhs.isNumber () && rhs.isNumber () && (lhs.index () != rhs.index ()))
1635 {
1636 if (lhs.isInt64 () && rhs.isUint64 ())
1637 {
1638 return lhs.getInt64 () >= 0 && uint64_t (lhs.getInt64 ()) == rhs.getUint64 ();
1639 }
1640 else if (lhs.isUint64 () && rhs.isInt64 ())
1641 {
1642 return rhs.getInt64 () >= 0 && uint64_t (rhs.getInt64 ()) == lhs.getUint64 ();
1643 }
1644 else
1645 {
1646 return lhs.getDouble () == rhs.getDouble ();
1647 }
1648 }
1649
1650 return lhs.equal (rhs);
1651 }
1652
1659 constexpr bool operator!= (const Value& lhs, const Value& rhs)
1660 {
1661 return !(lhs == rhs);
1662 }
1663
1670 constexpr bool operator< (const Value& lhs, const Value& rhs)
1671 {
1672 if (lhs.isNumber () && rhs.isNumber () && (lhs.index () != rhs.index ()))
1673 {
1674 if (lhs.isInt64 () && rhs.isUint64 ())
1675 {
1676 return lhs.getInt64 () < 0 || uint64_t (lhs.getInt64 ()) < rhs.getUint64 ();
1677 }
1678 else if (lhs.isUint64 () && rhs.isInt64 ())
1679 {
1680 return rhs.getInt64 () >= 0 && lhs.getUint64 () < uint64_t (rhs.getInt64 ());
1681 }
1682 else
1683 {
1684 return lhs.getDouble () < rhs.getDouble ();
1685 }
1686 }
1687
1688 return lhs.lower (rhs);
1689 }
1690
1697 constexpr bool operator> (const Value& lhs, const Value& rhs)
1698 {
1699 return rhs < lhs;
1700 }
1701
1708 constexpr bool operator<= (const Value& lhs, const Value& rhs)
1709 {
1710 return !(rhs < lhs);
1711 }
1712
1719 constexpr bool operator>= (const Value& lhs, const Value& rhs)
1720 {
1721 return !(lhs < rhs);
1722 }
1723}
1724
1725#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:578
constexpr bool isFloat() const
check if the variable held by value is a float value.
Definition value.hpp:832
size_t size() const
returns the number of elements in the nested container.
Definition value.hpp:1134
void clear()
erases all elements in the nested container.
Definition value.hpp:1186
constexpr std::string & getString()
get variable held by value as a string value.
Definition value.hpp:952
void swap(Value &other)
exchanges the contents of the value with those of other.
Definition value.hpp:1340
constexpr bool isFalse() const
check if the content of the value is false.
Definition value.hpp:227
int deserialize(std::fstream &document)
deserialize a document.
Definition value.hpp:1415
const Value & at(const Value &key) const
returns a reference to the value at position pos or mapped to key.
Definition value.hpp:1055
int deserialize(std::iostream &document)
deserialize a document.
Definition value.hpp:1439
int serialize(std::ostream &document) const
serialize data.
Definition value.hpp:1463
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:544
friend constexpr bool operator<(const Value &lhs, const Value &rhs)
compare if lower than.
Definition value.hpp:1670
friend constexpr bool operator==(const Value &lhs, const Value &rhs)
compare if equal.
Definition value.hpp:1632
constexpr bool isInt64() const
check if the variable held by value is a 64 bits integer value.
Definition value.hpp:691
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:1308
constexpr bool isTrue() const
check if the content of the value is true.
Definition value.hpp:217
Value & operator[](const Value &key)
returns a reference to the value at position pos or mapped to key.
Definition value.hpp:1079
constexpr const Array & getArray() const
get variable held by value as an array.
Definition value.hpp:981
Value & insert(Member &&member)
insert element in the nested container.
Definition value.hpp:1227
constexpr bool isArray() const
check if the variable held by value is an array.
Definition value.hpp:971
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:942
int deserialize(std::istringstream &document)
deserialize a document.
Definition value.hpp:1403
constexpr bool isUint64() const
check if the variable held by value is a 64 bits unsigned integer value.
Definition value.hpp:760
int deserialize(std::stringstream &document)
deserialize a document.
Definition value.hpp:1391
Value & pushBack(Value &&value)
appends element at the end of the nested container.
Definition value.hpp:1294
constexpr Object & getObject()
get variable held by value as an object.
Definition value.hpp:1020
int deserialize(std::ifstream &document)
deserialize a document.
Definition value.hpp:1427
constexpr bool isObject() const
check if the variable held by value is an object.
Definition value.hpp:1000
constexpr const Object & getObject() const
get variable held by value as an object.
Definition value.hpp:1010
void reserve(size_t cap)
increase the capacity of the nested container.
Definition value.hpp:1159
bool contains(const Value &key) const
checks if the nested container contains an element that is mapped to key.
Definition value.hpp:1318
constexpr uint8_t getUint8() const
get variable held by value as a 8 bits unsigned integer.
Definition value.hpp:355
size_t erase(const Value &key)
removes member with the key equivalent to key.
Definition value.hpp:1239
constexpr bool isUint() const
check if the variable held by value is a 32 bits unsigned integer value.
Definition value.hpp:618
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:1379
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:1367
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:1278
Value & at(const Value &key)
returns a reference to the value at position pos or mapped to key.
Definition value.hpp:1031
virtual ~Value()=default
destroy the Value instance.
int deserialize(const char *document, size_t length)
deserialize a document.
Definition value.hpp:1354
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:281
constexpr double getDouble() const
get variable held by value as a double value.
Definition value.hpp:892
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:651
constexpr bool isBool() const
check if the variable held by value is a boolean value.
Definition value.hpp:160
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:932
constexpr float getFloat() const
get variable held by value as a float value.
Definition value.hpp:842
constexpr bool isDouble() const
check if the variable held by value is a double value.
Definition value.hpp:882
Value & insert(const Member &member)
insert element in the nested container.
Definition value.hpp:1215
Value & emplaceBack(Args &&... args)
appends element at the end of the nested container.
Definition value.hpp:1263
constexpr uint64_t getUint64() const
get variable held by value as a 64 bits unsigned integer value.
Definition value.hpp:792
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:1109
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:504
constexpr bool isUint8() const
check if the variable held by value is a 8 bits unsigned integer value.
Definition value.hpp:321
constexpr int64_t getInt64() const
get variable held by value as a 64 bits integer value.
Definition value.hpp:720
int packWrite(std::ostream &document) const
serialize msgpack data.
Definition value.cpp:223
int deserialize(std::istream &document)
deserialize a document.
Definition value.hpp:1451
constexpr bool isInt8() const
check if the variable held by value is a 8 bits integer value.
Definition value.hpp:246
constexpr Array & getArray()
get variable held by value as an array.
Definition value.hpp:991
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 BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is lower or equal.
Definition endpoint.hpp:234
bool operator==(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:186
bool operator!=(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoints are not equal.
Definition endpoint.hpp:198
bool operator>=(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is greater or equal.
Definition endpoint.hpp:246
std::pair< Value, Value > Member
object member.
Definition value.hpp:53
bool operator<(const BasicDatagramSocket< Protocol > &a, const BasicDatagramSocket< Protocol > &b) noexcept
compare if socket handle is inferior.
Definition datagram_socket.hpp:394
std::vector< Value > Array
array.
Definition value.hpp:50
std::vector< Member > Object
object.
Definition value.hpp:56
bool operator>(const BasicLinkLayerEndpoint< Protocol > &a, const BasicLinkLayerEndpoint< Protocol > &b) noexcept
compare if endpoint is greater.
Definition endpoint.hpp:222
disambiguation tag to indicate that the contained object should be constructed in-place.
Definition traits.hpp:57