The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
typed_formula.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
3 by Mark de Wever <koraq@xs4all.nl>
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#pragma once
17
18#include "color.hpp"
19#include "formula/callable.hpp"
20#include "formula/formula.hpp"
21#include "formula/function.hpp"
22#include "gui/core/log.hpp"
24#include "lexical_cast.hpp"
26#include "tstring.hpp"
27
28#include <cassert>
30
31namespace gui2
32{
33
34/**
35 * Template class can hold a value or a formula to calculate the value.
36 *
37 * A string is a formula when it starts with a right paren, no other validation
38 * is done by this function, leading whitespace is significant.
39 *
40 * Upon getting the value of the formula a variable map is send. The variables
41 * in the map can be used in the formula. The 'owners' of the class need to
42 * document the variables available.
43 *
44 * @tparam T The type of the formula. This type needs to
45 * be constructable form a string, either by a
46 * lexical_cast or a template specialization in
47 * this header.
48 */
49template<typename T>
51{
52public:
53 /**
54 * Constructor.
55 *
56 * @param str The string used to initialize the class, this
57 * can either be a formula or a string which can
58 * be converted to the type T.
59 * @param value The default value for the object.
60 */
61 explicit typed_formula(const std::string& str, const T value = T());
62
63 explicit typed_formula(T value);
64
65 /** Set the value for this object */
66 void set_value(T value) { value_ = value; }
67
68 /**
69 * Returns the value, can only be used if the data is no formula.
70 *
71 * Another option would be to cache the output of the formula in value_
72 * and always allow this function. But for now decided that the caller
73 * needs to do the caching. It might be changed later.
74 */
75 T operator()() const
76 {
77 assert(!has_formula());
78 return value_;
79 }
80
81 /**
82 * Returns the value, can always be used.
83 *
84 * @param variables The state variables which might be used in
85 * the formula. For example, screen_width can
86 * be set so the formula can return the half
87 * width of the screen.
88 *
89 * @param functions The variables, which can be called during the
90 * evaluation of the formula. (Note it is also
91 * possible to add extra functions to the table,
92 * when the variable is not @c nullptr.
93 *
94 * @returns The stored result or the result of the
95 * evaluation of the formula.
96 */
98 wfl::function_symbol_table* functions = nullptr) const;
99
100 /** Determine whether the class contains a formula. */
101 bool has_formula() const
102 {
103 return formula_.has_value();
104 }
105
106 template <typename U>
107 friend std::ostream& operator<<(std::ostream& os, const typed_formula<U>& formula);
108private:
109 /**
110 * Converts the string to the template type.
111 *
112 * This function is used by the constructor to convert the string to the
113 * wanted value, if not a formula.
114 *
115 * @param str The str send to the constructor.
116 */
117 void convert(const std::string& str);
118
119 /**
120 * Executes the formula.
121 *
122 * This function does the calculation and can only be called if the object
123 * contains a formula.
124 *
125 * @param v A variant object containing the evaluated value
126 * of the formula.
127 *
128 * @returns The calculated value.
129 */
131
132 /**
133 * Contains the formula for the variable.
134 *
135 * If without value, there's no formula.
136 */
137 utils::optional<std::string> formula_;
138
139 /** If there's no formula it contains the value. */
141};
142
143template<typename T>
144typed_formula<T>::typed_formula(const std::string& str, const T value)
145 : formula_(), value_(value)
146{
147 if(str.empty()) {
148 return;
149 }
150
151 if(str[0] == '(') {
152 formula_ = str;
153 } else {
154 convert(str);
155 }
156}
157
158template<typename T>
160 : formula_(), value_(value)
161{
162}
163
164template<typename T>
166operator()(const wfl::map_formula_callable& variables, wfl::function_symbol_table* functions) const
167{
168 if(!has_formula()) {
169 return value_;
170 }
171
172 wfl::variant v = wfl::formula(*formula_, functions).evaluate(variables);
173 const T& result = execute(v);
174
175 DBG_GUI_D << "Formula: execute '" << *formula_ << "' result '" << result << "'.";
176
177 return result;
178}
179
180/**
181 * Template specializations.
182 *
183 * Each type must have an @ref execute specialization, and optionally one for @ref convert.
184 */
185
186template<>
188{
189 return v.as_bool();
190}
191
192template<>
193inline void typed_formula<bool>::convert(const std::string& str)
194{
196}
197
198
199template<>
201{
202 return v.as_int();
203}
204
205
206template<>
208{
209 // FIXME: Validate this? As is, the formula could return a negative number which is blindly converted to unsigned.
210 // Unfortunately, some places rely on this happening for diagnostic messages...
211 return v.as_int();
212}
213
214
215template<>
217{
218 return v.as_string();
219}
220
221template<>
222inline void typed_formula<std::string>::convert(const std::string& str)
223{
224 value_ = str;
225}
226
227
228template<>
230{
231 return v.as_string();
232}
233
234template<>
235inline void typed_formula<t_string>::convert(const std::string& str)
236{
237 value_ = str;
238}
239
240
241template<>
243{
245}
246
247template<>
248inline void typed_formula<PangoAlignment>::convert(const std::string& str)
249{
251}
252
253
254template<>
256{
257 const auto& result = v.as_list();
258 const int alpha = result.size() == 4 ? result[3].as_int() : ALPHA_OPAQUE;
259
260 return color_t(
261 result.at(0).as_int(),
262 result.at(1).as_int(),
263 result.at(2).as_int(),
264 alpha
265 );
266}
267
268template<>
269inline void typed_formula<color_t>::convert(const std::string& str)
270{
272}
273
274
275template<typename T>
277{
278 // Every type needs its own execute function avoid instantiation of the
279 // default execute.
280 static_assert(sizeof(T) == 0, "typed_formula: Missing execute specialization");
281 return T();
282}
283
284template<class T>
285inline void typed_formula<T>::convert(const std::string& str)
286{
287 value_ = lexical_cast_default<T>(str);
288}
289
290template <typename U>
291std::ostream& operator<<(std::ostream& os, const typed_formula<U>& formula)
292{
293 if(formula.has_formula()) {
294 os << *formula.formula_;
295 } else {
296 os << formula.value_;
297 }
298 return os;
299}
300
301} // namespace gui2
std::unique_ptr< PangoAttribute, void(*)(PangoAttribute *)> value_
Template class can hold a value or a formula to calculate the value.
bool has_formula() const
Determine whether the class contains a formula.
typed_formula(const std::string &str, const T value=T())
Constructor.
utils::optional< std::string > formula_
Contains the formula for the variable.
void convert(const std::string &str)
Converts the string to the template type.
T value_
If there's no formula it contains the value.
T operator()() const
Returns the value, can only be used if the data is no formula.
T execute(wfl::variant &v) const
Executes the formula.
friend std::ostream & operator<<(std::ostream &os, const typed_formula< U > &formula)
void set_value(T value)
Set the value for this object.
T operator()(const wfl::map_formula_callable &variables, wfl::function_symbol_table *functions=nullptr) const
Returns the value, can always be used.
static variant evaluate(const const_formula_ptr &f, const formula_callable &variables, formula_debugger *fdb=nullptr, variant default_res=variant(0))
Definition formula.hpp:48
int as_int(int fallback=0) const
Returns the variant's value as an integer.
Definition variant.cpp:336
const std::string & as_string() const
Definition variant.cpp:362
bool as_bool() const
Returns a boolean state of the variant value.
Definition variant.cpp:357
const std::vector< variant > & as_list() const
Definition variant.cpp:367
constexpr uint8_t ALPHA_OPAQUE
Definition color.hpp:37
Define the common log macros for the gui toolkit.
#define DBG_GUI_D
Definition log.hpp:29
New lexcical_cast header.
Generic file dialog.
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition helper.cpp:84
std::ostream & operator<<(std::ostream &os, const typed_formula< U > &formula)
bool string_bool(const std::string &str, bool def)
Convert no, false, off, 0, 0.0 to false, empty to def, and others to true.
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
The basic class for representing 8-bit RGB or RGBA colour values.
Definition color.hpp:51
static color_t from_rgba_string(std::string_view c)
Creates a new color_t object from a string variable in "R,G,B,A" format.
Definition color.cpp:23