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-2018, 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 "expr-precomp.h" // Precompiled headers
11 
13 #include <mrpt/core/exceptions.h>
14 #include <cmath> // M_PI
15 
16 #define exprtk_disable_string_capabilities // Workaround a bug in Ubuntu
17 // precise's GCC+libstdc++
18 
19 // Disable some parts of exprtk to build faster and reduce lib size:
20 #define exprtk_disable_enhanced_features
21 #define exprtk_disable_superscalar_unroll
22 #define exprtk_disable_rtl_vecops
23 #define exprtk_disable_rtl_io_file
24 #include <mrpt/otherlibs/exprtk.hpp>
25 
26 // We only need this to be on this translation unit, hence the advantage of
27 // using our MRPT wrapper instead
28 // of the original exprtk sources.
29 //PIMPL_IMPLEMENT(exprtk::expression<double>
30 
31 using namespace mrpt;
32 using namespace mrpt::expr;
33 
35 {
38 };
39 
42 {
43 }
44 
46 
48  /** [in] The expression to be compiled. */
49  const std::string& expression,
50  /** [in] Map of variables/constants by `name` -> `value`. The references to
51  the values in this map **must** be ensured to be valid thoughout all the
52  life of the compiled expression. */
53  const std::map<std::string, double>& variables,
54  /** A descriptive name of this formula, to be used when generating error
55  reports via an exception, if needed */
56  const std::string& expr_name_for_error_reporting)
57 {
58  m_impl->m_original_expr_str = expression;
59 
60  exprtk::symbol_table<double> symbol_table;
61  for (const auto& v : variables)
62  {
63  double& var = const_cast<double&>(v.second);
64  symbol_table.add_variable(v.first, var);
65  }
66  symbol_table.add_constant("M_PI", M_PI);
67  symbol_table.add_constants();
68 
69  m_impl->m_compiled_formula.register_symbol_table(symbol_table);
70 
71  // Compile user-given expressions:
72  exprtk::parser<double> parser;
73  if (!parser.compile(
74  expression,
75  m_impl->m_compiled_formula))
77  "Error compiling expression (name=`%s`): `%s`. Error: `%s`",
78  expr_name_for_error_reporting.c_str(), expression.c_str(),
79  parser.error().c_str());
80 }
81 
83 {
84  ASSERT_(m_impl);
85  return m_impl->m_compiled_formula.value();
86 }
87 
89  /** [in] Map of variables/constants by `name` -> `value`. The
90  references to the values in this map **must** be ensured to be valid
91  thoughout all the life of the compiled expression. */
92  const std::map<std::string, double*>& variables)
93 {
94  exprtk::symbol_table<double> symbol_table;
95  for (const auto& v : variables)
96  {
97  double* var = const_cast<double*>(v.second);
98  symbol_table.add_variable(v.first, *var);
99  }
100  m_impl->m_compiled_formula.register_symbol_table(symbol_table);
101 }
102 
104 {
105  ASSERT_(m_impl);
106  return m_impl->m_compiled_formula;
107 }
110 {
111  ASSERT_(m_impl);
112  return m_impl->m_compiled_formula;
113 }
114 
116 {
117  ASSERT_(m_impl);
118  return m_impl->m_compiled_formula;
119 }
121 {
122  return m_impl->m_original_expr_str;
123 }
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.
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
bool is_compiled() const
Returns true if compile() was called and ended without errors.
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...
GLsizei const GLchar ** string
Definition: glext.h:4101
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.
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double eval() const
Evaluates the current value of the precompiled formula.
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020