Main MRPT website > C++ reference for MRPT 1.5.5
CRuntimeCompiledExpression.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
13 
14 #define exprtk_disable_string_capabilities // Workaround a bug in Ubuntu precise's GCC+libstdc++
15 #include <mrpt/otherlibs/exprtk.hpp>
16 
17 // We only need this to be on this translation unit, hence the advantage of using our MRPT wrapper instead
18 // of the original exprtk sources.
19 PIMPL_IMPLEMENT(exprtk::expression<double>);
20 
21 using namespace mrpt;
22 using namespace mrpt::math;
23 
25 {
26  PIMPL_CONSTRUCT(exprtk::expression<double>, m_compiled_formula);
27 }
28 
30  const std::string &expression, //!< [in] The expression to be compiled.
31  const std::map<std::string, double> &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.
32  const std::string &expr_name_for_error_reporting //!< A descriptive name of this formula, to be used when generating error reports via an exception, if needed
33 )
34 {
35  m_original_expr_str = expression;
36 
37  exprtk::symbol_table<double> symbol_table;
38  for (const auto &v : variables) {
39  double & var = const_cast<double&>(v.second);
40  symbol_table.add_variable(v.first, var);
41  }
42  symbol_table.add_constant("M_PI", M_PI);
43  symbol_table.add_constants();
44 
45  PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula).register_symbol_table(symbol_table);
46  // Compile user-given expressions:
47  exprtk::parser<double> parser;
48  if (!parser.compile(expression, PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula)))
49  THROW_EXCEPTION_FMT("Error compiling expression (name=`%s`): `%s`. Error: `%s`", expr_name_for_error_reporting.c_str(), expression.c_str(), parser.error().c_str());
50 }
51 
53 {
54  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
55  return PIMPL_GET_CONSTREF(exprtk::expression<double>, m_compiled_formula).value();
56 }
57 
59  const std::map<std::string, double *> &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.
60 )
61 {
62  exprtk::symbol_table<double> symbol_table;
63  for (const auto &v : variables) {
64  double * var = const_cast<double*>(v.second);
65  symbol_table.add_variable(v.first, *var);
66  }
67  PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula).register_symbol_table(symbol_table);
68 }
69 
70 exprtk::expression<double> & CRuntimeCompiledExpression::get_raw_exprtk_expr() {
71  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
72  return PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula);
73 }
74 const exprtk::expression<double> & CRuntimeCompiledExpression::get_raw_exprtk_expr() const {
75  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
76  return PIMPL_GET_CONSTREF(exprtk::expression<double>, m_compiled_formula);
77 }
78 
80 {
81  return m_compiled_formula.ptr.get() != nullptr;
82 }
84 {
85  return m_original_expr_str;
86 }
exprtk::expression< double > & get_raw_exprtk_expr()
Access raw exprtk expression object.
PIMPL_IMPLEMENT(exprtk::expression< double >)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
#define M_PI
Definition: bits.h:78
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.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
double eval() const
Evaluates the current value of the precompiled formula.
#define PIMPL_GET_CONSTREF(_TYPE, _VAR_NAME)
Definition: pimpl.h:76
GLsizei const GLchar ** string
Definition: glext.h:3919
#define PIMPL_GET_REF(_TYPE, _VAR_NAME)
Definition: pimpl.h:75
bool is_compiled() const
Returns true if compile() was called and ended without errors.
#define PIMPL_CONSTRUCT(_TYPE, _VAR_NAME)
Definition: pimpl.h:72
const GLdouble * v
Definition: glext.h:3603
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const std::string & get_original_expression() const
Returns the original formula passed to compile(), or an empty string if still not compiled...
#define ASSERT_(f)
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...



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