MRPT  2.0.4
CRuntimeCompiledExpression.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/pimpl.h>
12 
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include "mrpt-expr_export.h"
17 
18 namespace exprtk
19 {
20 template <typename T>
21 class expression;
22 }
23 
24 namespace mrpt
25 {
26 namespace expr
27 {
28 /** A wrapper of `exprtk` runtime expression compiler: it takes a string
29  * representing an expression (from a simple mathematical formula to a complete
30  * program), compiles it and evaluates its result as many times as required. The
31  * result will change as the "variables" appearing in the expression (hold and
32  * managed by the user of this object) 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 library, at the cost of only exposing the
39  * 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  * If the environment variable `MRPT_EXPR_VERBOSE=1` is defined, debug
48  * information will be dumped to std::cout explaining the values of **all** the
49  * involved variables upon each call to `eval()`.
50  * Alternatively, the env var `MRPT_EXPR_VERBOSE` can be set to a list of
51  * terms split by `|`, and only those formulas that match (i.e. contain as
52  * substrings) any of the terms will be traced.
53  * Example: `MRPT_EXPR_VERBOSE="cos|sin|speed|if (x>0)"`.
54  *
55  * \note (New in MRPT 1.5.0)
56  * \note (`MRPT_EXPR_VERBOSE` new in MRPT 1.5.7)
57  * \ingroup mrpt_expr_grp
58  */
59 // Note: Leave the MRPT_EXPR_EXPORT macro in mrpt-expr, to avoid exporting ALL
60 // symbols which would increase the lib size too much.
62 {
63  public:
64  /** Default ctor */
67 
68  /** Initializes the object by compiling an expression.
69  * \exception std::runtime_error On any syntax error or undefined symbol
70  * while compiling the expression. The `e.what()` message describes what is
71  * exactly the problem.
72  * \sa register_symbol_table()
73  */
74  void compile(
75  /** [in] The expression to be compiled. */
76  const std::string& expression,
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  std::map<std::string, double>(),
82  /** A descriptive name of this formula, to be used when generating
83  error reports via an exception, if needed */
84  const std::string& expr_name_for_error_reporting = std::string());
85 
86  /** Can be used **before** calling compile() to register additional
87  * variables by means of **pointers** instead of a std::map */
88  void register_symbol_table(
89  /** [in] Map of variables/constants by `name` -> `value`. The
90  references to the values in this map **must** be ensured to be
91  valid thoughout all the life of the compiled expression. */
92  const std::map<std::string, double*>& variables);
93 
94  /** Evaluates the current value of the precompiled formula.
95  * \exception std::runtime_error If the formula has not been compiled yet.
96  */
97  double eval() const;
98 
99  /** Returns true if compile() was called and ended without errors. */
100  bool is_compiled() const;
101  /** Returns the original formula passed to compile(), or an empty string if
102  * still not compiled. */
103  const std::string& get_original_expression() const;
104 
105  /** Access raw exprtk expression object. */
106  exprtk::expression<double>& get_raw_exprtk_expr();
107  /** Access raw exprtk expression object. */
108  const exprtk::expression<double>& get_raw_exprtk_expr() const;
109 
110  private:
111  struct Impl;
113  struct ExprVerbose;
114  friend struct ExprVerbose;
115 }; // End of class def.
116 
117 } // namespace expr
118 
119 } // namespace mrpt
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
spimpl::impl_ptr< T > pimpl
Definition: pimpl.h:15
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_EXPR_EXPORT



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020