Skip to content
Snippets Groups Projects
Commit 45dabbfe authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: provide Enum failsafe lookup

- if the enum name is not found, then warn and return the default.
parent 84e47f2d
Branches
Tags
No related merge requests found
......@@ -215,10 +215,37 @@ EnumType Foam::Enum<EnumType>::lookupOrDefault
{
return lookup(key, dict);
}
else
return deflt;
}
template<class EnumType>
EnumType Foam::Enum<EnumType>::lookupOrFailsafe
(
const word& key,
const dictionary& dict,
const EnumType deflt
) const
{
if (dict.found(key))
{
return deflt;
const word enumName(dict.lookup(key));
const label idx = getIndex(enumName);
if (idx < 0)
{
IOWarningInFunction(dict)
<< "bad " << key <<" specifier " << enumName
<< " using " << getName(deflt) << endl;
}
else
{
return EnumType(values_[idx]);
}
}
return deflt;
}
......
......@@ -161,6 +161,18 @@ public:
) const;
//- Find the key in the dictionary and return the corresponding
// enumeration element based on its name.
// Return the default value if the key was not found in the dictionary
// or if the enumerated name was incorrect (emit warning)
EnumType lookupOrFailsafe
(
const word& key,
const dictionary& dict,
const EnumType deflt
) const;
// IO
//- Read a word from Istream and return the corresponding enumeration
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment