Main MRPT website > C++ reference for MRPT 1.5.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 
14 #include <cstdlib>
15 #include <iostream>
16 
17 #define exprtk_disable_string_capabilities // Workaround a bug in Ubuntu precise's GCC+libstdc++
18 #include <mrpt/otherlibs/exprtk.hpp>
19 
20 // We only need this to be on this translation unit, hence the advantage of using our MRPT wrapper instead
21 // of the original exprtk sources.
22 PIMPL_IMPLEMENT(exprtk::expression<double>);
23 
24 using namespace mrpt;
25 using namespace mrpt::math;
26 
28 {
30  {
32  return obj;
33  }
34  void process(const CRuntimeCompiledExpression& rce, const double ret);
35 
36 private:
38  std::vector<std::string> m_verbose_matches;
40  {
41  const char* sp = ::getenv("MRPT_EXPR_VERBOSE");
42  if (nullptr==sp) return;
44 
45  if (s==std::string("1"))
46  {
48  return;
49  }
51  }
52 };
53 
55 {
56  PIMPL_CONSTRUCT(exprtk::expression<double>, m_compiled_formula);
57 }
58 
60  const std::string &expression, //!< [in] The expression to be compiled.
61  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.
62  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
63 )
64 {
65  m_original_expr_str = expression;
66 
67  exprtk::symbol_table<double> symbol_table;
68  for (const auto &v : variables) {
69  double & var = const_cast<double&>(v.second);
70  symbol_table.add_variable(v.first, var);
71  }
72  symbol_table.add_constant("M_PI", M_PI);
73  symbol_table.add_constants();
74 
75  PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula).register_symbol_table(symbol_table);
76  // Compile user-given expressions:
77  exprtk::parser<double> parser;
78  if (!parser.compile(expression, PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula)))
79  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());
80 }
81 
83 {
84  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
85  double ret = PIMPL_GET_CONSTREF(exprtk::expression<double>, m_compiled_formula).value();
87  return ret;
88 }
89 
91 {
92  if (!m_verbose_always_enabled && m_verbose_matches.empty()) return;
93 
94  if (!m_verbose_matches.empty())
95  {
96  bool matched=false;
97  for (const auto &s:m_verbose_matches)
98  {
99  if (rce.m_original_expr_str.find(s)!=std::string::npos)
100  {
101  matched = true;
102  break;
103  }
104  }
105  if (!matched) return;
106  }
107 
108  std::vector<std::pair<std::string,double> > lst;
109  PIMPL_GET_REF(exprtk::expression<double>, rce.m_compiled_formula).get_symbol_table().get_variable_list(lst);
110  std::cout << "[CRuntimeCompiledExpression::eval()] DEBUG:\n"
111  "* Expression: "<< rce.m_original_expr_str << "\n"
112  "* Final value: " << ret << "\n"
113  "* Using these symbols:\n";
114  for (const auto &v : lst)
115  std::cout << " * " << v.first << " = " << v.second << "\n";
116 }
117 
118 void CRuntimeCompiledExpression::register_symbol_table(
119  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.
120 )
121 {
122  exprtk::symbol_table<double> symbol_table;
123  for (const auto &v : variables) {
124  double * var = const_cast<double*>(v.second);
125  symbol_table.add_variable(v.first, *var);
126  }
127  PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula).register_symbol_table(symbol_table);
128 }
129 
130 exprtk::expression<double> & CRuntimeCompiledExpression::get_raw_exprtk_expr() {
131  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
132  return PIMPL_GET_REF(exprtk::expression<double>, m_compiled_formula);
133 }
134 const exprtk::expression<double> & CRuntimeCompiledExpression::get_raw_exprtk_expr() const {
135  ASSERT_(m_compiled_formula.ptr.get() != nullptr);
136  return PIMPL_GET_CONSTREF(exprtk::expression<double>, m_compiled_formula);
137 }
138 
139 bool CRuntimeCompiledExpression::is_compiled() const
140 {
141  return m_compiled_formula.ptr.get() != nullptr;
142 }
143 const std::string & CRuntimeCompiledExpression::get_original_expression() const
144 {
145  return m_original_expr_str;
146 }
PIMPL_IMPLEMENT(exprtk::expression< double >)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
#define M_PI
Definition: bits.h:78
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
GLdouble s
Definition: glext.h:3602
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
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.
void process(const CRuntimeCompiledExpression &rce, const double ret)
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
static CRuntimeCompiledExpression::ExprVerbose & Instance()
GLsizei const GLchar ** string
Definition: glext.h:3919
#define PIMPL_GET_REF(_TYPE, _VAR_NAME)
Definition: pimpl.h:75
#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.
#define ASSERT_(f)
std::string BASE_IMPEXP trim(const std::string &str)
Removes leading and trailing spaces.
void BASE_IMPEXP tokenize(const std::string &inString, const std::string &inDelimiters, std::deque< std::string > &outTokens, bool skipBlankTokens=true) MRPT_NO_THROWS
Tokenizes a string according to a set of delimiting characters.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020