Main MRPT website > C++ reference for MRPT 1.9.9
CRuntimeCompiledExpression.h
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 #pragma once
10 
11 #include <mrpt/core/pimpl.h>
12 
13 #include <map>
14 #include <string>
15 #include <memory>
16 #include "mrpt-expr_export.h"
17 
18 namespace exprtk {
19  template <typename T>
20  class expression;
21 }
22 
23 namespace mrpt
24 {
25 namespace expr
26 {
27 /** A wrapper of `exprtk` runtime expression compiler: it takes a string
28  * representing an
29  * expression (from a simple mathematical formula to a complete program),
30  * compiles it
31  * and evaluates its result as many times as required. The result will change as
32  * the "variables"
33  * appearing in the expression (hold and managed by the user of this object)
34  * change.
35  *
36  * Refer to [exprtk documentation](https://github.com/ArashPartow/exprtk) for
37  * reference on supported formulas, control flow instructions, etc.
38  *
39  * This wrapper is provided to reduce the (very large) compilation time and
40  * memory required by the original
41  * library, at the cost of only exposing the most commonly used part of its API:
42  * - Only expressions returning `double` are supported.
43  * - Variables must be provided via a `std::map` container **or** pointers to
44  * user-stored variables.
45  *
46  * See examples of usage in the [unit test
47  * file](https://github.com/MRPT/mrpt/blob/master/libs/base/src/math/CRuntimeCompiledExpression_unittest.cpp).
48  *
49  * \note (New in MRPT 1.5.0)
50  * \ingroup mrpt_base_grp
51  */
53 {
54  public:
55  /** Default ctor */
58 
59  /** Initializes the object by compiling an expression.
60  * \exception std::runtime_error On any syntax error or undefined symbol
61  * while compiling the expression. The `e.what()` message describes what is
62  * exactly the problem.
63  * \sa register_symbol_table()
64  */
65  void compile(
66  /** [in] The expression to be compiled. */
67  const std::string& expression,
68  /** [in] Map of variables/constants by `name` -> `value`. The
69  references to the values in this map **must** be ensured to be
70  valid thoughout all the life of the compiled expression. */
71  const std::map<std::string, double>& variables =
72  std::map<std::string, double>(),
73  /** A descriptive name of this formula, to be used when generating
74  error reports via an exception, if needed */
75  const std::string& expr_name_for_error_reporting = std::string());
76 
77  /** Can be used **before** calling compile() to register additional
78  * variables by means of **pointers** instead of a std::map */
79  void register_symbol_table(
80  /** [in] Map of variables/constants by `name` -> `value`. The
81  references to the values in this map **must** be ensured to be
82  valid thoughout all the life of the compiled expression. */
83  const std::map<std::string, double*>& variables);
84 
85  /** Evaluates the current value of the precompiled formula.
86  * \exception std::runtime_error If the formula has not been compiled yet.
87  */
88  double eval() const;
89 
90  /** Returns true if compile() was called and ended without errors. */
91  bool is_compiled() const;
92  /** Returns the original formula passed to compile(), or an empty string if
93  * still not compiled. */
94  const std::string& get_original_expression() const;
95 
96  /** Access raw exprtk expression object. */
97  exprtk::expression<double>& get_raw_exprtk_expr();
98  /** Access raw exprtk expression object. */
99  const exprtk::expression<double>& get_raw_exprtk_expr() const;
100 
101  private:
102  struct Impl;
104 }; // End of class def.
105 
106 } // End of namespace
107 
108 
109 } // End of namespace
MRPT_EXPR_EXPORT
#define MRPT_EXPR_EXPORT
Definition: mrpt-expr_export.h:25
mrpt::pimpl
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::expr::CRuntimeCompiledExpression::Impl
Definition: CRuntimeCompiledExpression.cpp:34
mrpt-expr_export.h
exprtk::expression
Definition: CRuntimeCompiledExpression.h:20
exprtk
Definition: CRuntimeCompiledExpression.h:18
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
pimpl.h
string
GLsizei const GLchar ** string
Definition: glext.h:4101



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