-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathhjson_value.cpp
More file actions
1946 lines (1522 loc) · 39.7 KB
/
hjson_value.cpp
File metadata and controls
1946 lines (1522 loc) · 39.7 KB
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
#include "hjson.h"
#include <vector>
#include <assert.h>
#include <cstring>
#include <algorithm>
#if HJSON_USE_CHARCONV
# include <charconv>
# include <array>
#elif HJSON_USE_STRTOD
# include <cstdlib>
# include <cerrno>
# include <cstdio>
#else
# include <sstream>
#endif
namespace Hjson {
typedef std::vector<std::string> KeyVec;
typedef std::vector<Value> ValueVec;
typedef std::map<std::string, Value> ValueMap;
class ValueVecMap {
public:
KeyVec v;
ValueMap m;
};
class Value::ValueImpl {
public:
Type type;
union {
bool b;
double d;
std::int64_t i;
std::string *s;
ValueVec *v;
ValueVecMap *m;
};
ValueImpl();
ValueImpl(bool);
ValueImpl(double);
explicit ValueImpl(std::int64_t);
ValueImpl(const std::string&);
ValueImpl(Type);
~ValueImpl();
static void DeepClear(Value &val);
};
class Value::Comments {
public:
std::string m_commentBefore, m_commentKey, m_commentInside, m_commentAfter;
};
Value::ValueImpl::ValueImpl()
: type(Type::Undefined)
{
}
Value::ValueImpl::ValueImpl(bool input)
: type(Type::Bool),
b(input)
{
}
Value::ValueImpl::ValueImpl(double input)
: type(Type::Double),
d(input)
{
}
Value::ValueImpl::ValueImpl(std::int64_t input)
: type(Type::Int64),
i(input)
{
}
Value::ValueImpl::ValueImpl(const std::string &input)
: type(Type::String),
s(new std::string(input))
{
}
Value::ValueImpl::ValueImpl(Type _type)
: type(_type)
{
switch (_type)
{
case Type::String:
s = new std::string();
break;
case Type::Vector:
v = new ValueVec();
break;
case Type::Map:
m = new ValueVecMap();
break;
default:
break;
}
}
// Bottom-up destruction in order to avoid stack overflow due to recursive destructor calls.
void Value::ValueImpl::DeepClear(Value &val) {
// The map/vector will only be destroyed if use_count == 1
if (val.size() && val.prv.use_count() == 1) {
std::vector<std::pair<Value, int> > v;
v.emplace_back(val, 0);
while (!v.empty()) {
if (v.back().second >= v.back().first.size()) {
v.back().first.clear();
v.pop_back();
} else {
Value &n = v.back().first[v.back().second];
v.back().second++;
// The map/vector will only be destroyed if use_count == 1
if (n.size() && n.prv.use_count() == 1) {
v.emplace_back(v.back().first[v.back().second - 1], 0);
}
}
}
}
}
Value::ValueImpl::~ValueImpl() {
switch (type)
{
case Type::String:
delete s;
break;
case Type::Vector:
for (auto e = v->begin(); e != v->end(); ++e) {
DeepClear(*e);
}
delete v;
break;
case Type::Map:
for (auto e = m->m.begin(); e != m->m.end(); ++e) {
DeepClear(e->second);
}
delete m;
break;
default:
break;
}
}
// Sacrifice efficiency for predictability: It is allowed to do bracket
// assignment on an Undefined Value, and thereby turn it into a Map Value.
// A Map Value is passed by reference, therefore an Undefined Value should also
// be passed by reference, to avoid surprises when doing bracket assignment
// on a Value that has been passed around but is still of type Undefined.
Value::Value()
: prv(std::make_shared<ValueImpl>(Type::Undefined))
{
}
Value::Value(bool input)
: prv(std::make_shared<ValueImpl>(input))
{
}
Value::Value(float input)
: prv(std::make_shared<ValueImpl>(static_cast<double>(input)))
{
}
Value::Value(double input)
: prv(std::make_shared<ValueImpl>(input))
{
}
Value::Value(long double input)
: prv(std::make_shared<ValueImpl>(static_cast<double>(input)))
{
}
Value::Value(char input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(unsigned char input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(short input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(unsigned short input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(int input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(unsigned int input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(long input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(unsigned long input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(long long input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(unsigned long long input)
: prv(std::make_shared<ValueImpl>(static_cast<std::int64_t>(input)))
{
}
Value::Value(const char *input)
: prv(std::make_shared<ValueImpl>(std::string(input)))
{
}
Value::Value(const std::string& input)
: prv(std::make_shared<ValueImpl>(input))
{
}
Value::Value(Type _type)
: prv(std::make_shared<ValueImpl>(_type))
{
}
Value::Value(const Value& other)
: prv(other.prv)
{
if (other.cm) {
// Clone the comments instead of sharing the reference. This way a change
// in the other Value does not affect the comments in this Value.
cm.reset(new Comments(*other.cm));
}
}
Value::Value(Value&& other)
: prv(other.prv),
cm(other.cm)
{
}
// Even though the MapProxy is temporary, it contains references that are owned
// by a non-temporary object. Make sure the lvalue constructor is called.
Value::Value(MapProxy&& other)
: Value(other)
{
}
Value::Value(std::shared_ptr<ValueImpl> _prv, std::shared_ptr<Comments> _cm)
: prv(_prv),
cm(_cm)
{
}
Value::~Value() {
}
Value& Value::operator=(const Value& other) {
// So that comments are kept when assigning a Value to a new key in a map,
// or to a variable that has not been assigned any other value yet.
if (!this->defined()) {
this->set_comments(other);
}
this->prv = other.prv;
return *this;
}
Value& Value::operator=(Value&& other) {
// So that comments are kept when assigning a Value to a new key in a map,
// or to a variable that has not been assigned any other value yet.
if (!this->defined()) {
this->cm = other.cm;
}
this->prv = other.prv;
return *this;
}
const Value& Value::at(const std::string& name) const {
switch (prv->type)
{
case Type::Undefined:
throw index_out_of_bounds("Key not found.");
case Type::Map:
try {
return prv->m->m.at(name);
} catch(const std::out_of_range&) {}
throw index_out_of_bounds("Key not found.");
default:
throw type_mismatch("Must be of type Map for that operation.");
}
}
Value& Value::at(const std::string& name) {
switch (prv->type)
{
case Type::Undefined:
throw index_out_of_bounds("Key not found.");
case Type::Map:
try {
return prv->m->m.at(name);
} catch(const std::out_of_range&) {}
throw index_out_of_bounds("Key not found.");
default:
throw type_mismatch("Must be of type Map for that operation.");
}
}
const Value& Value::at(const char *name) const {
return at(std::string(name));
}
Value& Value::at(const char *name) {
return at(std::string(name));
}
const Value Value::operator[](const std::string& name) const {
if (prv->type == Type::Undefined) {
return Value();
} else if (prv->type == Type::Map) {
auto it = prv->m->m.find(name);
if (it == prv->m->m.end()) {
return Value();
}
return it->second;
}
throw type_mismatch("Must be of type Undefined or Map for that operation.");
}
MapProxy Value::operator[](const std::string& name) {
if (prv->type == Type::Undefined) {
prv->~ValueImpl();
// Recreate the private object using the same memory block.
new(&(*prv)) ValueImpl(Type::Map);
} else if (prv->type != Type::Map) {
throw type_mismatch("Must be of type Undefined or Map for that operation.");
}
auto it = prv->m->m.find(name);
if (it == prv->m->m.end()) {
return MapProxy(prv, name, 0);
}
return MapProxy(prv, name, &it->second);
}
const Value Value::operator[](const char *input) const {
return operator[](std::string(input));
}
MapProxy Value::operator[](const char *input) {
return operator[](std::string(input));
}
const Value Value::operator[](char *input) const {
return operator[](std::string(input));
}
MapProxy Value::operator[](char *input) {
return operator[](std::string(input));
}
const Value& Value::operator[](int index) const {
switch (prv->type)
{
case Type::Undefined:
throw index_out_of_bounds("Index out of bounds.");
case Type::Vector:
case Type::Map:
if (index < 0 || index >= size()) {
throw index_out_of_bounds("Index out of bounds.");
}
switch (prv->type)
{
case Type::Vector:
return prv->v[0][index];
case Type::Map:
{
auto it = prv->m->m.find(prv->m->v[index]);
assert(it != prv->m->m.end());
return it->second;
}
default:
break;
}
default:
throw type_mismatch("Must be of type Undefined, Vector or Map for that operation.");
}
}
Value& Value::operator[](int index) {
switch (prv->type)
{
case Type::Undefined:
throw index_out_of_bounds("Index out of bounds.");
case Type::Vector:
case Type::Map:
if (index < 0 || index >= size()) {
throw index_out_of_bounds("Index out of bounds.");
}
switch (prv->type)
{
case Type::Vector:
return prv->v[0][index];
case Type::Map:
{
auto it = prv->m->m.find(prv->m->v[index]);
assert(it != prv->m->m.end());
return it->second;
}
default:
break;
}
default:
throw type_mismatch("Must be of type Undefined, Vector or Map for that operation.");
}
}
bool Value::operator==(bool input) const {
return operator bool() == input;
}
bool Value::operator!=(bool input) const {
return !(*this == input);
}
#define RET_VAL(_T, _O) \
Value operator _O(_T a, const Value& b) { \
return Value(a) _O b; \
} \
Value operator _O(const Value& a, _T b) { \
return a _O Value(b); \
}
#define RET_BOOL(_T, _O) \
bool operator _O(_T a, const Value& b) { \
return Value(a) _O b; \
} \
bool operator _O(const Value& a, _T b) { \
return a _O Value(b); \
}
#define HJSON_OP_IMPL_A(_T) \
RET_BOOL(_T, <) \
RET_BOOL(_T, >) \
RET_BOOL(_T, <=) \
RET_BOOL(_T, >=) \
RET_BOOL(_T, ==) \
RET_BOOL(_T, !=)
#define HJSON_OP_IMPL_B(_T) \
HJSON_OP_IMPL_A(_T) \
RET_VAL(_T, +) \
RET_VAL(_T, -) \
RET_VAL(_T, *) \
RET_VAL(_T, /)
#define HJSON_OP_IMPL_C(_T) \
HJSON_OP_IMPL_B(_T) \
RET_VAL(_T, %)
HJSON_OP_IMPL_A(const char*)
HJSON_OP_IMPL_A(const std::string&)
HJSON_OP_IMPL_B(float)
HJSON_OP_IMPL_B(double)
HJSON_OP_IMPL_B(long double)
HJSON_OP_IMPL_C(char)
HJSON_OP_IMPL_C(unsigned char)
HJSON_OP_IMPL_C(short)
HJSON_OP_IMPL_C(unsigned short)
HJSON_OP_IMPL_C(int)
HJSON_OP_IMPL_C(unsigned int)
HJSON_OP_IMPL_C(long)
HJSON_OP_IMPL_C(unsigned long)
HJSON_OP_IMPL_C(long long)
HJSON_OP_IMPL_C(unsigned long long)
std::string operator+(const char *a, const Value& b) {
return std::string(a) + b.to_string();
}
std::string operator+(const Value& a ,const char *b) {
return a.to_string() + std::string(b);
}
std::string operator+(const std::string &a, const Value& b) {
return a + b.to_string();
}
std::string operator+(const Value& a, const std::string &b) {
return a.to_string() + b;
}
Value operator+(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d + b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i + b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d + b.prv->d;
case Type::Int64:
return a.prv->i + b.prv->i;
case Type::String:
return *a.prv->s + *b.prv->s;
default:
break;
}
throw type_mismatch("The values must be of type Double, Int64 or String for this operation.");
}
bool operator<(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d < b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i < b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d < b.prv->d;
case Type::Int64:
return a.prv->i < b.prv->i;
case Type::String:
return *a.prv->s < *b.prv->s;
default:
break;
}
throw type_mismatch("The values must be of type Double, Int64 or String for this operation.");
}
bool operator>(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d > b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i > b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d > b.prv->d;
case Type::Int64:
return a.prv->i > b.prv->i;
case Type::String:
return *a.prv->s > *b.prv->s;
default:
break;
}
throw type_mismatch("The values must be of type Double, Int64 or String for this operation.");
}
bool operator<=(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d <= b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i <= b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d <= b.prv->d;
case Type::Int64:
return a.prv->i <= b.prv->i;
case Type::String:
return *a.prv->s <= *b.prv->s;
default:
break;
}
throw type_mismatch("The values must be of type Double, Int64 or String for this operation.");
}
bool operator>=(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d >= b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i >= b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d >= b.prv->d;
case Type::Int64:
return a.prv->i >= b.prv->i;
case Type::String:
return *a.prv->s >= *b.prv->s;
default:
break;
}
throw type_mismatch("The values must be of type Double, Int64 or String for this operation.");
}
bool operator==(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d == b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i == b.prv->d;
}
if (a.prv->type != b.prv->type) {
return false;
}
switch (a.prv->type) {
case Type::Undefined:
case Type::Null:
return true;
case Type::Bool:
return a.prv->b == b.prv->b;
case Type::Double:
return a.prv->d == b.prv->d;
case Type::String:
return *a.prv->s == *b.prv->s;
case Type::Vector:
return a.prv->v == b.prv->v;
case Type::Map:
return a.prv->m == b.prv->m;
case Type::Int64:
return a.prv->i == b.prv->i;
}
assert(!"Unknown type");
return false;
}
bool operator!=(const Value& a, const Value& b) {
return !(a == b);
}
Value operator-(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d - b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i - b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d - b.prv->d;
case Type::Int64:
return a.prv->i - b.prv->i;
default:
break;
}
throw type_mismatch("The values must be of type Double or Int64 for this operation.");
}
Value operator*(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d * b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i * b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d * b.prv->d;
case Type::Int64:
return a.prv->i * b.prv->i;
default:
break;
}
throw type_mismatch("The values must be of type Double or Int64 for this operation.");
}
Value operator/(const Value& a, const Value& b) {
if (a.prv->type == Type::Double && b.prv->type == Type::Int64) {
return a.prv->d / b.prv->i;
} else if (a.prv->type == Type::Int64 && b.prv->type == Type::Double) {
return a.prv->i / b.prv->d;
}
if (a.prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (a.prv->type) {
case Type::Double:
return a.prv->d / b.prv->d;
case Type::Int64:
return a.prv->i / b.prv->i;
default:
break;
}
throw type_mismatch("The values must be of type Double or Int64 for this operation.");
}
Value operator%(const Value& a, const Value& b) {
if (a.prv->type != b.prv->type || a.prv->type != Type::Int64) {
throw type_mismatch("The values must be of the Int64 type for this operation.");
}
return a.prv->i % b.prv->i;
}
#define OP_ASS(_O, _T) \
Value& Value::operator _O(_T b) { \
return operator _O(Value(b)); \
}
#define HJSON_ASS_IMPL_A(_T) \
OP_ASS(+=, _T) \
OP_ASS(-=, _T) \
OP_ASS(*=, _T) \
OP_ASS(/=, _T)
#define HJSON_ASS_IMPL_B(_T) \
HJSON_ASS_IMPL_A(_T) \
OP_ASS(%=, _T)
HJSON_ASS_IMPL_A(float)
HJSON_ASS_IMPL_A(double)
HJSON_ASS_IMPL_A(long double)
HJSON_ASS_IMPL_B(char)
HJSON_ASS_IMPL_B(unsigned char)
HJSON_ASS_IMPL_B(short)
HJSON_ASS_IMPL_B(unsigned short)
HJSON_ASS_IMPL_B(int)
HJSON_ASS_IMPL_B(unsigned int)
HJSON_ASS_IMPL_B(long)
HJSON_ASS_IMPL_B(unsigned long)
HJSON_ASS_IMPL_B(long long)
HJSON_ASS_IMPL_B(unsigned long long)
Value& Value::operator+=(const char *b) {
return operator+=(std::string(b));
}
Value& Value::operator+=(const std::string& b) {
if (prv->type != Type::String) {
throw type_mismatch("The value must be of type String for this operation.");
}
*prv->s += b;
return *this;
}
Value& Value::operator+=(const Value& b) {
if (prv->type == Type::Double && b.prv->type == Type::Int64) {
prv->d += b.prv->i;
} else if (prv->type == Type::Int64 && b.prv->type == Type::Double) {
prv->i += static_cast<int64_t>(b.prv->d);
} else {
if (prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (prv->type) {
case Type::Double:
prv->d += b.prv->d;
break;
case Type::Int64:
prv->i += b.prv->i;
break;
case Type::String:
*prv->s += *b.prv->s;
break;
default:
throw type_mismatch("The values must be of type Double, Int64 or String for this operation.");
break;
}
}
return *this;
}
Value& Value::operator-=(const Value& b) {
operator +=(-b);
return *this;
}
Value& Value::operator*=(const Value& b) {
if (prv->type == Type::Double && b.prv->type == Type::Int64) {
prv->d *= b.prv->i;
} else if (prv->type == Type::Int64 && b.prv->type == Type::Double) {
prv->i = static_cast<int64_t>(prv->i * b.prv->d);
} else {
if (prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (prv->type) {
case Type::Double:
prv->d *= b.prv->d;
break;
case Type::Int64:
prv->i *= b.prv->i;
break;
default:
throw type_mismatch("The values must be of type Double or Int64 for this operation.");
break;
}
}
return *this;
}
Value& Value::operator/=(const Value& b) {
if (prv->type == Type::Double && b.prv->type == Type::Int64) {
prv->d /= b.prv->i;
} else if (prv->type == Type::Int64 && b.prv->type == Type::Double) {
prv->i = static_cast<int64_t>(prv->i / b.prv->d);
} else {
if (prv->type != b.prv->type) {
throw type_mismatch("The values must be of the same type for this operation.");
}
switch (prv->type) {
case Type::Double:
prv->d /= b.prv->d;
break;
case Type::Int64:
prv->i /= b.prv->i;
break;
default:
throw type_mismatch("The values must be of type Double or Int64 for this operation.");
break;
}
}
return *this;
}
Value& Value::operator%=(const Value& b) {
if (prv->type != b.prv->type || prv->type != Type::Int64) {
throw type_mismatch("The values must be of the Int64 type for this operation.");
}
prv->i %= b.prv->i;
return *this;
}
Value Value::operator+() const {
switch (prv->type) {
case Type::Double:
return prv->d;
case Type::Int64:
return prv->i;
default:
throw type_mismatch("The value must be of type Double or Int64 for this operation.");
break;
}
return *this;
}
Value Value::operator-() const {
switch (prv->type) {
case Type::Double:
return -prv->d;
case Type::Int64:
return -prv->i;
default:
throw type_mismatch("The value must be of type Double or Int64 for this operation.");
break;
}