The Battle for Wesnoth  1.13.4+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 - 2016 by Yurii Chernyi <[email protected]>
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 #ifndef VALUE_TRANSLATOR_HPP_INCLUDED
20 #define VALUE_TRANSLATOR_HPP_INCLUDED
21 
22 #include "ai/composite/engine.hpp"
23 #include "ai/composite/stage.hpp"
24 
25 #include "ai/manager.hpp"
26 #include "terrain/filter.hpp"
27 #include "util.hpp"
29 #include "resources.hpp"
31 
32 namespace ai {
33 
34 template<typename T>
36 public:
37 
38  static T cfg_to_value(const config &cfg)
39  {
40  return lexical_cast_default<T>(cfg["value"]);
41  }
42 
43  static void cfg_to_value(const config &cfg, T &value)
44  {
45  value = cfg_to_value(cfg);
46  }
47 
48  static void value_to_cfg(const T &value, config &cfg)
49  {
50  cfg["value"] = lexical_cast<std::string>(value);
51  }
52 
53  static config value_to_cfg(const T &value)
54  {
55  config cfg;
56  value_to_cfg(value,cfg);
57  return cfg;
58  }
59 };
60 
61 
62 template<>
64 public:
65 
66  static bool cfg_to_value(const config &cfg)
67  {
68  return cfg["value"].to_bool();
69  }
70 
71  static void cfg_to_value(const config &cfg, bool &value)
72  {
73  value = cfg_to_value(cfg);
74  }
75 
76  static void value_to_cfg(const bool &value, config &cfg)
77  {
78  cfg["value"] = value;
79  }
80 
81  static config value_to_cfg(const bool &value)
82  {
83  config cfg;
84  value_to_cfg(value,cfg);
85  return cfg;
86  }
87 
88 };
89 
90 template<>
91 class config_value_translator< std::vector<std::string> > {
92 public:
93 
94  static std::vector<std::string> cfg_to_value(const config &cfg)
95  {
96  return utils::split(cfg["value"]);
97  }
98 
99  static void cfg_to_value(const config &cfg, std::vector<std::string> &value)
100  {
101  value = cfg_to_value(cfg);
102  }
103 
104  static void value_to_cfg(const std::vector<std::string> &value, config &cfg)
105  {
106  cfg["value"] = utils::join(value);
107  }
108 
109  static config value_to_cfg(const std::vector<std::string> &value)
110  {
111  config cfg;
112  value_to_cfg(value,cfg);
113  return cfg;
114  }
115 };
116 
117 template<>
119 public:
120 
121  static void cfg_to_value(const config &cfg, config &value)
122  {
123  if (const config &v = cfg.child("value")) {
124  value = v;
125  } else {
126  value.clear();
127  }
128  }
129 
130  static void value_to_cfg(const config &value, config &cfg)
131  {
132  cfg.add_child("value",value);
133  }
134 
136  {
137  config cfg;
138  value_to_cfg(value,cfg);
139  return cfg;
140  }
141 
142  static config cfg_to_value(const config &cfg)
143  {
144  return cfg.child_or_empty("value");
145  }
146 };
147 
148 template<>
150 public:
151 
152  static terrain_filter cfg_to_value(const config &cfg)
153  {
154  if (const config &v = cfg.child("value")) {
156  }
157  static config c("not");
159  }
160 
161  static void cfg_to_value(const config &cfg, terrain_filter &value)
162  {
163  value = cfg_to_value(cfg);
164  }
165 
166  static void value_to_cfg(const terrain_filter &value, config &cfg)
167  {
168  cfg.add_child("value",value.to_config());
169  }
170 
172  {
173  config cfg;
174  value_to_cfg(value,cfg);
175  return cfg;
176  }
177 };
178 
179 template<>
181 public:
182 
184  {
185  return unit_advancements_aspect(cfg["value"]);
186  }
187 
189  {
190  value = cfg_to_value(cfg);
191  }
192 
194  {
195  cfg["value"] = value.get_value();
196 
197  }
198 
200  {
201  config cfg;
202  value_to_cfg(value,cfg);
203  return cfg;
204  }
205 };
206 
207 
208 
209 // variant value translator
210 
211 template<typename T>
213 public:
214 
215  static void variant_to_value(const variant &/*var*/, T &/*value*/)
216  {
217  assert(false);//not implemented
218  }
219 
220  static void value_to_variant(const T &/*value*/, variant &/*var*/)
221  {
222  assert(false);//not implemented
223  }
224 
225  static variant value_to_variant(const T &value)
226  {
227  variant var;
228  value_to_variant(value,var);
229  return var;
230  }
231 
232  static T variant_to_value(const variant &var)
233  {
234  T value = T();
235  variant_to_value(var,value);
236  return value;
237  }
238 };
239 
240 template<>
242 public:
243 
244  static void variant_to_value(const variant &var, int &value)
245  {
246  value = var.as_int();
247  }
248 
249  static void value_to_variant(const int &value, variant &var)
250  {
251  var = variant(value);
252  }
253 
254  static variant value_to_variant(const int &value)
255  {
256  variant var;
257  value_to_variant(value,var);
258  return var;
259  }
260 
261  static int variant_to_value(const variant &var)
262  {
263  int value;
264  variant_to_value(var,value);
265  return value;
266  }
267 };
268 
269 
270 template<>
272 public:
273 
274  static void variant_to_value(const variant &var, bool &value)
275  {
276  value = var.as_bool();
277  }
278 
279  static void value_to_variant(const bool &value, variant &var)
280  {
281  var = variant(value);
282  }
283 
284  static variant value_to_variant(const bool &value)
285  {
286  variant var;
287  value_to_variant(value,var);
288  return var;
289  }
290 
291  static bool variant_to_value(const variant &var)
292  {
293  bool value;
294  variant_to_value(var,value);
295  return value;
296  }
297 };
298 
299 
300 
301 template<>
303 public:
304 
305  static void variant_to_value(const variant &var, std::string &value)
306  {
307  value = var.as_string();
308  }
309 
310  static void value_to_variant(const std::string &value, variant &var)
311  {
312  var = variant(value);
313  }
314 
316  {
317  variant var;
318  value_to_variant(value,var);
319  return var;
320  }
321 
323  {
325  variant_to_value(var,value);
326  return value;
327  }
328 };
329 
330 
331 
332 template<>
334 public:
335 
336  static void variant_to_value(const variant &/*var*/, attacks_vector &/*value*/)
337  {
338  assert(false);//not implemented
339  }
340 
341  static void value_to_variant(const attacks_vector &value, variant &var)
342  {
343  std::vector<variant> vars;
344  for(attacks_vector::const_iterator i = value.begin(); i != value.end(); ++i) {
345  vars.push_back(variant(new attack_analysis(*i)));
346  }
347  var = variant(&vars);
348  }
349 
351  {
352  variant var;
353  value_to_variant(value,var);
354  return var;
355  }
356 
358  {
360  variant_to_value(var,value);
361  return value;
362  }
363 };
364 
365 
366 template<>
368 public:
369 
370  static void variant_to_value(const variant &/*var*/, terrain_filter &/*value*/)
371  {
372  assert(false);//not implemented
373  }
374 
375  static void value_to_variant(const terrain_filter &/*value*/, variant &/*var*/)
376  {
377  assert(false);//not implemented
378  }
379 
381  {
382  variant var;
383  value_to_variant(value,var);
384  return var;
385  }
386 
388  {
389  static config c("not");
391  variant_to_value(var,value);
392  return value;
393  }
394 };
395 } //end of namespace ai
396 
397 #endif
static config cfg_to_value(const config &cfg)
static void variant_to_value(const variant &, terrain_filter &)
static variant value_to_variant(const std::string &value)
static void cfg_to_value(const config &cfg, bool &value)
static void variant_to_value(const variant &var, int &value)
static variant value_to_variant(const T &value)
static config value_to_cfg(const terrain_filter &value)
static void value_to_cfg(const bool &value, config &cfg)
static void cfg_to_value(const config &cfg, config &value)
static void variant_to_value(const variant &, T &)
static void variant_to_value(const variant &var, std::string &value)
static config value_to_cfg(const unit_advancements_aspect &value)
static void variant_to_value(const variant &var, bool &value)
static void value_to_variant(const T &, variant &)
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 variant value_to_variant(const attacks_vector &value)
void clear()
Definition: config.cpp:1055
bool as_bool() const
Definition: variant.cpp:580
const config & child_or_empty(const std::string &key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:722
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 variant value_to_variant(const terrain_filter &value)
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:56
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:57
static void value_to_cfg(const config &value, config &cfg)
static void cfg_to_value(const config &cfg, terrain_filter &value)
static int variant_to_value(const 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)
int as_int() const
Definition: variant.cpp:558
const GLdouble * v
Definition: glew.h:1359
GLsizei const GLfloat * value
Definition: glew.h:1817
static void value_to_variant(const attacks_vector &value, variant &var)
static void value_to_variant(const terrain_filter &, variant &)
config & add_child(const std::string &key)
Definition: config.cpp:743
static variant value_to_variant(const int &value)
static terrain_filter variant_to_value(const variant &var)
Managing the AIs lifecycle - headers.
static std::vector< std::string > cfg_to_value(const config &cfg)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
Templates and utility-routines for strings and numbers.
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
static T cfg_to_value(const config &cfg)
static bool cfg_to_value(const config &cfg)
config to_config() const
Definition: filter.cpp:642
static void variant_to_value(const variant &, attacks_vector &)
static variant value_to_variant(const bool &value)
const std::string & as_string() const
Definition: variant.cpp:603
size_t i
Definition: function.cpp:1057
static config value_to_cfg(const bool &value)
static void value_to_variant(const std::string &value, variant &var)
static void value_to_variant(const bool &value, variant &var)
static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
static bool variant_to_value(const variant &var)
config & child(const std::string &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:658
static void value_to_cfg(const T &value, config &cfg)
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
const std::string get_value() const
#define c
Definition: glew.h:12743
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
static void value_to_variant(const int &value, variant &var)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
static config value_to_cfg(const std::vector< std::string > &value)
static std::string variant_to_value(const variant &var)
static config value_to_cfg(const config &value)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
static T variant_to_value(const variant &var)
static attacks_vector variant_to_value(const variant &var)