Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ParmParse::eval #4142

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading