Main MRPT website > C++ reference for MRPT 1.5.5
List of all members | Public Member Functions | Private Member Functions | Private Attributes
mrpt::math::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.

Note
(New in MRPT 1.5.0)

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::stringget_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
 

Constructor & Destructor Documentation

◆ CRuntimeCompiledExpression()

CRuntimeCompiledExpression::CRuntimeCompiledExpression ( )

Default ctor.

Definition at line 24 of file CRuntimeCompiledExpression.cpp.

References PIMPL_CONSTRUCT.

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 29 of file CRuntimeCompiledExpression.cpp.

References m_original_expr_str, M_PI, PIMPL_GET_REF, and THROW_EXCEPTION_FMT.

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

◆ 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 52 of file CRuntimeCompiledExpression.cpp.

References ASSERT_, and PIMPL_GET_CONSTREF.

Referenced by 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::utils::simpleini::MRPT_IniFileParser::parse_process_var_eval(), and TEST().

◆ 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 83 of file CRuntimeCompiledExpression.cpp.

References m_original_expr_str.

◆ get_raw_exprtk_expr() [1/2]

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

Access raw exprtk expression object.

Definition at line 70 of file CRuntimeCompiledExpression.cpp.

References ASSERT_, and PIMPL_GET_REF.

◆ get_raw_exprtk_expr() [2/2]

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

Access raw exprtk expression object.

Definition at line 74 of file CRuntimeCompiledExpression.cpp.

References ASSERT_, and PIMPL_GET_CONSTREF.

◆ is_compiled()

bool CRuntimeCompiledExpression::is_compiled ( ) const

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

Definition at line 79 of file CRuntimeCompiledExpression.cpp.

◆ PIMPL_DECLARE_TYPE()

mrpt::math::CRuntimeCompiledExpression::PIMPL_DECLARE_TYPE ( exprtk::expression< double >  ,
m_compiled_formula   
)
private

◆ 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 58 of file CRuntimeCompiledExpression.cpp.

References PIMPL_GET_REF.

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

Member Data Documentation

◆ m_original_expr_str

std::string mrpt::math::CRuntimeCompiledExpression::m_original_expr_str
private

Definition at line 73 of file CRuntimeCompiledExpression.h.

Referenced by compile(), and get_original_expression().




Page generated by Doxygen 1.8.14 for MRPT 1.5.5 Git: e06b63dbf Fri Dec 1 14:41:11 2017 +0100 at lun oct 28 01:31:35 CET 2019