Main MRPT website > C++ reference for MRPT 1.9.9
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
15 // precise's GCC+libstdc++
16 #include <mrpt/otherlibs/exprtk.hpp>
17 
18 // We only need this to be on this translation unit, hence the advantage of
19 // using our MRPT wrapper instead
20 // of the original exprtk sources.
21 PIMPL_IMPLEMENT(exprtk::expression<double>)
22 
23 using namespace mrpt;
24 using namespace mrpt::math;
25 
26 CRuntimeCompiledExpression::CRuntimeCompiledExpression()
27 {
28  PIMPL_CONSTRUCT(exprtk::expression<double>, m_compiled_formula);
29 }
30 
31 void CRuntimeCompiledExpression::compile(
32  /** [in] The expression to be compiled. */
33  const std::string& expression,
34  /** [in] Map of variables/constants by `name` -> `value`. The references to
35  the values in this map **must** be ensured to be valid thoughout all the
36  life of the compiled expression. */
37  const std::map<std::string, double>& variables,
38  /** A descriptive name of this formula, to be used when generating error
39  reports via an exception, if needed */
40  const std::string& expr_name_for_error_reporting)
41 {
42  m_original_expr_str = expression;
43 
44  exprtk::symbol_table<double> symbol_table;
45  for (const auto& v : variables)
46  {
47  double& var = const_cast<double&>(v.second);
48  symbol_table.add_variable(v.first, var);
49  }
50  symbol_table.add_constant("M_PI", M_PI);
51  symbol_table.add_constants();
52 
53  PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula)
54  .register_symbol_table(symbol_table);
55  // Compile user-given expressions:
56  exprtk::parser<double> parser;
57  if (!parser.compile(
58  expression,
59  PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula)))
61  "Error compiling expression (name=`%s`): `%s`. Error: `%s`",
62  expr_name_for_error_reporting.c_str(), expression.c_str(),
63  parser.error().c_str());
64 }
65 
66 double CRuntimeCompiledExpression::eval() const
67 {
68  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
69  return PIMPL_GET_CONSTREF(exprtk::expression<double>, m_compiled_formula)
70  .value();
71 }
72 
73 void CRuntimeCompiledExpression::register_symbol_table(
74  /** [in] Map of variables/constants by `name` -> `value`. The
75  references to the values in this map **must** be ensured to be valid
76  thoughout all the life of the compiled expression. */
77  const std::map<std::string, double*>& variables)
78 {
79  exprtk::symbol_table<double> symbol_table;
80  for (const auto& v : variables)
81  {
82  double* var = const_cast<double*>(v.second);
83  symbol_table.add_variable(v.first, *var);
84  }
85  PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula)
86  .register_symbol_table(symbol_table);
87 }
88 
89 exprtk::expression<double>& CRuntimeCompiledExpression::get_raw_exprtk_expr()
90 {
91  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
92  return PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula);
93 }
94 const exprtk::expression<double>&
95  CRuntimeCompiledExpression::get_raw_exprtk_expr() const
96 {
97  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
98  return PIMPL_GET_CONSTREF(exprtk::expression<double>, m_compiled_formula);
99 }
100 
101 bool CRuntimeCompiledExpression::is_compiled() const
102 {
103  return m_compiled_formula.ptr.get() != nullptr;
104 }
105 const std::string& CRuntimeCompiledExpression::get_original_expression() const
106 {
107  return m_original_expr_str;
108 }
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
#define M_PI
Definition: bits.h:92
#define PIMPL_IMPLEMENT(_TYPE)
Definition: pimpl.h:51
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
#define PIMPL_GET_CONSTREF(_TYPE, _VAR_NAME)
Definition: pimpl.h:99
GLsizei const GLchar ** string
Definition: glext.h:4101
#define PIMPL_GET_REF(_TYPE, _VAR_NAME)
Definition: pimpl.h:98
#define PIMPL_CONSTRUCT(_TYPE, _VAR_NAME)
Definition: pimpl.h:95
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define ASSERT_(f)



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019