Skip to content

Commit 6775fb3

Browse files
committed
Add ParmParse::query_enum_sloppy that can ignore characters
The characters in the `ignores` argument will be ignored as if they don't exist in the value part of `name = value`.
1 parent 484345c commit 6775fb3

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

Src/Base/AMReX_ParmParse.H

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,69 @@ public:
13651365
}
13661366
}
13671367

1368+
/**
1369+
* \brief. Query enum value using given name.
1370+
*
1371+
* Here T is an enum class defined by AMREX_ENUM. The return value
1372+
* indicates if `name` is found. An exception is thrown, if the found
1373+
* string associated with the name cannot be case-insensitively
1374+
* converted to an enumerator (i.e., the found string, not `name`, does
1375+
* not case-insensitively match any names in the definition of T). If
1376+
* there are multiple matches, the first one is used. Characters in
1377+
* `ignores` will be ignored as if they don't exist in the value part of
1378+
* ParamParse entries (e.g., `name = value`).
1379+
*/
1380+
template <typename T, typename ET = amrex_enum_traits<T>,
1381+
std::enable_if_t<ET::value,int> = 0>
1382+
int query_enum_sloppy (const char* name, T& ref, std::string_view const& ignores,
1383+
int ival = FIRST) const
1384+
{
1385+
std::string s;
1386+
int exist = this->query(name, s, ival);
1387+
if (exist) {
1388+
try {
1389+
s.erase(std::remove_if(s.begin(), s.end(),
1390+
[&] (auto const& c) {
1391+
return ignores.find(c) != std::string_view::npos; }),
1392+
s.end());
1393+
ref = amrex::getEnumCaseInsensitive<T>(s);
1394+
} catch (...) {
1395+
if (amrex::Verbose() > 0) {
1396+
amrex::Print() << "amrex::ParmParse::query_enum_sloppy (input name: "
1397+
<< this->prefixedName(name) << "):\n";
1398+
}
1399+
throw;
1400+
}
1401+
}
1402+
return exist;
1403+
}
1404+
1405+
/**
1406+
* \brief. Get enum value using given name.
1407+
*
1408+
* Here T is an enum class defined by AMREX_ENUM. It's a runtime error,
1409+
* if `name` is not found. An exception is thrown, if the found string
1410+
* associated with the name cannot be case-insensitively converted to an
1411+
* enumerator (i.e., the found string, not `name`, does not
1412+
* case-insensitively match any names in the definition of T). If there
1413+
* are multiple matches, the first one is used. Characters in `ignores`
1414+
* will be ignored as if they don't exist in the value part of
1415+
* ParamParse entries (e.g., `name = value`).
1416+
*/
1417+
template <typename T, typename ET = amrex_enum_traits<T>,
1418+
std::enable_if_t<ET::value,int> = 0>
1419+
void get_enum_sloppy (const char* name, T& ref, std::string_view const& ignores,
1420+
int ival = FIRST) const
1421+
{
1422+
int exist = this->query_enum_sloppy(name, ref, ignores, ival);
1423+
if (!exist) {
1424+
std::string msg("get_enum_sloppy(\"");
1425+
msg.append(name).append("\",").append(amrex::getEnumClassName<T>())
1426+
.append("&) failed.");
1427+
amrex::Abort(msg);
1428+
}
1429+
}
1430+
13681431
//! Remove given name from the table.
13691432
int remove (const char* name);
13701433

Tests/Enum/inputs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ color2 = green
44
color3 = blue
55
color4 = greenxxx
66
color5 = Blue
7+
color6 = G-r-e-e.n
78

89
colors = cyan yellow orange
9-
10-

Tests/Enum/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ int main (int argc, char* argv[])
134134
ParmParse pp;
135135
pp.query_enum_case_insensitive("color5", my_blue);
136136
AMREX_ALWAYS_ASSERT(my_blue == MyColor2::blue);
137+
138+
auto my_green = MyColor2::red;
139+
pp.query_enum_sloppy("color6", my_green, "-.");
140+
AMREX_ALWAYS_ASSERT(my_green == MyColor2::green);
137141
}
138142

139143
amrex::Finalize();

0 commit comments

Comments
 (0)