The Battle for Wesnoth  1.19.13+dev
value_translator.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2025
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 } //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
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:390
void clear()
Definition: config.cpp:824
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:380
config & add_child(config_key_type key)
Definition: config.cpp:436
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
AI Support engine - creating specific ai components from config.
const config * cfg
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
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