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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file Variant.hpp
*
* The Variant class is used to efficiently represent a variables of varying
* type. Variant instances are used to represent data given by the end user and
* to exchange information between the host application and the script clients.
*
* @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
*/
#ifndef _OUSIA_VARIANT_HPP_
#define _OUSIA_VARIANT_HPP_
#include <cstdint>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <ostream>
// TODO: Use
// http://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html
// later (will allow to use 8 bytes for a variant)
#include <core/RangeSet.hpp>
#include <core/managed/Managed.hpp>
#include "Exceptions.hpp"
namespace ousia {
// Forward declarations
class Function;
class Rtti;
class SourceLocation;
/**
* Enum containing the possible types a variant may have.
*/
enum class VariantType : uint8_t {
BOOL = 1,
INT = 2,
DOUBLE = 3,
STRING = 4,
MAGIC = 5,
ARRAY = 6,
MAP = 7,
OBJECT = 8,
CARDINALITY = 9,
FUNCTION = 10,
NULLPTR = 15
};
#pragma pack(push, 1)
/**
* Structure used to store the type of a variant and the location at which it
* was found in 8 Bytes.
*/
struct VariantMetadata {
/**
* Field used to store the type of a Variant (4 Bit, space for 16 objects).
*/
uint8_t variantType : 4;
/**
* Field used to store the location at which the Variant was found (30 Bit).
*/
uint32_t locationOffset : 30; // Enough for 1GB
/**
* Field used to store the length of the value from which the variant was
* parsed (14 Bit).
*/
uint16_t locationLength : 14; // 16.000 Bytes of context
/**
* Unique id of the file from which the variant was parsed.
*/
uint16_t locationSourceId : 16; // 65.000 Source files
/**
* Maximum byte offset for locations that can be stored.
*/
static constexpr uint32_t InvalidLocationOffset = 0x3FFFFFFF;
/**
* Maximum length for locations that can be sotred.
*/
static constexpr uint16_t InvalidLocationLength = 0x3FFF;
/**
* Maximum source id that can be stored.
*/
static constexpr uint16_t InvalidLocationSourceId = 0xFFFF;
/**
* Default constructor. Sets the type to nullptr and all other fields to
* invalid.
*/
VariantMetadata()
{
*(reinterpret_cast<uint64_t *>(this)) = 0xFFFFFFFFFFFFFFFFull;
}
/**
* Sets the type to the given type and all other fields to invalid.
*
* @param type is the type of the variant.
*/
VariantMetadata(VariantType type) : VariantMetadata()
{
variantType = static_cast<uint8_t>(type);
}
/**
* Returns the internally stored type.
*
* @return the variant type.
*/
VariantType getType() const
{
return static_cast<VariantType>(variantType);
}
/**
* Sets the type to the given value.
*
* @param type is the variant type that should be stored.
*/
void setType(VariantType type) { variantType = static_cast<uint8_t>(type); }
/**
* Returns true if the stored source id is not invalid.
*
* @retun true if the
*/
bool hasLocation() const;
/**
* Unpacks ans returns the stored source location. Note that the returned
* location may differ from the one given in "setLocation", if the values
* were too large to represent.
*
* @return the stored SourceLocation.
*/
SourceLocation getLocation() const;
/**
* Packs the given source location and stores it in the metadata. Not all
* SourceLocation values may be representable, as they are stored with fewer
* bits as in the SourceLocation structure.
*
* @param location is the SourceLocation that should be stored.
*/
void setLocation(const SourceLocation &location);
};
#pragma pack(pop)
/**
* Instances of the Variant class represent any kind of data that is exchanged
* between the host application and the script engine. Variants are immutable.
*/
class Variant {
public:
/**
* Exception thrown whenever a variant is accessed via a getter function
* that is not supported for the current variant type.
*/
class TypeException : public OusiaException {
private:
/**
* Internally used string holding the exception message.
*/
const std::string msg;
public:
/**
* Contains the actual type of the variant.
*/
const VariantType actualType;
/**
* Contains the requested type of the variant.
*/
const VariantType requestedType;
/**
* Constructor of the TypeException.
*
* @param actualType describes the actual type of the variant.
* @param requestedType describes the type in which the variant was
* requested.
*/
TypeException(VariantType actualType, VariantType requestedType);
};
using boolType = bool;
using intType = int32_t;
using doubleType = double;
using stringType = std::string;
using arrayType = std::vector<Variant>;
using mapType = std::map<std::string, Variant>;
using objectType = Rooted<Managed>;
using cardinalityType = Cardinality;
using rangeType = Range<size_t>;
using functionType = std::shared_ptr<Function>;
private:
/**
* Used to store the actual type of the variant and the location from which
* the variant was parsed.
*/
VariantMetadata meta;
/**
* Anonymous union containing the possible value of the variant.
*/
union {
/**
* The boolean value. Only valid if type is VariantType::BOOL.
*/
boolType boolVal;
/**
* The integer value. Only valid if type is VariantType::INT.
*/
intType intVal;
/**
* The number value. Only valid if type is VariantType::DOUBLE.
*/
doubleType doubleVal;
/**
* Pointer to the more complex data structures on the free store. Only
* valid if type is one of VariantType::STRING, VariantType::ARRAY,
* VariantType::MAP.
*/
void *ptrVal;
};
/**
* Internally used to convert the current pointer value to a reference of
* the specified type.
*/
template <typename T>
T &asObj(VariantType requestedType) const
{
const VariantType actualType = getType();
if (actualType == requestedType) {
return *(static_cast<T *>(ptrVal));
}
throw TypeException{actualType, requestedType};
}
/**
* Used internally to assign the value of another Variant instance to this
* instance.
*
* @param v is the Variant instance that should be copied to this instance.
*/
void copy(const Variant &v)
{
destroy();
meta = v.meta;
switch (meta.getType()) {
case VariantType::NULLPTR:
break;
case VariantType::BOOL:
boolVal = v.boolVal;
break;
case VariantType::INT:
intVal = v.intVal;
break;
case VariantType::DOUBLE:
doubleVal = v.doubleVal;
break;
case VariantType::STRING:
case VariantType::MAGIC:
ptrVal = new stringType(v.asString());
break;
case VariantType::ARRAY:
ptrVal = new arrayType(v.asArray());
break;
case VariantType::MAP:
ptrVal = new mapType(v.asMap());
break;
case VariantType::OBJECT:
ptrVal = new objectType(v.asObject());
break;
case VariantType::CARDINALITY:
ptrVal = new cardinalityType(v.asCardinality());
break;
case VariantType::FUNCTION:
ptrVal = new functionType(v.asFunction());
break;
}
}
/**
* Used internally to move the value of another Variant instance to this
* instance.
*
* @param v is the Variant instance that should be copied to this instance.
*/
void move(Variant &&v) noexcept
{
destroy();
meta = v.meta;
switch (meta.getType()) {
case VariantType::NULLPTR:
break;
case VariantType::BOOL:
boolVal = v.boolVal;
break;
case VariantType::INT:
intVal = v.intVal;
break;
case VariantType::DOUBLE:
doubleVal = v.doubleVal;
break;
case VariantType::STRING:
case VariantType::MAGIC:
case VariantType::ARRAY:
case VariantType::MAP:
case VariantType::OBJECT:
case VariantType::CARDINALITY:
case VariantType::FUNCTION:
ptrVal = v.ptrVal;
v.ptrVal = nullptr;
break;
}
v.meta.setType(VariantType::NULLPTR);
}
/**
* Used internally to destroy any value that was allocated on the heap.
*/
void destroy()
{
if (ptrVal) {
switch (meta.getType()) {
case VariantType::STRING:
case VariantType::MAGIC:
delete static_cast<stringType *>(ptrVal);
break;
case VariantType::ARRAY:
delete static_cast<arrayType *>(ptrVal);
break;
case VariantType::MAP:
delete static_cast<mapType *>(ptrVal);
break;
case VariantType::OBJECT:
delete static_cast<objectType *>(ptrVal);
break;
case VariantType::CARDINALITY:
delete static_cast<cardinalityType *>(ptrVal);
break;
case VariantType::FUNCTION:
delete static_cast<functionType *>(ptrVal);
break;
default:
break;
}
#ifndef NDEBUG
ptrVal = nullptr;
#endif
}
}
public:
/**
* Copy constructor of the Variant class.
*
* @param v is the Variant instance that should be cloned.
*/
Variant(const Variant &v) : ptrVal(nullptr) { copy(v); }
/**
* Move constructor of the Variant class.
*
* @param v is the reference to the Variant instance that should be moved,
* this instance is invalidated afterwards.
*/
Variant(Variant &&v) noexcept : ptrVal(nullptr) { move(std::move(v)); }
/**
* Default constructor. VariantType is set to VariantType:NULLPTR.
*/
Variant() : ptrVal(nullptr) { setNull(); }
/**
* Default destructor, frees any memory that was allocated on the heap.
*/
~Variant() { destroy(); }
/**
* Constructor for null values. Initializes the variant as null value.
*/
Variant(std::nullptr_t) : ptrVal(nullptr) { setNull(); }
/**
* Constructor for boolean values.
*
* @param b boolean value.
*/
Variant(boolType b) : ptrVal(nullptr) { setBool(b); }
/**
* Constructor for integer values.
*
* @param i integer value.
*/
Variant(intType i) : ptrVal(nullptr) { setInt(i); }
/**
* Constructor for double values.
*
* @param d double value.
*/
Variant(doubleType d) : ptrVal(nullptr) { setDouble(d); }
/**
* Constructor for string values. The given string is copied and managed by
* the new Variant instance.
*
* @param s is a reference to a C-Style string used as string value.
*/
Variant(const char *s) : ptrVal(nullptr) { setString(s); }
/**
* Constructor for array values. The given array is copied and managed by
* the new Variant instance.
*
* @param a is a reference to the array
*/
Variant(arrayType a) : ptrVal(nullptr) { setArray(std::move(a)); }
/**
* Constructor for map values. The given map is copied and managed by the
* new Variant instance.
*
* @param m is a reference to the map.
*/
Variant(mapType m) : ptrVal(nullptr) { setMap(std::move(m)); }
/**
* Named constructor for object values.
*
* @param o is an object that can be converted to a Rooted handle.
*/
template <class T>
static Variant fromObject(T o)
{
Variant res;
res.setObject(o);
return res;
}
/**
* Constructor for cardinality values. The given cardinality is copied and
*managed by the
* new Variant instance.
*
* @param c is a reference to the cardinality.
*/
Variant(cardinalityType c) : ptrVal(nullptr)
{
setCardinality(std::move(c));
}
/**
* Named constructor for function values.
*
* @param f is a shared pointer pointing at the Function instance.
*/
static Variant fromFunction(const functionType &f)
{
Variant res;
res.setFunction(f);
return res;
}
/**
* Named constructor for strings values.
*
* @param s is the std::string from which the variant should be constructed.
*/
static Variant fromString(const stringType &s)
{
Variant res;
res.setString(s.c_str());
return res;
}
/**
* Copy assignment operator.
*/
Variant &operator=(const Variant &v)
{
copy(v);
return *this;
}
/**
* Move assignment operator.
*/
Variant &operator=(Variant &&v) noexcept
{
move(std::move(v));
return *this;
}
/**
* Assign nullptr_t operator (allows to write Variant v = nullptr).
*
* @param p is an instance of std::nullptr_t.
*/
Variant &operator=(std::nullptr_t)
{
setNull();
return *this;
}
/**
* Assign a boolean value.
*
* @param b is the boolean value to which the variant should be set.
*/
Variant &operator=(boolType b)
{
setBool(b);
return *this;
}
/**
* Assign an integer value.
*
* @param i is the integer value to which the variant should be set.
*/
Variant &operator=(intType i)
{
setInt(i);
return *this;
}
/**
* Assign a double value.
*
* @param d is the double value to which the variant should be set.
*/
Variant &operator=(doubleType d)
{
setDouble(d);
return *this;
}
/**
* Assign a zero terminated const char array.
*
* @param s is the zero terminated const char array to which the variant
* should be set.
*/
Variant &operator=(const char *s)
{
setString(s);
return *this;
}
/**
* Checks whether this Variant instance represents the nullptr.
*
* @return true if the Variant instance represents the nullptr, false
* otherwise.
*/
bool isNull() const { return meta.getType() == VariantType::NULLPTR; }
/**
* Checks whether this Variant instance is a boolean.
*
* @return true if the Variant instance is a boolean, false otherwise.
*/
bool isBool() const { return meta.getType() == VariantType::BOOL; }
/**
* Checks whether this Variant instance is an integer.
*
* @return true if the Variant instance is an integer, false otherwise.
*/
bool isInt() const { return meta.getType() == VariantType::INT; }
/**
* Checks whether this Variant instance is a double.
*
* @return true if the Variant instance is a double, false otherwise.
*/
bool isDouble() const { return meta.getType() == VariantType::DOUBLE; }
/**
* Checks whether this Variant instance is a string or a magic string.
*
* @return true if the Variant instance is a string, false otherwise.
*/
bool isString() const
{
return meta.getType() == VariantType::STRING ||
meta.getType() == VariantType::MAGIC;
}
/**
* Checks whether this Variant instance is a magic string. Magic strings
* are created if a unquoted string is parsed and may e.g. be treated as
* constants.
*
* @return true if the Variant instance is a string, false otherwise.
*/
bool isMagic() const { return meta.getType() == VariantType::MAGIC; }
/**
* Checks whether this Variant instance is an array.
*
* @return true if the Variant instance is an array, false otherwise.
*/
bool isArray() const { return meta.getType() == VariantType::ARRAY; }
/**
* Checks whether this Variant instance is a map.
*
* @return true if the Variant instance is a map, false otherwise.
*/
bool isMap() const { return meta.getType() == VariantType::MAP; }
/**
* Checks whether this Variant instance is an object.
*
* @return true if the Variant instance is an object, false otherwise.
*/
bool isObject() const { return meta.getType() == VariantType::OBJECT; }
/**
* Checks whether this Variant instance is a cardinality.
*
* @return true if the Variant instance is an cardinality, false otherwise.
*/
bool isCardinality() const
{
return meta.getType() == VariantType::CARDINALITY;
}
/**
* Checks whether this Variant instance is a function.
*
* @return true if the Variant instance is a function, false otherwise.
*/
bool isFunction() const { return meta.getType() == VariantType::FUNCTION; }
/**
* Checks whether this Variant instance is a primitive type.
*
* @return true if the Variant instance is a primitive type.
*/
bool isPrimitive() const
{
switch (meta.getType()) {
case VariantType::NULLPTR:
case VariantType::BOOL:
case VariantType::INT:
case VariantType::DOUBLE:
case VariantType::STRING:
return true;
default:
return false;
}
}
/**
* Returns the Variant boolean value. Performs no type conversion. Throws an
* exception if the underlying type is not a boolean.
*
* @return the boolean value.
*/
boolType asBool() const
{
if (isBool()) {
return boolVal;
}
throw TypeException{getType(), VariantType::BOOL};
}
/**
* Returns the Variant integer value. Performs no type conversion. Throws an
* exception if the underlying type is not an integer.
*
* @return the integer value.
*/
intType asInt() const
{
if (isInt()) {
return intVal;
}
throw TypeException{getType(), VariantType::INT};
}
/**
* Returns the Variant double value. Performs no type conversion. Throws an
* exception if the underlying type is not a double.
*
* @return the double value.
*/
doubleType asDouble() const
{
if (isDouble()) {
return doubleVal;
}
throw TypeException{getType(), VariantType::DOUBLE};
}
/**
* Returns a const reference to the string value. Performs no type
* conversion. Throws an exception if the underlying type is not a string.
*
* @return the string value as const reference.
*/
const stringType &asString() const
{
return asObj<stringType>(VariantType::STRING);
}
/**
* Returns a reference to the string value. Performs no type conversion.
* Throws an exception if the underlying type is not a string.
*
* @return the string value as reference.
*/
stringType &asString() { return asObj<stringType>(VariantType::STRING); }
/**
* Returns a const reference to the magic string value. Performs no type
* conversion. Throws an exception if the underlying type is not a magic
* string.
*
* @return the magic string value as const reference.
*/
const stringType &asMagic() const
{
if (meta.getType() == VariantType::MAGIC) {
return asObj<stringType>(VariantType::STRING);
}
throw TypeException{getType(), VariantType::MAGIC};
}
/**
* Returns a reference to the magic string value. Performs no type
* conversion. Throws an exception if the underlying type is not a magic
* string.
*
* @return the magic string value as const reference.
*/
stringType &asMagic()
{
if (meta.getType() == VariantType::MAGIC) {
return asObj<stringType>(VariantType::STRING);
}
throw TypeException{getType(), VariantType::MAGIC};
}
/**
* Returns a const reference to the array value. Performs no type
* conversion. Throws an exception if the underlying type is not an array.
*
* @return the array value as const reference.
*/
const arrayType &asArray() const
{
return asObj<arrayType>(VariantType::ARRAY);
}
/**
* Returns a const reference to the array value. Performs no type
* conversion. Throws an exception if the underlying type is not an array.
*
* @return the array value as reference.
*/
arrayType &asArray() { return asObj<arrayType>(VariantType::ARRAY); }
/**
* Returns a const reference to the map value. Performs no type
* conversion. Throws an exception if the underlying type is not a map.
*
* @return the map value as const reference.
*/
const mapType &asMap() const { return asObj<mapType>(VariantType::MAP); }
/**
* Returns a reference to the map value. Performs no type conversion.
* Throws an exception if the underlying type is not a map.
*
* @return the map value as reference.
*/
mapType &asMap() { return asObj<mapType>(VariantType::MAP); }
/**
* Returns a pointer pointing at the stored managed object. Performs no type
* conversion. Throws an exception if the underlying type is not a managed
* object.
*
* @return const pointer at the stored managed object.
*/
const objectType asObject() const
{
return asObj<objectType>(VariantType::OBJECT);
}
/**
* Returns a pointer pointing at the stored managed object. Performs no type
* conversion. Throws an exception if the underlying type is not a managed
* object.
*
* @return pointer at the stored managed object.
*/
objectType asObject() { return asObj<objectType>(VariantType::OBJECT); }
/**
* Returns a reference to the cardinality value. Performs no type
* conversion.
* Throws an exception if the underlying type is not a cardinality.
*
* @return the cardinality value as reference.
*/
const cardinalityType &asCardinality() const
{
return asObj<cardinalityType>(VariantType::CARDINALITY);
}
/**
* Returns a reference to the cardinality value. Performs no type
* conversion.
* Throws an exception if the underlying type is not a cardinality.
*
* @return the cardinality value as reference.
*/
cardinalityType &asCardinality()
{
return asObj<cardinalityType>(VariantType::CARDINALITY);
}
/**
* Returns a shared pointer pointing at the stored function object. Performs
* no type conversion. Throws an exception if the underlying type is not a
* function.
*
* @return pointer at the stored managed object.
*/
functionType &asFunction()
{
return asObj<functionType>(VariantType::FUNCTION);
}
/**
* Returns a shared pointer pointing at the stored function object. Performs
* no type conversion. Throws an exception if the underlying type is not a
* function.
*
* @return const pointer at the stored managed object.
*/
const functionType &asFunction() const
{
return asObj<functionType>(VariantType::FUNCTION);
}
/**
* If the value of the variant already is a string, the markAsMagic function
* marks this string as a "magic" value (a variant which might also be an
* identifier). Throws an exception if the variant is not a string or magic
* value.
*/
void markAsMagic()
{
if (getType() == VariantType::STRING) {
meta.setType(VariantType::MAGIC);
return;
}
throw TypeException{getType(), VariantType::STRING};
}
/**
* Returns the value of the Variant as boolean, performs type conversion.
*
* @return the Variant value converted to a boolean value.
*/
boolType toBool() const;
/**
* Returns the value of the Variant as integer, performs type conversion.
*
* @return the Variant value converted to an integer value.
*/
intType toInt() const;
/**
* Returns the value of the Variant as double, performs type conversion.
*
* @return the Variant value converted to a double value.
*/
doubleType toDouble() const;
/**
* Returns the value of the Variant as string, performs type conversion.
*
* @return the value of the variant as string.
*/
stringType toString() const;
/**
* Returns the value of the Variant as array, performs type conversion. If
* the variant is not an array yet, the current value is inserted into a
* one-element array.
*
* @return the value of the variant as array.
*/
arrayType toArray() const;
/**
* Returns the value of the Variant as array, performs type conversion. If
* the variant is not an array yet, the current value is inserted into a
* one-element array.
*
* @param innerType is the inner type the array entries should be converted
* to.
* @return the value of the variant as array.
*/
arrayType toArray(const Rtti *innerType) const;
/**
* Returns the value of the Variant as map.
*
* @return the value of the variant as map.
*/
mapType toMap() const;
/**
* Returns the value of the Variant as map, performs type conversion of the
* map entries to the given inner type.
*
* @param innerType is the inner type the map entries should be converted
* to.
* @return the value of the variant as map.
*/
mapType toMap(const Rtti *innerType) const;
/**
* Returns the value of the Variant as cardinality.
*
* @return the value of the variant as cardinality.
*/
cardinalityType toCardinality() const;
/**
* Sets the variant to null.
*/
void setNull()
{
destroy();
meta.setType(VariantType::NULLPTR);
ptrVal = nullptr;
}
/**
* Sets the variant to the given boolean value.
*
* @param b is the new boolean value.
*/
void setBool(boolType b)
{
destroy();
meta.setType(VariantType::BOOL);
boolVal = b;
}
/**
* Sets the variant to the given integer value.
*
* @param i is the new integer value.
*/
void setInt(intType i)
{
destroy();
meta.setType(VariantType::INT);
intVal = i;
}
/**
* Sets the variant to the given double value.
*
* @param d is the new double value.
*/
void setDouble(doubleType d)
{
destroy();
meta.setType(VariantType::DOUBLE);
doubleVal = d;
}
/**
* Sets the variant to the given string value.
*
* @param s is the new string value.
*/
void setString(const char *s)
{
if (isString()) {
meta.setType(VariantType::STRING);
asString().assign(s);
} else {
destroy();
meta.setType(VariantType::STRING);
ptrVal = new stringType(s);
}
}
/**
* Sets the variant to the given magic string value.
*
* @param s is the new magic string value.
*/
void setMagic(const char *s)
{
if (isString()) {
meta.setType(VariantType::MAGIC);
asString().assign(s);
} else {
destroy();
meta.setType(VariantType::MAGIC);
ptrVal = new stringType(s);
}
}
/**
* Sets the variant to the given array value.
*
* @param a is the new array value.
*/
void setArray(arrayType a)
{
if (isArray()) {
asArray().swap(a);
} else {
destroy();
meta.setType(VariantType::ARRAY);
ptrVal = new arrayType(std::move(a));
}
}
/**
* Sets the variant to the given map value.
*
* @param m is the new map value.
*/
void setMap(mapType m)
{
if (isMap()) {
asMap().swap(m);
} else {
destroy();
meta.setType(VariantType::MAP);
ptrVal = new mapType(std::move(m));
}
}
/**
* Sets the variant to the given managed object. The variant is equivalent
* to a Rooted handle.
*/
template <class T>
void setObject(T o)
{
destroy();
meta.setType(VariantType::OBJECT);
ptrVal = new objectType(o);
}
/**
* Sets the variant to the given cardinality value.
*
* @param c is the new cardinality value.
*/
void setCardinality(cardinalityType c)
{
destroy();
meta.setType(VariantType::CARDINALITY);
ptrVal = new cardinalityType(std::move(c));
}
/**
* Sets the variant to the given function.
*
* @param f is a std::shared_ptr pointing at a instance of the Function
* class the Variant should be set to.
*/
void setFunction(functionType f)
{
destroy();
meta.setType(VariantType::FUNCTION);
ptrVal = new functionType(f);
}
/**
* Returns the current type of the Variant.
*
* @return the current type of the Variant.
*/
VariantType getType() const
{
if (isMagic()) {
return VariantType::STRING;
}
return meta.getType();
}
/**
* Returns the current Rtti type descriptor of the Variant.
*
* @return the Rtti type descriptor. Either one of RttiTypes::Int,
* RttiTypes::Bool, RttiTypes::Double, RttiTypes::String, RttiTypes::Array
* or RttiTypes::Function or -- in case an object is stored inside the
* variant -- the Rtti of that object.
*/
const Rtti *getRtti() const;
/**
* Returns the name of the given variant type as C-style string.
*/
static const char *getTypeName(VariantType type);
/**
* Returns the name of the type of this variant instance.
*/
const char *getTypeName() const { return Variant::getTypeName(getType()); }
/*
* Source location
*/
/**
* Returns true if the stored source id is not invalid.
*
* @retun true if the
*/
bool hasLocation() const { return meta.hasLocation(); }
/**
* Unpacks ans returns the stored source location. Note that the returned
* location may differ from the one given in "setLocation", if the values
* were too large to represent.
*
* @return the stored SourceLocation.
*/
SourceLocation getLocation() const { return meta.getLocation(); }
/**
* Packs the given source location and stores it in the metadata. Not all
* SourceLocation values may be representable, as they are stored with fewer
* bits as in the SourceLocation structure.
*
* @param location is the SourceLocation that should be stored.
*/
void setLocation(const SourceLocation &location)
{
return meta.setLocation(location);
}
/*
* Output stream operator.
*/
/**
* Prints the Variant to the output stream as JSON data.
*
* @param os is the output stream the variant should be written to.
* @param v is the variant that should be written to the output stream.
*/
friend std::ostream &operator<<(std::ostream &os, const Variant &v);
/*
* Comprison operators.
*/
/**
* Returns true if the given left hand side is smaller than the right hand
* side. Uses the comparison algorithm of the stored object. Throws an
* exception if the types of the two variants are not equal.
*
* @param lhs is the left hand side of the comparison.
* @param rhs is the right hand side of the comparison.
* @return true if lhs is smaller than rhs.
*/
friend bool operator<(const Variant &lhs, const Variant &rhs);
/**
* Returns true if the given left hand side is larger than the right hand
* side. Uses the comparison algorithm of the stored object. Throws an
* exception if the types of the two variants are not equal.
*
* @param lhs is the left hand side of the comparison.
* @param rhs is the right hand side of the comparison.
* @return true if lhs is larger than rhs.
*/
friend bool operator>(const Variant &lhs, const Variant &rhs);
/**
* Returns true if the given left hand side is smaller or equal to the
* right hand side. Uses the comparison algorithm of the stored object.
* Throws an exception if the types of the two variants are not equal.
*
* @param lhs is the left hand side of the comparison.
* @param rhs is the right hand side of the comparison.
* @return true if lhs is smaller than or equal to rhs.
*/
friend bool operator<=(const Variant &lhs, const Variant &rhs);
/**
* Returns true if the given left hand side is larger or equal to the
* right hand side. Uses the comparison algorithm of the stored object.
* Throws an exception if the types of the two variants are not equal.
*
* @param lhs is the left hand side of the comparison.
* @param rhs is the right hand side of the comparison.
* @return true if lhs is larger than or equal to rhs.
*/
friend bool operator>=(const Variant &lhs, const Variant &rhs);
/**
* Returns true if the given left hand side and right hand side are equal.
* Uses the comparison algorithm of the stored object. Returns false if the
* two variants do not have the same type.
*
* @param lhs is the left hand side of the comparison.
* @param rhs is the right hand side of the comparison.
* @return true if lhs equals rhs.
*/
friend bool operator==(const Variant &lhs, const Variant &rhs);
/**
* Returns true if the given left hand side are equal. Uses the comparison
* algorithm of the stored object. Returns true if the two variants do not
* have the same type.
*
* @param lhs is the left hand side of the comparison.
* @param rhs is the right hand side of the comparison.
* @return true if lhs is not equal to rhs.
*/
friend bool operator!=(const Variant &lhs, const Variant &rhs);
};
/* Static Assertions */
// Make sure VariantData has a length of 8 bytes
static_assert(sizeof(VariantMetadata) == 8,
"VariantMetadata should have a length of 8 Bytes");
// Make sure VariantData has a length of 16 bytes
static_assert(sizeof(Variant) == 16,
"Variant should have a length of 16 bytes");
}
#endif /* _OUSIA_VARIANT_HPP_ */
|