Main MRPT website > C++ reference for MRPT 1.5.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 <mrpt/base/link_pragmas.h>
14 #include <map>
15 #include <string>
16 
17 PIMPL_FORWARD_DECLARATION(namespace exprtk { template <typename T> class expression; })
18 
19 namespace mrpt
20 {
21 namespace math
22 {
23  /** A wrapper of `exprtk` runtime expression compiler: it takes a string representing an
24  * expression (from a simple mathematical formula to a complete program), compiles it
25  * and evaluates its result as many times as required. The result will change as the "variables"
26  * appearing in the expression (hold and managed by the user of this object) change.
27  *
28  * Refer to [exprtk documentation](https://github.com/ArashPartow/exprtk) for reference on supported formulas, control flow instructions, etc.
29  *
30  * This wrapper is provided to reduce the (very large) compilation time and memory required by the original
31  * library, at the cost of only exposing the most commonly used part of its API:
32  * - Only expressions returning `double` are supported.
33  * - Variables must be provided via a `std::map` container **or** pointers to user-stored variables.
34  *
35  * See examples of usage in the [unit test file](https://github.com/MRPT/mrpt/blob/master/libs/base/src/math/CRuntimeCompiledExpression_unittest.cpp).
36  *
37  * If the environment variable `MRPT_EXPR_VERBOSE=1` is defined, debug
38  * information will be dumped to std::cout explaining the values of **all** the
39  * involved variables upon each call to `eval()`.
40  * Alternatively, the env var `MRPT_EXPR_VERBOSE` can be set to a list of
41  * terms split by `|`, and only those formulas that match (i.e. contain as
42  * substrings) any of the terms will be traced.
43  * Example: `MRPT_EXPR_VERBOSE="cos|sin|speed|if (x>0)"`.
44  *
45  * \note (New in MRPT 1.5.0)
46  * \note (`MRPT_EXPR_VERBOSE` new in MRPT 1.5.7)
47  * \ingroup mrpt_base_grp
48  */
50  {
51  public:
52  CRuntimeCompiledExpression(); //!< Default ctor
53 
54  /** Initializes the object by compiling an expression.
55  * \exception std::runtime_error On any syntax error or undefined symbol while compiling the expression. The `e.what()` message describes what is exactly the problem.
56  * \sa register_symbol_table()
57  */
58  void compile(
59  const std::string &expression, //!< [in] The expression to be compiled.
60  const std::map<std::string, double> &variables = std::map<std::string, double>(), //!< [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.
61  const std::string &expr_name_for_error_reporting = std::string() //!< A descriptive name of this formula, to be used when generating error reports via an exception, if needed
62  );
63 
64  /** Can be used **before** calling compile() to register additional variables by means of **pointers** instead of a std::map */
65  void register_symbol_table(
66  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.
67  );
68 
69  /** Evaluates the current value of the precompiled formula.
70  * \exception std::runtime_error If the formula has not been compiled yet.
71  */
72  double eval() const;
73 
74  bool is_compiled() const; //!< Returns true if compile() was called and ended without errors.
75  const std::string & get_original_expression() const; //!< Returns the original formula passed to compile(), or an empty string if still not compiled.
76 
77  exprtk::expression<double> & get_raw_exprtk_expr(); //!< Access raw exprtk expression object.
78  const exprtk::expression<double> & get_raw_exprtk_expr() const; //!< Access raw exprtk expression object.
79 
80  private:
81  PIMPL_DECLARE_TYPE(exprtk::expression<double>, m_compiled_formula);
83 
84  struct ExprVerbose;
85  friend struct ExprVerbose;
86  }; // End of class def.
87 
88  } // End of namespace
89 } // End of namespace
90 
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
#define PIMPL_DECLARE_TYPE(_TYPE, _VAR_NAME)
Definition: pimpl.h:49
GLsizei const GLchar ** string
Definition: glext.h:3919
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define PIMPL_FORWARD_DECLARATION(_TYPE)
Definition: pimpl.h:47



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