mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: provide Enum failsafe lookup
- if the enum name is not found, then warn and return the default.
This commit is contained in:
@ -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
|
||||
|
||||
Reference in New Issue
Block a user