diff --git a/src/OpenFOAM/primitives/enums/Enum.C b/src/OpenFOAM/primitives/enums/Enum.C index 8b4f2100f0d8a03d772619074e8d863d5434c70a..76f1323b8a3c42e82613185f455d6be8b628a740 100644 --- a/src/OpenFOAM/primitives/enums/Enum.C +++ b/src/OpenFOAM/primitives/enums/Enum.C @@ -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; } diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index dde620fb26a3e3ab7126325eb2884d32429628d2..b57fc9891ac1c73fe6a883f04fd404e0480e4acf 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -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