The Battle for Wesnoth  1.19.5+dev
Public Member Functions | Private Member Functions | Private Attributes | List of all members
gui2::typed_formula< T > Class Template Reference

Template class can hold a value or a formula to calculate the value. More...

#include <typed_formula.hpp>

Public Member Functions

 typed_formula (const std::string &str, const T value=T())
 Constructor. More...
 
operator() () const
 Returns the value, can only be used if the data is no formula. More...
 
operator() (const wfl::map_formula_callable &variables, wfl::function_symbol_table *functions=nullptr) const
 Returns the value, can always be used. More...
 
bool has_formula () const
 Determine whether the class contains a formula. More...
 

Private Member Functions

void convert (const std::string &str)
 Converts the string to the template type. More...
 
execute (wfl::variant &v) const
 Executes the formula. More...
 
bool execute (wfl::variant &v) const
 Template specializations. More...
 
void convert (const std::string &str)
 
int execute (wfl::variant &v) const
 
unsigned execute (wfl::variant &v) const
 
std::string execute (wfl::variant &v) const
 
void convert (const std::string &str)
 
t_string execute (wfl::variant &v) const
 
void convert (const std::string &str)
 
PangoAlignment execute (wfl::variant &v) const
 
void convert (const std::string &str)
 
color_t execute (wfl::variant &v) const
 
void convert (const std::string &str)
 

Private Attributes

utils::optional< std::string > formula_
 Contains the formula for the variable. More...
 
value_
 If there's no formula it contains the value. More...
 

Detailed Description

template<typename T>
class gui2::typed_formula< T >

Template class can hold a value or a formula to calculate the value.

A string is a formula when it starts with a right paren, no other validation is done by this function, leading whitespace is significant.

Upon getting the value of the formula a variable map is send. The variables in the map can be used in the formula. The 'owners' of the class need to document the variables available.

Template Parameters
TThe type of the formula. This type needs to be constructable form a string, either by a lexical_cast or a template specialization in this header.

Definition at line 50 of file typed_formula.hpp.

Constructor & Destructor Documentation

◆ typed_formula()

template<typename T >
gui2::typed_formula< T >::typed_formula ( const std::string &  str,
const T  value = T() 
)
explicit

Constructor.

Parameters
strThe string used to initialize the class, this can either be a formula or a string which can be converted to the type T.
valueThe default value for the object.

Definition at line 137 of file typed_formula.hpp.

References gui2::typed_formula< T >::convert(), and gui2::typed_formula< T >::formula_.

Member Function Documentation

◆ convert() [1/6]

template<class T >
void gui2::typed_formula< T >::convert ( const std::string &  str)
inlineprivate

Converts the string to the template type.

This function is used by the constructor to convert the string to the wanted value, if not a formula.

Parameters
strThe str send to the constructor.

Definition at line 272 of file typed_formula.hpp.

Referenced by gui2::typed_formula< T >::typed_formula().

◆ convert() [2/6]

void gui2::typed_formula< bool >::convert ( const std::string &  str)
inlineprivate

Definition at line 180 of file typed_formula.hpp.

References utils::string_bool().

◆ convert() [3/6]

void gui2::typed_formula< std::string >::convert ( const std::string &  str)
inlineprivate

Definition at line 209 of file typed_formula.hpp.

◆ convert() [4/6]

void gui2::typed_formula< t_string >::convert ( const std::string &  str)
inlineprivate

Definition at line 222 of file typed_formula.hpp.

◆ convert() [5/6]

void gui2::typed_formula< PangoAlignment >::convert ( const std::string &  str)
inlineprivate

Definition at line 235 of file typed_formula.hpp.

References gui2::decode_text_alignment().

◆ convert() [6/6]

void gui2::typed_formula< color_t >::convert ( const std::string &  str)
inlineprivate

Definition at line 256 of file typed_formula.hpp.

References color_t::from_rgba_string().

◆ execute() [1/8]

template<typename T >
T gui2::typed_formula< T >::execute ( wfl::variant v) const
inlineprivate

Executes the formula.

This function does the calculation and can only be called if the object contains a formula.

Parameters
vA variant object containing the evaluated value of the formula.
Returns
The calculated value.

Definition at line 263 of file typed_formula.hpp.

◆ execute() [2/8]

bool gui2::typed_formula< bool >::execute ( wfl::variant v) const
inlineprivate

Template specializations.

Each type must have an execute specialization, and optionally one for convert.

Definition at line 174 of file typed_formula.hpp.

References wfl::variant::as_bool().

◆ execute() [3/8]

int gui2::typed_formula< int >::execute ( wfl::variant v) const
inlineprivate

Definition at line 187 of file typed_formula.hpp.

References wfl::variant::as_int().

◆ execute() [4/8]

unsigned gui2::typed_formula< unsigned >::execute ( wfl::variant v) const
inlineprivate

Definition at line 194 of file typed_formula.hpp.

References wfl::variant::as_int().

◆ execute() [5/8]

std::string gui2::typed_formula< std::string >::execute ( wfl::variant v) const
inlineprivate

Definition at line 203 of file typed_formula.hpp.

References wfl::variant::as_string().

◆ execute() [6/8]

t_string gui2::typed_formula< t_string >::execute ( wfl::variant v) const
inlineprivate

Definition at line 216 of file typed_formula.hpp.

References wfl::variant::as_string().

◆ execute() [7/8]

PangoAlignment gui2::typed_formula< PangoAlignment >::execute ( wfl::variant v) const
inlineprivate

Definition at line 229 of file typed_formula.hpp.

References wfl::variant::as_string(), and gui2::decode_text_alignment().

◆ execute() [8/8]

color_t gui2::typed_formula< color_t >::execute ( wfl::variant v) const
inlineprivate

Definition at line 242 of file typed_formula.hpp.

References ALPHA_OPAQUE, and wfl::variant::as_list().

◆ has_formula()

template<typename T >
bool gui2::typed_formula< T >::has_formula ( ) const
inline

◆ operator()() [1/2]

template<typename T >
T gui2::typed_formula< T >::operator() ( ) const
inline

Returns the value, can only be used if the data is no formula.

Another option would be to cache the output of the formula in value_ and always allow this function. But for now decided that the caller needs to do the caching. It might be changed later.

Definition at line 70 of file typed_formula.hpp.

References gui2::typed_formula< T >::has_formula(), and gui2::typed_formula< T >::value_.

◆ operator()() [2/2]

template<typename T >
T gui2::typed_formula< T >::operator() ( const wfl::map_formula_callable variables,
wfl::function_symbol_table functions = nullptr 
) const
inline

Returns the value, can always be used.

Parameters
variablesThe state variables which might be used in the formula. For example, screen_width can be set so the formula can return the half width of the screen.
functionsThe variables, which can be called during the evaluation of the formula. (Note it is also possible to add extra functions to the table, when the variable is not nullptr.
Returns
The stored result or the result of the evaluation of the formula.

Definition at line 152 of file typed_formula.hpp.

References DBG_GUI_D, and wfl::formula::evaluate().

Member Data Documentation

◆ formula_

template<typename T >
utils::optional<std::string> gui2::typed_formula< T >::formula_
private

Contains the formula for the variable.

If without value, there's no formula.

Definition at line 130 of file typed_formula.hpp.

Referenced by gui2::typed_formula< T >::has_formula(), and gui2::typed_formula< T >::typed_formula().

◆ value_

template<typename T >
T gui2::typed_formula< T >::value_
private

If there's no formula it contains the value.

Definition at line 133 of file typed_formula.hpp.

Referenced by gui2::typed_formula< T >::operator()().


The documentation for this class was generated from the following file: