diff --git a/applications/test/namedDictionary/Test-namedDictionary.C b/applications/test/namedDictionary/Test-namedDictionary.C index fc7e418547..9a6b38c3d7 100644 --- a/applications/test/namedDictionary/Test-namedDictionary.C +++ b/applications/test/namedDictionary/Test-namedDictionary.C @@ -66,13 +66,13 @@ int main(int argc, char *argv[]) for (const entry& dEntry : dict) { - if (!dEntry.isStream()) + if (dEntry.isStream()) { - continue; + List list(dEntry.stream()); + + Info<< "input: " << dEntry << nl + << "list: " << list << nl; } - Info<< "input: " << dEntry << nl; - List list(dEntry.stream()); - Info<< "list: " << list << nl; } } } diff --git a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C index bff76e88fd..35392b2ef5 100644 --- a/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C +++ b/applications/utilities/miscellaneous/foamDictionary/foamDictionary.C @@ -549,16 +549,17 @@ int main(int argc, char *argv[]) { Info<< finder.dict(); } - else if (finder.ref().isStream()) + else if (finder.isStream()) { - bool addSep = false; + bool separator = false; - const tokenList& tokens = finder.ref().stream(); - - for (const token& tok : tokens) + for (const token& tok : finder.stream()) { - if (addSep) Info<< token::SPACE; - addSep = true; + if (separator) + { + Info<< token::SPACE; + } + separator = true; Info<< tok; } Info<< endl; diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index b3452bb382..2273848d60 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -121,6 +121,12 @@ class IOobject; template<> Ostream& operator<<(Ostream&, const InfoProxy&); +// Traits + +//- Trait for specifying global vs. local IOobject file types +template +struct is_globalIOobject : std::false_type {}; + /*---------------------------------------------------------------------------*\ Class IOobject Declaration @@ -712,6 +718,8 @@ public: }; +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + //- Specialization for \c void always returns true (no headerClassName check). template<> inline bool IOobject::isHeaderClass() const @@ -720,11 +728,6 @@ inline bool IOobject::isHeaderClass() const } -//- Trait for specifying global vs. local file types -template -struct is_globalIOobject : std::false_type {}; - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 0f8b64394f..e0c045b571 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2017 OpenFOAM Foundation - Copyright (C) 2016-2022 OpenCFD Ltd. + Copyright (C) 2016-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -183,8 +183,7 @@ public: pointer eptr_; - //- Construct for the given dictionary context. - // Allow implicit conversion + //- Implicit construct for the given dictionary context Searcher(dict_pointer dict) noexcept : dict_(dict), @@ -212,6 +211,7 @@ public: bool good() const noexcept { return eptr_; } //- True if entry was found + // \deprecated(2019-01) - prefer good() method bool found() const noexcept { return eptr_; } //- The containing dictionary context @@ -229,19 +229,32 @@ public: return (eptr_ && eptr_->dictPtr()); } + //- True if found entry is a stream. + bool isStream() const noexcept + { + return (eptr_ && eptr_->streamPtr()); + } + //- Pointer to the found entry as a dictionary, nullptr otherwise dict_pointer dictPtr() const noexcept { - return eptr_ ? eptr_->dictPtr() : nullptr; + return (eptr_ ? eptr_->dictPtr() : nullptr); } - //- Reference the found entry as a dictionary. - // (Error if not found, or not a dictionary). - dict_reference dict() const + //- Pointer to the found entry as a stream, nullptr otherwise + ITstream* streamPtr() const noexcept { - return eptr_->dict(); + return (eptr_ ? eptr_->streamPtr() : nullptr); } + //- Return the found entry as a dictionary. + //- Error if not found, or not a dictionary. + dict_reference dict() const { return eptr_->dict(); } + + //- Return the found entry as a ITstream. + //- Error if not found, or not a stream. + ITstream& stream() const { return eptr_->stream(); } + //- Permit an explicit cast to the other (const/non-const) searcher explicit operator const Searcher&() const { @@ -249,16 +262,10 @@ public: } //- A pointer to the entry (nullptr if not found) - pointer operator->() const noexcept - { - return eptr_; - } + pointer operator->() const noexcept { return eptr_; } //- A reference to the entry (Error if not found) - reference operator*() const - { - return *eptr_; - } + reference operator*() const { return *eptr_; } }; @@ -557,7 +564,7 @@ public: ) const; //- Find and return a sub-dictionary pointer if present - //- (and a sub-dictionary) otherwise return nullptr. + //- (and it is a dictionary) otherwise return nullptr. // // \param keyword the keyword to search for // \param matchOpt search mode (default: non-recursive with patterns) @@ -570,7 +577,7 @@ public: ) const; //- Find and return a sub-dictionary pointer if present - //- (and a sub-dictionary) otherwise return nullptr. + //- (and it is a dictionary) otherwise return nullptr. // // \param keyword the keyword to search for // \param matchOpt search mode (default: non-recursive with patterns) @@ -582,13 +589,14 @@ public: enum keyType::option matchOpt = keyType::REGEX ); - //- Find a sub-dictionary. + //- Find and return an entry stream if present + //- (and it is a stream) otherwise return nullptr. // // \param keyword the keyword to search for // \param matchOpt search mode (default: non-recursive with patterns) // - // \return true if the sub-dictionary was found - inline bool isDict + // \return pointer to ITstream or a nullptr + inline ITstream* findStream ( const word& keyword, enum keyType::option matchOpt = keyType::REGEX @@ -1283,7 +1291,7 @@ public: // Shortcuts - when a templated classes also inherits from a dictionary - #undef defineDictionaryGetter + #undef defineDictionaryGetter #define defineDictionaryGetter(Func, Type) \ /*! \brief Same as get\(const word&, keyType::option) */ \ Type Func \ @@ -1307,6 +1315,18 @@ public: // Housekeeping + //- Check for existence of a sub-dictionary. + //- Generally prefer findDict() for more flexibility. + FOAM_DEPRECATED_STRICT(2024-05, "findDict()") + bool isDict + ( + const word& keyword, + enum keyType::option matchOpt = keyType::REGEX + ) const + { + return static_cast(findDict(keyword, matchOpt)); + } + //- Same as getOrDefault() template FOAM_DEPRECATED_STRICT(2019-06, "getOrDefault()") @@ -1320,7 +1340,6 @@ public: return getOrDefault(keyword, deflt, matchOpt); } - //- Same as getOrAdd() template FOAM_DEPRECATED_STRICT(2019-06, "getOrAdd()") diff --git a/src/OpenFOAM/db/dictionary/dictionaryI.H b/src/OpenFOAM/db/dictionary/dictionaryI.H index 612e53acbb..61568506a3 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryI.H +++ b/src/OpenFOAM/db/dictionary/dictionaryI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021-2023 OpenCFD Ltd. + Copyright (C) 2021-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -147,13 +147,13 @@ inline Foam::dictionary* Foam::dictionary::findDict } -inline bool Foam::dictionary::isDict +inline Foam::ITstream* Foam::dictionary::findStream ( const word& keyword, enum keyType::option matchOpt ) const { - return static_cast(findDict(keyword, matchOpt)); + return csearch(keyword, matchOpt).streamPtr(); } diff --git a/src/OpenFOAM/db/dictionary/entry/entry.H b/src/OpenFOAM/db/dictionary/entry/entry.H index 5846171fdb..0274232889 100644 --- a/src/OpenFOAM/db/dictionary/entry/entry.H +++ b/src/OpenFOAM/db/dictionary/entry/entry.H @@ -214,32 +214,34 @@ public: virtual label endLineNumber() const = 0; - //- Return true if this entry is a stream - virtual bool isStream() const noexcept { return false; } + //- True if this entry is a stream + virtual bool isStream() const noexcept { return this->streamPtr(); } - //- Return pointer to token stream, if entry is a primitive entry - // Return nullptr if the entry is not a primitive entry - virtual ITstream* streamPtr() const { return nullptr; } + //- Return pointer to token stream, if it is a primitive entry, + //- otherwise return nullptr + virtual ITstream* streamPtr() const noexcept { return nullptr; } //- Return token stream, if entry is a primitive entry virtual ITstream& stream() const = 0; - //- Return true if this entry is a dictionary + //- True if this entry is a dictionary virtual bool isDict() const noexcept { return this->dictPtr(); } - //- Return pointer to dictionary, if entry is a dictionary. - // Return nullptr if the entry is not a dictionary. + //- Return pointer to dictionary, if entry is a dictionary, + //- otherwise return nullptr. virtual const dictionary* dictPtr() const noexcept { return nullptr; } - //- Return non-const pointer to dictionary, if entry is a dictionary - // Return nullptr if the entry is not a dictionary. + //- Return non-const pointer to dictionary, if entry is a dictionary, + //- otherwise return nullptr. virtual dictionary* dictPtr() noexcept { return nullptr; } - //- Return dictionary, if entry is a dictionary + //- Return dictionary, if entry is a dictionary, + //- otherwise Fatal. virtual const dictionary& dict() const = 0; - //- Return non-const access to dictionary, if entry is a dictionary + //- Return non-const access to dictionary, if entry is a dictionary, + //- otherwise Fatal. virtual dictionary& dict() = 0; diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index 1b5d9fab2a..9b2d8312fc 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -247,7 +247,7 @@ bool Foam::entry::New if (finder.good()) { // Read as primitiveEntry - const keyType newKeyword(finder.ptr()->stream()); + const keyType newKeyword(finder.stream()); return parentDict.add ( diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C index 8c48b5161b..7ec54d3db9 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.C @@ -273,7 +273,7 @@ Foam::primitiveEntry::primitiveEntry // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -Foam::ITstream* Foam::primitiveEntry::streamPtr() const +Foam::ITstream* Foam::primitiveEntry::streamPtr() const noexcept { ITstream* ptr = const_cast(this); ptr->seek(0); diff --git a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H index 346e849041..082ec95454 100644 --- a/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H +++ b/src/OpenFOAM/db/dictionary/primitiveEntry/primitiveEntry.H @@ -181,14 +181,8 @@ public: return ITstream::endLineNumber(); } - //- Return true - this entry is a stream - virtual bool isStream() const noexcept - { - return true; - } - //- Return pointer to token stream for this primitive entry - virtual ITstream* streamPtr() const; + virtual ITstream* streamPtr() const noexcept; //- Return token stream for this primitive entry virtual ITstream& stream() const; diff --git a/src/OpenFOAM/matrices/solution/solution.C b/src/OpenFOAM/matrices/solution/solution.C index 397bae49ae..e62f9ee605 100644 --- a/src/OpenFOAM/matrices/solution/solution.C +++ b/src/OpenFOAM/matrices/solution/solution.C @@ -241,7 +241,7 @@ Foam::label Foam::solution::upgradeSolverDict // recast primitive entries into dictionary entries for (const entry& dEntry : dict) { - if (!dEntry.isDict()) + if (dEntry.isStream()) { ITstream& is = dEntry.stream(); word name(is); @@ -256,11 +256,12 @@ Foam::label Foam::solution::upgradeSolverDict // transform primitiveEntry with settings -> dictionaryEntry for (const word& dictName : subDictNames) { - entry* eptr = subdict.findEntry(dictName, keyType::LITERAL); + ITstream* streamPtr = + subdict.findStream(dictName, keyType::LITERAL); - if (eptr && !eptr->isDict()) + if (streamPtr) { - ITstream& is = eptr->stream(); + auto& is = *streamPtr; is >> name; if (!is.eof()) diff --git a/src/OpenFOAM/primitives/coordinate/systems/coordinateSystemNew.C b/src/OpenFOAM/primitives/coordinate/systems/coordinateSystemNew.C index 3fc948b219..c397adcc26 100644 --- a/src/OpenFOAM/primitives/coordinate/systems/coordinateSystemNew.C +++ b/src/OpenFOAM/primitives/coordinate/systems/coordinateSystemNew.C @@ -129,9 +129,9 @@ Foam::coordinateSystem::New { dictPtr = finder.dictPtr(); } - else if (finder.good()) + else if (finder.isStream()) { - const word csName(finder.ref().stream()); + const word csName(finder.stream()); // Deprecated, unsupported syntax if (error::master()) diff --git a/src/conversion/common/tables/cellTable.C b/src/conversion/common/tables/cellTable.C index 082f845793..ad4cf7929d 100644 --- a/src/conversion/common/tables/cellTable.C +++ b/src/conversion/common/tables/cellTable.C @@ -522,9 +522,9 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds) labelList mapping(identity(this->maxIndex() + 1)); bool remap = false; - forAllConstIters(mapDict, iter) + for (const entry& dEntry : mapDict) { - wordRes patterns(iter().stream()); + wordRes patterns(dEntry.stream()); // find all matches Map matches; @@ -538,14 +538,14 @@ void Foam::cellTable::combine(const dictionary& mapDict, labelList& tableIds) if (matches.size()) { - label targetId = this->findIndex(iter().keyword()); + label targetId = this->findIndex(dEntry.keyword()); - Info<< "combine cellTable: " << iter().keyword(); + Info<< "combine cellTable: " << dEntry.keyword(); if (targetId < 0) { // not found - reuse 1st element but with different name targetId = min(matches.toc()); - operator[](targetId).set("Label", iter().keyword()); + operator[](targetId).set("Label", dEntry.keyword()); Info<< " = ("; } diff --git a/src/mesh/blockMesh/blockMeshTools/blockMeshTools.C b/src/mesh/blockMesh/blockMeshTools/blockMeshTools.C index 350635bc7c..e01d1050b7 100644 --- a/src/mesh/blockMesh/blockMeshTools/blockMeshTools.C +++ b/src/mesh/blockMesh/blockMeshTools/blockMeshTools.C @@ -35,17 +35,11 @@ namespace Foam static inline const Foam::entry* resolveLabel(const entry& e, const label val) { - if (e.isStream()) - { - const tokenList& toks = e.stream(); - - if (!toks.empty() && toks[0].isLabel(val)) - { - return &e; - } - } - - return nullptr; + return + ( + (e.isStream() && e.stream().front().isLabel(val)) + ? &e : nullptr + ); } } // End namespace Foam diff --git a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C index 6073e1e7d7..8384390a1b 100644 --- a/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C +++ b/src/optimisation/adjointOptimisation/adjoint/optimisation/optimisationManager/optimisationManager/optimisationManager.C @@ -323,29 +323,29 @@ Foam::optimisationManager::optimisationManager(fvMesh& mesh) "optimisationDict", mesh.time().system(), mesh, - IOobject::MUST_READ_IF_MODIFIED, + IOobject::READ_MODIFIED, IOobject::NO_WRITE, IOobject::REGISTER ) ), mesh_(mesh), time_(const_cast(mesh.time())), - designVars_ - ( - this->subOrEmptyDict("optimisation").isDict("designVariables") ? - designVariables::New - ( - mesh_, - subDict("optimisation").subDict("designVariables") - ) : - nullptr - ), + designVars_(nullptr), primalSolvers_(), adjointSolverManagers_(), managerType_(get("optimisationManager")), dvUpdate_(nullptr), shouldUpdateDesignVariables_(true) -{} +{ + // The "designVariables" sub-dictionary is optional + const dictionary* designVarsDictPtr = + this->subOrEmptyDict("optimisation").findDict("designVariables"); + + if (designVarsDictPtr) + { + designVars_ = designVariables::New(mesh_, *designVarsDictPtr); + } +} // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * // diff --git a/src/transportModels/interfaceProperties/surfaceTensionModels/surfaceTensionModel/surfaceTensionModelNew.C b/src/transportModels/interfaceProperties/surfaceTensionModels/surfaceTensionModel/surfaceTensionModelNew.C index e6c1f680af..dd68bbb220 100644 --- a/src/transportModels/interfaceProperties/surfaceTensionModels/surfaceTensionModel/surfaceTensionModelNew.C +++ b/src/transportModels/interfaceProperties/surfaceTensionModels/surfaceTensionModel/surfaceTensionModelNew.C @@ -37,9 +37,11 @@ Foam::autoPtr Foam::surfaceTensionModel::New const fvMesh& mesh ) { - if (dict.isDict("sigma")) + const dictionary* sigmaDictPtr = dict.findDict("sigma"); + + if (sigmaDictPtr) { - const dictionary& sigmaDict = surfaceTensionModel::sigmaDict(dict); + const dictionary& sigmaDict = *sigmaDictPtr; const word modelType(sigmaDict.get("type"));