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... | |
T | operator() () const |
Returns the value, can only be used if the data is no formula. More... | |
T | 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 Attributes | |
std::string | formula_ |
Contains the formula for the variable. More... | |
T | value_ |
If there's no formula it contains the value. More... | |
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.
T | The 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.
|
explicit |
Constructor.
str | The string used to initialize the class, this can either be a formula or a string which can be converted to the type T. |
value | The default value for the object. |
Definition at line 139 of file formula.hpp.
References gui2::tformula< T >::convert(), and gui2::tformula< T >::formula_.
|
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.
str | The str send to the constructor. |
Definition at line 265 of file formula.hpp.
Referenced by gui2::tformula< T >::tformula().
|
inlineprivate |
Definition at line 241 of file formula.hpp.
References utils::string_bool().
|
inlineprivate |
Definition at line 247 of file formula.hpp.
|
inlineprivate |
Definition at line 253 of file formula.hpp.
|
inlineprivate |
Definition at line 259 of file formula.hpp.
References gui2::decode_text_alignment().
|
inlineprivate |
Executes the formula.
This function does the calculation and can only be called if the object contains a formula.
variables | The 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. |
functions | The 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 . |
Definition at line 230 of file formula.hpp.
|
inlineprivate |
Definition at line 170 of file formula.hpp.
References variant::as_bool(), and game_logic::formula::evaluate().
|
inlineprivate |
Definition at line 180 of file formula.hpp.
References variant::as_int(), and game_logic::formula::evaluate().
|
inlineprivate |
Definition at line 190 of file formula.hpp.
References variant::as_int(), and game_logic::formula::evaluate().
|
inlineprivate |
Definition at line 199 of file formula.hpp.
References variant::as_string(), and game_logic::formula::evaluate().
|
inlineprivate |
Definition at line 210 of file formula.hpp.
References variant::as_string(), and game_logic::formula::evaluate().
|
inlineprivate |
Definition at line 219 of file formula.hpp.
References gui2::decode_text_alignment().
|
inline |
Determine whether the class contains a formula.
Definition at line 91 of file formula.hpp.
Referenced by gui2::tformula< bool >::operator()().
|
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.
|
inline |
Returns the value, can always be used.
variables | The variables, which can be used during the evaluation of the formula. |
functions | The 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 . |
Definition at line 155 of file formula.hpp.
References LOG_GUI_D.
|
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().
|
private |
If there's no formula it contains the value.
Definition at line 135 of file formula.hpp.
Referenced by gui2::tformula< bool >::operator()().