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-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 }
mrpt::expr::CRuntimeCompiledExpression::is_compiled
bool is_compiled() const
Returns true if compile() was called and ended without errors.
Definition: CRuntimeCompiledExpression.cpp:115
expr-precomp.h
exceptions.h
mrpt::expr::CRuntimeCompiledExpression::compile
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.
Definition: CRuntimeCompiledExpression.cpp:47
mrpt::expr::CRuntimeCompiledExpression::CRuntimeCompiledExpression
CRuntimeCompiledExpression()
Default ctor.
Definition: CRuntimeCompiledExpression.cpp:40
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::expr::CRuntimeCompiledExpression::register_symbol_table
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...
Definition: CRuntimeCompiledExpression.cpp:88
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
mrpt::expr::CRuntimeCompiledExpression::Impl
Definition: CRuntimeCompiledExpression.cpp:34
CRuntimeCompiledExpression.h
exprtk::expression< double >
v
const GLdouble * v
Definition: glext.h:3678
M_PI
#define M_PI
Definition: core/include/mrpt/core/bits_math.h:38
mrpt::expr::CRuntimeCompiledExpression::m_impl
mrpt::pimpl< Impl > m_impl
Definition: CRuntimeCompiledExpression.h:102
mrpt::expr::CRuntimeCompiledExpression
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
Definition: CRuntimeCompiledExpression.h:52
mrpt::expr::CRuntimeCompiledExpression::~CRuntimeCompiledExpression
~CRuntimeCompiledExpression()
Definition: CRuntimeCompiledExpression.cpp:45
mrpt::expr::CRuntimeCompiledExpression::eval
double eval() const
Evaluates the current value of the precompiled formula.
Definition: CRuntimeCompiledExpression.cpp:82
mrpt::make_impl
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
mrpt::expr::CRuntimeCompiledExpression::get_original_expression
const std::string & get_original_expression() const
Returns the original formula passed to compile(), or an empty string if still not compiled.
Definition: CRuntimeCompiledExpression.cpp:120
mrpt::expr::CRuntimeCompiledExpression::get_raw_exprtk_expr
exprtk::expression< double > & get_raw_exprtk_expr()
Access raw exprtk expression object.
Definition: CRuntimeCompiledExpression.cpp:103
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::expr
Definition: CRuntimeCompiledExpression.h:25
mrpt::expr::CRuntimeCompiledExpression::Impl::m_compiled_formula
exprtk::expression< double > m_compiled_formula
Definition: CRuntimeCompiledExpression.cpp:36
mrpt::expr::CRuntimeCompiledExpression::Impl::m_original_expr_str
std::string m_original_expr_str
Definition: CRuntimeCompiledExpression.cpp:37



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST