Class CRuntimeCompiledExpression
Defined in File CRuntimeCompiledExpression.h
Class Documentation
-
class CRuntimeCompiledExpression
A wrapper of
exprtkruntime 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
doubleare supported.Variables must be provided via a
std::mapcontainer or pointers to user-stored variables.Custom user-defined functions taking 0-3 arguments (New in MRPT 2.5.8).
See examples of usage in the unit test file.
If the environment variable
MRPT_EXPR_VERBOSE=1is defined, debug information will be dumped to std::cout explaining the values of all the involved variables upon each call toeval(). Alternatively, the env varMRPT_EXPR_VERBOSEcan 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)".Note
(New in MRPT 1.5.0)
Note
(
MRPT_EXPR_VERBOSEnew in MRPT 1.5.7)Public Functions
-
CRuntimeCompiledExpression()
Default ctor
-
void compile(const std::string &expression, const std::map<std::string, double> &variables = {}, const std::string &expr_name_for_error_reporting = {})
Initializes the object by compiling an expression.
See also
- Parameters:
expression – [in] The expression to be compiled.
variables – [in] 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 – [in] A descriptive name of this formula, to be used when generating error reports via an exception, if needed
- Throws:
std::runtime_error – On any syntax error or undefined symbol while compiling the expression. The
e.what()message describes what is exactly the problem.
-
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
- Parameters:
variables – [in] 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.
-
inline void register_function(const std::string &name, const std::function<double()> &func)
Register a user-defined nullary function. (New in MRPT 2.5.8)
-
inline void register_function(const std::string &name, const std::function<double(double)> &func)
Register a user-defined unary function. (New in MRPT 2.5.8)
-
inline void register_function(const std::string &name, const std::function<double(double, double)> &func)
Register a user-defined binary function. (New in MRPT 2.5.8)
-
inline void register_function(const std::string &name, const std::function<double(double, double, double)> &func)
Register a user-defined ternary function. (New in MRPT 2.5.8)
-
double eval() const
Evaluates the current value of the precompiled formula.
- Throws:
std::runtime_error – If the formula has not been compiled yet.
-
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.