A wrapper of exprtk
runtime expression compiler: it takes a string representing an expression (from a simple mathematical formula to a complete program), compiles it and evaluates its result as many times as required.
The result will change as the "variables" appearing in the expression (hold and managed by the user of this object) change.
Refer to exprtk documentation for reference on supported formulas, control flow instructions, etc.
This wrapper is provided to reduce the (very large) compilation time and memory required by the original library, at the cost of only exposing the most commonly used part of its API:
double
are supported.std::map
container or pointers to user-stored variables.See examples of usage in the unit test file.
Definition at line 40 of file CRuntimeCompiledExpression.h.
#include <mrpt/math/CRuntimeCompiledExpression.h>
Public Member Functions | |
CRuntimeCompiledExpression () | |
Default ctor. More... | |
void | compile (const std::string &expression, const std::map< std::string, double > &variables=std::map< std::string, double >(), const std::string &expr_name_for_error_reporting=std::string()) |
Initializes the object by compiling an expression. More... | |
void | register_symbol_table (const std::map< std::string, double *> &variables) |
Can be used before calling compile() to register additional variables by means of pointers instead of a std::map. More... | |
double | eval () const |
Evaluates the current value of the precompiled formula. More... | |
bool | is_compiled () const |
Returns true if compile() was called and ended without errors. More... | |
const std::string & | get_original_expression () const |
Returns the original formula passed to compile(), or an empty string if still not compiled. More... | |
exprtk::expression< double > & | get_raw_exprtk_expr () |
Access raw exprtk expression object. More... | |
const exprtk::expression< double > & | get_raw_exprtk_expr () const |
Access raw exprtk expression object. More... | |
Private Member Functions | |
PIMPL_DECLARE_TYPE (exprtk::expression< double >, m_compiled_formula) | |
Private Attributes | |
std::string | m_original_expr_str |
mrpt::math::CRuntimeCompiledExpression::CRuntimeCompiledExpression | ( | ) |
Default ctor.
void mrpt::math::CRuntimeCompiledExpression::compile | ( | const std::string & | expression, |
const std::map< std::string, double > & | variables = std::map< std::string, double >() , |
||
const std::string & | expr_name_for_error_reporting = std::string() |
||
) |
Initializes the object by compiling an expression.
std::runtime_error | On any syntax error or undefined symbol while compiling the expression. The e.what() message describes what is exactly the problem. |
[in] | expression | The expression to be compiled. |
[in] | variables | Map of variables/constants by name -> value . The references to the values in this map must be ensured to be valid thoughout all the life of the compiled expression. |
expr_name_for_error_reporting | A descriptive name of this formula, to be used when generating error reports via an exception, if needed |
double mrpt::math::CRuntimeCompiledExpression::eval | ( | ) | const |
Evaluates the current value of the precompiled formula.
std::runtime_error | If the formula has not been compiled yet. |
const std::string& mrpt::math::CRuntimeCompiledExpression::get_original_expression | ( | ) | const |
Returns the original formula passed to compile(), or an empty string if still not compiled.
exprtk::expression<double>& mrpt::math::CRuntimeCompiledExpression::get_raw_exprtk_expr | ( | ) |
Access raw exprtk expression object.
const exprtk::expression<double>& mrpt::math::CRuntimeCompiledExpression::get_raw_exprtk_expr | ( | ) | const |
Access raw exprtk expression object.
bool mrpt::math::CRuntimeCompiledExpression::is_compiled | ( | ) | const |
Returns true if compile() was called and ended without errors.
|
private |
void mrpt::math::CRuntimeCompiledExpression::register_symbol_table | ( | const std::map< std::string, double *> & | variables | ) |
Can be used before calling compile() to register additional variables by means of pointers instead of a std::map.
[in] | variables | Map of variables/constants by name -> value . The references to the values in this map must be ensured to be valid thoughout all the life of the compiled expression. |
|
private |
Definition at line 73 of file CRuntimeCompiledExpression.h.
Page generated by Doxygen 1.8.13 for MRPT 1.5.3 at Mon Oct 30 10:27:08 UTC 2017 |