The Battle for Wesnoth  1.19.7+dev
value_translator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Yurii Chernyi <terraninfo@terraninfo.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  */
19 
20 #pragma once
21 
22 #include "ai/composite/engine.hpp"
23 #include "ai/composite/stage.hpp"
25 #include "ai/manager.hpp"
26 #include "lexical_cast.hpp"
27 #include "resources.hpp"
29 #include "terrain/filter.hpp"
30 #include "utils/variant.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 template<>
62 class config_value_translator<std::string> {
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 template<>
91 public:
92 
93  static bool cfg_to_value(const config &cfg)
94  {
95  return cfg["value"].to_bool();
96  }
97 
98  static void cfg_to_value(const config &cfg, bool &value)
99  {
100  value = cfg_to_value(cfg);
101  }
102 
103  static void value_to_cfg(const bool &value, config &cfg)
104  {
105  cfg["value"] = value;
106  }
107 
108  static config value_to_cfg(const bool &value)
109  {
110  config cfg;
111  value_to_cfg(value,cfg);
112  return cfg;
113  }
114 };
115 
117 #ifdef USING_BOOST_VARIANT
118  : public boost::static_visitor<std::string>
119 #endif
120 {
121 public:
122  std::string operator()(const bool b) const {
123  if (b) {
124  return "yes";
125  } else {
126  return "no";
127  }
128  }
129  std::string operator()(const std::vector<std::string> s) const { return utils::join(s); }
130 };
131 
132 template<>
133 class config_value_translator<utils::variant<bool, std::vector<std::string>>> {
134 public:
135 
136  static utils::variant<bool, std::vector<std::string>> cfg_to_value(const config &cfg)
137  {
138  if (cfg["value"].to_bool(true) == cfg["value"].to_bool(false)) {
139  return cfg["value"].to_bool();
140  }
141  return utils::split(cfg["value"]);
142  }
143 
144  static void cfg_to_value(const config &cfg, utils::variant<bool, std::vector<std::string>> &value)
145  {
146  value = cfg_to_value(cfg);
147  }
148 
149  static void value_to_cfg(const utils::variant<bool, std::vector<std::string>> &value, config &cfg)
150  {
151  cfg["value"] = utils::visit(leader_aspects_visitor(), value);
152  }
153 
154  static config value_to_cfg(const utils::variant<bool, std::vector<std::string>> &value)
155  {
156  config cfg;
157  value_to_cfg(value,cfg);
158  return cfg;
159  }
160 };
161 
162 template<>
163 class config_value_translator< std::vector<std::string>> {
164 public:
165 
166  static std::vector<std::string> cfg_to_value(const config &cfg)
167  {
168  return utils::split(cfg["value"]);
169  }
170 
171  static void cfg_to_value(const config &cfg, std::vector<std::string> &value)
172  {
173  value = cfg_to_value(cfg);
174  }
175 
176  static void value_to_cfg(const std::vector<std::string> &value, config &cfg)
177  {
178  cfg["value"] = utils::join(value);
179  }
180 
181  static config value_to_cfg(const std::vector<std::string> &value)
182  {
183  config cfg;
184  value_to_cfg(value,cfg);
185  return cfg;
186  }
187 };
188 
189 template<>
191 public:
192 
193  static void cfg_to_value(const config &cfg, config &value)
194  {
195  if (auto v = cfg.optional_child("value")) {
196  value = *v;
197  } else {
198  value.clear();
199  }
200  }
201 
202  static void value_to_cfg(const config &value, config &cfg)
203  {
204  cfg.add_child("value",value);
205  }
206 
207  static config value_to_cfg(const config &value)
208  {
209  config cfg;
210  value_to_cfg(value,cfg);
211  return cfg;
212  }
213 
214  static config cfg_to_value(const config &cfg)
215  {
216  return cfg.child_or_empty("value");
217  }
218 };
219 
220 template<>
221 class config_value_translator<terrain_filter> {
222 public:
223 
224  static terrain_filter cfg_to_value(const config &cfg)
225  {
226  if (auto v = cfg.optional_child("value")) {
227  return terrain_filter(vconfig(*v), resources::filter_con, false);
228  }
229  static config c("not");
230  return terrain_filter(vconfig(c),resources::filter_con, false);
231  }
232 
233  static void cfg_to_value(const config &cfg, terrain_filter &value)
234  {
235  value = cfg_to_value(cfg);
236  }
237 
238  static void value_to_cfg(const terrain_filter &value, config &cfg)
239  {
240  cfg.add_child("value",value.to_config());
241  }
242 
243  static config value_to_cfg(const terrain_filter &value)
244  {
245  config cfg;
246  value_to_cfg(value,cfg);
247  return cfg;
248  }
249 };
250 
251 template<>
253 public:
254 
256  {
257  return unit_advancements_aspect(cfg["value"]);
258  }
259 
260  static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
261  {
262  value = cfg_to_value(cfg);
263  }
264 
265  static void value_to_cfg(const unit_advancements_aspect &value, config &cfg)
266  {
267  cfg["value"] = value.get_value();
268 
269  }
270 
272  {
273  config cfg;
274  value_to_cfg(value,cfg);
275  return cfg;
276  }
277 };
278 
279 // variant value translator
280 
281 template<typename T>
283 public:
284 
285  static void variant_to_value(const wfl::variant &/*var*/, T &/*value*/)
286  {
287  assert(false);//not implemented
288  }
289 
290  static void value_to_variant(const T &/*value*/, wfl::variant &/*var*/)
291  {
292  assert(false);//not implemented
293  }
294 
295  static wfl::variant value_to_variant(const T &value)
296  {
297  wfl::variant var;
298  value_to_variant(value,var);
299  return var;
300  }
301 
302  static T variant_to_value(const wfl::variant &var)
303  {
304  T value = T();
305  variant_to_value(var,value);
306  return value;
307  }
308 };
309 
310 template<>
312 public:
313 
314  static void variant_to_value(const wfl::variant &var, int &value)
315  {
316  value = var.as_int();
317  }
318 
319  static void value_to_variant(const int &value, wfl::variant &var)
320  {
321  var = wfl::variant(value);
322  }
323 
324  static wfl::variant value_to_variant(const int &value)
325  {
326  wfl::variant var;
327  value_to_variant(value,var);
328  return var;
329  }
330 
331  static int variant_to_value(const wfl::variant &var)
332  {
333  int value;
334  variant_to_value(var,value);
335  return value;
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 template<>
369 class variant_value_translator<std::string> {
370 public:
371 
372  static void variant_to_value(const wfl::variant &var, std::string &value)
373  {
374  value = var.as_string();
375  }
376 
377  static void value_to_variant(const std::string &value, wfl::variant &var)
378  {
379  var = wfl::variant(value);
380  }
381 
382  static wfl::variant value_to_variant(const std::string &value)
383  {
384  wfl::variant var;
385  value_to_variant(value,var);
386  return var;
387  }
388 
389  static std::string variant_to_value(const wfl::variant &var)
390  {
391  std::string value;
392  variant_to_value(var,value);
393  return value;
394  }
395 };
396 
397 template<>
399 public:
400 
401  static void variant_to_value(const wfl::variant &/*var*/, attacks_vector &/*value*/)
402  {
403  assert(false);//not implemented
404  }
405 
406  static void value_to_variant(const attacks_vector &value, wfl::variant &var)
407  {
408  std::vector<wfl::variant> vars;
409  for(attacks_vector::const_iterator i = value.begin(); i != value.end(); ++i) {
410  vars.emplace_back(std::make_shared<attack_analysis>(*i));
411  }
412  var = wfl::variant(vars);
413  }
414 
416  {
417  wfl::variant var;
418  value_to_variant(value,var);
419  return var;
420  }
421 
423  {
424  attacks_vector value;
425  variant_to_value(var,value);
426  return value;
427  }
428 };
429 
430 template<>
431 class variant_value_translator<terrain_filter> {
432 public:
433 
434  static void variant_to_value(const wfl::variant &/*var*/, terrain_filter &/*value*/)
435  {
436  assert(false);//not implemented
437  }
438 
439  static void value_to_variant(const terrain_filter &/*value*/, wfl::variant &/*var*/)
440  {
441  assert(false);//not implemented
442  }
443 
444  static wfl::variant value_to_variant(const terrain_filter &value)
445  {
446  wfl::variant var;
447  value_to_variant(value,var);
448  return var;
449  }
450 
451  static terrain_filter variant_to_value(const wfl::variant &var)
452  {
453  static config c("not");
454  terrain_filter value(vconfig(c), resources::filter_con, false);
455  variant_to_value(var,value);
456  return value;
457  }
458 };
459 } //end of namespace ai
Managing the AIs lifecycle - headers TODO: Refactor history handling and internal commands.
static bool cfg_to_value(const config &cfg)
static void value_to_cfg(const bool &value, config &cfg)
static config value_to_cfg(const bool &value)
static void cfg_to_value(const config &cfg, bool &value)
static config value_to_cfg(const config &value)
static config cfg_to_value(const config &cfg)
static void value_to_cfg(const config &value, config &cfg)
static void cfg_to_value(const config &cfg, config &value)
static config value_to_cfg(const std::string &value)
static void cfg_to_value(const config &cfg, std::string &value)
static void value_to_cfg(const std::string &value, config &cfg)
static std::string cfg_to_value(const config &cfg)
static config value_to_cfg(const std::vector< std::string > &value)
static std::vector< std::string > cfg_to_value(const config &cfg)
static void cfg_to_value(const config &cfg, std::vector< std::string > &value)
static void value_to_cfg(const std::vector< std::string > &value, config &cfg)
static void cfg_to_value(const config &cfg, terrain_filter &value)
static terrain_filter cfg_to_value(const config &cfg)
static config value_to_cfg(const terrain_filter &value)
static void value_to_cfg(const terrain_filter &value, config &cfg)
static unit_advancements_aspect cfg_to_value(const config &cfg)
static config value_to_cfg(const unit_advancements_aspect &value)
static void value_to_cfg(const unit_advancements_aspect &value, config &cfg)
static void cfg_to_value(const config &cfg, unit_advancements_aspect &value)
static utils::variant< bool, std::vector< 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 void cfg_to_value(const config &cfg, utils::variant< bool, std::vector< std::string >> &value)
static config value_to_cfg(const utils::variant< bool, std::vector< std::string >> &value)
static T cfg_to_value(const config &cfg)
static config value_to_cfg(const T &value)
static void value_to_cfg(const T &value, config &cfg)
static void cfg_to_value(const config &cfg, T &value)
std::string operator()(const std::vector< std::string > s) const
std::string operator()(const bool b) const
const std::string get_value() const
static void variant_to_value(const wfl::variant &, attacks_vector &)
static void value_to_variant(const attacks_vector &value, wfl::variant &var)
static attacks_vector variant_to_value(const wfl::variant &var)
static wfl::variant value_to_variant(const attacks_vector &value)
static void variant_to_value(const wfl::variant &var, bool &value)
static wfl::variant value_to_variant(const bool &value)
static bool variant_to_value(const wfl::variant &var)
static void value_to_variant(const bool &value, wfl::variant &var)
static wfl::variant value_to_variant(const int &value)
static int variant_to_value(const wfl::variant &var)
static void variant_to_value(const wfl::variant &var, int &value)
static void value_to_variant(const int &value, wfl::variant &var)
static wfl::variant value_to_variant(const std::string &value)
static void value_to_variant(const std::string &value, wfl::variant &var)
static void variant_to_value(const wfl::variant &var, std::string &value)
static std::string variant_to_value(const wfl::variant &var)
static void variant_to_value(const wfl::variant &, terrain_filter &)
static wfl::variant value_to_variant(const terrain_filter &value)
static terrain_filter variant_to_value(const wfl::variant &var)
static void value_to_variant(const terrain_filter &, wfl::variant &)
static void value_to_variant(const T &, wfl::variant &)
static T variant_to_value(const wfl::variant &var)
static void variant_to_value(const wfl::variant &, T &)
static wfl::variant value_to_variant(const T &value)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
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:394
void clear()
Definition: config.cpp:828
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:384
config & add_child(config_key_type key)
Definition: config.cpp:440
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
int as_int() const
Definition: variant.cpp:291
const std::string & as_string() const
Definition: variant.cpp:318
bool as_bool() const
Returns a boolean state of the variant value.
Definition: variant.cpp:313
AI Support engine - creating specific ai components from config.
std::size_t i
Definition: function.cpp:1029
New lexcical_cast header.
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:59
std::vector< attack_analysis > attacks_vector
Definition: game_info.hpp:51
filter_context * filter_con
Definition: resources.cpp:23
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
Composite AI stages.
mock_char c
static map_location::direction s
MacOS doesn't support std::visit when targing MacOS < 10.14 (currently we target 10....
#define b