class mrpt::expr::CRuntimeCompiledExpression¶
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:
Only expressions returning
double
are supported.Variables must be provided via a
std::map
container or pointers to user-stored variables.
See examples of usage in the unit test file.
If the environment variable MRPT_EXPR_VERBOSE=1
is defined, debug information will be dumped to std::cout explaining the values of all the involved variables upon each call to eval()
. Alternatively, the env var MRPT_EXPR_VERBOSE
can be set to a list of terms split by |
, and only those formulas that match (i.e. contain as substrings) any of the terms will be traced. Example: MRPT_EXPR_VERBOSE="cos|sin|speed|if (x>0)"
.
(New in MRPT 1.5.0)
(MRPT_EXPR_VERBOSE
new in MRPT 1.5.7)
#include <mrpt/expr/CRuntimeCompiledExpression.h> class CRuntimeCompiledExpression { public: // structs struct ExprVerbose; struct Impl; // methods 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() ); void register_symbol_table(const std::map<std::string, double*>& variables); double eval() const; bool is_compiled() const; const std::string& get_original_expression() const; exprtk::expression<double>& get_raw_exprtk_expr(); const exprtk::expression<double>& get_raw_exprtk_expr() const; };
Methods¶
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.
Parameters:
std::runtime_error |
On any syntax error or undefined symbol while compiling the expression. The |
See also:
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.
double eval() const
Evaluates the current value of the precompiled formula.
Parameters:
std::runtime_error |
If the formula has not been compiled yet. |
bool is_compiled() const
Returns true if compile() was called and ended without errors.
const std::string& get_original_expression() const
Returns the original formula passed to compile(), or an empty string if still not compiled.
exprtk::expression<double>& get_raw_exprtk_expr()
Access raw exprtk expression object.
const exprtk::expression<double>& get_raw_exprtk_expr() const
Access raw exprtk expression object.