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
|
/*
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/>.
*/
#ifndef _OUSIA_VARIANT_HPP_
#define _OUSIA_VARIANT_HPP_
#include <cstdint>
#include <exception>
#include <map>
#include <ostream>
#include <string>
#include <vector>
// TODO: Replace VariantType::number with VariantType::double
// TODO: Convert VariantType::integer to int32_t
// TODO: Use std::unique_ptr for *Function
// TODO: Move semantic in complex constructors
// TODO: Delete default constructors/assignment operators in pretty much everything
// TODO: Remove implicit type conversions, but add explicit conversion function!
namespace ousia {
namespace script {
/* Class forward declarations to avoid cyclic dependencies in the header */
class Object;
class Function;
/**
* Enum containing the possible types a variant may have.
*/
enum class VariantType : int16_t {
null = 0x0001,
boolean = 0x0002,
integer = 0x0004,
number = 0x0008,
string = 0x0010,
array = 0x0020,
map = 0x0040,
function = 0x0080,
object = 0x0100,
buffer = 0x0200
};
/**
* Exception thrown whenever a variant is accessed via a getter function that
* is not supported for the current variant type.
*/
class VariantTypeException : public std::exception {
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 VariantTypeException.
*
* @param actualType describes the actual type of the variant.
* @param requestedType describes the type in which the variant was
* requested.
*/
VariantTypeException(VariantType actualType, VariantType requestedType);
/**
* Returns the error message of the exception.
*
* @return the error message as C string.
*/
virtual const char *what() const noexcept override;
};
/**
* 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 {
private:
/**
* Used to store the actual type of the variant.
*/
const VariantType type;
/**
* Anonymous union containing the possible value of the variant.
*/
union {
/**
* The boolean value. Only valid if type is VariantType::boolean.
*/
bool booleanValue;
/**
* The integer value. Only valid if type is VariantType::integer.
*/
int64_t integerValue;
/**
* The number value. Only valid if type is VariantType::double.
*/
double numberValue;
/**
* Pointer to the more complex data structures on the free store. Only
* valid if type is one of VariantType::string, VariantType::array,
* VariantType::map, VariantType::function, VariantType::object.
*/
void *objectValue = nullptr;
};
public:
using Int = int32_t;
/**
* Copy constructor of the Variant class.
*
* @param v is the Variant instance that should be cloned.
*/
Variant(const Variant &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);
/**
* Default constructor. Type is set to VariantType:null.
*/
Variant();
/**
* Constructor for boolean values.
*
* @param b boolean value.
*/
Variant(bool b);
/**
* Constructor for integer values.
*
* @param i integer value.
*/
Variant(int64_t i);
/**
* Constructor for number values.
*
* @param d number (double) value.
*/
Variant(double 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);
/**
* 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(const std::vector<Variant> &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(const std::map<std::string, Variant> &m);
/**
* Constructor for function values. The given pointer to the function object
*is cloned and managed by the new Variant instance.
*
* @param f is a reference to the function.
*/
Variant(const Function *f);
/**
* Constructor for object values. The given Object is copied and managed by
* the new Variant instance.
*
* @param o is a reference to the object.
*/
Variant(const Object &o);
/**
* Destructor of the Variant class.
*/
~Variant();
/**
* Assign operator.
*/
Variant &operator=(const Variant &v) = delete;
/**
* Move assign operator.
*/
Variant &operator=(Variant &&v) = delete;
/**
* Returns the current type of the Variant.
*
* @return the current type of the Variant.
*/
VariantType getType() const { return type; }
bool getBooleanValue() const;
int64_t getIntegerValue() const;
double getNumberValue() const;
const std::string &getStringValue() const;
const std::vector<Variant> &getArrayValue() const;
const std::map<std::string, Variant> &getMapValue() const;
const Function *getFunctionValue() const;
const Object &getObjectValue() const;
/**
* Shorthand for a constant representing a "null" as a variant.
*/
static const Variant Null;
/**
* Returns the name of the given variant type as C-style string.
*/
static const char *getTypeName(VariantType type);
/**
* Prints the object as JSON to the output stream.
*/
friend std::ostream &operator<<(std::ostream &os, const Variant &v);
};
}
}
#endif /* _OUSIA_VARIANT_HPP_ */
|