Skip to content
Snippets Groups Projects
Commit ca5e7a52 authored by Mark OLESEN's avatar Mark OLESEN Committed by Andrew Heather
Browse files

ENH: properly trap incorrect enumerations (#1372)

- the Enum::readEntry() method was previously as bit sloppy with
  respect to the enumeration that it accepted. If the input was
  non-mandatory, typos would go unnoticed. Now tighten things so that
  if an enumeration is found, it must also be valid.

STYLE: remove unused/deprecated Enum::lookupOrFailsafe() method

- this was only used in a few places internally in 1712 and 1806 but
  has since then been superseded by getOrDefault() with an optional
  'failsafe' flag.
parent 1fb9e68d
Branches
Tags
No related merge requests found
...@@ -164,7 +164,7 @@ EnumType Foam::Enum<EnumType>::getOrDefault ...@@ -164,7 +164,7 @@ EnumType Foam::Enum<EnumType>::getOrDefault
IOWarningInFunction(dict) IOWarningInFunction(dict)
<< enumName << " is not in enumeration: " << *this << nl << enumName << " is not in enumeration: " << *this << nl
<< "using failsafe " << get(deflt) << "using failsafe " << get(deflt)
<< " (value " << int(deflt) << ")" << endl; << " (value " << int(deflt) << ')' << endl;
} }
else else
{ {
...@@ -184,7 +184,7 @@ bool Foam::Enum<EnumType>::readEntry ...@@ -184,7 +184,7 @@ bool Foam::Enum<EnumType>::readEntry
const word& key, const word& key,
const dictionary& dict, const dictionary& dict,
EnumType& val, EnumType& val,
bool mandatory const bool mandatory
) const ) const
{ {
const entry* eptr = dict.findEntry(key, keyType::LITERAL); const entry* eptr = dict.findEntry(key, keyType::LITERAL);
...@@ -198,16 +198,12 @@ bool Foam::Enum<EnumType>::readEntry ...@@ -198,16 +198,12 @@ bool Foam::Enum<EnumType>::readEntry
if (idx >= 0) if (idx >= 0)
{ {
val = EnumType(vals_[idx]); val = EnumType(vals_[idx]);
return true; return true;
} }
if (mandatory) FatalIOErrorInFunction(dict)
{ << enumName << " is not in enumeration: " << *this << nl
FatalIOErrorInFunction(dict) << exit(FatalIOError);
<< enumName << " is not in enumeration: " << *this << nl
<< exit(FatalIOError);
}
} }
else if (mandatory) else if (mandatory)
{ {
......
...@@ -26,7 +26,7 @@ Class ...@@ -26,7 +26,7 @@ Class
Description Description
Enum is a wrapper around a list of names/values that represent particular Enum is a wrapper around a list of names/values that represent particular
enumeration values. enumeration values. All dictionary searches use a literal (not regex).
SourceFiles SourceFiles
Enum.C Enum.C
...@@ -143,7 +143,7 @@ public: ...@@ -143,7 +143,7 @@ public:
//- Get the key in the dictionary and return the corresponding //- Get the key in the dictionary and return the corresponding
//- enumeration element based on its name. //- enumeration element based on its name.
// FatalError if anything is incorrect. // FatalIOError if anything is incorrect.
EnumType get EnumType get
( (
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
...@@ -154,7 +154,8 @@ public: ...@@ -154,7 +154,8 @@ public:
//- enumeration element based on its name. //- enumeration element based on its name.
// //
// \return The value found or default if not found in dictionary. // \return The value found or default if not found in dictionary.
// FatalError (or Warning) if the enumerated name was incorrect. // FatalIOError if the enumeration is incorrect.
// Specifying failsafe downgrades the FatalIOError to an IOWarning.
EnumType getOrDefault EnumType getOrDefault
( (
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
...@@ -164,8 +165,8 @@ public: ...@@ -164,8 +165,8 @@ public:
) const; ) const;
//- Find entry and assign to T val. //- Find entry and assign to T val.
//- FatalIOError if it is found and the number of tokens is incorrect, // FatalIOError if the enumeration is incorrect,
//- or it is mandatory and not found. // or when it is mandatory but was not found.
// //
// \return true if the entry was found. // \return true if the entry was found.
bool readEntry bool readEntry
...@@ -173,11 +174,11 @@ public: ...@@ -173,11 +174,11 @@ public:
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
const dictionary& dict, //!< dictionary const dictionary& dict, //!< dictionary
EnumType& val, //!< the value to read into EnumType& val, //!< the value to read into
bool mandatory = true //!< the keyword is mandatory const bool mandatory = true //!< the keyword is mandatory
) const; ) const;
//- Find an entry if present, and assign to T val. //- Find an entry if present, and assign to T val.
//- FatalIOError if it is found and the number of tokens is incorrect. // FatalIOError if the enumeration is incorrect.
// Default search: non-recursive with patterns. // Default search: non-recursive with patterns.
// //
// \return true if the entry was found. // \return true if the entry was found.
...@@ -230,7 +231,7 @@ public: ...@@ -230,7 +231,7 @@ public:
//- enumeration element based on its name. //- enumeration element based on its name.
// //
// \return The value found or default if not found in dictionary. // \return The value found or default if not found in dictionary.
// FatalError (or Warning) if the enumerated name was incorrect. // FatalError (or Warning) if the enumeration was incorrect.
EnumType lookupOrDefault EnumType lookupOrDefault
( (
const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const word& key, //!< Lookup key. Uses LITERAL (not REGEX)
...@@ -249,19 +250,6 @@ public: ...@@ -249,19 +250,6 @@ public:
{ {
return get(key, dict); return get(key, dict);
} }
//- Deprecated(2018-10) lookupOrDefault with warnings instead of error.
// \deprecated(2018-10) - use getOrDefault() with failsafe option
EnumType FOAM_DEPRECATED_FOR(2018-10, "getOrDefault() method")
lookupOrFailsafe
(
const word& key,
const dictionary& dict,
const EnumType deflt
) const
{
return getOrDefault(key, dict, deflt, true);
}
}; };
......
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