-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
variant_value.hpp
492 lines (392 loc) · 11.2 KB
/
variant_value.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
/*
Copyright (C) 2017 by the Battle for Wesnoth Project http://www.wesnoth.org/
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 2 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.
See the COPYING file for more details.
*/
#ifndef VARIANT_PRIVATE_HPP_INCLUDED
#define VARIANT_PRIVATE_HPP_INCLUDED
#include "exceptions.hpp"
#include "formula/callable_fwd.hpp"
#include "utils/general.hpp"
#include "utils/make_enum.hpp"
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <utility>
#include <vector>
class variant;
/** The various types the variant class is designed to handle */
MAKE_ENUM(VARIANT_TYPE,
(TYPE_NULL, "null")
(TYPE_INT, "int")
(TYPE_DECIMAL, "decimal")
(TYPE_CALLABLE, "object")
(TYPE_LIST, "list")
(TYPE_STRING, "string")
(TYPE_MAP, "map")
);
using variant_vector = std::vector<variant>;
using variant_map_raw = std::map<variant, variant>;
struct type_error : public game::error
{
explicit type_error(const std::string& str);
};
namespace game_logic
{
class formula_callable;
class variant_value_base;
using value_base_ptr = std::shared_ptr<variant_value_base>;
/** Casts a @ref variant_value_base shared pointer to a new derived type. */
template<typename T>
static std::shared_ptr<T> value_cast(value_base_ptr ptr)
{
std::shared_ptr<T> res = std::dynamic_pointer_cast<T>(ptr);
if(!res) {
throw type_error("Could not cast type");
}
return res;
}
/** Casts a @ref variant_value_base reference to a new derived type. */
template<typename T>
static T& value_ref_cast(variant_value_base& ptr)
{
try {
return dynamic_cast<T&>(ptr);
} catch(std::bad_cast&) {
throw type_error("Could not cast type");
}
}
/**
* Base class for all variant types.
*
* This provides a common interface for all type classes to implement, as well as
* giving variant a base pointer type for its value member. It also serves as the
* implementation of the 'null' variant value.
*
* Do note this class should *not* implement any data members.
*/
class variant_value_base
{
public:
/** Returns the number of elements in a type. Not relevant for every derivative. */
virtual size_t num_elements() const
{
return 0;
}
/** Whether the stored value is considered empty or not. */
virtual bool is_empty() const
{
return true;
}
/** Returns the stored variant value in plain string form. */
virtual std::string string_cast() const
{
return "0";
}
/** Returns the stored variant value in formula syntax. */
virtual std::string get_serialized_string() const
{
return "null()";
}
/** Returns debug info for the variant value. */
virtual std::string get_debug_string(formula_seen_stack& /*seen*/, bool /*verbose*/) const
{
return get_serialized_string();
}
/** Returns a bool expression of the variant value. */
virtual bool as_bool() const
{
return false;
}
/** Called to determine if this variant is equal to another _of the same type_.
This function is _only_ called if get_type() returns the same result for both arguments. */
virtual bool equals(variant_value_base& /*other*/) const
{
return true; // null is equal to null
}
/** Called to determine if this variant is less than another _of the same type_.
This function is _only_ called if get_type() returns the same result for both arguments. */
virtual bool less_than(variant_value_base& /*other*/) const
{
return false; // null is not less than null
}
/** Returns the id of the variant type */
virtual const VARIANT_TYPE& get_type() const
{
static VARIANT_TYPE type = VARIANT_TYPE::TYPE_NULL;
return type;
}
};
class variant_int : public virtual variant_value_base
{
public:
explicit variant_int(int value) : value_(value) {}
virtual bool as_bool() const override
{
return value_ != 0;
}
int get_integer() const
{
return value_;
}
variant build_range_variant(int limit) const;
virtual std::string string_cast() const override
{
return std::to_string(value_);
}
virtual std::string get_serialized_string() const override
{
return string_cast();
}
virtual std::string get_debug_string(formula_seen_stack& /*seen*/, bool /*verbose*/) const override
{
return string_cast();
}
virtual bool equals(variant_value_base& other) const override
{
return value_ == value_ref_cast<variant_int>(other).value_;
}
virtual bool less_than(variant_value_base& other) const override
{
return value_ < value_ref_cast<variant_int>(other).value_;
}
virtual const VARIANT_TYPE& get_type() const override
{
static VARIANT_TYPE type = VARIANT_TYPE::TYPE_INT;
return type;
}
private:
int value_;
};
class variant_decimal : public virtual variant_value_base
{
public:
explicit variant_decimal(int value) : value_(value) {}
explicit variant_decimal(double value) : value_(0)
{
value *= 1000;
value_ = static_cast<int>(value);
value -= value_;
if(value > 0.5) {
value_++;
} else if(value < -0.5) {
value_--;
}
}
virtual bool as_bool() const override
{
return value_ != 0;
}
int get_decimal() const
{
return value_;
}
virtual std::string string_cast() const override
{
return to_string_impl(false);
}
virtual std::string get_serialized_string() const override
{
return to_string_impl(false);
}
virtual std::string get_debug_string(formula_seen_stack& /*seen*/, bool /*verbose*/) const override
{
return to_string_impl(true);
}
virtual bool equals(variant_value_base& other) const override
{
return value_ == value_ref_cast<variant_decimal>(other).value_;
}
virtual bool less_than(variant_value_base& other) const override
{
return value_ < value_ref_cast<variant_decimal>(other).value_;
}
virtual const VARIANT_TYPE& get_type() const override
{
static VARIANT_TYPE type = VARIANT_TYPE::TYPE_DECIMAL;
return type;
}
private:
std::string to_string_impl(const bool sign_value) const;
int value_;
};
class variant_callable : public virtual variant_value_base
{
public:
explicit variant_callable(const formula_callable* callable);
virtual bool as_bool() const override
{
return callable_ != nullptr;
}
virtual size_t num_elements() const override
{
return 1;
}
const formula_callable* get_callable() const
{
return callable_;
}
virtual std::string string_cast() const override
{
return "(object)";
}
virtual std::string get_serialized_string() const override;
virtual std::string get_debug_string(formula_seen_stack& seen, bool verbose) const override;
virtual bool equals(variant_value_base& other) const override;
virtual bool less_than(variant_value_base& other) const override;
virtual const VARIANT_TYPE& get_type() const override
{
static VARIANT_TYPE type = VARIANT_TYPE::TYPE_CALLABLE;
return type;
}
private:
const formula_callable* callable_;
};
class variant_string : public virtual variant_value_base
{
public:
explicit variant_string(const std::string& str) : string_(str) {}
virtual bool is_empty() const override
{
return string_.empty();
}
virtual bool as_bool() const override
{
return !is_empty();
}
const std::string& get_string() const
{
return string_;
}
virtual std::string string_cast() const override
{
return string_;
}
virtual std::string get_serialized_string() const override;
virtual std::string get_debug_string(formula_seen_stack& /*seen*/, bool /*verbose*/) const override
{
return string_;
}
virtual bool equals(variant_value_base& other) const override
{
return string_ == value_ref_cast<variant_string>(other).string_;
}
virtual bool less_than(variant_value_base& other) const override
{
return string_ < value_ref_cast<variant_string>(other).string_;
}
virtual const VARIANT_TYPE& get_type() const override
{
static VARIANT_TYPE type = VARIANT_TYPE::TYPE_STRING;
return type;
}
private:
std::string string_;
};
/**
* Generalized implementation handling container variants.
*
* This class shouldn't usually be used directly. Instead, it's better to
* create a new derived class specialized to a specific container type.
*/
template<typename T>
class variant_container : public virtual variant_value_base
{
public:
explicit variant_container(const T& container)
: container_(container)
{
// NOTE: add more conditions if this changes.
static_assert((std::is_same<variant_vector, T>::value || std::is_same<variant_map_raw, T>::value),
"variant_container only accepts vector or map specifications.");
}
virtual bool is_empty() const override
{
return container_.empty();
}
virtual size_t num_elements() const override
{
return container_.size();
}
virtual bool as_bool() const override
{
return !is_empty();
}
T& get_container()
{
return container_;
}
const T& get_container() const
{
return container_;
}
virtual std::string string_cast() const override;
virtual std::string get_serialized_string() const override;
virtual std::string get_debug_string(formula_seen_stack& seen, bool verbose) const override;
bool contains(const variant& member) const
{
return util::contains<T, variant>(container_, member);
}
protected:
using mod_func_t = std::function<std::string(const variant&)>;
virtual std::string to_string_detail(const typename T::value_type& value, mod_func_t mod_func) const = 0;
private:
/**
* Implementation to handle string conversion for @ref string_cast, @ref get_serialized_string,
* and @ref get_debug_string.
*
* Derived classes should provide type-specific value handling by implementing @ref to_string_detail.
*/
std::string to_string_impl(bool annotate, bool annotate_empty, mod_func_t mod_func) const;
T container_;
};
class variant_list : public variant_container<variant_vector>
{
public:
explicit variant_list(const variant_vector& vec)
: variant_container<variant_vector>(vec)
{}
/**
* Applies the provided function to the corresponding variants in this and another list.
*/
variant list_op(value_base_ptr second, std::function<variant(variant&, variant&)> op_func);
virtual bool equals(variant_value_base& other) const override;
virtual bool less_than(variant_value_base& other) const override;
virtual const VARIANT_TYPE& get_type() const override
{
static VARIANT_TYPE type = VARIANT_TYPE::TYPE_LIST;
return type;
}
private:
virtual std::string to_string_detail(const variant_vector::value_type& container_val, mod_func_t mod_func) const override
{
return mod_func(container_val);
}
};
class variant_map : public variant_container<variant_map_raw>
{
public:
explicit variant_map(const variant_map_raw& map)
: variant_container<variant_map_raw>(map)
{}
virtual bool equals(variant_value_base& other) const override;
virtual bool less_than(variant_value_base& other) const override;
virtual const VARIANT_TYPE& get_type() const override
{
static VARIANT_TYPE type = VARIANT_TYPE::TYPE_MAP;
return type;
}
private:
virtual std::string to_string_detail(const variant_map_raw::value_type& container_val, mod_func_t mod_func) const override;
};
using variant_container_vector = variant_container<variant_vector>;
using variant_container_map = variant_container<variant_map_raw>;
} // namespace game_logic
#endif