Skip to content

Commit 467f217

Browse files
committed
Support using system library libexprtk-dev
1 parent 60a3f21 commit 467f217

File tree

4 files changed

+79
-16
lines changed

4 files changed

+79
-16
lines changed

.vscode/settings.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@
4040
"**/log": true
4141
},
4242
"cSpell.words": [
43-
"CArchive",
44-
"Colorf",
45-
"Coordf",
4643
"Eigen",
47-
"idft",
48-
"IFFT",
49-
"Kbhit",
5044
"mrpt",
5145
"MRPT",
5246
"SSSE",
53-
"stbi",
5447
"undistort",
5548
"undistortion"
5649
],
5750
"ros.distro": "humble",
51+
"cSpell.ignoreWords": [
52+
"CArchive",
53+
"Colorf",
54+
"Coordf",
55+
"exprtk",
56+
"idft",
57+
"IFFT",
58+
"Kbhit",
59+
"stbi"
60+
],
5861
}

modules/mrpt_expr/CMakeLists.txt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,33 @@ set(LIB_SRCS
2020
src/CRuntimeCompiledExpression_unittest.cpp
2121
)
2222

23+
# Use system exprtk library?
24+
# Find the /usr/include/exprtk.hpp file:
25+
find_path(
26+
EXPRTK_INCLUDE_DIR
27+
NAMES exprtk.hpp
28+
PATHS /usr/include /usr/local/include
29+
NO_DEFAULT_PATH
30+
)
31+
if(EXPRTK_INCLUDE_DIR)
32+
message(STATUS "Using system exprtk.hpp from: ${EXPRTK_INCLUDE_DIR}")
33+
set(MRPT_USE_SYSTEM_EXPRTK ON)
34+
else()
35+
message(STATUS "System exprtk.hpp not found, using bundled version.")
36+
set(MRPT_USE_SYSTEM_EXPRTK OFF)
37+
endif()
38+
2339
set(LIB_PUBLIC_HDRS
24-
include/mrpt/3rdparty/exprtk.hpp
2540
include/mrpt/expr/mrpt-expr_export.h
2641
include/mrpt/expr/CRuntimeCompiledExpression.h
2742
)
2843

44+
if (NOT MRPT_USE_SYSTEM_EXPRTK)
45+
list(APPEND LIB_PUBLIC_HDRS
46+
include/mrpt/expr/exprtk.hpp
47+
)
48+
endif()
49+
2950
mrpt_add_library(
3051
TARGET ${PROJECT_NAME}
3152
SOURCES ${LIB_SRCS} ${LIB_PUBLIC_HDRS}
@@ -36,6 +57,12 @@ mrpt_add_library(
3657
mrpt_system
3758
)
3859

60+
if (MRPT_USE_SYSTEM_EXPRTK)
61+
target_compile_definitions(${PROJECT_NAME} PRIVATE MRPT_USE_SYSTEM_EXPRTK)
62+
# Add include path as system path to avoid warnings from exprtk.hpp:
63+
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${EXPRTK_INCLUDE_DIR})
64+
endif()
65+
3966
# Don't export ALL symbols for the huge exprtk lib
4067
set_target_properties(${PROJECT_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS 0)
4168
# Minimize debug info for this module:

modules/mrpt_expr/include/mrpt/expr/CRuntimeCompiledExpression.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class MRPT_EXPR_EXPORT CRuntimeCompiledExpression
7070
public:
7171
/** Default ctor */
7272
CRuntimeCompiledExpression();
73-
~CRuntimeCompiledExpression();
7473

7574
/** Initializes the object by compiling an expression.
7675
* \exception std::runtime_error On any syntax error or undefined symbol

modules/mrpt_expr/src/CRuntimeCompiledExpression.cpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
#define exprtk_disable_superscalar_unroll
2929
#define exprtk_disable_rtl_vecops
3030
#define exprtk_disable_rtl_io_file
31+
32+
#if defined(MRPT_USE_SYSTEM_EXPRTK)
33+
#include <exprtk.hpp>
34+
#else
3135
#include <mrpt/3rdparty/exprtk.hpp>
36+
#endif
3237

3338
using namespace mrpt;
3439
using namespace mrpt::expr;
@@ -48,7 +53,10 @@ struct CRuntimeCompiledExpression::ExprVerbose
4853
ExprVerbose()
4954
{
5055
const char* sp = ::getenv("MRPT_EXPR_VERBOSE");
51-
if (nullptr == sp) return;
56+
if (nullptr == sp)
57+
{
58+
return;
59+
}
5260
const std::string s = mrpt::system::trim(std::string(sp));
5361

5462
if (s == std::string("1"))
@@ -74,8 +82,6 @@ CRuntimeCompiledExpression::CRuntimeCompiledExpression() :
7482
{
7583
}
7684

77-
CRuntimeCompiledExpression::~CRuntimeCompiledExpression() = default;
78-
7985
void CRuntimeCompiledExpression::compile(
8086
const std::string& expression,
8187
const std::map<std::string, double>& variables,
@@ -95,28 +101,47 @@ void CRuntimeCompiledExpression::compile(
95101

96102
// Convert from std::function<> to raw functors:
97103
for (const auto& kv : m_funcs_0)
98-
if (auto ptr = kv.second.target<double (*)()>(); ptr) symbol_table.add_function(kv.first, *ptr);
104+
{
105+
if (auto ptr = kv.second.target<double (*)()>(); ptr)
106+
{
107+
symbol_table.add_function(kv.first, *ptr);
108+
}
109+
}
99110

100111
for (const auto& kv : m_funcs_1)
112+
{
101113
if (auto ptr = kv.second.target<double (*)(double)>(); ptr)
114+
{
102115
symbol_table.add_function(kv.first, *ptr);
116+
}
117+
}
103118

104119
for (const auto& kv : m_funcs_2)
120+
{
105121
if (auto ptr = kv.second.target<double (*)(double, double)>(); ptr)
122+
{
106123
symbol_table.add_function(kv.first, *ptr);
124+
}
125+
}
107126

108127
for (const auto& kv : m_funcs_3)
128+
{
109129
if (auto ptr = kv.second.target<double (*)(double, double, double)>(); ptr)
130+
{
110131
symbol_table.add_function(kv.first, *ptr);
132+
}
133+
}
111134

112135
m_impl->m_compiled_formula.register_symbol_table(symbol_table);
113136

114137
// Compile user-given expressions:
115138
exprtk::parser<double> parser;
116139
if (!parser.compile(expression, m_impl->m_compiled_formula))
140+
{
117141
THROW_EXCEPTION_FMT(
118142
"Error compiling expression (name=`%s`): `%s`. Error: `%s`",
119143
expr_name_for_error_reporting.c_str(), expression.c_str(), parser.error().c_str());
144+
}
120145

121146
m_impl->m_compiled = true;
122147
}
@@ -168,7 +193,10 @@ const std::string& CRuntimeCompiledExpression::get_original_expression() const
168193
void CRuntimeCompiledExpression::ExprVerbose::process(
169194
const CRuntimeCompiledExpression& rce, const double ret)
170195
{
171-
if (!m_verbose_always_enabled && m_verbose_matches.empty()) return;
196+
if (!m_verbose_always_enabled && m_verbose_matches.empty())
197+
{
198+
return;
199+
}
172200

173201
const auto& exp = *rce.m_impl.get();
174202

@@ -183,7 +211,10 @@ void CRuntimeCompiledExpression::ExprVerbose::process(
183211
break;
184212
}
185213
}
186-
if (!matched) return;
214+
if (!matched)
215+
{
216+
return;
217+
}
187218
}
188219

189220
std::vector<std::pair<std::string, double>> lst;
@@ -195,5 +226,8 @@ void CRuntimeCompiledExpression::ExprVerbose::process(
195226
"* Final value: " << ret << "\n"
196227
"* Using these symbols:\n";
197228
// clang-format on
198-
for (const auto& v : lst) std::cout << " * " << v.first << " = " << v.second << "\n";
229+
for (const auto& v : lst)
230+
{
231+
std::cout << " * " << v.first << " = " << v.second << "\n";
232+
}
199233
}

0 commit comments

Comments
 (0)