Skip to content

Commit f547cb5

Browse files
authored
Add ParmParse::eval (#4142)
This function can be used to evaluate a math expression given as a string. For unknown symbols, ParmParse will be used to find the values.
1 parent c6b4fde commit f547cb5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Src/Base/AMReX_ParmParse.H

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,29 @@ public:
11531153
}
11541154
}
11551155

1156+
/*
1157+
* \brief Evaluate given string as math expression
1158+
*
1159+
* For unknown symbols, ParmParse database will be queried.
1160+
*/
1161+
template <typename T, std::enable_if_t<std::is_same_v<T,int> ||
1162+
std::is_same_v<T,long> ||
1163+
std::is_same_v<T,long long> ||
1164+
std::is_same_v<T,float> ||
1165+
std::is_same_v<T,double>,int> = 0>
1166+
T eval (std::string const& expr) const
1167+
{
1168+
if constexpr (std::is_integral_v<T>) {
1169+
auto const parser = this->makeIParser(expr, {});
1170+
auto const exe = parser.compileHost<0>();
1171+
return static_cast<T>(exe()); // In the future, we might add safety check.
1172+
} else {
1173+
auto const parser = this->makeParser(expr, {});
1174+
auto const exe = parser.compileHost<0>();
1175+
return static_cast<T>(exe());
1176+
}
1177+
}
1178+
11561179
/*
11571180
* \brief Query two names.
11581181
*

0 commit comments

Comments
 (0)