Skip to content

Commit

Permalink
Capability adds for ParmParse enum (AMReX-Codes#4119)
Browse files Browse the repository at this point in the history
## Summary

- The ParmParse functions for AMReX Enums added in AMReX-Codes#4069 did not carry
the optional arguments found in other ParmParse functions. Those are
added so that, for example, querying an enum has the same interface as
querying other types.
- These functions shouldn't modify the ParmParse object, so they are
made `const`
- The `prefixedName` function is made a public member instead of a
protected member.

## Additional background

The use case for these changes is
erf-model/ERF#1772
  • Loading branch information
baperry2 authored Sep 4, 2024
1 parent 61b91f9 commit c88b2b5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
62 changes: 46 additions & 16 deletions Src/Base/AMReX_ParmParse.H
Original file line number Diff line number Diff line change
Expand Up @@ -1192,14 +1192,18 @@ public:
*/
template <typename T, typename ET = amrex_enum_traits<T>,
std::enable_if_t<ET::value,int> = 0>
int query (const char* name, T& ref)
int query (const char* name, T& ref, int ival = FIRST) const
{
std::string s;
int exist = this->query(name, s);
int exist = this->query(name, s, ival);
if (exist) {
try {
ref = amrex::getEnum<T>(s);
} catch (...) {
if (amrex::Verbose() > 0 ) {
amrex::Print() << "amrex::ParmParse::query (input name: "
<< this->prefixedName(name) << "):\n";
}
throw;
}
}
Expand All @@ -1216,28 +1220,43 @@ public:
*/
template <typename T, typename ET = amrex_enum_traits<T>,
std::enable_if_t<ET::value,int> = 0>
void get (const char* name, T& ref)
void get (const char* name, T& ref, int ival = FIRST) const
{
std::string s;
this->get(name, s);
this->get(name, s, ival);
try {
ref = amrex::getEnum<T>(s);
} catch (...) {
if (amrex::Verbose() > 0 ) {
amrex::Print() << "amrex::ParmParse::get (input name: "
<< this->prefixedName(name) << "):\n";
}
throw;
}
}

//! Query an array of enum values using given name.
template <typename T, typename ET = amrex_enum_traits<T>,
std::enable_if_t<ET::value,int> = 0>
int queryarr (const char* name, std::vector<T>& ref)
int queryarr (const char* name,
std::vector<T>& ref,
int start_ix = FIRST,
int num_val = ALL) const
{
std::vector<std::string> s;
int exist = this->queryarr(name, s);
int exist = this->queryarr(name, s, start_ix, num_val);
if (exist) {
ref.resize(s.size());
for (std::size_t i = 0; i < s.size(); ++i) {
ref[i] = amrex::getEnum<T>(s[i]);
try {
ref[i] = amrex::getEnum<T>(s[i]);
} catch (...) {
if (amrex::Verbose() > 0 ) {
amrex::Print() << "amrex::ParmParse::queryarr (input name: "
<< this->prefixedName(name) << "):\n";
}
throw;
}
}
}
return exist;
Expand All @@ -1246,13 +1265,24 @@ public:
//! Get an array of enum values using given name.
template <typename T, typename ET = amrex_enum_traits<T>,
std::enable_if_t<ET::value,int> = 0>
void getarr (const char* name, std::vector<T>& ref)
void getarr (const char* name,
std::vector<T>& ref,
int start_ix = FIRST,
int num_val = ALL) const
{
std::vector<std::string> s;
this->getarr(name, s);
this->getarr(name, s, start_ix, num_val);
ref.resize(s.size());
for (std::size_t i = 0; i < s.size(); ++i) {
ref[i] = amrex::getEnum<T>(s[i]);
try {
ref[i] = amrex::getEnum<T>(s[i]);
} catch (...) {
if (amrex::Verbose() > 0 ) {
amrex::Print() << "amrex::ParmParse::getarr (input name: "
<< this->prefixedName(name) << "):\n";
}
throw;
}
}
}

Expand All @@ -1268,10 +1298,10 @@ public:
*/
template <typename T, typename ET = amrex_enum_traits<T>,
std::enable_if_t<ET::value,int> = 0>
int query_enum_case_insensitive (const char* name, T& ref)
int query_enum_case_insensitive (const char* name, T& ref, int ival = FIRST) const
{
std::string s;
int exist = this->query(name, s);
int exist = this->query(name, s, ival);
if (exist) {
s = amrex::toLower(s);
auto const& enum_names = amrex::getEnumNameStrings<T>();
Expand Down Expand Up @@ -1303,9 +1333,9 @@ public:
*/
template <typename T, typename ET = amrex_enum_traits<T>,
std::enable_if_t<ET::value,int> = 0>
void get_enum_case_insensitive (const char* name, T& ref)
void get_enum_case_insensitive (const char* name, T& ref, int ival = FIRST) const
{
int exist = this->query_enum_case_insensitive(name, ref);
int exist = this->query_enum_case_insensitive(name, ref, ival);
if (!exist) {
std::string msg("get_enum_case_insensitive(\"");
msg.append(name).append("\",").append(amrex::getEnumClassName<T>())
Expand Down Expand Up @@ -1387,10 +1417,10 @@ public:

static std::string ParserPrefix;

protected:

[[nodiscard]] std::string prefixedName (const std::string_view& str) const;

protected:

std::string m_prefix; // Prefix used in keyword search
std::string m_parser_prefix; // Prefix used by Parser
Table* m_table;
Expand Down
34 changes: 17 additions & 17 deletions Src/Base/AMReX_ParmParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ ParmParse::query (const char* name,
}

void
ParmParse::add (const char* name,
ParmParse::add (const char* name, // NOLINT(readability-make-member-function-const)
const bool val)
{
saddval(prefixedName(name),val);
Expand Down Expand Up @@ -1315,7 +1315,7 @@ ParmParse::query (const char* name, int& ref, int ival) const
}

void
ParmParse::add (const char* name, const int val)
ParmParse::add (const char* name, const int val) // NOLINT(readability-make-member-function-const)
{
saddval(prefixedName(name),val);
}
Expand Down Expand Up @@ -1349,7 +1349,7 @@ ParmParse::queryarr (const char* name, std::vector<int>& ref, int start_ix,
}

void
ParmParse::addarr (const char* name, const std::vector<int>& ref)
ParmParse::addarr (const char* name, const std::vector<int>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down Expand Up @@ -1381,7 +1381,7 @@ ParmParse::query (const char* name, long& ref, int ival) const
}

void
ParmParse::add (const char* name,
ParmParse::add (const char* name, // NOLINT(readability-make-member-function-const)
const long val)
{
saddval(prefixedName(name),val);
Expand Down Expand Up @@ -1416,7 +1416,7 @@ ParmParse::queryarr (const char* name, std::vector<long>& ref, int start_ix,
}

void
ParmParse::addarr (const char* name, const std::vector<long>& ref)
ParmParse::addarr (const char* name, const std::vector<long>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down Expand Up @@ -1447,7 +1447,7 @@ ParmParse::query (const char* name, long long& ref, int ival) const
}

void
ParmParse::add (const char* name, const long long val)
ParmParse::add (const char* name, const long long val) // NOLINT(readability-make-member-function-const)
{
saddval(prefixedName(name),val);
}
Expand Down Expand Up @@ -1481,7 +1481,7 @@ ParmParse::queryarr (const char* name, std::vector<long long>& ref, int start_ix
}

void
ParmParse::addarr (const char* name, const std::vector<long long>& ref)
ParmParse::addarr (const char* name, const std::vector<long long>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down Expand Up @@ -1512,7 +1512,7 @@ ParmParse::query (const char* name, float& ref, int ival) const
}

void
ParmParse::add (const char* name, const float val)
ParmParse::add (const char* name, const float val) // NOLINT(readability-make-member-function-const)
{
saddval(prefixedName(name),val);
}
Expand Down Expand Up @@ -1546,7 +1546,7 @@ ParmParse::queryarr (const char* name, std::vector<float>& ref, int start_ix,
}

void
ParmParse::addarr (const char* name, const std::vector<float>& ref)
ParmParse::addarr (const char* name, const std::vector<float>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down Expand Up @@ -1579,7 +1579,7 @@ ParmParse::query (const char* name, double& ref, int ival) const
}

void
ParmParse::add (const char* name, const double val)
ParmParse::add (const char* name, const double val) // NOLINT(readability-make-member-function-const)
{
saddval(prefixedName(name),val);
}
Expand Down Expand Up @@ -1613,7 +1613,7 @@ ParmParse::queryarr (const char* name, std::vector<double>& ref, int start_ix,
}

void
ParmParse::addarr (const char* name, const std::vector<double>& ref)
ParmParse::addarr (const char* name, const std::vector<double>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down Expand Up @@ -1646,7 +1646,7 @@ ParmParse::query (const char* name, std::string& ref, int ival) const
}

void
ParmParse::add (const char* name, const std::string& val)
ParmParse::add (const char* name, const std::string& val) // NOLINT(readability-make-member-function-const)
{
saddval(prefixedName(name),val);
}
Expand Down Expand Up @@ -1680,7 +1680,7 @@ ParmParse::queryarr (const char* name, std::vector<std::string>& ref,
}

void
ParmParse::addarr (const char* name, const std::vector<std::string>& ref)
ParmParse::addarr (const char* name, const std::vector<std::string>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down Expand Up @@ -1713,7 +1713,7 @@ ParmParse::query (const char* name, IntVect& ref, int ival) const
}

void
ParmParse::add (const char* name, const IntVect& val)
ParmParse::add (const char* name, const IntVect& val) // NOLINT(readability-make-member-function-const)
{
saddval(prefixedName(name),val);
}
Expand Down Expand Up @@ -1747,7 +1747,7 @@ ParmParse::queryarr (const char* name, std::vector<IntVect>& ref,
}

void
ParmParse::addarr (const char* name, const std::vector<IntVect>& ref)
ParmParse::addarr (const char* name, const std::vector<IntVect>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down Expand Up @@ -1778,7 +1778,7 @@ ParmParse::query (const char* name, Box& ref, int ival) const
}

void
ParmParse::add (const char* name, const Box& val)
ParmParse::add (const char* name, const Box& val) // NOLINT(readability-make-member-function-const)
{
saddval(prefixedName(name),val);
}
Expand Down Expand Up @@ -1812,7 +1812,7 @@ ParmParse::queryarr (const char* name, std::vector<Box>& ref,
}

void
ParmParse::addarr (const char* name, const std::vector<Box>& ref)
ParmParse::addarr (const char* name, const std::vector<Box>& ref) // NOLINT(readability-make-member-function-const)
{
saddarr(prefixedName(name),ref);
}
Expand Down

0 comments on commit c88b2b5

Please sign in to comment.