The Battle for Wesnoth  1.15.3+dev
value_translator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  */
18 
19 #pragma once
20 
21 #include "ai/composite/engine.hpp"
22 #include "ai/composite/stage.hpp"
23 
24 #include "ai/manager.hpp"
25 #include "terrain/filter.hpp"
26 #include "lexical_cast.hpp"
28 #include "resources.hpp"
30 
31 namespace ai {
32 
33 template<typename T>
35 public:
36 
37  static T cfg_to_value(const config &cfg)
38  {
39  return lexical_cast_default<T>(cfg["value"]);
40  }
41 
42  static void cfg_to_value(const config &cfg, T &value)
43  {
44  value = cfg_to_value(cfg);
45  }
46 
47  static void value_to_cfg(const T &value, config &cfg)
48  {
49  cfg["value"] = lexical_cast<std::string>(value);
50  }
51 
52  static config value_to_cfg(const T &value)
53  {
54  config cfg;
55  value_to_cfg(value,cfg);
56  return cfg;
57  }
58 };
59 
60 
61 template<>
63 public:
64 
65  static std::string cfg_to_value(const config &cfg)
66  {
67  return cfg["value"].str();
68  }
69 
70  static void cfg_to_value(const config &cfg, std::string &value)
71  {
72  value = cfg_to_value(cfg);
73  }
74 
75  static void value_to_cfg(const std::string &value, config &cfg)
76  {
77  cfg["value"] = value;
78  }
79 
80  static config value_to_cfg(const std::string &value)
81  {
82  config cfg;
83  value_to_cfg(value,cfg);
84  return cfg;
85  }
86 
87 };
88 
89 
90 template<>
92 public:
93 
94  static bool cfg_to_value(const config &cfg)
95  {
96  return cfg["value"].to_bool();
97  }
98 
99  static void cfg_to_value(const config &cfg, bool &value)
100  {
101  value = cfg_to_value(cfg);
102  }
103 
104  static void value_to_cfg(const bool &value, config &cfg)
105  {
106  cfg["value"] = value;
107  }
108 
109  static config value_to_cfg(const bool &value)
110  {
111  config cfg;
112  value_to_cfg(value,cfg);
113  return cfg;
114  }
115 };
116 
117 class leader_aspects_visitor : public boost::static_visitor<std::string> {
118 public:
119  std::string operator()(const bool b) const {
120  if (b) {
121  return "yes";
122  } else {
123  return "no";
124  }
125  }
126  std::string operator()(const std::vector<std::string> s) const { return utils::join(s); }
127 };
128 
129 template<>
130 class config_value_translator< boost::variant<bool, std::vector<std::string>> > {
131 public:
132 
133  static boost::variant<bool, std::vector<std::string>> cfg_to_value(const config &cfg)
134  {
135  if (cfg["value"].to_bool(true) == cfg["value"].to_bool(false)) {
136  return cfg["value"].to_bool();
137  }
138  return utils::split(cfg["value"]);
139  }
140 
141  static void cfg_to_value(const config &cfg, boost::variant<bool, std::vector<std::string>> &value)
142  {
143  value = cfg_to_value(cfg);
144  }
145 
146  static void value_to_cfg(const boost::variant<bool, std::vector<std::string>> &value, config &cfg)
147  {
148  cfg["value"] = boost::apply_visitor(leader_aspects_visitor(), value);
149  }
150 
151  static config value_to_cfg(const boost::variant<bool, std::vector<std::string>> &value)
152  {
153  config cfg;
154  value_to_cfg(value,cfg);
155  return cfg;
156  }
157 };
158 
159 template<>
160 class config_value_translator< std::vector<std::string>> {
161 public:
162 
163  static std::vector<std::string> cfg_to_value(const config &cfg)
164  {
165  return utils::split(cfg["value"]);
166  }
167 
168  static void cfg_to_value(const config &cfg, std::vector<std::string> &value)
169  {
170  value = cfg_to_value(cfg);
171  }
172 
173  static void value_to_cfg(const std::vector<std::string> &value, config &cfg)
174  {
175  cfg["value"] = utils::join(value);
176  }
177 
178  static config value_to_cfg(const std::vector<std::string> &value)
179  {
180  config cfg;
181  value_to_cfg(value,cfg);
182  return cfg;
183  }
184 };
185 
186 template<>
188 public:
189 
190  static void cfg_to_value(const config &cfg, config &value)
191  {
192  if (const config &v = cfg.child("value")) {
193  value = v;
194  } else {
195  value.clear();
196  }
197  }
198 
199  static void value_to_cfg(const config &value, config &cfg)
200  {
201  cfg.add_child("value",value);
202  }
203 
204  static config value_to_cfg(const config &value)
205  {
206  config cfg;
207  value_to_cfg(value,cfg);
208  return cfg;
209  }
210 
211  static config cfg_to_value(const config &cfg)
212  {
213  return cfg.child_or_empty("value");
214  }
215 };
216 
217 template<>
218 class config_value_translator<terrain_filter> {
219 public:
220 
221  static terrain_filter cfg_to_value(const config &cfg)
222  {
223  if (const config &v = cfg.child("value")) {
224  return terrain_filter(vconfig(v), resources::filter_con);
225  }
226  static config c("not");
227  return terrain_filter(vconfig(c),resources::filter_con);
228  }
229 
230  static void cfg_to_value(const config &cfg, terrain_filter &value)
231  {
232  value = cfg_to_value(cfg);
233  }
234 
235  static void value_to_cfg(const terrain_filter &value, config &cfg)
236  {
237  cfg.add_child("value",value.to_config());
238  }
239 
240  static config value_to_cfg(const terrain_filter &value)
241  {
242  config cfg;
243  value_to_cfg(value,cfg);
244  return cfg;
245  }
246 };
247 
248 template<>
250 public:
251 
253  {
254  return unit_advancements_aspect(cfg["value"]);
255  }
256 
257  static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
258  {
259  value = cfg_to_value(cfg);
260  }
261 
262  static void value_to_cfg(const unit_advancements_aspect &value, config &cfg)
263  {
264  cfg["value"] = value.get_value();
265 
266  }
267 
269  {
270  config cfg;
271  value_to_cfg(value,cfg);
272  return cfg;
273  }
274 };
275 
276 
277 
278 // variant value translator
279 
280 template<typename T>
282 public:
283 
284  static void variant_to_value(const wfl::variant &/*var*/, T &/*value*/)
285  {
286  assert(false);//not implemented
287  }
288 
289  static void value_to_variant(const T &/*value*/, wfl::variant &/*var*/)
290  {
291  assert(false);//not implemented
292  }
293 
294  static wfl::variant value_to_variant(const T &value)
295  {
296  wfl::variant var;
297  value_to_variant(value,var);
298  return var;
299  }
300 
301  static T variant_to_value(const wfl::variant &var)
302  {
303  T value = T();
304  variant_to_value(var,value);
305  return value;
306  }
307 };
308 
309 template<>
311 public:
312 
313  static void variant_to_value(const wfl::variant &var, int &value)
314  {
315  value = var.as_int();
316  }
317 
318  static void value_to_variant(const int &value, wfl::variant &var)
319  {
320  var = wfl::variant(value);
321  }
322 
323  static wfl::variant value_to_variant(const int &value)
324  {
325  wfl::variant var;
326  value_to_variant(value,var);
327  return var;
328  }
329 
330  static int variant_to_value(const wfl::variant &var)
331  {
332  int value;
333  variant_to_value(var,value);
334  return value;
335  }
336 };
337 
338 
339 template<>
341 public:
342 
343  static void variant_to_value(const wfl::variant &var, bool &value)
344  {
345  value = var.as_bool();
346  }
347 
348  static void value_to_variant(const bool &value, wfl::variant &var)
349  {
350  var = wfl::variant(value);
351  }
352 
353  static wfl::variant value_to_variant(const bool &value)
354  {
355  wfl::variant var;
356  value_to_variant(value,var);
357  return var;
358  }
359 
360  static bool variant_to_value(const wfl::variant &var)
361  {
362  bool value;
363  variant_to_value(var,value);
364  return value;
365  }
366 };
367 
368 
369 
370 template<>
372 public:
373 
374  static void variant_to_value(const wfl::variant &var, std::string &value)
375  {
376  value = var.as_string();
377  }
378 
379  static void value_to_variant(const std::string &value, wfl::variant &var)
380  {
381  var = wfl::variant(value);
382  }
383 
385  {
386  wfl::variant var;
387  value_to_variant(value,var);
388  return var;
389  }
390 
392  {
393  std::string value;
394  variant_to_value(var,value);
395  return value;
396  }
397 };
398 
399 
400 
401 template<>
403 public:
404 
405  static void variant_to_value(const wfl::variant &/*var*/, attacks_vector &/*value*/)
406  {
407  assert(false);//not implemented
408  }
409 
410  static void value_to_variant(const attacks_vector &value, wfl::variant &var)
411  {
412  std::vector<wfl::variant> vars;
413  for(attacks_vector::const_iterator i = value.begin(); i != value.end(); ++i) {
414  vars.emplace_back(std::make_shared<attack_analysis>(*i));
415  }
416  var = wfl::variant(vars);
417  }
418 
420  {
421  wfl::variant var;
422  value_to_variant(value,var);
423  return var;
424  }
425 
427  {
428  attacks_vector value;
429  variant_to_value(var,value);
430  return value;
431  }
432 };
433 
434 
435 template<>
436 class variant_value_translator<terrain_filter> {
437 public:
438 
439  static void variant_to_value(const wfl::variant &/*var*/, terrain_filter &/*value*/)
440  {
441  assert(false);//not implemented
442  }
443 
444  static void value_to_variant(const terrain_filter &/*value*/, wfl::variant &/*var*/)
445  {
446  assert(false);//not implemented
447  }
448 
449  static wfl::variant value_to_variant(const terrain_filter &value)
450  {
451  wfl::variant var;
452  value_to_variant(value,var);
453  return var;
454  }
455 
456  static terrain_filter variant_to_value(const wfl::variant &var)
457  {
458  static config c("not");
459  terrain_filter value(vconfig(c),resources::filter_con);
460  variant_to_value(var,value);
461  return value;
462  }
463 };
464 } //end of namespace ai
static config cfg_to_value(const config &cfg)
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:420
static void cfg_to_value(const config &cfg, bool &value)
V::result_t apply_visitor(typename V::param_t state, T &&... args)
Helper function to apply the result of a specified visitor to a variable_info object.
static void value_to_cfg(const std::string &value, config &cfg)
static std::string variant_to_value(const wfl::variant &var)
static config value_to_cfg(const terrain_filter &value)
static void value_to_variant(const bool &value, wfl::variant &var)
static void value_to_cfg(const bool &value, config &cfg)
static void cfg_to_value(const config &cfg, config &value)
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
static wfl::variant value_to_variant(const int &value)
static terrain_filter variant_to_value(const wfl::variant &var)
static void variant_to_value(const wfl::variant &var, int &value)
New lexcical_cast header.
int as_int() const
Definition: variant.cpp:292
static void value_to_variant(const T &, wfl::variant &)
static wfl::variant value_to_variant(const terrain_filter &value)
static std::string cfg_to_value(const config &cfg)
static config value_to_cfg(const unit_advancements_aspect &value)
static void variant_to_value(const wfl::variant &var, std::string &value)
Composite AI stages.
static unit_advancements_aspect cfg_to_value(const config &cfg)
AI Support engine - creating specific ai components from config.
STL namespace.
static wfl::variant value_to_variant(const attacks_vector &value)
void clear()
Definition: config.cpp:863
static void value_to_cfg(const unit_advancements_aspect &value, config &cfg)
To lexical_cast(From value)
Lexical cast converts one type to another.
static terrain_filter cfg_to_value(const config &cfg)
static wfl::variant value_to_variant(const std::string &value)
static void variant_to_value(const wfl::variant &, attacks_vector &)
static void value_to_cfg(const std::vector< std::string > &value, config &cfg)
static void value_to_cfg(const terrain_filter &value, config &cfg)
const std::string & as_string() const
Definition: variant.cpp:319
#define b
std::vector< attack_analysis > attacks_vector
Definition: game_info.hpp:50
static void cfg_to_value(const config &cfg, std::vector< std::string > &value)
static void cfg_to_value(const config &cfg, std::string &value)
A small explanation about what&#39;s going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
static void value_to_cfg(const config &value, config &cfg)
static void value_to_variant(const int &value, wfl::variant &var)
static void cfg_to_value(const config &cfg, terrain_filter &value)
static void value_to_variant(const std::string &value, wfl::variant &var)
static config value_to_cfg(const T &value)
filter_context * filter_con
Definition: resources.cpp:23
static void cfg_to_value(const config &cfg, T &value)
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands...
static std::vector< std::string > cfg_to_value(const config &cfg)
static void variant_to_value(const wfl::variant &, T &)
static config value_to_cfg(const boost::variant< bool, std::vector< std::string >> &value)
static config value_to_cfg(const std::string &value)
static T cfg_to_value(const config &cfg)
std::string operator()(const bool b) const
static bool cfg_to_value(const config &cfg)
std::size_t i
Definition: function.cpp:933
static void cfg_to_value(const config &cfg, boost::variant< bool, std::vector< std::string >> &value)
static map_location::DIRECTION s
static boost::variant< bool, std::vector< std::string > > cfg_to_value(const config &cfg)
static config value_to_cfg(const bool &value)
static void variant_to_value(const wfl::variant &, terrain_filter &)
static T variant_to_value(const wfl::variant &var)
static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
const std::string get_value() const
config & add_child(config_key_type key)
Definition: config.cpp:476
std::string operator()(const std::vector< std::string > s) const
std::vector< std::string > split(const config_attribute_value &val)
static void variant_to_value(const wfl::variant &var, bool &value)
static void value_to_cfg(const T &value, config &cfg)
A variable-expanding proxy for the config class.
Definition: variable.hpp:44
static int variant_to_value(const wfl::variant &var)
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:453
static void value_to_cfg(const boost::variant< bool, std::vector< std::string >> &value, config &cfg)
bool as_bool() const
Returns a boolean state of the variant value.
Definition: variant.cpp:314
static void value_to_variant(const attacks_vector &value, wfl::variant &var)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
static config value_to_cfg(const std::vector< std::string > &value)
static bool variant_to_value(const wfl::variant &var)
static wfl::variant value_to_variant(const T &value)
static config value_to_cfg(const config &value)
static attacks_vector variant_to_value(const wfl::variant &var)
static wfl::variant value_to_variant(const bool &value)
static void value_to_variant(const terrain_filter &, wfl::variant &)