From fb26fcedfc5b525529a41352f602c189d4a7f4e0 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 13 Oct 2023 19:27:27 +0200 Subject: [PATCH] STYLE: use getOrDefault instead of lookupOrDefault - now mark methods with strict deprecation, to make it easier to find their use but without adding extra compilation noise for others ENH: minor update for Enum methods and iterator - add warnOnly (failsafe) option for readEntry and getOrDefault - add good() method to Enum iterator (simliar to HashTable) - replace unused/fragile Enum find() methods with iterator return that can be used more generally --- applications/test/Enum/Test-Enum.C | 30 +++++++--- .../dimensionedType/Test-dimensionedType.C | 6 +- .../decomposePar/decomposePar.C | 8 ++- src/OpenFOAM/db/dictionary/dictionary.H | 17 ++---- .../dimensionedType/dimensionedType.H | 19 +++--- src/OpenFOAM/global/argList/argList.H | 4 ++ .../GAMGAgglomeration/GAMGAgglomeration.C | 2 +- src/OpenFOAM/primitives/bools/Switch/Switch.C | 17 +++++- src/OpenFOAM/primitives/bools/Switch/Switch.H | 13 +++- src/OpenFOAM/primitives/enums/Enum.C | 59 ++++++++++++------- src/OpenFOAM/primitives/enums/Enum.H | 45 +++++++------- src/OpenFOAM/primitives/enums/EnumI.H | 44 ++++++++------ .../primitives/enums/{ => compat}/NamedEnum.C | 0 .../primitives/enums/{ => compat}/NamedEnum.H | 6 +- .../enums/{ => compat}/NamedEnumI.H | 0 .../primitives/ranges/sliceRange/sliceRange.C | 13 +++- .../primitives/ranges/sliceRange/sliceRange.H | 11 +++- .../turbulenceModels/LES/sigma/sigma.C | 6 +- .../expressions/patch/patchExprScanner.cc | 2 +- .../expressions/patch/patchExprScanner.rl | 2 +- .../expressions/volume/volumeExprScanner.cc | 4 +- .../expressions/volume/volumeExprScanner.rl | 4 +- .../mappedPolyPatch/mappedPatchBase.C | 2 +- .../adjointkOmegaSST/adjointkOmegaSST.C | 8 +-- .../setTimeStepFaRegionsFunctionObject.C | 2 +- .../liquidFilm/liquidFilmBase.C | 2 +- 26 files changed, 197 insertions(+), 129 deletions(-) rename src/OpenFOAM/primitives/enums/{ => compat}/NamedEnum.C (100%) rename src/OpenFOAM/primitives/enums/{ => compat}/NamedEnum.H (98%) rename src/OpenFOAM/primitives/enums/{ => compat}/NamedEnumI.H (100%) diff --git a/applications/test/Enum/Test-Enum.C b/applications/test/Enum/Test-Enum.C index 56a461fd91..669b5e41af 100644 --- a/applications/test/Enum/Test-Enum.C +++ b/applications/test/Enum/Test-Enum.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018-2020 OpenCFD Ltd. + Copyright (C) 2018-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -110,7 +110,7 @@ int main(int argc, char *argv[]) << " values: " << flatOutput(otherNames2.values()) << nl << nl; - otherNames2.append + otherNames2.push_back ({ { 15, "fifteen"}, { 16, "sixteen"} @@ -138,7 +138,7 @@ int main(int argc, char *argv[]) Info<< nl; otherNames2.clear(); - otherNames2.append + otherNames2.push_back ({ { 1, "one"}, { 2, "two"} @@ -149,6 +149,18 @@ int main(int argc, char *argv[]) << otherNames2.values() << nl << nl; + for (const auto& k : {"one", "two", "three", "four" }) + { + const auto iter = otherNames2.find(k); + + Info<< "find(" << k << ") good:" << iter.good(); + if (iter.good()) + { + Info<< " value:" << int(iter.val()); + } + Info<< nl; + } + dictionary testDict; testDict.add("lookup1", "c"); @@ -162,10 +174,10 @@ int main(int argc, char *argv[]) { Info<< "dict: " << testDict << endl; - Info<< "lookupOrDefault(notFound) = " + Info<< "getOrDefault(notFound) = " << int ( - testing::option1Names.lookupOrDefault + testing::option1Names.getOrDefault ( "notFound", testDict, @@ -174,10 +186,10 @@ int main(int argc, char *argv[]) ) << nl; - Info<< "lookupOrDefault(lookup1) = " + Info<< "getOrDefault(lookup1) = " << int ( - testing::option1Names.lookupOrDefault + testing::option1Names.getOrDefault ( "lookup1", testDict, @@ -186,10 +198,10 @@ int main(int argc, char *argv[]) ) << nl; - Info<< "lookupOrDefault(lookup1) = " + Info<< "getOrDefault(lookup1) = " << int ( - testing::option2Names.lookupOrDefault + testing::option2Names.getOrDefault ( "lookup1", testDict, diff --git a/applications/test/dimensionedType/Test-dimensionedType.C b/applications/test/dimensionedType/Test-dimensionedType.C index 20bcf4748c..7e40add951 100644 --- a/applications/test/dimensionedType/Test-dimensionedType.C +++ b/applications/test/dimensionedType/Test-dimensionedType.C @@ -136,13 +136,13 @@ int main(int argc, char *argv[]) Info<< "test2 : " << dimensionedScalar("test2", dimless, 20, dict) << nl; Info<< "test2a : " << dimensionedScalar("test2a", dimless, 20, dict) << nl; Info<< "test3 : " - << dimensionedScalar::lookupOrDefault("test3", dict, 30) << nl; + << dimensionedScalar::getOrDefault("test3", dict, 30) << nl; Info<< "test4 : " - << dimensionedScalar::lookupOrAddToDict("test4", dict, 40) << nl; + << dimensionedScalar::getOrAddToDict("test4", dict, 40) << nl; Info<< "test5 : " - << dimensionedScalar::lookupOrAddToDict("test5", dict, -50) << nl; + << dimensionedScalar::getOrAddToDict("test5", dict, -50) << nl; // Deprecated Info<< "Deprecated constructors" << nl; diff --git a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C index 333e2901e9..130bf98a46 100644 --- a/applications/utilities/parallelProcessing/decomposePar/decomposePar.C +++ b/applications/utilities/parallelProcessing/decomposePar/decomposePar.C @@ -469,8 +469,12 @@ int main(int argc, char *argv[]) // Allow override of decomposeParDict location - fileName decompDictFile(args.get("decomposeParDict", "")); - if (!decompDictFile.empty() && !decompDictFile.isAbsolute()) + fileName decompDictFile; + if + ( + args.readIfPresent("decomposeParDict", decompDictFile) + && !decompDictFile.empty() && !decompDictFile.isAbsolute() + ) { decompDictFile = runTime.globalPath()/decompDictFile; } diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index fc9b1a9ad2..0f8b64394f 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -1307,13 +1307,9 @@ public: // Housekeeping - //- Find and return a T, or return the given default value. - //- FatalIOError if it is found and the number of tokens is incorrect. - // - // \param keyword the keyword to search for - // \param deflt the default value to use - // \param matchOpt search mode (default: non-recursive with patterns) + //- Same as getOrDefault() template + FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault()") T lookupOrDefault ( const word& keyword, @@ -1325,14 +1321,9 @@ public: } - //- Find and return a T, or return the given default value - //- and add it to dictionary. - //- FatalIOError if it is found and the number of tokens is incorrect. - // - // \param keyword the keyword to search for - // \param deflt the default value to use - // \param matchOpt search mode (default: non-recursive with patterns) + //- Same as getOrAdd() template + FOAM_DEPRECATED_STRICT(2019-06, "getOrAdd()") T lookupOrAddDefault ( const word& keyword, diff --git a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H index 89ec5f9bed..9f840e2698 100644 --- a/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H +++ b/src/OpenFOAM/dimensionedTypes/dimensionedType/dimensionedType.H @@ -400,9 +400,8 @@ public: FOAM_DEPRECATED_FOR(2018-11, "construct from dictionary or entry") dimensioned(const word& name, const dimensionSet& dims, Istream& is); - - //- Construct dimensioned from dictionary, with default value. - //- FatalIOError if there are excess tokens. + //- Same as getOrDefault() + FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault()") static dimensioned lookupOrDefault ( const word& name, @@ -414,8 +413,8 @@ public: return getOrDefault(name, dict, dims, deflt); } - //- Construct dimensionless from dictionary, with default value. - // FatalIOError if it is found and there are excess tokens. + //- Same as getOrDefault() + FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault()") static dimensioned lookupOrDefault ( const word& name, @@ -426,9 +425,8 @@ public: return getOrDefault(name, dict, deflt); } - //- Construct dimensioned from dictionary, with default value. - // If the value is not found, it is added into the dictionary. - // FatalIOError if it is found and there are excess tokens. + //- Same as getOrAddToDict() + FOAM_DEPRECATED_STRICT(2019-06, "getOrAddToDict()") static dimensioned lookupOrAddToDict ( const word& name, @@ -440,9 +438,8 @@ public: return getOrAddToDict(name, dict, dims, deflt); } - //- Construct dimensionless from dictionary, with default value. - // If the value is not found, it is added into the dictionary. - // FatalIOError if it is found and there are excess tokens. + //- Same as getOrAddToDict() + FOAM_DEPRECATED_STRICT(2019-06, "getOrAddToDict()") static dimensioned lookupOrAddToDict ( const word& name, diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H index 0b0efa5844..ad3b1691e1 100644 --- a/src/OpenFOAM/global/argList/argList.H +++ b/src/OpenFOAM/global/argList/argList.H @@ -729,6 +729,7 @@ public: //- Deprecated(2020-05) identical to get(const word& optName) // \deprecated(2020-05) - use get() method template + FOAM_DEPRECATED_STRICT(2020-06, "get()") T opt(const word& optName) const { return this->get(optName); @@ -737,6 +738,7 @@ public: //- Deprecated(2020-05) identical to getOrDefault(...) // \deprecated(2020-05) - use getOrDefault() method template + FOAM_DEPRECATED_STRICT(2020-06, "getOrDefault()") T opt(const word& optName, const T& deflt) const { return this->getOrDefault(optName, deflt); @@ -745,6 +747,7 @@ public: //- Deprecated(2020-05) identical to getOrDefault(...) // \deprecated(2020-05) - use getOrDefault() method template + FOAM_DEPRECATED_STRICT(2020-06, "getOrDefault()") T get(const word& optName, const T& deflt) const { return this->getOrDefault(optName, deflt); @@ -753,6 +756,7 @@ public: //- Deprecated(2020-05) identical to getOrDefault(...) // \deprecated(2020-05) - use getOrDefault() method template + FOAM_DEPRECATED_STRICT(2020-06, "getOrDefault()") T lookupOrDefault(const word& optName, const T& deflt) const { return this->getOrDefault(optName, deflt); diff --git a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomeration.C b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomeration.C index 1bcb5d600a..ec6d12ebff 100644 --- a/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomeration.C +++ b/src/OpenFOAM/matrices/lduMatrix/solvers/GAMG/GAMGAgglomerations/GAMGAgglomeration/GAMGAgglomeration.C @@ -425,7 +425,7 @@ const Foam::GAMGAgglomeration& Foam::GAMGAgglomeration::New { const word agglomeratorType ( - controlDict.lookupOrDefault("agglomerator", "faceAreaPair") + controlDict.getOrDefault("agglomerator", "faceAreaPair") ); const_cast(mesh.thisDb().time()).libs().open diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.C b/src/OpenFOAM/primitives/bools/Switch/Switch.C index 8e3528e048..dd9a7d2551 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.C +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.C @@ -160,6 +160,17 @@ bool Foam::Switch::contains(const std::string& str) } +Foam::Switch Foam::Switch::getOrDefault +( + const word& key, + const dictionary& dict, + const Switch deflt +) +{ + return dict.getOrDefault(key, deflt, keyType::LITERAL); +} + + Foam::Switch Foam::Switch::getOrAddToDict ( const word& key, @@ -260,7 +271,7 @@ Foam::Switch::Switch const word& key, const dictionary& dict, const Switch deflt, - const bool failsafe + const bool warnOnly ) : value_(deflt.value_) @@ -275,10 +286,10 @@ Foam::Switch::Switch { (*this) = sw; } - else if (failsafe) + else if (warnOnly) { printTokenError(IOWarningInFunction(dict), tok) - << "using failsafe " << deflt.c_str() << endl; + << "using default " << deflt.c_str() << endl; } else { diff --git a/src/OpenFOAM/primitives/bools/Switch/Switch.H b/src/OpenFOAM/primitives/bools/Switch/Switch.H index a420eaddd8..ddef797909 100644 --- a/src/OpenFOAM/primitives/bools/Switch/Switch.H +++ b/src/OpenFOAM/primitives/bools/Switch/Switch.H @@ -179,13 +179,13 @@ public: //- switch value or the default if not found in dictionary. // // FatalIOError if the switch name is incorrect. - // Specifying failsafe downgrades the FatalIOError to an IOWarning. + // Specifying warnOnly downgrades the FatalIOError to an IOWarning. Switch ( const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const dictionary& dict, //!< dictionary const Switch deflt, //!< fallback if not found - const bool failsafe = false //!< Warn only on bad input + const bool warnOnly = false //!< Warn (not fail) on bad input ); //- Construct from Istream by reading a token @@ -194,6 +194,14 @@ public: // Helpers + //- Construct Switch from dictionary, with default value + static Switch getOrDefault + ( + const word& key, //!< Lookup key. Uses LITERAL (not REGEX) + const dictionary& dict, //!< dictionary + const Switch deflt = switchType::FALSE //!< fallback if not found + ); + //- Construct from dictionary, supplying default value so that if the //- value is not found, it is added into the dictionary. static Switch getOrAddToDict @@ -290,6 +298,7 @@ public: bool valid() const noexcept { return good(); } //- Same as getOrAddToDict() + FOAM_DEPRECATED_STRICT(2019-06, "getOrAddToDict()") static Switch lookupOrAddToDict ( const word& name, diff --git a/src/OpenFOAM/primitives/enums/Enum.C b/src/OpenFOAM/primitives/enums/Enum.C index 503e3d499c..4cfa5a6ae2 100644 --- a/src/OpenFOAM/primitives/enums/Enum.C +++ b/src/OpenFOAM/primitives/enums/Enum.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2017-2022 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -74,7 +74,7 @@ void Foam::Enum::push_back template EnumType Foam::Enum::get(const word& enumName) const { - const label idx = find(enumName); + const label idx = keys_.find(enumName); if (idx < 0) { @@ -94,7 +94,7 @@ EnumType Foam::Enum::lookup const EnumType deflt ) const { - const label idx = find(enumName); + const label idx = keys_.find(enumName); if (idx < 0) { @@ -110,7 +110,7 @@ EnumType Foam::Enum::read(Istream& is) const { const word enumName(is); - const label idx = find(enumName); + const label idx = keys_.find(enumName); if (idx < 0) { @@ -133,7 +133,7 @@ bool Foam::Enum::read { const word enumName(is); - const label idx = find(enumName); + const label idx = keys_.find(enumName); if (idx >= 0) { @@ -161,12 +161,13 @@ EnumType Foam::Enum::get { const word enumName(dict.get(key, keyType::LITERAL)); - const label idx = find(enumName); + const label idx = keys_.find(enumName); if (idx < 0) { FatalIOErrorInFunction(dict) - << enumName << " is not in enumeration: " << *this << nl + << "Lookup:" << key << " enumeration " << enumName + << " is not in enumeration: " << *this << nl << exit(FatalIOError); } @@ -180,7 +181,7 @@ EnumType Foam::Enum::getOrDefault const word& key, const dictionary& dict, const EnumType deflt, - const bool failsafe + const bool warnOnly ) const { const entry* eptr = dict.findEntry(key, keyType::LITERAL); @@ -189,26 +190,28 @@ EnumType Foam::Enum::getOrDefault { const word enumName(eptr->get()); - const label idx = find(enumName); + const label idx = keys_.find(enumName); if (idx >= 0) { return EnumType(vals_[idx]); } - // Found the entry, but failed the name lookup + // Found the dictionary entry, but the name not in enumeration - if (failsafe) + if (warnOnly) { IOWarningInFunction(dict) - << enumName << " is not in enumeration: " << *this << nl - << "using failsafe " << get(deflt) + << "Lookup:" << key << " enumeration " << enumName + << " is not in enumeration: " << *this << nl + << "using default " << get(deflt) << " (value " << int(deflt) << ')' << endl; } else { FatalIOErrorInFunction(dict) - << enumName << " is not in enumeration: " << *this << nl + << "Lookup:" << key << " enumeration " << enumName + << " is not in enumeration: " << *this << nl << exit(FatalIOError); } } @@ -223,7 +226,8 @@ bool Foam::Enum::readEntry const word& key, const dictionary& dict, EnumType& val, - const bool mandatory + const bool mandatory, + const bool warnOnly ) const { const entry* eptr = dict.findEntry(key, keyType::LITERAL); @@ -232,7 +236,7 @@ bool Foam::Enum::readEntry { const word enumName(eptr->get()); - const label idx = find(enumName); + const label idx = keys_.find(enumName); if (idx >= 0) { @@ -240,14 +244,29 @@ bool Foam::Enum::readEntry return true; } - FatalIOErrorInFunction(dict) - << enumName << " is not in enumeration: " << *this << nl - << exit(FatalIOError); + // Found the dictionary entry, but the name not in enumeration + + if (warnOnly) + { + IOWarningInFunction(dict) + << "Lookup:" << key << " enumeration " << enumName + << " is not in enumeration: " << *this << nl + << "leaving value unchanged" + << " (value " << int(val) << ')' << endl; + } + else + { + FatalIOErrorInFunction(dict) + << "Lookup:" << key << " enumeration " << enumName + << " is not in enumeration: " << *this << nl + << exit(FatalIOError); + } } else if (mandatory) { FatalIOErrorInFunction(dict) - << "'" << key << "' not found in dictionary " << dict.name() << nl + << "Lookup:" << key + << " not found in dictionary " << dict.name() << nl << exit(FatalIOError); } diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index 321e2ec4a1..d990c3ca47 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -145,14 +145,6 @@ public: //- True if there is a name corresponding to the given enumeration. inline bool contains(const EnumType e) const; - //- Find the index of the given name. - // \return position in list or -1 if not found. - inline label find(const word& enumName) const; - - //- Find the first index of given enumeration. - // \return position in list or -1 if not found. - inline label find(const EnumType e) const; - //- The enumeration corresponding to the given name. // FatalError if not found. EnumType get(const word& enumName) const; @@ -183,26 +175,28 @@ public: // // \return The value found or default if not found in dictionary. // FatalIOError if the enumeration is incorrect. - // Specifying failsafe downgrades the FatalIOError to an IOWarning. + // Specifying warnOnly downgrades the FatalIOError to an IOWarning. EnumType getOrDefault ( const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const dictionary& dict, //!< dictionary const EnumType deflt, //!< fallback if not found - const bool failsafe = false //!< Warn only on bad enumeration + const bool warnOnly = false //!< Warn (not fail) on bad enumeration ) const; //- Find entry and assign to T val. // FatalIOError if the enumeration is incorrect, // or when it is mandatory but was not found. // + // (TDB: handle IOobjectOption::readOption instead) // \return true if the entry was found. bool readEntry ( const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const dictionary& dict, //!< dictionary - EnumType& val, //!< the value to read into - const bool mandatory = true //!< the keyword is mandatory + EnumType& val, //!< The value to read into + const bool mandatory = true, //!< The keyword is mandatory + const bool warnOnly = false //!< Warn (not fail) on bad enumeration ) const; //- Find an entry if present, and assign to T val. @@ -214,7 +208,8 @@ public: ( const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const dictionary& dict, //!< dictionary - EnumType& val //!< the value to read into + EnumType& val, //!< The value to read into + const bool warnOnly = false //!< Warn (not fail) on bad enumeration ) const; @@ -233,7 +228,7 @@ public: ) const; //- Write the name representation of the enumeration to an Ostream - // A noop if the enumeration wasn't found. + // A no-op if the enumeration does not have a corresponding name. inline void write(const EnumType e, Ostream& os) const; //- Write enumeration names as a list without line-breaks @@ -276,6 +271,9 @@ public: const label idx = 0 ) noexcept; + //- True if iterator points to an entry + inline bool good() const noexcept; + //- The name at the current index inline const word& key() const; @@ -300,23 +298,25 @@ public: const_iterator begin() const noexcept { return cbegin(); } const_iterator end() const noexcept { return cend(); } + //- Find the enumeration by name. + // Equal to cend() if not found, or test if good() + inline const_iterator cfind(const word& key) const; + const_iterator find(const word& key) const { return cfind(key); } + // Housekeeping - //- Find the key in the dictionary and return the corresponding - //- enumeration element based on its name. - // - // \return The value found or default if not found in dictionary. - // FatalError (or Warning) if the enumeration was incorrect. + //- Same as getOrDefault() + FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault() method") EnumType lookupOrDefault ( const word& key, //!< Lookup key. Uses LITERAL (not REGEX) const dictionary& dict, //!< dictionary const EnumType deflt, //!< fallback if not found - const bool failsafe = false //!< Warn only on bad enumeration + const bool warnOnly = false //!< Warn (not fail) on bad enumeration ) const { - return getOrDefault(key, dict, deflt, failsafe); + return getOrDefault(key, dict, deflt, warnOnly); } //- Deprecated(2020-11) use get() method @@ -353,6 +353,7 @@ public: //- Append value/key pairs to the lists of known enumerations // Does not check for duplicate entries + FOAM_DEPRECATED_STRICT(2023-06, "push_back() method") void append ( std::initializer_list> list @@ -362,7 +363,7 @@ public: } //- Same as contains() - bool found(const word& enumName) const { return contains(enumName); } + bool found(const word& key) const { return contains(key); } //- Same as contains() bool found(const EnumType e) const { return contains(e); } diff --git a/src/OpenFOAM/primitives/enums/EnumI.H b/src/OpenFOAM/primitives/enums/EnumI.H index 4a8a3d0086..891dbd7da9 100644 --- a/src/OpenFOAM/primitives/enums/EnumI.H +++ b/src/OpenFOAM/primitives/enums/EnumI.H @@ -85,20 +85,6 @@ inline void Foam::Enum::clear() } -template -inline Foam::label Foam::Enum::find(const word& enumName) const -{ - return keys_.find(enumName); -} - - -template -inline Foam::label Foam::Enum::find(const EnumType e) const -{ - return vals_.find(int(e)); -} - - template inline bool Foam::Enum::contains(const word& enumName) const { @@ -116,7 +102,7 @@ inline bool Foam::Enum::contains(const EnumType e) const template inline const Foam::word& Foam::Enum::get(const EnumType e) const { - const label idx = find(e); + const label idx = vals_.find(int(e)); if (idx < 0) { @@ -132,18 +118,19 @@ inline bool Foam::Enum::readIfPresent ( const word& key, const dictionary& dict, - EnumType& val + EnumType& val, + const bool warnOnly ) const { // Reading is non-mandatory - return readEntry(key, dict, val, false); + return readEntry(key, dict, val, false, warnOnly); } template inline void Foam::Enum::write(const EnumType e, Ostream& os) const { - const label idx = find(e); + const label idx = vals_.find(int(e)); if (idx >= 0) { @@ -199,6 +186,13 @@ inline EnumType Foam::Enum::const_iterator::val() const } +template +inline bool Foam::Enum::const_iterator::good() const noexcept +{ + return (ptr_ && idx_ >= 0 && idx_ < ptr_->size()); +} + + template inline typename Foam::Enum::const_iterator& Foam::Enum::const_iterator::operator++() noexcept @@ -244,6 +238,20 @@ Foam::Enum::cend() const noexcept } +template +inline typename Foam::Enum::const_iterator +Foam::Enum::cfind(const word& key) const +{ + const label idx = keys_.find(key); + + return typename Enum::const_iterator + ( + this, + (idx >= 0 ? idx : this->size()) + ); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template diff --git a/src/OpenFOAM/primitives/enums/NamedEnum.C b/src/OpenFOAM/primitives/enums/compat/NamedEnum.C similarity index 100% rename from src/OpenFOAM/primitives/enums/NamedEnum.C rename to src/OpenFOAM/primitives/enums/compat/NamedEnum.C diff --git a/src/OpenFOAM/primitives/enums/NamedEnum.H b/src/OpenFOAM/primitives/enums/compat/NamedEnum.H similarity index 98% rename from src/OpenFOAM/primitives/enums/NamedEnum.H rename to src/OpenFOAM/primitives/enums/compat/NamedEnum.H index be8b7a8e4d..273d5f2890 100644 --- a/src/OpenFOAM/primitives/enums/NamedEnum.H +++ b/src/OpenFOAM/primitives/enums/compat/NamedEnum.H @@ -32,7 +32,7 @@ Description particular enumeration values. \deprecated(2017-05) This class is retained for compatibility only and - should be used for any new code. + should be NOT used for any new code. The Foam::Enum class is robuster, more flexible, easier to use. See Also @@ -43,8 +43,8 @@ SourceFiles \*---------------------------------------------------------------------------*/ -#ifndef NamedEnum_H -#define NamedEnum_H +#ifndef FoamCompat_NamedEnum_H +#define FoamCompat_NamedEnum_H #include "HashTable.H" #include "wordList.H" diff --git a/src/OpenFOAM/primitives/enums/NamedEnumI.H b/src/OpenFOAM/primitives/enums/compat/NamedEnumI.H similarity index 100% rename from src/OpenFOAM/primitives/enums/NamedEnumI.H rename to src/OpenFOAM/primitives/enums/compat/NamedEnumI.H diff --git a/src/OpenFOAM/primitives/ranges/sliceRange/sliceRange.C b/src/OpenFOAM/primitives/ranges/sliceRange/sliceRange.C index c9f47203e5..e3a677b9c1 100644 --- a/src/OpenFOAM/primitives/ranges/sliceRange/sliceRange.C +++ b/src/OpenFOAM/primitives/ranges/sliceRange/sliceRange.C @@ -26,9 +26,12 @@ License \*---------------------------------------------------------------------------*/ #include "sliceRange.H" +#include "token.H" #include "FixedList.H" #include "List.H" -#include "token.H" +#include "Istream.H" +#include "Ostream.H" +#include // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -46,14 +49,18 @@ Foam::List Foam::sliceRange::labels() const { List