The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
gui2::tformula< T > Class Template Reference

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

#include <formula.hpp>

Public Member Functions

 tformula (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 game_logic::map_formula_callable &variables, game_logic::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 (const game_logic::map_formula_callable &variables, game_logic::function_symbol_table *functions) const
 Executes the formula. More...
 
template<>
bool execute (const game_logic::map_formula_callable &variables, game_logic::function_symbol_table *functions) const
 
template<>
int execute (const game_logic::map_formula_callable &variables, game_logic::function_symbol_table *functions) const
 
template<>
unsigned execute (const game_logic::map_formula_callable &variables, game_logic::function_symbol_table *functions) const
 
template<>
std::string execute (const game_logic::map_formula_callable &variables, game_logic::function_symbol_table *functions) const
 
template<>
t_string execute (const game_logic::map_formula_callable &variables, game_logic::function_symbol_table *functions) const
 
template<>
PangoAlignment execute (const game_logic::map_formula_callable &variables, game_logic::function_symbol_table *functions) const
 
template<>
void convert (const std::string &str)
 
template<>
void convert (const std::string &str)
 
template<>
void convert (const std::string &str)
 
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<class T>
class gui2::tformula< 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 formula.hpp.

Constructor & Destructor Documentation

template<class T>
gui2::tformula< T >::tformula ( 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 139 of file formula.hpp.

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

Member Function Documentation

template<class T >
void gui2::tformula< 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 265 of file formula.hpp.

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

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

Definition at line 241 of file formula.hpp.

References utils::string_bool().

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

Definition at line 247 of file formula.hpp.

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

Definition at line 253 of file formula.hpp.

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

Definition at line 259 of file formula.hpp.

References gui2::decode_text_alignment().

template<class T >
T gui2::tformula< T >::execute ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions 
) const
inlineprivate

Executes the formula.

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

Parameters
variablesThe state variables which might be used in the formula. For example a 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 is is also possible to add extra functions to the table, when the variable is not nullptr.
Returns
The calculated value.

Definition at line 230 of file formula.hpp.

template<>
bool gui2::tformula< bool >::execute ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions 
) const
inlineprivate

Definition at line 170 of file formula.hpp.

References variant::as_bool(), and game_logic::formula::evaluate().

template<>
int gui2::tformula< int >::execute ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions 
) const
inlineprivate

Definition at line 180 of file formula.hpp.

References variant::as_int(), and game_logic::formula::evaluate().

template<>
unsigned gui2::tformula< unsigned >::execute ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions 
) const
inlineprivate

Definition at line 190 of file formula.hpp.

References variant::as_int(), and game_logic::formula::evaluate().

template<>
std::string gui2::tformula< std::string >::execute ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions 
) const
inlineprivate

Definition at line 199 of file formula.hpp.

References variant::as_string(), and game_logic::formula::evaluate().

template<>
t_string gui2::tformula< t_string >::execute ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions 
) const
inlineprivate

Definition at line 210 of file formula.hpp.

References variant::as_string(), and game_logic::formula::evaluate().

template<>
PangoAlignment gui2::tformula< PangoAlignment >::execute ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions 
) const
inlineprivate

Definition at line 219 of file formula.hpp.

References gui2::decode_text_alignment().

template<class T>
bool gui2::tformula< T >::has_formula ( ) const
inline

Determine whether the class contains a formula.

Definition at line 91 of file formula.hpp.

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

template<class T>
T gui2::tformula< 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 formula.hpp.

template<class T >
T gui2::tformula< T >::operator() ( const game_logic::map_formula_callable variables,
game_logic::function_symbol_table functions = nullptr 
) const
inline

Returns the value, can always be used.

Parameters
variablesThe variables, which can be used during the evaluation of the formula.
functionsThe variables, which can be called during the evaluation of the formula. (Note is 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 155 of file formula.hpp.

References LOG_GUI_D.

Member Data Documentation

template<class T>
std::string gui2::tformula< T >::formula_
private

Contains the formula for the variable.

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

Definition at line 132 of file formula.hpp.

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

template<class T>
T gui2::tformula< T >::value_
private

If there's no formula it contains the value.

Definition at line 135 of file formula.hpp.

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


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