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.

  • 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=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;

    // construction

    CRuntimeCompiledExpression();

    //
methods

    void compile(
        const std::string& expression,
        const std::map<std::string, double>& variables = {},
        const std::string& expr_name_for_error_reporting = {}
        );

    void register_symbol_table(const std::map<std::string, double*>& variables);
    void register_function(const std::string& name, const std::function<double()>& func);
    void register_function(const std::string& name, const std::function<double(double)>& func);

    void register_function(
        const std::string& name,
        const std::function<double(double, double)>& func
        );

    void register_function(
        const std::string& name,
        const std::function<double(double, double, double)>& func
        );

    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;
};

Construction

CRuntimeCompiledExpression()

Default ctor.

Methods

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.

Parameters:

std::runtime_error

On any syntax error or undefined symbol while compiling the expression. The e.what() message describes what is exactly the problem.

See also:

register_symbol_table()

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

void register_function(
    const std::string& name,
    const std::function<double()>& func
    )

Register a user-defined nullary function.

(New in MRPT 2.5.8)

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)

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)

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.

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.