diff --git a/applications/test/Enum/Test-Enum.C b/applications/test/Enum/Test-Enum.C index f6f4d507f9..56a461fd91 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-2019 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -125,6 +125,17 @@ int main(int argc, char *argv[]) <<"stdout: "<< otherNames2 << nl << nl; + Info<< "iterate:" << nl; + forAllConstIters(otherNames2, iter) + { + Info<< "key=" << iter.key() << " val=" << iter.val() << nl; + } + + for (const word& k : otherNames2) + { + Info<< " " << k << " is " << otherNames2[k] << nl; + } + Info<< nl; otherNames2.clear(); otherNames2.append diff --git a/src/OpenFOAM/expressions/fields/fieldExprScanner.cc b/src/OpenFOAM/expressions/fields/fieldExprScanner.cc index 4ed61751d4..7a0587338e 100644 --- a/src/OpenFOAM/expressions/fields/fieldExprScanner.cc +++ b/src/OpenFOAM/expressions/fields/fieldExprScanner.cc @@ -204,7 +204,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_method << "Method:" << ident << " at " << driver_.parsePosition() << nl; - const int methType = fieldMethodEnums.get(ident, -1); + const int methType = fieldMethodEnums.lookup(ident, -1); if (methType > 0) { @@ -243,7 +243,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_ident else { // Check for function name - tokType = funcTokenEnums.get(ident, -1); + tokType = funcTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -257,7 +257,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_ident #ifdef HAS_LOOKBEHIND_TOKENS // Specials such "cset" also reset the look-behind - tokType = lookBehindTokenEnums.get(ident, -1); + tokType = lookBehindTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -299,7 +299,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_ident ( quoted || dot == std::string::npos ? -1 - : fieldMethodEnums.get(ident.substr(dot+1), -1) + : fieldMethodEnums.lookup(ident.substr(dot+1), -1) ); if diff --git a/src/OpenFOAM/expressions/fields/fieldExprScanner.rl b/src/OpenFOAM/expressions/fields/fieldExprScanner.rl index 2024cf1e6f..b4425210df 100644 --- a/src/OpenFOAM/expressions/fields/fieldExprScanner.rl +++ b/src/OpenFOAM/expressions/fields/fieldExprScanner.rl @@ -333,7 +333,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_method << "Method:" << ident << " at " << driver_.parsePosition() << nl; - const int methType = fieldMethodEnums.get(ident, -1); + const int methType = fieldMethodEnums.lookup(ident, -1); if (methType > 0) { @@ -372,7 +372,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_ident else { // Check for function name - tokType = funcTokenEnums.get(ident, -1); + tokType = funcTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -386,7 +386,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_ident #ifdef HAS_LOOKBEHIND_TOKENS // Specials such "cset" also reset the look-behind - tokType = lookBehindTokenEnums.get(ident, -1); + tokType = lookBehindTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -428,7 +428,7 @@ bool Foam::expressions::fieldExpr::scanner::dispatch_ident ( quoted || dot == std::string::npos ? -1 - : fieldMethodEnums.get(ident.substr(dot+1), -1) + : fieldMethodEnums.lookup(ident.substr(dot+1), -1) ); if diff --git a/src/OpenFOAM/primitives/enums/Enum.C b/src/OpenFOAM/primitives/enums/Enum.C index 42821200d0..136171884b 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-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -51,17 +51,6 @@ Foam::Enum::Enum // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -template -Foam::List Foam::Enum::sortedToc() const -{ - List list(keys_); - - Foam::sort(list); - - return list; -} - - template void Foam::Enum::append ( @@ -99,7 +88,7 @@ EnumType Foam::Enum::get(const word& enumName) const template -EnumType Foam::Enum::get +EnumType Foam::Enum::lookup ( const word& enumName, const EnumType deflt diff --git a/src/OpenFOAM/primitives/enums/Enum.H b/src/OpenFOAM/primitives/enums/Enum.H index 9294762140..dea4e3c420 100644 --- a/src/OpenFOAM/primitives/enums/Enum.H +++ b/src/OpenFOAM/primitives/enums/Enum.H @@ -66,15 +66,10 @@ class Enum //- The names for the enum List keys_; - //- The values for the enum + //- The values for the enum, stored as int List vals_; -public: - - //- The type of enumeration represented by the Enum - typedef EnumType value_type; - // Allow enums and integrals (should fit within an int) static_assert ( @@ -82,6 +77,16 @@ public: "Enum must be enum or an integral type" ); +public: + + // Typedefs + + //- The type of keys used + typedef word key_type; + + //- The type of enumeration represented by the Enum + typedef EnumType value_type; + // Constructors @@ -108,16 +113,16 @@ public: inline label size() const noexcept; //- The list of enum names, in construction order. Same as toc() - inline const List& names() const; - - //- The list of enum names, in construction order. Same as names() - inline const List& toc() const; - - //- The sorted list of enum names. - List sortedToc() const; + inline const List& names() const noexcept; //- The list of enum values, in construction order. - inline const List& values() const; + inline const List& values() const noexcept; + + //- The list of enum names, in construction order. Same as names() + inline const List& toc() const noexcept; + + //- The sorted list of enum names. + inline List sortedToc() const; // Modify @@ -143,23 +148,24 @@ public: // \return position in list or -1 if not found. inline label find(const EnumType e) const; - //- Test if there is an enumeration corresponding to the given name. + //- True 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. + //- True if there is a name corresponding to the given enumeration. inline bool found(const EnumType e) const; //- The enumeration corresponding to the given name. // FatalError if not found. EnumType get(const word& enumName) const; + //- The name corresponding to the given enumeration. + // Return an empty word if there is no corresponding name for it. + inline const word& get(const EnumType e) const; + //- The enumeration corresponding to the given name. // \return The enumeration or default if not found. - EnumType get(const word& enumName, const EnumType deflt) const; - - //- The name corresponding to the given enumeration. - // Return an empty word if not found. - inline const word& get(const EnumType e) const; + // \note Method name compatibility with HashTable + EnumType lookup(const word& enumName, const EnumType deflt) const; // Read @@ -241,26 +247,59 @@ public: //- Return the enumeration corresponding to the given name // FatalError if the name is not found. - // Identical to single parameter get() + // Identical to get() inline EnumType operator[](const word& enumName) const; //- Return the first name corresponding to the given enumeration, //- or an empty word on failure. - // Identical to single parameter get() + // Identical to get() inline const word& operator[](const EnumType e) const; - //- Return the first name corresponding to the given enumeration, - //- or an empty word on failure. - // Identical to single parameter get() - 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; + // Iteration + + //- A const_iterator for iterating an Enum list + // \note The iterator dereference returns the \b key + class const_iterator + { + //- The list being iterated + const Enum* ptr_; + + //- Index in the list + label idx_; + + public: + + //- Default construct, construct at given position + inline explicit const_iterator + ( + const Enum* eptr = nullptr, + const label idx = 0 + ) noexcept; + + //- The name at the current index + inline const word& key() const; + + //- Enumeration value at the current index + inline EnumType val() const; + + //- De-referencing returns the name (key) + // This is similar to HashSet (not HashTable!) and allows + // convenient output and traversing of the names + const word& operator*() const { return key(); } + + //- Move to the next index + inline const_iterator& operator++() noexcept; + + inline bool operator==(const const_iterator& iter) const noexcept; + inline bool operator!=(const const_iterator& iter) const noexcept; + }; + + inline const_iterator cbegin() const; + inline const_iterator cend() const; + + const_iterator begin() const { return cbegin(); } + const_iterator end() const { return cend(); } // Housekeeping @@ -281,7 +320,32 @@ public: return getOrDefault(key, dict, deflt, failsafe); } - //- Deprecated(2018-10) same as two-parameter get() + + //- Deprecated(2020-11) use get() method + // \deprecated(2020-11) - use get() method + FOAM_DEPRECATED_FOR(2020-11, "get() method") + const word& operator()(const EnumType e) const + { + return get(e); + } + + //- Deprecated(2020-11) use two-parameter lookup() method + // \deprecated(2020-11) - use two-parameter lookup() method + FOAM_DEPRECATED_FOR(2020-11, "lookup() method") + EnumType operator()(const word& key, const EnumType deflt) const + { + return lookup(key, deflt); + } + + //- Deprecated(2020-11) use two-parameter lookup() method + // \deprecated(2020-11) - use two-parameter lookup() method + FOAM_DEPRECATED_FOR(2020-11, "lookup() method") + EnumType get(const word& key, const EnumType deflt) const + { + return lookup(key, deflt); + } + + //- Deprecated(2018-10) same as two-parameter get() method // \deprecated(2018-10) - use two-parameter get() method FOAM_DEPRECATED_FOR(2018-10, "get() method") EnumType lookup(const word& key, const dictionary& dict) const diff --git a/src/OpenFOAM/primitives/enums/EnumI.H b/src/OpenFOAM/primitives/enums/EnumI.H index 33f8aaa20e..b7c12e43fe 100644 --- a/src/OpenFOAM/primitives/enums/EnumI.H +++ b/src/OpenFOAM/primitives/enums/EnumI.H @@ -42,26 +42,41 @@ inline Foam::label Foam::Enum::size() const noexcept template -inline const Foam::wordList& Foam::Enum::names() const +inline const Foam::List& +Foam::Enum::names() const noexcept { return keys_; } template -inline const Foam::wordList& Foam::Enum::toc() const -{ - return keys_; -} - - -template -inline const Foam::List& Foam::Enum::values() const +inline const Foam::List& +Foam::Enum::values() const noexcept { return vals_; } +template +inline const Foam::List& +Foam::Enum::toc() const noexcept +{ + return keys_; +} + + +template +inline Foam::List +Foam::Enum::sortedToc() const +{ + List list(keys_); + + Foam::sort(list); + + return list; +} + + template inline void Foam::Enum::clear() { @@ -155,6 +170,80 @@ inline OS& Foam::Enum::writeList(OS& os, const label) const } +// * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * // + +template +inline Foam::Enum::const_iterator::const_iterator +( + const Enum* eptr, + const label idx +) noexcept +: + ptr_(eptr), + idx_(idx) +{} + + +template +inline const Foam::word& +Foam::Enum::const_iterator::key() const +{ + return ptr_->names()[idx_]; +} + + +template +inline EnumType Foam::Enum::const_iterator::val() const +{ + return EnumType(ptr_->values()[idx_]); +} + + +template +inline typename Foam::Enum::const_iterator& +Foam::Enum::const_iterator::operator++() noexcept +{ + ++idx_; + return *this; +} + + +template +inline bool Foam::Enum::const_iterator::operator== +( + const const_iterator& iter +) const noexcept +{ + return idx_ == iter.idx_; +} + + +template +inline bool Foam::Enum::const_iterator::operator!= +( + const const_iterator& iter +) const noexcept +{ + return idx_ != iter.idx_; +} + + +template +inline typename Foam::Enum::const_iterator +Foam::Enum::cbegin() const +{ + return typename Enum::const_iterator(this); +} + + +template +inline typename Foam::Enum::const_iterator +Foam::Enum::cend() const +{ + return typename Enum::const_iterator(this, this->size()); +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template @@ -177,34 +266,6 @@ inline const Foam::word& Foam::Enum::operator[] } -template -inline const Foam::word& Foam::Enum::operator() -( - const EnumType e -) const -{ - return get(e); -} - - -template -inline EnumType Foam::Enum::operator() -( - const word& enumName, - const EnumType deflt -) const -{ - const label idx = find(enumName); - - if (idx >= 0) - { - return EnumType(vals_[idx]); - } - - return deflt; -} - - // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // template diff --git a/src/finiteVolume/expressions/patch/patchExprScanner.cc b/src/finiteVolume/expressions/patch/patchExprScanner.cc index 7650f62338..a66fc3ede3 100644 --- a/src/finiteVolume/expressions/patch/patchExprScanner.cc +++ b/src/finiteVolume/expressions/patch/patchExprScanner.cc @@ -258,7 +258,7 @@ static int driverTokenType { const word fieldType(driver_.getFieldClassName(ident)); - int tokType = fieldTokenEnums().get(fieldType, -1); + int tokType = fieldTokenEnums().lookup(fieldType, -1); if (tokType > 0) { @@ -328,7 +328,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_method << "Method:" << ident << " at " << driver_.parsePosition() << nl; - const int methType = fieldMethodEnums.get(ident, -1); + const int methType = fieldMethodEnums.lookup(ident, -1); if (methType > 0) { @@ -367,7 +367,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_ident else { // Check for function name - tokType = funcTokenEnums.get(ident, -1); + tokType = funcTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -381,7 +381,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_ident #ifdef HAS_LOOKBEHIND_TOKENS // Specials such "cset" also reset the look-behind - tokType = lookBehindTokenEnums.get(ident, -1); + tokType = lookBehindTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -423,7 +423,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_ident ( quoted || dot == std::string::npos ? -1 - : fieldMethodEnums.get(ident.substr(dot+1), -1) + : fieldMethodEnums.lookup(ident.substr(dot+1), -1) ); if diff --git a/src/finiteVolume/expressions/patch/patchExprScanner.rl b/src/finiteVolume/expressions/patch/patchExprScanner.rl index 87b4b3c600..4748732d05 100644 --- a/src/finiteVolume/expressions/patch/patchExprScanner.rl +++ b/src/finiteVolume/expressions/patch/patchExprScanner.rl @@ -256,7 +256,7 @@ static int driverTokenType { const word fieldType(driver_.getFieldClassName(ident)); - int tokType = fieldTokenEnums().get(fieldType, -1); + int tokType = fieldTokenEnums().lookup(fieldType, -1); if (tokType > 0) { @@ -459,7 +459,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_method << "Method:" << ident << " at " << driver_.parsePosition() << nl; - const int methType = fieldMethodEnums.get(ident, -1); + const int methType = fieldMethodEnums.lookup(ident, -1); if (methType > 0) { @@ -498,7 +498,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_ident else { // Check for function name - tokType = funcTokenEnums.get(ident, -1); + tokType = funcTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -512,7 +512,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_ident #ifdef HAS_LOOKBEHIND_TOKENS // Specials such "cset" also reset the look-behind - tokType = lookBehindTokenEnums.get(ident, -1); + tokType = lookBehindTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -554,7 +554,7 @@ bool Foam::expressions::patchExpr::scanner::dispatch_ident ( quoted || dot == std::string::npos ? -1 - : fieldMethodEnums.get(ident.substr(dot+1), -1) + : fieldMethodEnums.lookup(ident.substr(dot+1), -1) ); if diff --git a/src/finiteVolume/expressions/volume/volumeExprScanner.cc b/src/finiteVolume/expressions/volume/volumeExprScanner.cc index aafaa94ae9..5d0c84e293 100644 --- a/src/finiteVolume/expressions/volume/volumeExprScanner.cc +++ b/src/finiteVolume/expressions/volume/volumeExprScanner.cc @@ -287,7 +287,7 @@ static int driverTokenType { const word fieldType(driver_.getFieldClassName(ident)); - int tokType = fieldTokenEnums().get(fieldType, -1); + int tokType = fieldTokenEnums().lookup(fieldType, -1); if (tokType > 0) { @@ -357,7 +357,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_method << "Method:" << ident << " at " << driver_.parsePosition() << nl; - const int methType = fieldMethodEnums.get(ident, -1); + const int methType = fieldMethodEnums.lookup(ident, -1); if (methType > 0) { @@ -396,7 +396,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_ident else { // Check for function name - tokType = funcTokenEnums.get(ident, -1); + tokType = funcTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -410,7 +410,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_ident #ifdef HAS_LOOKBEHIND_TOKENS // Specials such "cset" also reset the look-behind - tokType = lookBehindTokenEnums.get(ident, -1); + tokType = lookBehindTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -452,7 +452,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_ident ( quoted || dot == std::string::npos ? -1 - : fieldMethodEnums.get(ident.substr(dot+1), -1) + : fieldMethodEnums.lookup(ident.substr(dot+1), -1) ); if diff --git a/src/finiteVolume/expressions/volume/volumeExprScanner.rl b/src/finiteVolume/expressions/volume/volumeExprScanner.rl index 13cba251bd..ea957317fb 100644 --- a/src/finiteVolume/expressions/volume/volumeExprScanner.rl +++ b/src/finiteVolume/expressions/volume/volumeExprScanner.rl @@ -285,7 +285,7 @@ static int driverTokenType { const word fieldType(driver_.getFieldClassName(ident)); - int tokType = fieldTokenEnums().get(fieldType, -1); + int tokType = fieldTokenEnums().lookup(fieldType, -1); if (tokType > 0) { @@ -488,7 +488,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_method << "Method:" << ident << " at " << driver_.parsePosition() << nl; - const int methType = fieldMethodEnums.get(ident, -1); + const int methType = fieldMethodEnums.lookup(ident, -1); if (methType > 0) { @@ -527,7 +527,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_ident else { // Check for function name - tokType = funcTokenEnums.get(ident, -1); + tokType = funcTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -541,7 +541,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_ident #ifdef HAS_LOOKBEHIND_TOKENS // Specials such "cset" also reset the look-behind - tokType = lookBehindTokenEnums.get(ident, -1); + tokType = lookBehindTokenEnums.lookup(ident, -1); if (tokType > 0) { @@ -583,7 +583,7 @@ bool Foam::expressions::volumeExpr::scanner::dispatch_ident ( quoted || dot == std::string::npos ? -1 - : fieldMethodEnums.get(ident.substr(dot+1), -1) + : fieldMethodEnums.lookup(ident.substr(dot+1), -1) ); if diff --git a/src/functionObjects/field/derivedFields/derivedFields.C b/src/functionObjects/field/derivedFields/derivedFields.C index 664faa3a9e..9092b16e13 100644 --- a/src/functionObjects/field/derivedFields/derivedFields.C +++ b/src/functionObjects/field/derivedFields/derivedFields.C @@ -202,7 +202,7 @@ bool Foam::functionObjects::derivedFields::read(const dictionary& dict) for (const word& key : derivedNames) { - derivedTypes_[ngood] = knownNames.get(key, derivedType::UNKNOWN); + derivedTypes_[ngood] = knownNames.lookup(key, derivedType::UNKNOWN); switch (derivedTypes_[ngood]) { diff --git a/src/functionObjects/utilities/abort/abort.C b/src/functionObjects/utilities/abort/abort.C index 2e85147db5..7f0a560388 100644 --- a/src/functionObjects/utilities/abort/abort.C +++ b/src/functionObjects/utilities/abort/abort.C @@ -80,7 +80,7 @@ static enum Time::stopAtControls getStopAction(const std::string& filename) const word actionName(word::validate(fileContent.substr(equals+1))); return - Time::stopAtControlNames + Time::stopAtControlNames.lookup ( actionName, Time::stopAtControls::saUnknown diff --git a/src/meshTools/coupling/externalFileCoupler.C b/src/meshTools/coupling/externalFileCoupler.C index f52b3a4063..0cae8f6dcd 100644 --- a/src/meshTools/coupling/externalFileCoupler.C +++ b/src/meshTools/coupling/externalFileCoupler.C @@ -77,7 +77,7 @@ static enum Time::stopAtControls getStopAction(const std::string& filename) const word actionName(word::validate(fileContent.substr(equals+1))); return - Time::stopAtControlNames + Time::stopAtControlNames.lookup ( actionName, Time::stopAtControls::saUnknown