Main MRPT website > C++ reference for MRPT 1.5.6
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  * \note (New in MRPT 1.5.0)
38  * \ingroup mrpt_base_grp
39  */
41  {
42  public:
43  CRuntimeCompiledExpression(); //!< Default ctor
44 
45  /** Initializes the object by compiling an expression.
46  * \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.
47  * \sa register_symbol_table()
48  */
49  void compile(
50  const std::string &expression, //!< [in] The expression to be compiled.
51  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.
52  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
53  );
54 
55  /** Can be used **before** calling compile() to register additional variables by means of **pointers** instead of a std::map */
56  void register_symbol_table(
57  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.
58  );
59 
60  /** Evaluates the current value of the precompiled formula.
61  * \exception std::runtime_error If the formula has not been compiled yet.
62  */
63  double eval() const;
64 
65  bool is_compiled() const; //!< Returns true if compile() was called and ended without errors.
66  const std::string & get_original_expression() const; //!< Returns the original formula passed to compile(), or an empty string if still not compiled.
67 
68  exprtk::expression<double> & get_raw_exprtk_expr(); //!< Access raw exprtk expression object.
69  const exprtk::expression<double> & get_raw_exprtk_expr() const; //!< Access raw exprtk expression object.
70 
71  private:
72  PIMPL_DECLARE_TYPE(exprtk::expression<double>, m_compiled_formula);
74 
75  }; // End of class def.
76 
77  } // End of namespace
78 } // End of namespace
79 
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 GLcharARB ** string
Definition: glew.h:3293
#define PIMPL_FORWARD_DECLARATION(_TYPE)
Definition: pimpl.h:47



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018