MRPT  2.0.1
List of all members | Classes | Public Member Functions | Private Attributes | Friends
mrpt::expr::CRuntimeCompiledExpression Class Reference

Detailed Description

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:

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)".

Note
(New in MRPT 1.5.0)
(MRPT_EXPR_VERBOSE new in MRPT 1.5.7)

Definition at line 61 of file CRuntimeCompiledExpression.h.

#include <mrpt/expr/CRuntimeCompiledExpression.h>

Classes

struct  ExprVerbose
 
struct  Impl
 

Public Member Functions

 CRuntimeCompiledExpression ()
 Default ctor. More...
 
 ~CRuntimeCompiledExpression ()
 
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 Attributes

mrpt::pimpl< Implm_impl
 

Friends

struct ExprVerbose
 

Constructor & Destructor Documentation

◆ CRuntimeCompiledExpression()

CRuntimeCompiledExpression::CRuntimeCompiledExpression ( )

Default ctor.

Definition at line 67 of file CRuntimeCompiledExpression.cpp.

◆ ~CRuntimeCompiledExpression()

CRuntimeCompiledExpression::~CRuntimeCompiledExpression ( )
default

Member Function Documentation

◆ compile()

void 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.

Exceptions
std::runtime_errorOn 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()
Parameters
[in]expressionThe expression to be compiled.
[in]variablesMap 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_reportingA descriptive name of this formula, to be used when generating error reports via an exception, if needed

Definition at line 74 of file CRuntimeCompiledExpression.cpp.

References m_impl, M_PI, and THROW_EXCEPTION_FMT.

Referenced by mrpt::nav::CMultiObjMotionOpt_Scalarization::impl_decide(), mrpt::nav::CPTG_Holo_Blend::internal_initialize(), mrpt::config::simpleini::MRPT_IniFileParser::parse_process_var_eval(), and TEST().

Here is the caller graph for this function:

◆ eval()

double CRuntimeCompiledExpression::eval ( ) const

Evaluates the current value of the precompiled formula.

Exceptions
std::runtime_errorIf the formula has not been compiled yet.

Definition at line 107 of file CRuntimeCompiledExpression.cpp.

References ASSERT_, mrpt::expr::CRuntimeCompiledExpression::ExprVerbose::Instance(), m_impl, and mrpt::expr::CRuntimeCompiledExpression::ExprVerbose::process().

Referenced by mrpt::nav::CMultiObjMotionOpt_Scalarization::impl_decide(), mrpt::nav::CPTG_Holo_Blend::internal_get_T_ramp(), mrpt::nav::CPTG_Holo_Blend::internal_get_v(), mrpt::nav::CPTG_Holo_Blend::internal_get_w(), mrpt::config::simpleini::MRPT_IniFileParser::parse_process_var_eval(), and TEST().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_original_expression()

const std::string & CRuntimeCompiledExpression::get_original_expression ( ) const

Returns the original formula passed to compile(), or an empty string if still not compiled.

Definition at line 147 of file CRuntimeCompiledExpression.cpp.

References m_impl.

◆ get_raw_exprtk_expr() [1/2]

exprtk::expression< double > & CRuntimeCompiledExpression::get_raw_exprtk_expr ( )

Access raw exprtk expression object.

Definition at line 130 of file CRuntimeCompiledExpression.cpp.

References ASSERT_, and m_impl.

◆ get_raw_exprtk_expr() [2/2]

const exprtk::expression< double > & CRuntimeCompiledExpression::get_raw_exprtk_expr ( ) const

Access raw exprtk expression object.

Definition at line 136 of file CRuntimeCompiledExpression.cpp.

References ASSERT_, and m_impl.

◆ is_compiled()

bool CRuntimeCompiledExpression::is_compiled ( ) const

Returns true if compile() was called and ended without errors.

Definition at line 142 of file CRuntimeCompiledExpression.cpp.

References ASSERT_, and m_impl.

◆ register_symbol_table()

void 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.

Parameters
[in]variablesMap 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.

Definition at line 115 of file CRuntimeCompiledExpression.cpp.

References m_impl.

Referenced by mrpt::nav::CPTG_Holo_Blend::internal_construct_exprs().

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ ExprVerbose

friend struct ExprVerbose
friend

Definition at line 113 of file CRuntimeCompiledExpression.h.

Member Data Documentation

◆ m_impl

mrpt::pimpl<Impl> mrpt::expr::CRuntimeCompiledExpression::m_impl
private



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020