The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
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
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
32namespace ai {
33
34template<typename T>
36public:
37
38 static T cfg_to_value(const config &cfg)
39 {
40 return cfg["value"].to(T{});
41 }
42
43 static void cfg_to_value(const config &cfg, T &value)
44 {
46 }
47
48 static void value_to_cfg(const T &value, config &cfg)
49 {
50 cfg["value"] = value;
51 }
52
53 static config value_to_cfg(const T &value)
54 {
55 config cfg;
57 return cfg;
58 }
59};
60
61template<>
63public:
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 {
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;
84 return cfg;
85 }
86
87};
88
89template<>
91public:
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 {
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;
112 return cfg;
113 }
114};
115
117#ifdef USING_BOOST_VARIANT
118 : public boost::static_visitor<std::string>
119#endif
120{
121public:
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
132template<>
133class config_value_translator<utils::variant<bool, std::vector<std::string>>> {
134public:
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 {
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;
158 return cfg;
159 }
160};
161
162template<>
163class config_value_translator< std::vector<std::string>> {
164public:
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 {
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;
185 return cfg;
186 }
187};
188
189template<>
191public:
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
208 {
209 config cfg;
211 return cfg;
212 }
213
215 {
216 return cfg.child_or_empty("value");
217 }
218};
219
220template<>
221class config_value_translator<terrain_filter> {
222public:
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 {
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;
247 return cfg;
248 }
249};
250
251template<>
253public:
254
256 {
257 return unit_advancements_aspect(cfg["value"]);
258 }
259
261 {
263 }
264
266 {
267 cfg["value"] = value.get_value();
268
269 }
270
272 {
273 config 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 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 utils::variant< bool, std::vector< std::string > > cfg_to_value(const config &cfg)
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
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
config & add_child(std::string_view key)
Definition config.cpp:436
optional_config_impl< config > optional_child(std::string_view key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
Definition config.cpp:380
const config & child_or_empty(std::string_view key) const
Returns the first child with the given key, or an empty config if there is none.
Definition config.cpp:390
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
STL namespace.
std::string join(const Range &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)
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
Composite AI stages.
static map_location::direction s
MacOS doesn't support std::visit when targing MacOS < 10.14 (currently we target 10....
#define c
#define b