The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
value_translator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project http://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 bool cfg_to_value(const config &cfg)
66  {
67  return cfg["value"].to_bool();
68  }
69 
70  static void cfg_to_value(const config &cfg, bool &value)
71  {
72  value = cfg_to_value(cfg);
73  }
74 
75  static void value_to_cfg(const bool &value, config &cfg)
76  {
77  cfg["value"] = value;
78  }
79 
80  static config value_to_cfg(const bool &value)
81  {
82  config cfg;
83  value_to_cfg(value,cfg);
84  return cfg;
85  }
86 
87 };
88 
89 template<>
90 class config_value_translator< std::vector<std::string> > {
91 public:
92 
93  static std::vector<std::string> cfg_to_value(const config &cfg)
94  {
95  return utils::split(cfg["value"]);
96  }
97 
98  static void cfg_to_value(const config &cfg, std::vector<std::string> &value)
99  {
100  value = cfg_to_value(cfg);
101  }
102 
103  static void value_to_cfg(const std::vector<std::string> &value, config &cfg)
104  {
105  cfg["value"] = utils::join(value);
106  }
107 
108  static config value_to_cfg(const std::vector<std::string> &value)
109  {
110  config cfg;
111  value_to_cfg(value,cfg);
112  return cfg;
113  }
114 };
115 
116 template<>
118 public:
119 
120  static void cfg_to_value(const config &cfg, config &value)
121  {
122  if (const config &v = cfg.child("value")) {
123  value = v;
124  } else {
125  value.clear();
126  }
127  }
128 
129  static void value_to_cfg(const config &value, config &cfg)
130  {
131  cfg.add_child("value",value);
132  }
133 
134  static config value_to_cfg(const config &value)
135  {
136  config cfg;
137  value_to_cfg(value,cfg);
138  return cfg;
139  }
140 
141  static config cfg_to_value(const config &cfg)
142  {
143  return cfg.child_or_empty("value");
144  }
145 };
146 
147 template<>
149 public:
150 
151  static terrain_filter cfg_to_value(const config &cfg)
152  {
153  if (const config &v = cfg.child("value")) {
155  }
156  static config c("not");
158  }
159 
160  static void cfg_to_value(const config &cfg, terrain_filter &value)
161  {
162  value = cfg_to_value(cfg);
163  }
164 
165  static void value_to_cfg(const terrain_filter &value, config &cfg)
166  {
167  cfg.add_child("value",value.to_config());
168  }
169 
170  static config value_to_cfg(const terrain_filter &value)
171  {
172  config cfg;
173  value_to_cfg(value,cfg);
174  return cfg;
175  }
176 };
177 
178 template<>
180 public:
181 
183  {
184  return unit_advancements_aspect(cfg["value"]);
185  }
186 
187  static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
188  {
189  value = cfg_to_value(cfg);
190  }
191 
192  static void value_to_cfg(const unit_advancements_aspect &value, config &cfg)
193  {
194  cfg["value"] = value.get_value();
195 
196  }
197 
199  {
200  config cfg;
201  value_to_cfg(value,cfg);
202  return cfg;
203  }
204 };
205 
206 
207 
208 // variant value translator
209 
210 template<typename T>
212 public:
213 
214  static void variant_to_value(const wfl::variant &/*var*/, T &/*value*/)
215  {
216  assert(false);//not implemented
217  }
218 
219  static void value_to_variant(const T &/*value*/, wfl::variant &/*var*/)
220  {
221  assert(false);//not implemented
222  }
223 
224  static wfl::variant value_to_variant(const T &value)
225  {
226  wfl::variant var;
227  value_to_variant(value,var);
228  return var;
229  }
230 
231  static T variant_to_value(const wfl::variant &var)
232  {
233  T value = T();
234  variant_to_value(var,value);
235  return value;
236  }
237 };
238 
239 template<>
241 public:
242 
243  static void variant_to_value(const wfl::variant &var, int &value)
244  {
245  value = var.as_int();
246  }
247 
248  static void value_to_variant(const int &value, wfl::variant &var)
249  {
250  var = wfl::variant(value);
251  }
252 
253  static wfl::variant value_to_variant(const int &value)
254  {
255  wfl::variant var;
256  value_to_variant(value,var);
257  return var;
258  }
259 
260  static int variant_to_value(const wfl::variant &var)
261  {
262  int value;
263  variant_to_value(var,value);
264  return value;
265  }
266 };
267 
268 
269 template<>
271 public:
272 
273  static void variant_to_value(const wfl::variant &var, bool &value)
274  {
275  value = var.as_bool();
276  }
277 
278  static void value_to_variant(const bool &value, wfl::variant &var)
279  {
280  var = wfl::variant(value);
281  }
282 
283  static wfl::variant value_to_variant(const bool &value)
284  {
285  wfl::variant var;
286  value_to_variant(value,var);
287  return var;
288  }
289 
290  static bool variant_to_value(const wfl::variant &var)
291  {
292  bool value;
293  variant_to_value(var,value);
294  return value;
295  }
296 };
297 
298 
299 
300 template<>
302 public:
303 
304  static void variant_to_value(const wfl::variant &var, std::string &value)
305  {
306  value = var.as_string();
307  }
308 
309  static void value_to_variant(const std::string &value, wfl::variant &var)
310  {
311  var = wfl::variant(value);
312  }
313 
315  {
316  wfl::variant var;
317  value_to_variant(value,var);
318  return var;
319  }
320 
322  {
323  std::string value;
324  variant_to_value(var,value);
325  return value;
326  }
327 };
328 
329 
330 
331 template<>
333 public:
334 
335  static void variant_to_value(const wfl::variant &/*var*/, attacks_vector &/*value*/)
336  {
337  assert(false);//not implemented
338  }
339 
340  static void value_to_variant(const attacks_vector &value, wfl::variant &var)
341  {
342  std::vector<wfl::variant> vars;
343  for(attacks_vector::const_iterator i = value.begin(); i != value.end(); ++i) {
344  vars.emplace_back(std::make_shared<attack_analysis>(*i));
345  }
346  var = wfl::variant(vars);
347  }
348 
350  {
351  wfl::variant var;
352  value_to_variant(value,var);
353  return var;
354  }
355 
357  {
358  attacks_vector value;
359  variant_to_value(var,value);
360  return value;
361  }
362 };
363 
364 
365 template<>
367 public:
368 
369  static void variant_to_value(const wfl::variant &/*var*/, terrain_filter &/*value*/)
370  {
371  assert(false);//not implemented
372  }
373 
374  static void value_to_variant(const terrain_filter &/*value*/, wfl::variant &/*var*/)
375  {
376  assert(false);//not implemented
377  }
378 
380  {
381  wfl::variant var;
382  value_to_variant(value,var);
383  return var;
384  }
385 
387  {
388  static config c("not");
390  variant_to_value(var,value);
391  return value;
392  }
393 };
394 } //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:363
std::vector< char_t > string
static void cfg_to_value(const config &cfg, bool &value)
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:298
static void value_to_variant(const T &, wfl::variant &)
static wfl::variant value_to_variant(const terrain_filter &value)
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:753
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)
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
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)
std::vector< attack_analysis > attacks_vector
Definition: game_info.hpp:54
static void cfg_to_value(const config &cfg, std::vector< std::string > &value)
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
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)
bool as_bool() const
Returns a boolean state of the variant value.
Definition: variant.cpp:320
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.
static std::vector< std::string > cfg_to_value(const config &cfg)
static void variant_to_value(const wfl::variant &, T &)
static T cfg_to_value(const config &cfg)
static bool cfg_to_value(const config &cfg)
config to_config() const
Definition: filter.cpp:663
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:396
size_t i
Definition: function.cpp:933
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)
config & add_child(config_key_type key)
Definition: config.cpp:419
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:42
static int variant_to_value(const wfl::variant &var)
const std::string get_value() const
const std::string & as_string() const
Definition: variant.cpp:325
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:93
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 &)