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
|
/*
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 <ostream>
#include <string>
#include <vector>
#include <map>
namespace ousia {
namespace script {
/**
* Enum containing the possible types a variant may have.
*/
enum class VariantType {
none, integer, number, string, array, map, function, object, buffer
};
/**
* Instances of the Variant class represent any kind of data that is exchanged
* between the host application and the script engine.
*/
class Variant {
private:
VariantType type;
union {
int64_t integerValue;
double numberValue;
std::string stringValue;
std::vector<Variant> arrayValue;
std::map<std::string, Variant> mapValue;
};
/**
* Private function calling the destructor of the currently used union
* member.
*/
void free() {
// Explicitly call the destructor
switch (type) {
case VariantType::string:
stringValue.std::string::~string();
break;
case VariantType::array:
arrayValue.std::vector<Variant>::~vector();
break;
case VariantType::map:
mapValue.std::map<std::string, Variant>::~map();
break;
default:
break;
}
// Reset the type
type = VariantType::none;
}
/**
* Function for copying the content of the given instance v to this
* instance. Callers must make sure the storage space has been freed
* beforehand.
*/
void copy(const Variant &v)
{
type = v.type;
switch (type) {
case VariantType::integer:
integerValue = v.integerValue;
break;
case VariantType::number:
numberValue = v.numberValue;
break;
case VariantType::string:
new (&stringValue) std::string(v.stringValue);
break;
case VariantType::array:
new (&arrayValue) std::vector<Variant>(v.arrayValue);
break;
case VariantType::map:
new (&mapValue) std::map<std::string, Variant>(v.mapValue);
break;
default:
break;
}
}
/**
* Function for moving the content of the given instance v to this instance.
* No copy operation is used. Callers must make sure the storage space has
* been freed beforehand.
*/
void move(Variant &v)
{
type = v.type;
switch (type) {
case VariantType::integer:
integerValue = v.integerValue;
break;
case VariantType::number:
numberValue = v.numberValue;
break;
case VariantType::string:
new (&stringValue) std::string(std::move(v.stringValue));
break;
case VariantType::array:
new (&arrayValue) std::vector<Variant>(std::move(v.arrayValue));
break;
case VariantType::map:
new (&mapValue) std::map<std::string, Variant>(std::move(v.mapValue));
break;
default:
break;
}
// Reset the type of v to "none"
v.type = VariantType::none;
}
public:
class EBadEntry {};
Variant(const Variant &v)
{
copy(v);
}
Variant(Variant &&v)
{
move(v);
}
Variant& operator=(const Variant &v)
{
free();
copy(v);
return *this;
}
Variant& operator=(Variant &&v)
{
free();
move(v);
return *this;
}
Variant(int64_t i) :
type(VariantType::integer),
integerValue(i)
{
// Do nothing here
}
Variant(double d) :
type(VariantType::number),
numberValue(d)
{
// Do nothing here
}
Variant(const char *s) :
type(VariantType::string)
{
new (&stringValue) std::string(s);
}
Variant(const std::vector<Variant> &a) :
type(VariantType::array)
{
new (&arrayValue) std::vector<Variant>(a);
}
Variant(const std::map<std::string, Variant> &m) :
type(VariantType::map)
{
new (&mapValue) std::map<std::string, Variant>(m);
}
~Variant()
{
free();
}
VariantType getType() const
{
return type;
}
int64_t getIntegerValue() const
{
switch (type) {
case VariantType::integer:
return integerValue;
case VariantType::number:
return static_cast<int64_t>(numberValue);
default:
throw EBadEntry{};
}
}
double getNumberValue() const
{
switch (type) {
case VariantType::integer:
return static_cast<double>(integerValue);
case VariantType::number:
return numberValue;
default:
throw EBadEntry{};
}
}
std::string getStringValue() const
{
switch (type) {
case VariantType::string:
return stringValue;
default:
throw EBadEntry {};
}
}
const std::vector<Variant>& getArrayValue() const
{
switch (type) {
case VariantType::array:
return arrayValue;
default:
throw EBadEntry {};
}
}
const std::map<std::string, Variant>& getMapValue() const
{
switch (type) {
case VariantType::map:
return mapValue;
default:
throw EBadEntry {};
}
}
friend std::ostream& operator<< (std::ostream& os, const Variant &v);
};
}
}
#endif /* _OUSIA_VARIANT_HPP_ */
|