The Battle for Wesnoth  1.15.2+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...
 
template<>
bool execute (wfl::variant &v) const
 Template specializations. More...
 
template<>
void convert (const std::string &str)
 
template<>
int execute (wfl::variant &v) const
 
template<>
unsigned execute (wfl::variant &v) const
 
template<>
std::string execute (wfl::variant &v) const
 
template<>
void convert (const std::string &str)
 
template<>
t_string execute (wfl::variant &v) const
 
template<>
void convert (const std::string &str)
 
template<>
PangoAlignment execute (wfl::variant &v) const
 
template<>
void convert (const std::string &str)
 
template<>
color_t execute (wfl::variant &v) const
 
template<>
void convert (const std::string &str)
 

Private Attributes

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 48 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 135 of file typed_formula.hpp.

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 270 of file typed_formula.hpp.

Referenced by gui2::typed_formula< bool >::has_formula().

◆ convert() [2/6]

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

Definition at line 178 of file typed_formula.hpp.

◆ convert() [3/6]

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

Definition at line 207 of file typed_formula.hpp.

◆ convert() [4/6]

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

Definition at line 220 of file typed_formula.hpp.

◆ convert() [5/6]

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

Definition at line 233 of file typed_formula.hpp.

◆ convert() [6/6]

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

Definition at line 254 of file typed_formula.hpp.

◆ 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 261 of file typed_formula.hpp.

Referenced by gui2::typed_formula< bool >::has_formula().

◆ execute() [2/8]

template<>
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 172 of file typed_formula.hpp.

◆ execute() [3/8]

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

Definition at line 185 of file typed_formula.hpp.

◆ execute() [4/8]

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

Definition at line 192 of file typed_formula.hpp.

◆ execute() [5/8]

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

Definition at line 201 of file typed_formula.hpp.

◆ execute() [6/8]

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

Definition at line 214 of file typed_formula.hpp.

◆ execute() [7/8]

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

Definition at line 227 of file typed_formula.hpp.

◆ execute() [8/8]

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

Definition at line 240 of file typed_formula.hpp.

◆ 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 68 of file typed_formula.hpp.

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

◆ 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 151 of file typed_formula.hpp.

Member Data Documentation

◆ formula_

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

Contains the formula for the variable.

If the string is empty, there's no formula.

Definition at line 128 of file typed_formula.hpp.

Referenced by gui2::typed_formula< bool >::has_formula().

◆ value_

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

If there's no formula it contains the value.

Definition at line 131 of file typed_formula.hpp.

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


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