-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
variable_info.cpp
640 lines (596 loc) · 19.5 KB
/
variable_info.cpp
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
/*
Copyright (C) 2003 by David White <dave@whitevine.net>
Copyright (C) 2005 - 2014 by Philippe Plantier <ayin@anathas.org>
Part of 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.
*/
/**
* @file
* Manage WML-variables.
*/
#include "variable_info.hpp"
#include "game_config.hpp"
using namespace variable_info_3_detail;
/// general helpers
namespace
{
void resolve_negative_value(int size, int& val)
{
if(val < 0)
{
val = size + val;
}
//val is still < 0? We don't accept!
if(val < 0)
{
throw invalid_variablename_exception();
}
}
/// Checks if name is a valid key for an attribute_value/child
void check_valid_name(const std::string& name)
{
if(name.empty())
{
throw invalid_variablename_exception();
}
}
template<const variable_info_3_type vit>
typename maybe_const<vit, config>::type& get_child_at(typename maybe_const<vit, config>::type& cfg, const std::string& key, int index = 0);
template<>
config& get_child_at<vit_create_if_not_existent>(config& cfg, const std::string& key, int index)
{
assert(index >= 0);
check_valid_name(key);
// the 'create_if_not_existent' logic.
while(static_cast<int>(cfg.child_count(key)) <= index)
{
cfg.add_child(key);
}
return cfg.child(key, index);
}
//helper variable for get_child_at<vit_const>
const config empty_const_cfg;
template<>
const config& get_child_at<vit_const>(const config& cfg, const std::string& key, int index)
{
assert(index >= 0);
check_valid_name(key);
//cfg.child_or_empty does not support index parameter
if(const config& child = cfg.child(key, index))
{
return child;
}
else
{
return empty_const_cfg;
}
}
template<>
config& get_child_at<vit_throw_if_not_existent>(config& cfg, const std::string& key, int index)
{
assert(index >= 0);
check_valid_name(key);
if(config& child = cfg.child(key, index))
{
return child;
}
else
{
throw invalid_variablename_exception();
}
}
template <typename TVal>
config::attribute_value attribute_value_of(const TVal& val)
{
config::attribute_value v;
v = val;
return v;
}
/// @param TVisitor
/// TVisitor shoudl have 4 methods:
/// from_named if the variable ended with a .somename
/// from_indexed if the variable enden with .somename[someindex]
/// from_temporary if the variable is a readonly value (.somename.length)
/// from_start if the variablename was previously empty, this can only happen
/// during calculate_value()
/// TVisitor should derive from variable_info_visitor(_const) which makes default implementation for these (as not supported)
template <typename TVisitor>
typename TVisitor::result_type apply_visitor(const TVisitor& visitor, typename TVisitor::param_type state)
{
switch(state.type_)
{
case state_start:
return visitor.from_start(state);
case state_named:
return visitor.from_named(state);
case state_indexed:
return visitor.from_indexed(state);
case state_temporary:
return visitor.from_temporary(state);
default:
throw std::exception();
}
};
template <const variable_info_3_type vit, typename TResult>
class variable_info_visitor
{
public:
typedef TResult result_type;
typedef variable_info_3_state<vit>& param_type;
#define DEFAULTHANDLER(name) result_type name(param_type) const { throw invalid_variablename_exception(); }
DEFAULTHANDLER(from_start)
DEFAULTHANDLER(from_named)
DEFAULTHANDLER(from_indexed)
DEFAULTHANDLER(from_temporary)
#undef DEFAULTHANDLER
};
template <const variable_info_3_type vit, typename TResult>
class variable_info_visitor_const
{
public:
typedef TResult result_type;
typedef const variable_info_3_state<vit>& param_type;
#define DEFAULTHANDLER(name) result_type name(param_type) const { throw invalid_variablename_exception(); }
DEFAULTHANDLER(from_start)
DEFAULTHANDLER(from_named)
DEFAULTHANDLER(from_indexed)
DEFAULTHANDLER(from_temporary)
#undef DEFAULTHANDLER
};
}
/// calculate_value() helpers
namespace
{
/// Parses a ']' terminated string.
/// This is a important optimisation of lexical_cast_default
int parse_index(const char* index_str)
{
char* endptr;
int res = strtol(index_str, &endptr, 10);
if (*endptr != ']') {
res = 0;//default
}
if(res > int(game_config::max_loop))
{
throw invalid_variablename_exception();
}
return res;
}
/// Adds a '.<key>' to the current variable
template<const variable_info_3_type vit>
class get_variable_key_visitor
: public variable_info_visitor<vit, void>
{
public:
get_variable_key_visitor(const std::string& key) : key_(key) {}
void from_named(typename get_variable_key_visitor::param_type state) const
{
if(key_ == "length")
{
state.temp_val_ = state.child_->child_count(state.key_);
state.type_ = state_temporary;
return;
}
else
{
return do_from_config(get_child_at<vit>(*state.child_, state.key_, 0), state);
}
}
void from_start(typename get_variable_key_visitor::param_type state) const
{
return do_from_config(*state.child_, state);
}
void from_indexed(typename get_variable_key_visitor::param_type state) const
{
//we dont support aaa[0].length
return do_from_config(get_child_at<vit>(*state.child_, state.key_, state.index_), state);
}
private:
void do_from_config(typename maybe_const<vit, config>::type & cfg, typename get_variable_key_visitor::param_type state) const
{
state.type_ = state_named;
state.key_ = key_;
state.child_ = &cfg;
}
const std::string& key_;
};
/// appens a [index] to the variable.
/// we only support from_named since [index][index2] or a.length[index] both doesn't make sense.
template<const variable_info_3_type vit>
class get_variable_index_visitor
: public variable_info_visitor<vit, void>
{
public:
get_variable_index_visitor(int n) : n_(n) {}
void from_named(typename get_variable_index_visitor::param_type state) const
{
state.index_ = n_;
resolve_negative_value(state.child_->child_count(state.key_), state.index_);
state.type_ = state_indexed;
}
private:
const int n_;
};
}
/// as... visitors
namespace
{
///tries to convert it to an (maybe const) attribute value
template<const variable_info_3_type vit>
class as_skalar_visitor
: public variable_info_visitor_const<vit, typename maybe_const<vit, config::attribute_value>::type&>
{
public:
typename as_skalar_visitor::result_type from_named(typename as_skalar_visitor::param_type state) const
{
check_valid_name(state.key_);
return (*state.child_)[state.key_];
}
///Defined below for different cases.
typename as_skalar_visitor::result_type from_temporary(typename as_skalar_visitor::param_type state) const;
};
/// this type of value like '.length' are readonly values so we only support them for reading.
/// espacily we don't want to return non const references.
template<>
const config::attribute_value & as_skalar_visitor<vit_const>::from_temporary(typename as_skalar_visitor::param_type state) const
{
return state.temp_val_;
}
template<>
config::attribute_value & as_skalar_visitor<vit_create_if_not_existent>::from_temporary(typename as_skalar_visitor::param_type) const
{
throw invalid_variablename_exception();
}
template<>
config::attribute_value & as_skalar_visitor<vit_throw_if_not_existent>::from_temporary(typename as_skalar_visitor::param_type) const
{
throw invalid_variablename_exception();
}
/// tries to convert to a (const) config&, unlike range based operation this also supports 'from_start'
/// Note: Currently getting the 'from_start' case here is impossible, becasue we always apply at least one string key.
template<const variable_info_3_type vit>
class as_container_visitor
: public variable_info_visitor_const<vit, typename maybe_const<vit, config>::type&>
{
public:
typename as_container_visitor::result_type from_named(typename as_container_visitor::param_type state) const
{
return get_child_at<vit>(*state.child_, state.key_, 0);
}
typename as_container_visitor::result_type from_start(typename as_container_visitor::param_type state) const
{
return *state.child_;
}
typename as_container_visitor::result_type from_indexed(typename as_container_visitor::param_type state) const
{
return get_child_at<vit>(*state.child_, state.key_, state.index_);
}
};
}
/// range_based operations
namespace {
template<const variable_info_3_type vit, typename THandler>
class as_range_visitor_base
: public variable_info_visitor_const<vit, typename THandler::result_type>
{
public:
as_range_visitor_base(const THandler& handler) : handler_(handler) {}
typename as_range_visitor_base::result_type from_named(typename as_range_visitor_base::param_type state) const
{
return handler_(*state.child_, state.key_, 0, state.child_->child_count(state.key_));
}
typename as_range_visitor_base::result_type from_indexed(typename as_range_visitor_base::param_type state) const
{
//Ensure we have a config at the given explicit position.
get_child_at<vit>(*state.child_, state.key_, state.index_);
return handler_(*state.child_, state.key_, state.index_, state.index_ + 1);
}
private:
const THandler& handler_;
};
template<const variable_info_3_type vit>
class variable_as_array_h
{
public:
typedef typename maybe_const<vit, config::child_itors>::type result_type;
result_type operator()(typename maybe_const<vit, config>::type& child, const std::string& key, int startindex, int endindex) const
{
result_type r = child.child_range(key);
std::advance(r.first, startindex);
r.second = r.first;
std::advance(r.second, endindex - startindex);
return r;
}
};
/**
replaces the child in [startindex, endindex) with 'source'
'insert' and 'append' are subcases of this.
*/
class replace_range_h
{
public:
typedef config::child_itors result_type;
replace_range_h(std::vector<config>& source) : datasource_(source) { }
result_type operator()(config& child, const std::string& key, int startindex, int endindex) const
{
int size_diff = datasource_.size() - (endindex - startindex);
//remove configs first
while(size_diff < 0)
{
child.remove_child(key, startindex);
++size_diff;
}
size_t index = 0;
for(index = 0; index < static_cast<size_t>(size_diff); ++index)
{
child.add_child_at(key, config(), startindex + index).swap(datasource_[index]);
}
for(; index < datasource_.size(); ++index)
{
child.child(key, startindex + index).swap(datasource_[index]);
}
/// variable_as_array_h only uses the template argument (vit_throw_if_not_existent here)
/// to determine constness which is always false here
return variable_as_array_h<vit_throw_if_not_existent>()(child, key, startindex, datasource_.size());
}
private:
std::vector<config>& datasource_;
};
class insert_range_h : replace_range_h
{
public:
typedef config::child_itors result_type;
insert_range_h(std::vector<config>& source) : replace_range_h(source) { }
result_type operator()(config& child, const std::string& key, int startindex, int /*endindex*/) const
{
//insert == replace empty range with data.
return replace_range_h::operator()(child, key, startindex, startindex);
}
};
class append_range_h : insert_range_h
{
public:
typedef config::child_itors result_type;
append_range_h(std::vector<config>& source) : insert_range_h(source) { }
result_type operator()(config& child, const std::string& key, int /*startindex*/, int /*endindex*/) const
{
//append == insert at end.
int inser_pos = child.child_count(key);
return insert_range_h::operator()(child, key, inser_pos, inser_pos /*ignored by insert_range_h*/);
}
};
class merge_range_h
{
public:
typedef void result_type;
merge_range_h(std::vector<config>& source) : datasource_(source) { }
void operator()(config& child, const std::string& key, int startindex, int /*endindex*/) const
{
//the merge_with function accepts only configs so we convert vector -> config.
config datatemp;
//Add emtpy config to 'shift' the merge to startindex
for(int index = 0; index < startindex; ++index)
{
datatemp.add_child(key);
}
//move datasource_ -> datatemp
for(size_t index = 0; index < datasource_.size(); ++index)
{
datatemp.add_child(key).swap(datasource_[index]);
}
child.merge_with(datatemp);
}
private:
std::vector<config>& datasource_;
};
}//annonymous namespace end based operations
/// misc
namespace
{
template<const variable_info_3_type vit>
class clear_value_visitor
: public variable_info_visitor_const<vit, void>
{
public:
clear_value_visitor(bool only_tables) : only_tables_(only_tables) {}
void from_named(typename clear_value_visitor::param_type state) const
{
if(!only_tables_)
{
state.child_->remove_attribute(state.key_);
}
state.child_->clear_children(state.key_);
}
void from_indexed(typename clear_value_visitor::param_type state) const
{
state.child_->remove_child(state.key_, state.index_);
}
private:
bool only_tables_;
};
template<const variable_info_3_type vit>
class exists_as_container_visitor
: public variable_info_visitor_const<vit, bool>
{
public:
typename exists_as_container_visitor::result_type from_named(typename exists_as_container_visitor::param_type state) const
{
return state.child_->has_child(state.key_);
}
typename exists_as_container_visitor::result_type from_indexed(typename exists_as_container_visitor::param_type state) const
{
return state.child_->child_count(state.key_) > static_cast<size_t>(state.index_);
}
typename exists_as_container_visitor::result_type from_start(typename exists_as_container_visitor::param_type) const
{
return true;
}
typename exists_as_container_visitor::result_type from_temporary(typename exists_as_container_visitor::param_type) const
{
return false;
}
};
}
template<const variable_info_3_type vit>
variable_info_3<vit>::variable_info_3(const std::string& varname, t_config& vars)
: name_(varname)
, state_(vars)
, valid_(true)
{
try
{
this->calculate_value();
}
catch(const invalid_variablename_exception&)
{
this->valid_ = false;
}
}
template<const variable_info_3_type vit>
variable_info_3<vit>::~variable_info_3()
{
}
template<const variable_info_3_type vit>
void variable_info_3<vit>::calculate_value()
{
// this->state_ is initialized in the constructor.
size_t previous_index = 0;
size_t name_size = this->name_.size();
for(size_t loop_index = 0; loop_index < name_size; loop_index++)
{
switch(this->name_[loop_index])
{
case '.':
case '[':
// '.', '[' mark the end of a string key.
// the result is oviously that '.' and '[' are
// treated equally so 'aaa.9].bbbb[zzz.uu.7]'
// is interpreted as 'aaa[9].bbbb.zzz.uu[7]'
// use is_valid_variable function for stricter variablename checking.
apply_visitor(get_variable_key_visitor<vit>(this->name_.substr(previous_index, loop_index-previous_index)), this->state_);
previous_index = loop_index + 1;
break;
case ']':
// ']' marks the end of an integer key.
apply_visitor(get_variable_index_visitor<vit>(parse_index(&this->name_[previous_index])), this->state_);
//after ']' we always expect a '.' or the end of the string
//ignore the next char which is a '.'
loop_index++;
if(loop_index < this->name_.length() && this->name_[loop_index] != '.')
{
throw invalid_variablename_exception();
}
previous_index = loop_index + 1;
break;
default:
break;
}
}
if(previous_index != this->name_.length() + 1)
{
// the string ended not with ']'
// in this case we still didn't add the key behind the last '.'
apply_visitor(get_variable_key_visitor<vit>(this->name_.substr(previous_index)), this->state_);
}
}
template<const variable_info_3_type vit>
bool variable_info_3<vit>::explicit_index() const
{
throw_on_invalid();
return this->state_.type_ == state_start || this->state_.type_ == state_indexed;
}
template<const variable_info_3_type vit>
typename maybe_const<vit, config::attribute_value>::type& variable_info_3<vit>::as_scalar() const
{
throw_on_invalid();
return apply_visitor(as_skalar_visitor<vit>(), this->state_);
}
template<const variable_info_3_type vit>
typename maybe_const<vit, config>::type& variable_info_3<vit>::as_container() const
{
throw_on_invalid();
return apply_visitor(as_container_visitor<vit>(), this->state_);
}
template<const variable_info_3_type vit>
typename maybe_const<vit, config::child_itors>::type variable_info_3<vit>::as_array() const
{
throw_on_invalid();
return apply_visitor(as_range_visitor_base<vit,variable_as_array_h<vit> >(variable_as_array_h<vit>()), this->state_);
}
template<const variable_info_3_type vit>
void variable_info_3<vit>::throw_on_invalid() const
{
if(!this->valid_)
{
throw invalid_variablename_exception();
}
}
template<>
std::string variable_info_3<vit_const>::get_error_message() const
{
return "Cannot resolve variablename '" + this->name_ + "' for reading.";
}
template<>
std::string variable_info_3<vit_create_if_not_existent>::get_error_message() const
{
return "Cannot resolve variablename '" + this->name_ + "' for writing.";
}
template<>
std::string variable_info_3<vit_throw_if_not_existent>::get_error_message() const
{
return "Cannot resolve variablename '" + this->name_ + "' for writing without creating new childs.";
}
template<const variable_info_3_type vit>
void non_const_variable_info_3<vit>::clear(bool only_tables) const
{
this->throw_on_invalid();
return apply_visitor(clear_value_visitor<vit>(only_tables), this->state_);
}
template<const variable_info_3_type vit>
config::child_itors non_const_variable_info_3<vit>::append_array(std::vector<config> childs) const
{
this->throw_on_invalid();
return apply_visitor(as_range_visitor_base<vit,append_range_h>(append_range_h(childs)), this->state_);
}
template<const variable_info_3_type vit>
config::child_itors non_const_variable_info_3<vit>::insert_array(std::vector<config> childs) const
{
this->throw_on_invalid();
return apply_visitor(as_range_visitor_base<vit,insert_range_h>(insert_range_h(childs)), this->state_);
}
template<const variable_info_3_type vit>
config::child_itors non_const_variable_info_3<vit>::replace_array(std::vector<config> childs) const
{
this->throw_on_invalid();
return apply_visitor(as_range_visitor_base<vit,replace_range_h>(replace_range_h(childs)), this->state_);
}
template<const variable_info_3_type vit>
void non_const_variable_info_3<vit>::merge_array(std::vector<config> childs) const
{
this->throw_on_invalid();
apply_visitor(as_range_visitor_base<vit,merge_range_h>(merge_range_h(childs)), this->state_);
}
template<const variable_info_3_type vit>
bool variable_info_3<vit>::exists_as_attribute() const
{
this->throw_on_invalid();
return (this->state_.type_ == state_temporary) || ((this->state_.type_ == state_named) && this->state_.child_->has_attribute(this->state_.key_));
}
template<const variable_info_3_type vit>
bool variable_info_3<vit>::exists_as_container() const
{
this->throw_on_invalid();
return apply_visitor(exists_as_container_visitor<vit>(), this->state_);
}
///explicit instantiations
template class variable_info_3<vit_const>;
template class variable_info_3<vit_create_if_not_existent>;
template class variable_info_3<vit_throw_if_not_existent>;
template class non_const_variable_info_3<vit_create_if_not_existent>;
template class non_const_variable_info_3<vit_throw_if_not_existent>;