-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqc-json.hpp
1512 lines (1319 loc) · 45.4 KB
/
qc-json.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
///
/// QC JSON 2.0.2
///
/// Quick and clean JSON5 header library for C++20
///
/// Austin Quick : 2019 - 2022
///
/// https://github.com/daskie/qc-json
///
/// This header provides a DOM interface for encoding and decoding JSON5
///
/// Uses `qc-json-encode.hpp` to do the encoding and `qc-json-decode.hpp` to do the decoding
///
/// See the README for more info and examples!
///
#include <cstring>
#include <algorithm>
#include <concepts>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <variant>
#include <qc-json-decode.hpp>
#include <qc-json-encode.hpp>
namespace qc::json
{
///
/// Required for low-order bit packing
///
static_assert(__STDCPP_DEFAULT_NEW_ALIGNMENT__ >= 8);
///
/// This will be thrown when attempting to access a value as the wrong type
///
struct TypeError : Error {};
///
/// An essentially cosmetic means of making certain unsafe methods more clearly so
///
/// The user may choose to `using enum qc::json::Safety` to reduce code verbosity
///
enum Safety { safe, unsafe };
///
/// The type of the JSON value
///
enum class Type : uint8_t
{
null = 0b000u,
object = 0b001u,
array = 0b010u,
string = 0b011u,
integer = 0b100u,
unsigner = 0b101u,
floater = 0b110u,
boolean = 0b111u
};
// Forward declaration
class Value;
///
/// Convenience type alias
///
/// The internal representation for objects is `std::map<string, qc::json::value>`
///
using Object = std::map<string, Value>;
///
/// Convenience type alias
///
/// The internal representation for arrays is `std::vector<qc::json::Value>`
///
using Array = std::vector<Value>;
///
/// Specialize `qc::json::ValueFrom` to enable `Value` construction from custom types
///
/// Example:
/// template <>
/// struct qc::json::ValueFrom<std::pair<int, int>> {
/// qc::json::Value operator()(const std::pair<int, int> & v) const {
/// return qc::json::makeArray(v.first, f.second);
/// }
/// };
///
template <typename T> struct ValueFrom;
///
/// Concept describing a type for which `qc::json::ValueFrom` has been specialized
///
template <typename T> concept ValueFromAble = requires (T v) { { ::qc::json::ValueFrom<T>{}(v) } -> std::same_as<Value>; };
///
/// Specialize `qc::json::ValueTo` to enable `Value::as` for custom types
///
/// Example:
/// template <>
/// struct qc::json::ValueTo<std::pair<int, int>> {
/// std::pair<int, int> operator()(const qc::json::Value & v) const {
/// const qc::json::Array & arr{v.asArray()};
/// return {arr.at(0).get<int>(), arr.at(1).get<int>()};
/// }
/// };
///
template <typename T> struct ValueTo;
///
/// Concept describing a type for which `qc::json::ValueTo` has been specialized
///
template <typename T> concept ValueToAble = requires (Value v) { { ::qc::json::ValueTo<T>{}(v) } -> std::same_as<T>; };
///
/// Represents one JSON value, which can be an object, array, string, number, boolean, or null
///
class Value
{
public: //--------------------------------------------------------------
///
/// Constructs a null value
///
Value(std::nullptr_t = nullptr) noexcept {}
///
/// @param val the value whith which to be constructed
///
Value(Object && val, Density density = Density::unspecified) noexcept;
Value(Array && val, Density density = Density::unspecified) noexcept;
Value(string && val) noexcept;
Value(string_view val) noexcept;
Value(const char * val) noexcept;
Value(char * val) noexcept;
Value(char val) noexcept;
Value(int64_t val) noexcept;
Value(int32_t val) noexcept;
Value(int16_t val) noexcept;
Value(int8_t val) noexcept;
Value(uint64_t val) noexcept;
Value(uint32_t val) noexcept;
Value(uint16_t val) noexcept;
Value(uint8_t val) noexcept;
Value(double val) noexcept;
Value(float val) noexcept;
Value(bool val) noexcept;
///
/// Attempts to construct a value from a custom type `T` using a specialized `qc::json::ValueFrom` function,
/// details of which can be found below
///
/// @tparam T the custom type
/// @param val the custom type value
///
template <ValueFromAble T> Value(const T & val);
Value(const Value &) = delete;
Value(Value && other) noexcept;
///
/// Assigns a new value to the json value
///
/// @param val
/// @return this
///
Value & operator=(Object && val);
Value & operator=(Array && val);
Value & operator=(string && val);
Value & operator=(string_view val);
Value & operator=(const char * val);
Value & operator=(char val);
Value & operator=(int64_t val);
Value & operator=(int32_t val);
Value & operator=(int16_t val);
Value & operator=(int8_t val);
Value & operator=(uint64_t val);
Value & operator=(uint32_t val);
Value & operator=(uint16_t val);
Value & operator=(uint8_t val);
Value & operator=(double val);
Value & operator=(float val);
Value & operator=(bool val);
Value & operator=(nullptr_t);
Value & operator=(const Value &) = delete;
Value & operator=(Value && other) noexcept;
~Value() noexcept;
///
/// @return the type of the value
///
Type type() const noexcept;
///
/// @return the density of the object or array, or `unspecified` if not an object or array
///
Density density() const noexcept;
///
/// Sets the density of the object or array
///
/// @param density the new density
///
void setDensity(Density density) noexcept;
///
/// @return whether the value is an object
///
bool isObject() const noexcept;
///
/// @return whether the value is an array
///
bool isArray() const noexcept;
///
/// @return whether the value is a string
///
bool isString() const noexcept;
///
/// @return whether the value is a number
///
bool isNumber() const noexcept;
///
/// @return whether the value is a signed integer
///
bool isInteger() const noexcept;
///
/// @return whether the value is an unsigned integer
///
bool isUnsigner() const noexcept;
///
/// @return whether the value is a floater
///
bool isFloater() const noexcept;
///
/// @return whether the value is a boolean
///
bool isBoolean() const noexcept;
///
/// @return whether the value is null
///
bool isNull() const noexcept;
///
/// Determines if the value type is compatible with `T`, which is to say calling `get<T>()` would be valid. See
/// the `get` method docs below for more details
///
/// @tparam T the type in question, e.g. `int` or `std::string`
/// @return whether the value type is compatible with type `T`
///
template <typename T> bool is() const noexcept;
///
/// @tparam safety whether to check if this value is actually an object
/// @return this value as an object
/// @throw `TypeError` if this value is not an object and safety is enabled
///
template <Safety safety = safe> Object & asObject() noexcept(safety == unsafe);
template <Safety safety = safe> const Object & asObject() const noexcept(safety == unsafe);
///
/// @tparam safety whether to check if this value is actually an array
/// @return this value as an array
/// @throw `TypeError` if this value is not an array and safety is enabled
///
template <Safety safety = safe> Array & asArray() noexcept(safety == unsafe);
template <Safety safety = safe> const Array & asArray() const noexcept(safety == unsafe);
///
/// @tparam safety whether to check if this value is actually a string
/// @return this value as a string
/// @throw `TypeError` if this value is not a string and safety is enabled
///
template <Safety safety = safe> string & asString() noexcept(safety == unsafe);
template <Safety safety = safe> const string & asString() const noexcept(safety == unsafe);
///
/// @tparam safety whether to check if this value is actually a signed integer
/// @return this value as a signed integer
/// @throw `TypeError` if this value is not a signed integer and safety is enabled
///
template <Safety safety = safe> int64_t & asInteger() noexcept(safety == unsafe);
template <Safety safety = safe> const int64_t & asInteger() const noexcept(safety == unsafe);
///
/// @tparam safety whether to check if this value is actually an unsigned integer
/// @return this value as an unsigned integer
/// @throw `TypeError` if this value is not an unsigned integer and safety is enabled
///
template <Safety safety = safe> uint64_t & asUnsigner() noexcept(safety == unsafe);
template <Safety safety = safe> const uint64_t & asUnsigner() const noexcept(safety == unsafe);
///
/// @tparam safety whether to check if this value is actually a floater
/// @return this value as a floater
/// @throw `TypeError` if this value is not a floater and safety is enabled
///
template <Safety safety = safe> double & asFloater() noexcept(safety == unsafe);
template <Safety safety = safe> const double & asFloater() const noexcept(safety == unsafe);
///
/// @tparam safety whether to check if this value is actually a boolean
/// @return this value as a boolean
/// @throw `TypeError` if this value is not a boolean and safety is enabled
///
template <Safety safety = safe> bool & asBoolean() noexcept(safety == unsafe);
template <Safety safety = safe> const bool & asBoolean() const noexcept(safety == unsafe);
///
/// Retrieves the value as the given type
///
/// If the actual type does not match the requested type and safety is enabled, a `TypeError` is thrown
///
/// If `T` is `std::string`, this call is equivalent to `asString`, except a copy of the string is returnsd
///
/// If `T` is `std::string_view`, `const char *`, or `char *`, a view/pointer to the current string is returned
///
/// If `T` is `char`, the first/only character of the current string is returned. If the current string has more
/// then one character `TypeError` is thrown. Note that in c++ `char`, `signed char`, and `unsigned char` are
/// distinct types. Asking for a `signed char` or `unsigned char` will instead try to fetch a number of type
/// `int8_t` or `uint8_t` respectively
///
/// If `T` is a numeric type...
/// ...and the value is a positive integer, it may be accessed as:
/// - any floater type (`double`, `float`)
/// - any signed integer type (`int64_t`, `int32_t`, `int16_t`, `int8_t`), but only if it can fit
/// - any unsigned integer type (`uint64_t`, `uint32_t`, `uint16_t`, `uint8_t`), but only if it can fit
/// ...and the value is a negative integer, it may be accessed as:
/// - any floater type (`double`, `float`)
/// - any signed integer type (`int64_t`, `int32_t`, `int16_t`, `int8_t`), but only if it can fit
/// ...and the value is not an integer, it may only be accessed as a floater (`double`, `float`)
///
/// If `T` is `bool`, this call is equivalent to `asBoolean` by value
///
/// If `T` is `nullptr_t` simply returns `nullptr`
///
/// If `T` is an unrecognized type, then we attempt to use the specialized `qc::json::ValueTo` struct, details
/// of which can be found below
///
template <typename T, Safety safety = safe> T get() const;
///
/// @return whether the value has a comment
///
bool hasComment() const noexcept;
///
/// @return the value's comment, or `nullptr` if it has no comment
///
string * comment() noexcept;
const string * comment() const noexcept;
///
/// @param str the new comment
///
void setComment(string && str);
void setComment(string_view str);
void setComment(const char * str);
///
/// Removes the value's comment
///
/// @return ownership of the value's comment
///
std::unique_ptr<string> removeComment() noexcept;
///
/// Compares if two values are equivalent, that is they have the same type and value
///
/// The presence/content of comments is ignored
///
/// @param other the value to compare with
/// @return whether this is equivalent to the other value
///
bool operator==(const Value & other) const noexcept;
///
/// Directly compares if this value is equivalent to that provided
///
bool operator==(const Object & val) const noexcept;
bool operator==(const Array & val) const noexcept;
bool operator==(const string & val) const noexcept;
bool operator==(string_view val) const noexcept;
bool operator==(const char * val) const noexcept;
bool operator==(char val) const noexcept;
bool operator==(int64_t val) const noexcept;
bool operator==(int32_t val) const noexcept;
bool operator==(int16_t val) const noexcept;
bool operator==(int8_t val) const noexcept;
bool operator==(uint64_t val) const noexcept;
bool operator==(uint32_t val) const noexcept;
bool operator==(uint16_t val) const noexcept;
bool operator==(uint8_t val) const noexcept;
bool operator==(double val) const noexcept;
bool operator==(float val) const noexcept;
bool operator==(bool val) const noexcept;
bool operator==(nullptr_t) const noexcept;
private: //-------------------------------------------------------------
union
{
uintptr_t _ptrAndDensity;
string * _string;
int64_t _integer;
uint64_t _unsigner;
double _floater;
bool _boolean;
nullptr_t _null{};
};
uintptr_t _typeAndComment{};
void _setType(Type type);
template <typename T> void _setComment(T && str);
void _deleteValue();
void _deleteComment();
};
///
/// Efficiently creates an object from the given key and value arguments
///
/// @param key the first key, forwarded to `std::string` constructor
/// @param val the first value, forwarded to `qc::json::Value` constructor
/// @param more any number of additional key and value arguments
/// @return the created object
///
template <typename K, typename V, typename... MoreKVs> Object makeObject(K && key, V && val, MoreKVs &&... moreKVs);
Object makeObject();
///
/// Efficiently creates an array from the given value arguments
///
/// @param vals the values, each forwarded to `qc::json::Value` constructor
/// @return the created array
///
template <typename... Vs> Array makeArray(Vs &&... vals);
///
/// @param json the JSON string to decode
/// @return the decoded value of the JSON
/// @throw `DecodeError` if the JSON string is invalid or could otherwise not be parsed
///
Value decode(string_view json);
///
/// @param val the JSON value to encode
/// @param density the base density of the encoded JSON string
/// @param indentSpaces the number of spaces to insert per level of indentation
/// @param singleQuotes whether to use `'` instead of `"` for strings
/// @param identifiers whether to encode all eligible keys as identifiers instead of strings
/// @return an encoded JSON string of the given JSON value
/// @throw `EncodeError` if there was an issue encoding the JSON
///
string encode(const Value & val, Density density = Density::multiline, size_t indentSpaces = 4u, bool singleQuotes = false, bool identifiers = false);
///
/// Specialization of the encoder's `operator<<` for `Value`
/// @param encoder the encoder
/// @param val the JSON value to encode
/// @return `encoder`
/// @throw `EncodeError` if there was an issue encoding the JSON value
///
Encoder & operator<<(Encoder & encoder, const Value & val);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace qc::json
{
class _Composer
{
public: //--------------------------------------------------------------
struct State { Value * node; Container container; };
State object(State & outerState)
{
Value * innerNode;
switch (outerState.container)
{
case Container::object:
innerNode = &outerState.node->asObject<unsafe>().emplace(std::move(_key), Object{}).first->second;
break;
case Container::array:
innerNode = &outerState.node->asArray<unsafe>().emplace_back(Object{});
break;
default:
*outerState.node = Object{};
innerNode = outerState.node;
}
if (!_comment.empty())
{
innerNode->setComment(std::move(_comment));
}
return {innerNode, Container::object};
}
State array(State & outerState)
{
Value * innerNode;
switch (outerState.container)
{
case Container::object:
innerNode = &outerState.node->asObject<unsafe>().emplace(std::move(_key), Array{}).first->second;
break;
case Container::array:
innerNode = &outerState.node->asArray<unsafe>().emplace_back(Array{});
break;
default:
*outerState.node = Array{};
innerNode = outerState.node;
}
if (!_comment.empty())
{
innerNode->setComment(std::move(_comment));
}
return {innerNode, Container::array};
}
void key(const string_view k, State & /*state*/)
{
_key = k;
}
void end(const Density density, State && innerState, State & /*outerState*/)
{
switch (innerState.container)
{
case Container::object:
innerState.node->setDensity(density);
break;
case Container::array:
innerState.node->setDensity(density);
break;
default:
break;
}
_comment.clear();
}
template <typename T>
void val(const T v, State & state)
{
Value * composedVal;
switch (state.container)
{
case Container::object:
composedVal = &state.node->asObject<unsafe>().emplace(std::move(_key), v).first->second;
break;
case Container::array:
composedVal = &state.node->asArray<unsafe>().emplace_back(v);
break;
default:
*state.node = v;
composedVal = state.node;
}
if (!_comment.empty())
{
composedVal->setComment(std::move(_comment));
}
}
void comment(const string_view comment, State & /*state*/)
{
_comment = comment;
}
private: //-------------------------------------------------------------
string _key{};
string _comment{};
};
inline Value::Value(Object && val, const Density density) noexcept :
_ptrAndDensity{reinterpret_cast<uintptr_t>(new Object{std::move(val)}) | uintptr_t(density)},
_typeAndComment{uintptr_t(Type::object)}
{}
inline Value::Value(Array && val, const Density density) noexcept :
_ptrAndDensity{reinterpret_cast<uintptr_t>(new Array{std::move(val)}) | uintptr_t(density)},
_typeAndComment{uintptr_t(Type::array)}
{}
inline Value::Value(string && val) noexcept :
_string{new string{std::move(val)}},
_typeAndComment{uintptr_t(Type::string)}
{}
inline Value::Value(const string_view val) noexcept :
_string{new string{val}},
_typeAndComment{uintptr_t(Type::string)}
{}
inline Value::Value(const char * const val) noexcept :
Value(string_view{val})
{}
inline Value::Value(char * const val) noexcept :
Value(string_view{val})
{}
inline Value::Value(const char val) noexcept :
Value{string_view{&val, 1u}}
{}
inline Value::Value(const int64_t val) noexcept :
_integer{val},
_typeAndComment{uintptr_t(Type::integer)}
{}
inline Value::Value(const int32_t val) noexcept :
Value{int64_t{val}}
{}
inline Value::Value(const int16_t val) noexcept :
Value{int64_t{val}}
{}
inline Value::Value(const int8_t val) noexcept :
Value{int64_t{val}}
{}
inline Value::Value(const uint64_t val) noexcept :
_unsigner{val},
_typeAndComment{uintptr_t(Type::unsigner)}
{}
inline Value::Value(const uint32_t val) noexcept :
Value{uint64_t{val}}
{}
inline Value::Value(const uint16_t val) noexcept :
Value{uint64_t{val}}
{}
inline Value::Value(const uint8_t val) noexcept :
Value{uint64_t{val}}
{}
inline Value::Value(const double val) noexcept :
_floater{val},
_typeAndComment{uintptr_t(Type::floater)}
{}
inline Value::Value(const float val) noexcept :
Value{double{val}}
{}
inline Value::Value(const bool val) noexcept :
_boolean{val},
_typeAndComment{uintptr_t(Type::boolean)}
{}
template <ValueFromAble T>
inline Value::Value(const T & val) :
Value{::qc::json::ValueFrom<T>{}(val)}
{}
inline Value::Value(Value && other) noexcept :
_unsigner{std::exchange(other._unsigner, 0u)},
_typeAndComment{std::exchange(other._typeAndComment, 0u)}
{}
inline Value & Value::operator=(Object && val)
{
if (type() == Type::object)
{
asObject<unsafe>() = std::move(val);
}
else
{
_deleteValue();
_setType(Type::object);
_ptrAndDensity = reinterpret_cast<uintptr_t>(new Object{std::move(val)});
}
return *this;
}
inline Value & Value::operator=(Array && val)
{
if (type() == Type::array)
{
asArray<unsafe>() = std::move(val);
}
else
{
_deleteValue();
_setType(Type::array);
_ptrAndDensity = reinterpret_cast<uintptr_t>(new Array{std::move(val)});
}
return *this;
}
inline Value & Value::operator=(string && val)
{
if (type() == Type::string)
{
asString<unsafe>() = std::move(val);
}
else
{
_deleteValue();
_setType(Type::string);
_string = new string{std::move(val)};
}
return *this;
}
inline Value & Value::operator=(const string_view val)
{
if (type() == Type::string)
{
asString<unsafe>() = val;
}
else
{
_deleteValue();
_setType(Type::string);
_string = new string{val};
}
return *this;
}
inline Value & Value::operator=(const char * const val)
{
return *this = string_view{val};
}
inline Value & Value::operator=(const char val)
{
return *this = string_view{&val, 1u};
}
inline Value & Value::operator=(const int64_t val)
{
if (type() != Type::integer)
{
_deleteValue();
_setType(Type::integer);
}
_integer = val;
return *this;
}
inline Value & Value::operator=(const int32_t val)
{
return *this = int64_t{val};
}
inline Value & Value::operator=(const int16_t val)
{
return *this = int64_t{val};
}
inline Value & Value::operator=(const int8_t val)
{
return *this = int64_t{val};
}
inline Value & Value::operator=(const uint64_t val)
{
if (type() != Type::unsigner)
{
_deleteValue();
_setType(Type::unsigner);
}
_unsigner = val;
return *this;
}
inline Value & Value::operator=(const uint32_t val)
{
return *this = uint64_t{val};
}
inline Value & Value::operator=(const uint16_t val)
{
return *this = uint64_t{val};
}
inline Value & Value::operator=(const uint8_t val)
{
return *this = uint64_t{val};
}
inline Value & Value::operator=(const double val)
{
if (type() != Type::floater)
{
_deleteValue();
_setType(Type::floater);
}
_floater = val;
return *this;
}
inline Value & Value::operator=(const float val)
{
return *this = double{val};
}
inline Value & Value::operator=(const bool val)
{
if (type() != Type::boolean)
{
_deleteValue();
_setType(Type::boolean);
}
_boolean = val;
return *this;
}
inline Value & Value::operator=(const nullptr_t)
{
if (type() != Type::null)
{
_deleteValue();
_setType(Type::null);
}
_null = nullptr;
return *this;
}
inline Value & Value::operator=(Value && other) noexcept
{
_deleteValue();
_deleteComment();
_unsigner = std::exchange(other._unsigner, 0u);
_typeAndComment = std::exchange(other._typeAndComment, 0u);
return *this;
}
inline Value::~Value() noexcept
{
_deleteValue();
_deleteComment();
}
inline Type Value::type() const noexcept
{
return Type(_typeAndComment & 0b111u);
}
inline Density Value::density() const noexcept
{
const Type type{this->type()};
if (type == Type::object || type == Type::array)
{
return Density(_ptrAndDensity & 0b111u);
}
else
{
return Density::unspecified;
}
}
inline void Value::setDensity(const Density density) noexcept
{
const Type type{this->type()};
if (type == Type::object || type == Type::array)
{
_ptrAndDensity &= ~uintptr_t{0b111u};
_ptrAndDensity |= uintptr_t(density);
}
}
inline bool Value::isObject() const noexcept
{
return type() == Type::object;
}
inline bool Value::isArray() const noexcept
{
return type() == Type::array;
}
inline bool Value::isString() const noexcept
{
return type() == Type::string;
}
inline bool Value::isNumber() const noexcept
{
const Type type{this->type()};
return type == Type::integer || type == Type::unsigner || type == Type::floater;
}
inline bool Value::isInteger() const noexcept
{
return type() == Type::integer;
}
inline bool Value::isUnsigner() const noexcept
{
return type() == Type::unsigner;
}
inline bool Value::isFloater() const noexcept
{
return type() == Type::floater;
}
inline bool Value::isBoolean() const noexcept
{
return type() == Type::boolean;
}
inline bool Value::isNull() const noexcept
{
return type() == Type::null;
}
template <typename T>
inline bool Value::is() const noexcept
{
using U = std::decay_t<T>;
// Object
if constexpr (std::is_same_v<U, Object>)
{
return isObject();
}
// Array
else if constexpr (std::is_same_v<U, Array>)
{
return isArray();
}
// String
else if constexpr (std::is_same_v<U, string> || std::is_same_v<U, string_view> || std::is_same_v<U, const char *> || std::is_same_v<U, char *>)
{
return isString();
}
// Character
else if constexpr (std::is_same_v<U, char>)
{
return isString() && asString<unsafe>().size() == 1u;
}
// Boolean
else if constexpr (std::is_same_v<U, bool>)
{
return isBoolean();
}
// Signed integer
else if constexpr (std::is_integral_v<U> && std::is_signed_v<U>)
{
switch (type())
{
case Type::integer:
{
if constexpr (std::is_same_v<U, int64_t>)
{
return true;
}
else
{
return _integer <= std::numeric_limits<U>::max() && _integer >= std::numeric_limits<U>::min();
}
}
case Type::unsigner:
{
return _unsigner <= uint64_t(std::numeric_limits<U>::max());
}
case Type::floater:
{
return double(U(_floater)) == _floater;
}
default:
{
return false;
}
}