The Battle for Wesnoth  1.19.1+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 <variant>
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 {
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<std::variant<bool, std::vector<std::string>>> {
131 public:
132 
133  static std::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, std::variant<bool, std::vector<std::string>> &value)
142  {
143  value = cfg_to_value(cfg);
144  }
145 
146  static void value_to_cfg(const std::variant<bool, std::vector<std::string>> &value, config &cfg)
147  {
148  cfg["value"] = std::visit(leader_aspects_visitor(), value);
149  }
150 
151  static config value_to_cfg(const std::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 (auto v = cfg.optional_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 (auto v = cfg.optional_child("value")) {
224  return terrain_filter(vconfig(*v), resources::filter_con, false);
225  }
226  static config c("not");
227  return terrain_filter(vconfig(c),resources::filter_con, false);
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 // variant value translator
277 
278 template<typename T>
280 public:
281 
282  static void variant_to_value(const wfl::variant &/*var*/, T &/*value*/)
283  {
284  assert(false);//not implemented
285  }
286 
287  static void value_to_variant(const T &/*value*/, wfl::variant &/*var*/)
288  {
289  assert(false);//not implemented
290  }
291 
292  static wfl::variant value_to_variant(const T &value)
293  {
294  wfl::variant var;
295  value_to_variant(value,var);
296  return var;
297  }
298 
299  static T variant_to_value(const wfl::variant &var)
300  {
301  T value = T();
302  variant_to_value(var,value);
303  return value;
304  }
305 };
306 
307 template<>
309 public:
310 
311  static void variant_to_value(const wfl::variant &var, int &value)
312  {
313  value = var.as_int();
314  }
315 
316  static void value_to_variant(const int &value, wfl::variant &var)
317  {
318  var = wfl::variant(value);
319  }
320 
321  static wfl::variant value_to_variant(const int &value)
322  {
323  wfl::variant var;
324  value_to_variant(value,var);
325  return var;
326  }
327 
328  static int variant_to_value(const wfl::variant &var)
329  {
330  int value;
331  variant_to_value(var,value);
332  return value;
333  }
334 };
335 
336 template<>
338 public:
339 
340  static void variant_to_value(const wfl::variant &var, bool &value)
341  {
342  value = var.as_bool();
343  }
344 
345  static void value_to_variant(const bool &value, wfl::variant &var)
346  {
347  var = wfl::variant(value);
348  }
349 
350  static wfl::variant value_to_variant(const bool &value)
351  {
352  wfl::variant var;
353  value_to_variant(value,var);
354  return var;
355  }
356 
357  static bool variant_to_value(const wfl::variant &var)
358  {
359  bool value;
360  variant_to_value(var,value);
361  return value;
362  }
363 };
364 
365 template<>
366 class variant_value_translator<std::string> {
367 public:
368 
369  static void variant_to_value(const wfl::variant &var, std::string &value)
370  {
371  value = var.as_string();
372  }
373 
374  static void value_to_variant(const std::string &value, wfl::variant &var)
375  {
376  var = wfl::variant(value);
377  }
378 
379  static wfl::variant value_to_variant(const std::string &value)
380  {
381  wfl::variant var;
382  value_to_variant(value,var);
383  return var;
384  }
385 
386  static std::string variant_to_value(const wfl::variant &var)
387  {
388  std::string value;
389  variant_to_value(var,value);
390  return value;
391  }
392 };
393 
394 template<>
396 public:
397 
398  static void variant_to_value(const wfl::variant &/*var*/, attacks_vector &/*value*/)
399  {
400  assert(false);//not implemented
401  }
402 
403  static void value_to_variant(const attacks_vector &value, wfl::variant &var)
404  {
405  std::vector<wfl::variant> vars;
406  for(attacks_vector::const_iterator i = value.begin(); i != value.end(); ++i) {
407  vars.emplace_back(std::make_shared<attack_analysis>(*i));
408  }
409  var = wfl::variant(vars);
410  }
411 
413  {
414  wfl::variant var;
415  value_to_variant(value,var);
416  return var;
417  }
418 
420  {
421  attacks_vector value;
422  variant_to_value(var,value);
423  return value;
424  }
425 };
426 
427 template<>
428 class variant_value_translator<terrain_filter> {
429 public:
430 
431  static void variant_to_value(const wfl::variant &/*var*/, terrain_filter &/*value*/)
432  {
433  assert(false);//not implemented
434  }
435 
436  static void value_to_variant(const terrain_filter &/*value*/, wfl::variant &/*var*/)
437  {
438  assert(false);//not implemented
439  }
440 
441  static wfl::variant value_to_variant(const terrain_filter &value)
442  {
443  wfl::variant var;
444  value_to_variant(value,var);
445  return var;
446  }
447 
448  static terrain_filter variant_to_value(const wfl::variant &var)
449  {
450  static config c("not");
451  terrain_filter value(vconfig(c), resources::filter_con, false);
452  variant_to_value(var,value);
453  return value;
454  }
455 };
456 } //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::variant< bool, std::vector< std::string >> &value)
static void value_to_cfg(const std::variant< bool, std::vector< std::string >> &value, config &cfg)
static std::variant< bool, std::vector< std::string > > cfg_to_value(const config &cfg)
static void cfg_to_value(const config &cfg, std::variant< bool, std::vector< std::string >> &value)
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 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:159
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:395
void clear()
Definition: config.cpp:831
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Euivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition: config.cpp:385
config & add_child(config_key_type key)
Definition: config.cpp:441
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:968
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
#define b