ENH: minor improvements for Enum

- found() method for consistency with other classes

- operator()(name, deflt) for similarity to lookupOrDefault,
  but without a dictionary
This commit is contained in:
Mark Olesen
2017-11-28 09:15:04 +01:00
parent b53e5b7404
commit c0e86940b0
10 changed files with 78 additions and 31 deletions

View File

@ -309,7 +309,7 @@ int main(int argc, char *argv[])
if
(
matchName != "perfect"
&& !slidingInterface::typeOfMatchNames.hasEnum(matchName)
&& !slidingInterface::typeOfMatchNames.found(matchName)
)
{
Info<< "Error: unknown match type - " << matchName

View File

@ -229,7 +229,7 @@ int main(int argc, char *argv[])
word patchMapMethod;
if (meshToMesh::interpolationMethodNames_.hasEnum(mapMethod))
if (meshToMesh::interpolationMethodNames_.found(mapMethod))
{
// Lookup corresponding AMI method
meshToMesh::interpolationMethod method =

View File

@ -1577,7 +1577,7 @@ int main(int argc, char *argv[])
{ booleanSurface::DIFFERENCE, "difference" }
};
if (!validActions.hasEnum(action))
if (!validActions.found(action))
{
FatalErrorInFunction
<< "Unsupported action " << action << endl

View File

@ -117,7 +117,7 @@ bool Foam::functionEntries::inputMode::execute
const word modeName(is);
// Behaviour like Enum lookupOrFailsafe()
if (selectableNames.hasEnum(modeName))
if (selectableNames.found(modeName))
{
entry::globalInputMode = selectableNames[modeName];
}

View File

@ -87,10 +87,8 @@ const Foam::word& Foam::Enum<EnumType>::getName(const EnumType e) const
{
return word::null;
}
else
{
return names_[idx];
}
return names_[idx];
}
@ -233,16 +231,14 @@ EnumType Foam::Enum<EnumType>::lookupOrFailsafe
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
if (idx >= 0)
{
return EnumType(values_[idx]);
}
IOWarningInFunction(dict)
<< "bad " << key <<" specifier " << enumName
<< " using " << getName(deflt) << endl;
}
return deflt;

View File

@ -25,10 +25,11 @@ Class
Foam::Enum
Description
A Enum is a wrapper around a list of names/values that represent
particular enumeration values.
Enum is a wrapper around a list of names/values that represent particular
enumeration values.
SourceFiles
EnumI.H
Enum.C
\*---------------------------------------------------------------------------*/
@ -111,7 +112,7 @@ public:
// Member Functions
// Access
// Access
//- The number of lookup names for the enumeration
inline label size() const;
@ -129,20 +130,28 @@ public:
inline const List<int>& values() const;
// Query
// Query
//- Test if there is an enumeration corresponding to the given name.
inline bool found(const word& enumName) const;
//- Test if there is a name corresponding to the given enumeration.
inline bool found(const EnumType e) const;
//- Test if there is an enumeration corresponding to the given name.
// \deprecated Use found() - for compatibility with NamedEnum
inline bool hasEnum(const word& enumName) const;
//- Test if there is a name corresponding to the given enumeration.
// \deprecated Use found() - for compatibility with NamedEnum
inline bool hasName(const EnumType e) const;
// Lookup
// Lookup
//- Lookup the key in the dictionary and return the corresponding
//- enumeration element based on its name.
// Fatal if anything is incorrect.
// FatalError if anything is incorrect.
EnumType lookup
(
const word& key,
@ -152,7 +161,7 @@ public:
//- 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.
// Fatal if the enumerated name was incorrect.
// FatalError if the enumerated name was incorrect.
EnumType lookupOrDefault
(
const word& key,
@ -173,7 +182,7 @@ public:
) const;
// IO
// IO
//- Read a word from Istream and return the corresponding enumeration
EnumType read(Istream& is) const;
@ -193,13 +202,23 @@ public:
// Member Operators
//- Return the enumeration corresponding to the given name
// Fatal if the name cannot be found.
inline const EnumType operator[](const word& name) const;
// FatalError if the name is not found.
// Identical to getEnum()
inline EnumType operator[](const word& enumName) const;
//- Return the first name corresponding to the given enumeration.
// Returns an empty word on failure.
// Identical to getName()
inline const word& operator[](const EnumType e) const;
//- Return the enumeration corresponding to the given name or deflt
//- if the name is not found.
inline EnumType operator()
(
const word& enumName,
const EnumType deflt
) const;
// IOstream operators

View File

@ -53,6 +53,20 @@ inline const Foam::List<int>& Foam::Enum<EnumType>::values() const
}
template<class EnumType>
inline bool Foam::Enum<EnumType>::found(const word& enumName) const
{
return getIndex(enumName) >= 0;
}
template<class EnumType>
inline bool Foam::Enum<EnumType>::found(const EnumType e) const
{
return getIndex(e) >= 0;
}
template<class EnumType>
inline bool Foam::Enum<EnumType>::hasEnum(const word& enumName) const
{
@ -81,12 +95,12 @@ inline Foam::Ostream& Foam::Enum<EnumType>::writeList
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class EnumType>
inline const EnumType Foam::Enum<EnumType>::operator[]
inline EnumType Foam::Enum<EnumType>::operator[]
(
const word& name
const word& enumName
) const
{
return getEnum(name);
return getEnum(enumName);
}
@ -100,4 +114,22 @@ inline const Foam::word& Foam::Enum<EnumType>::operator[]
}
template<class EnumType>
inline EnumType Foam::Enum<EnumType>::operator()
(
const word& enumName,
const EnumType deflt
) const
{
const label idx = getIndex(enumName);
if (idx >= 0)
{
return EnumType(values_[idx]);
}
return deflt;
}
// ************************************************************************* //

View File

@ -377,7 +377,7 @@ bool Foam::fileFormats::FIREMeshReader::readGeometry(const scalar scaleFactor)
IOstream::streamFormat fmt = IOstream::ASCII;
const word ext = geometryFile_.ext();
bool supported = FIRECore::file3dExtensions.hasEnum(ext);
bool supported = FIRECore::file3dExtensions.found(ext);
if (supported)
{
FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];

View File

@ -278,7 +278,7 @@ bool Foam::fileFormats::FIREMeshWriter::write(const fileName& meshName) const
{
const word ext = baseName.ext();
if (FIRECore::file3dExtensions.hasEnum(ext))
if (FIRECore::file3dExtensions.found(ext))
{
FIRECore::fileExt3d fireFileType = FIRECore::file3dExtensions[ext];
if (fireFileType == FIRECore::fileExt3d::POLY_ASCII)

View File

@ -72,7 +72,7 @@ void Foam::functionObjects::mapFields::createInterpolation
);
const fvMesh& mapRegion = mapRegionPtr_();
const word mapMethodName(dict.lookup("mapMethod"));
if (!meshToMesh::interpolationMethodNames_.hasEnum(mapMethodName))
if (!meshToMesh::interpolationMethodNames_.found(mapMethodName))
{
FatalErrorInFunction
<< type() << " " << name() << ": unknown map method "