From c0e86940b0369d0b5981863be26bead8d0f085b9 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@esi-group.com> Date: Tue, 28 Nov 2017 09:15:04 +0100 Subject: [PATCH] ENH: minor improvements for Enum - found() method for consistency with other classes - operator()(name, deflt) for similarity to lookupOrDefault, but without a dictionary --- .../mesh/manipulation/stitchMesh/stitchMesh.C | 2 +- .../preProcessing/mapFieldsPar/mapFieldsPar.C | 2 +- .../surfaceBooleanFeatures.C | 2 +- .../functionEntries/inputMode/inputMode.C | 2 +- src/OpenFOAM/primitives/enums/Enum.C | 18 ++++----- src/OpenFOAM/primitives/enums/Enum.H | 39 ++++++++++++++----- src/OpenFOAM/primitives/enums/EnumI.H | 38 ++++++++++++++++-- src/conversion/fire/FIREMeshReader.C | 2 +- src/conversion/fire/FIREMeshWriter.C | 2 +- .../field/mapFields/mapFields.C | 2 +- 10 files changed, 78 insertions(+), 31 deletions(-) diff --git a/applications/utilities/mesh/manipulation/stitchMesh/stitchMesh.C b/applications/utilities/mesh/manipulation/stitchMesh/stitchMesh.C index ced51d723e4..453807a36ed 100644 --- a/applications/utilities/mesh/manipulation/stitchMesh/stitchMesh.C +++ b/applications/utilities/mesh/manipulation/stitchMesh/stitchMesh.C @@ -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 diff --git a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C index 29bcd4bf9a7..fc1173a90ae 100644 --- a/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C +++ b/applications/utilities/preProcessing/mapFieldsPar/mapFieldsPar.C @@ -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 = diff --git a/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C b/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C index 4eb2917f420..4b2221536ed 100644 --- a/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C +++ b/applications/utilities/surface/surfaceBooleanFeatures/surfaceBooleanFeatures.C @@ -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 diff --git a/src/OpenFOAM/db/dictionary/functionEntries/inputMode/inputMode.C b/src/OpenFOAM/db/dictionary/functionEntries/inputMode/inputMode.C index 160d5ec87d8..960e10e86dc 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/inputMode/inputMode.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/inputMode/inputMode.C @@ -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]; } diff --git a/src/OpenFOAM/primitives/enums/Enum.C b/src/OpenFOAM/primitives/enums/Enum.C index 76f1323b8a3..671af7fe2dc 100644 --- a/src/OpenFOAM/primitives/enums/Enum.C +++ b/src/OpenFOAM/primitives/enums/Enum.C @@ -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; diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index 85b96f314f5..3329aa12842 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -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 diff --git a/src/OpenFOAM/primitives/enums/EnumI.H b/src/OpenFOAM/primitives/enums/EnumI.H index a991fd53d35..d6e547c4bf1 100644 --- a/src/OpenFOAM/primitives/enums/EnumI.H +++ b/src/OpenFOAM/primitives/enums/EnumI.H @@ -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; +} + + // ************************************************************************* // diff --git a/src/conversion/fire/FIREMeshReader.C b/src/conversion/fire/FIREMeshReader.C index 239e6a5a87b..0634417e663 100644 --- a/src/conversion/fire/FIREMeshReader.C +++ b/src/conversion/fire/FIREMeshReader.C @@ -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]; diff --git a/src/conversion/fire/FIREMeshWriter.C b/src/conversion/fire/FIREMeshWriter.C index 2fc9de6e1f3..048dc2afd86 100644 --- a/src/conversion/fire/FIREMeshWriter.C +++ b/src/conversion/fire/FIREMeshWriter.C @@ -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) diff --git a/src/functionObjects/field/mapFields/mapFields.C b/src/functionObjects/field/mapFields/mapFields.C index f0d250f659d..ee5a2679e7c 100644 --- a/src/functionObjects/field/mapFields/mapFields.C +++ b/src/functionObjects/field/mapFields/mapFields.C @@ -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 " -- GitLab