19 #define exprtk_disable_string_capabilities // Workaround a bug in Ubuntu 23 #define exprtk_disable_enhanced_features 24 #define exprtk_disable_superscalar_unroll 25 #define exprtk_disable_rtl_vecops 26 #define exprtk_disable_rtl_io_file 27 #include <mrpt/3rdparty/exprtk.hpp> 46 const char* sp = ::getenv(
"MRPT_EXPR_VERBOSE");
47 if (
nullptr == sp)
return;
50 if (s == std::string(
"1"))
76 const std::string& expression,
80 const std::map<std::string, double>& variables,
83 const std::string& expr_name_for_error_reporting)
85 m_impl->m_original_expr_str = expression;
87 exprtk::symbol_table<double> symbol_table;
88 for (
const auto& v : variables)
90 auto& var =
const_cast<double&
>(v.second);
91 symbol_table.add_variable(v.first, var);
93 symbol_table.add_constant(
"M_PI",
M_PI);
94 symbol_table.add_constants();
96 m_impl->m_compiled_formula.register_symbol_table(symbol_table);
99 exprtk::parser<double> parser;
100 if (!parser.compile(expression,
m_impl->m_compiled_formula))
102 "Error compiling expression (name=`%s`): `%s`. Error: `%s`",
103 expr_name_for_error_reporting.c_str(), expression.c_str(),
104 parser.error().c_str());
110 double ret =
m_impl->m_compiled_formula.value();
119 const std::map<std::string, double*>& variables)
121 exprtk::symbol_table<double> symbol_table;
122 for (
const auto& v : variables)
124 auto* var =
const_cast<double*
>(v.second);
125 symbol_table.add_variable(v.first, *var);
127 m_impl->m_compiled_formula.register_symbol_table(symbol_table);
133 return m_impl->m_compiled_formula;
139 return m_impl->m_compiled_formula;
145 return m_impl->m_compiled_formula;
149 return m_impl->m_original_expr_str;
157 const auto& exp = *rce.
m_impl.get();
161 bool matched =
false;
164 if (exp.m_original_expr_str.find(s) != std::string::npos)
170 if (!matched)
return;
173 std::vector<std::pair<std::string, double>> lst;
174 exp.m_compiled_formula.get_symbol_table().get_variable_list(lst);
176 std::cout <<
"[CRuntimeCompiledExpression::eval()] DEBUG:\n" 178 << exp.m_original_expr_str <<
"\n" 179 "* Final value: " << ret <<
"\n" 180 "* Using these symbols:\n";
182 for (
const auto& v : lst)
183 std::cout <<
" * " << v.first <<
" = " << v.second <<
"\n";
const std::string & get_original_expression() const
Returns the original formula passed to compile(), or an empty string if still not compiled...
exprtk::expression< double > & get_raw_exprtk_expr()
Access raw exprtk expression object.
exprtk::expression< double > m_compiled_formula
std::string m_original_expr_str
A wrapper of exprtk runtime expression compiler: it takes a string representing an expression (from a...
bool m_verbose_always_enabled
void tokenize(const std::string &inString, const std::string &inDelimiters, OUT_CONTAINER &outTokens, bool skipBlankTokens=true) noexcept
Tokenizes a string according to a set of delimiting characters.
static CRuntimeCompiledExpression::ExprVerbose & Instance()
#define ASSERT_(f)
Defines an assertion mechanism.
void process(const CRuntimeCompiledExpression &rce, const double ret)
std::vector< std::string > m_verbose_matches
bool is_compiled() const
Returns true if compile() was called and ended without errors.
CRuntimeCompiledExpression()
Default ctor.
void register_symbol_table(const std::map< std::string, double *> &variables)
Can be used before calling compile() to register additional variables by means of pointers instead of...
void compile(const std::string &expression, const std::map< std::string, double > &variables=std::map< std::string, double >(), const std::string &expr_name_for_error_reporting=std::string())
Initializes the object by compiling an expression.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double eval() const
Evaluates the current value of the precompiled formula.
std::string trim(const std::string &str)
Removes leading and trailing spaces.
pimpl< T > make_impl(Args &&... args)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
~CRuntimeCompiledExpression()
mrpt::pimpl< Impl > m_impl