Skip to content

Commit

Permalink
Add ParmParse::eval (#4142)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
WeiqunZhang committed Sep 9, 2024
1 parent c6b4fde commit f547cb5
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Src/Base/AMReX_ParmParse.H
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,29 @@ public:
}
}

/*
* \brief Evaluate given string as math expression
*
* For unknown symbols, ParmParse database will be queried.
*/
template <typename T, std::enable_if_t<std::is_same_v<T,int> ||
std::is_same_v<T,long> ||
std::is_same_v<T,long long> ||
std::is_same_v<T,float> ||
std::is_same_v<T,double>,int> = 0>
T eval (std::string const& expr) const
{
if constexpr (std::is_integral_v<T>) {
auto const parser = this->makeIParser(expr, {});
auto const exe = parser.compileHost<0>();
return static_cast<T>(exe()); // In the future, we might add safety check.
} else {
auto const parser = this->makeParser(expr, {});
auto const exe = parser.compileHost<0>();
return static_cast<T>(exe());
}
}

/*
* \brief Query two names.
*
Expand Down

0 comments on commit f547cb5

Please sign in to comment.