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



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