The Battle for Wesnoth  1.15.11+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"
24 #include "ai/manager.hpp"
25 #include "lexical_cast.hpp"
26 #include "resources.hpp"
28 #include "terrain/filter.hpp"
29 #include "utils/variant.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 template<>
61 class config_value_translator<std::string> {
62 public:
63 
64  static std::string cfg_to_value(const config &cfg)
65  {
66  return cfg["value"].str();
67  }
68 
69  static void cfg_to_value(const config &cfg, std::string &value)
70  {
71  value = cfg_to_value(cfg);
72  }
73 
74  static void value_to_cfg(const std::string &value, config &cfg)
75  {
76  cfg["value"] = value;
77  }
78 
79  static config value_to_cfg(const std::string &value)
80  {
81  config cfg;
82  value_to_cfg(value,cfg);
83  return cfg;
84  }
85 
86 };
87 
88 template<>
90 public:
91 
92  static bool cfg_to_value(const config &cfg)
93  {
94  return cfg["value"].to_bool();
95  }
96 
97  static void cfg_to_value(const config &cfg, bool &value)
98  {
99  value = cfg_to_value(cfg);
100  }
101 
102  static void value_to_cfg(const bool &value, config &cfg)
103  {
104  cfg["value"] = value;
105  }
106 
107  static config value_to_cfg(const bool &value)
108  {
109  config cfg;
110  value_to_cfg(value,cfg);
111  return cfg;
112  }
113 };
114 
116 #ifdef USING_BOOST_VARIANT
117  : public boost::static_visitor<std::string>
118 #endif
119 {
120 public:
121  std::string operator()(const bool b) const {
122  if (b) {
123  return "yes";
124  } else {
125  return "no";
126  }
127  }
128  std::string operator()(const std::vector<std::string> s) const { return utils::join(s); }
129 };
130 
131 template<>
132 class config_value_translator<utils::variant<bool, std::vector<std::string>>> {
133 public:
134 
135  static utils::variant<bool, std::vector<std::string>> cfg_to_value(const config &cfg)
136  {
137  if (cfg["value"].to_bool(true) == cfg["value"].to_bool(false)) {
138  return cfg["value"].to_bool();
139  }
140  return utils::split(cfg["value"]);
141  }
142 
143  static void cfg_to_value(const config &cfg, utils::variant<bool, std::vector<std::string>> &value)
144  {
145  value = cfg_to_value(cfg);
146  }
147 
148  static void value_to_cfg(const utils::variant<bool, std::vector<std::string>> &value, config &cfg)
149  {
150  cfg["value"] = utils::visit(leader_aspects_visitor(), value);
151  }
152 
153  static config value_to_cfg(const utils::variant<bool, std::vector<std::string>> &value)
154  {
155  config cfg;
156  value_to_cfg(value,cfg);
157  return cfg;
158  }
159 };
160 
161 template<>
162 class config_value_translator< std::vector<std::string>> {
163 public:
164 
165  static std::vector<std::string> cfg_to_value(const config &cfg)
166  {
167  return utils::split(cfg["value"]);
168  }
169 
170  static void cfg_to_value(const config &cfg, std::vector<std::string> &value)
171  {
172  value = cfg_to_value(cfg);
173  }
174 
175  static void value_to_cfg(const std::vector<std::string> &value, config &cfg)
176  {
177  cfg["value"] = utils::join(value);
178  }
179 
180  static config value_to_cfg(const std::vector<std::string> &value)
181  {
182  config cfg;
183  value_to_cfg(value,cfg);
184  return cfg;
185  }
186 };
187 
188 template<>
190 public:
191 
192  static void cfg_to_value(const config &cfg, config &value)
193  {
194  if (const config &v = cfg.child("value")) {
195  value = v;
196  } else {
197  value.clear();
198  }
199  }
200 
201  static void value_to_cfg(const config &value, config &cfg)
202  {
203  cfg.add_child("value",value);
204  }
205 
206  static config value_to_cfg(const config &value)
207  {
208  config cfg;
209  value_to_cfg(value,cfg);
210  return cfg;
211  }
212 
213  static config cfg_to_value(const config &cfg)
214  {
215  return cfg.child_or_empty("value");
216  }
217 };
218 
219 template<>
220 class config_value_translator<terrain_filter> {
221 public:
222 
223  static terrain_filter cfg_to_value(const config &cfg)
224  {
225  if (const config &v = cfg.child("value")) {
226  return terrain_filter(vconfig(v), resources::filter_con, false);
227  }
228  static config c("not");
229  return terrain_filter(vconfig(c),resources::filter_con, false);
230  }
231 
232  static void cfg_to_value(const config &cfg, terrain_filter &value)
233  {
234  value = cfg_to_value(cfg);
235  }
236 
237  static void value_to_cfg(const terrain_filter &value, config &cfg)
238  {
239  cfg.add_child("value",value.to_config());
240  }
241 
242  static config value_to_cfg(const terrain_filter &value)
243  {
244  config cfg;
245  value_to_cfg(value,cfg);
246  return cfg;
247  }
248 };
249 
250 template<>
252 public:
253 
255  {
256  return unit_advancements_aspect(cfg["value"]);
257  }
258 
259  static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
260  {
261  value = cfg_to_value(cfg);
262  }
263 
264  static void value_to_cfg(const unit_advancements_aspect &value, config &cfg)
265  {
266  cfg["value"] = value.get_value();
267 
268  }
269 
271  {
272  config cfg;
273  value_to_cfg(value,cfg);
274  return cfg;
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 template<>
340 public:
341 
342  static void variant_to_value(const wfl::variant &var, bool &value)
343  {
344  value = var.as_bool();
345  }
346 
347  static void value_to_variant(const bool &value, wfl::variant &var)
348  {
349  var = wfl::variant(value);
350  }
351 
352  static wfl::variant value_to_variant(const bool &value)
353  {
354  wfl::variant var;
355  value_to_variant(value,var);
356  return var;
357  }
358 
359  static bool variant_to_value(const wfl::variant &var)
360  {
361  bool value;
362  variant_to_value(var,value);
363  return value;
364  }
365 };
366 
367 template<>
369 public:
370 
371  static void variant_to_value(const wfl::variant &var, std::string &value)
372  {
373  value = var.as_string();
374  }
375 
376  static void value_to_variant(const std::string &value, wfl::variant &var)
377  {
378  var = wfl::variant(value);
379  }
380 
381  static wfl::variant value_to_variant(const std::string &value)
382  {
383  wfl::variant var;
384  value_to_variant(value,var);
385  return var;
386  }
387 
388  static std::string variant_to_value(const wfl::variant &var)
389  {
390  std::string value;
391  variant_to_value(var,value);
392  return value;
393  }
394 };
395 
396 template<>
398 public:
399 
400  static void variant_to_value(const wfl::variant &/*var*/, attacks_vector &/*value*/)
401  {
402  assert(false);//not implemented
403  }
404 
405  static void value_to_variant(const attacks_vector &value, wfl::variant &var)
406  {
407  std::vector<wfl::variant> vars;
408  for(attacks_vector::const_iterator i = value.begin(); i != value.end(); ++i) {
409  vars.emplace_back(std::make_shared<attack_analysis>(*i));
410  }
411  var = wfl::variant(vars);
412  }
413 
415  {
416  wfl::variant var;
417  value_to_variant(value,var);
418  return var;
419  }
420 
422  {
423  attacks_vector value;
424  variant_to_value(var,value);
425  return value;
426  }
427 };
428 
429 template<>
430 class variant_value_translator<terrain_filter> {
431 public:
432 
433  static void variant_to_value(const wfl::variant &/*var*/, terrain_filter &/*value*/)
434  {
435  assert(false);//not implemented
436  }
437 
438  static void value_to_variant(const terrain_filter &/*value*/, wfl::variant &/*var*/)
439  {
440  assert(false);//not implemented
441  }
442 
443  static wfl::variant value_to_variant(const terrain_filter &value)
444  {
445  wfl::variant var;
446  value_to_variant(value,var);
447  return var;
448  }
449 
450  static terrain_filter variant_to_value(const wfl::variant &var)
451  {
452  static config c("not");
453  terrain_filter value(vconfig(c), resources::filter_con, false);
454  variant_to_value(var,value);
455  return value;
456  }
457 };
458 } //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:414
static void cfg_to_value(const config &cfg, bool &value)
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:294
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 void value_to_cfg(const utils::variant< bool, std::vector< std::string >> &value, 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:895
static void cfg_to_value(const config &cfg, utils::variant< bool, std::vector< std::string >> &value)
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:321
#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)
static config value_to_cfg(const utils::variant< bool, std::vector< std::string >> &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 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:940
static map_location::DIRECTION s
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:500
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)
MacOS doesn&#39;t support std::visit when targing MacOS < 10.14 (currently we target 10.11).
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:477
bool as_bool() const
Returns a boolean state of the variant value.
Definition: variant.cpp:316
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:59
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 utils::variant< bool, std::vector< std::string > > cfg_to_value(const config &cfg)
static void value_to_variant(const terrain_filter &, wfl::variant &)