diff --git a/applications/test/IOobjectList/Test-IOobjectList.C b/applications/test/IOobjectList/Test-IOobjectList.C index 38031bee7f..ec7ce76299 100644 --- a/applications/test/IOobjectList/Test-IOobjectList.C +++ b/applications/test/IOobjectList/Test-IOobjectList.C @@ -49,10 +49,31 @@ void reportDetail(const IOobjectList& objects) for (const word& key : objects.sortedNames()) { - IOobject* io = objects.lookup(key); + // Canonical method name (NOV-2018) + IOobject* io = objects.findObject(key); + + label count = 0; + + // Test deprecated alternatives + { + // (const char*) + IOobject* ptr = objects.lookup("SomeNonExistentName"); + if (ptr) ++count; + } + { + // (const word&) + IOobject* ptr = objects.lookup(key); + if (ptr) ++count; + } Info<< key << " (" << io->headerClassName() << ") = addr " << long(io) << nl; + + if (count != 1) + { + Warning + << key << " had incorrect lookup?" << nl; + } } Info<<"====" << nl; diff --git a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C index 01c9d0244b..6b269b4ceb 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C +++ b/applications/utilities/postProcessing/dataConversion/foamToEnsightParts/foamToEnsightParts.C @@ -412,7 +412,7 @@ int main(int argc, char *argv[]) const word& fieldName = fieldIter.key(); const word& fieldType = fieldIter.object(); - IOobject *fieldObject = cloudObjs.lookup(fieldName); + IOobject *fieldObject = cloudObjs.findObject(fieldName); if (!fieldObject) { diff --git a/applications/utilities/postProcessing/lagrangian/steadyParticleTracks/steadyParticleTracksTemplates.C b/applications/utilities/postProcessing/lagrangian/steadyParticleTracks/steadyParticleTracksTemplates.C index 8ac395f249..72123f45e8 100644 --- a/applications/utilities/postProcessing/lagrangian/steadyParticleTracks/steadyParticleTracksTemplates.C +++ b/applications/utilities/postProcessing/lagrangian/steadyParticleTracks/steadyParticleTracksTemplates.C @@ -32,7 +32,7 @@ bool Foam::fieldOk(const IOobjectList& cloudObjs, const word& name) { IOobjectList objects(cloudObjs.lookupClass(IOField::typeName)); - return (objects.lookup(name) != nullptr); + return (objects.findObject(name) != nullptr); } @@ -45,7 +45,7 @@ Foam::tmp> Foam::readParticleField { IOobjectList objects(cloudObjs.lookupClass(IOField::typeName)); - const IOobject* obj = objects.lookup(name); + const IOobject* obj = objects.findObject(name); if (obj != nullptr) { IOField newField(*obj); @@ -72,7 +72,7 @@ void Foam::readFields forAll(fieldNames, j) { - const IOobject* obj = objects.lookup(fieldNames[j]); + const IOobject* obj = objects.findObject(fieldNames[j]); if (obj != nullptr) { Info<< " reading field " << fieldNames[j] << endl; @@ -158,7 +158,7 @@ void Foam::processFields DynamicList fieldNames(objects.size()); forAll(userFieldNames, i) { - IOobject* obj = objects.lookup(userFieldNames[i]); + IOobject* obj = objects.findObject(userFieldNames[i]); if (obj != nullptr) { fieldNames.append(obj->name()); diff --git a/applications/utilities/postProcessing/lumped/lumpedPointForces/lumpedPointForces.C b/applications/utilities/postProcessing/lumped/lumpedPointForces/lumpedPointForces.C index d65a20d8bf..d61edb57ef 100644 --- a/applications/utilities/postProcessing/lumped/lumpedPointForces/lumpedPointForces.C +++ b/applications/utilities/postProcessing/lumped/lumpedPointForces/lumpedPointForces.C @@ -141,7 +141,7 @@ int main(int argc, char *argv[]) // Pressure field autoPtr pField - = loadField(mesh, objects.lookup("p")); + = loadField(mesh, objects.findObject("p")); // The forces per zone if (movement().forcesAndMoments(mesh, forces, moments)) diff --git a/applications/utilities/postProcessing/miscellaneous/profilingSummary/profilingSummary.C b/applications/utilities/postProcessing/miscellaneous/profilingSummary/profilingSummary.C index 357c889d8a..3f7c7aec6b 100644 --- a/applications/utilities/postProcessing/miscellaneous/profilingSummary/profilingSummary.C +++ b/applications/utilities/postProcessing/miscellaneous/profilingSummary/profilingSummary.C @@ -173,7 +173,7 @@ int main(int argc, char *argv[]) "uniform" ); - IOobject* ioptr = objects.lookup(profilingFileName); + IOobject* ioptr = objects.findObject(profilingFileName); if (ioptr) { IOdictionary dict(*ioptr); diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index b96667dc82..4c983fa449 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -25,169 +25,37 @@ License #include "IOobjectList.H" #include "Time.H" -#include "OSspecific.H" #include "IOList.H" #include "predicates.H" +#include "OSspecific.H" -// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -namespace Foam +bool Foam::IOobjectList::checkNames(wordList& masterNames, const bool syncPar) { - // Templated implementation for lookup() - file-scope - template - static IOobjectList lookupImpl - ( - const IOobjectList& list, - const UnaryMatchPredicate& matcher - ) + // Sort for consistent order on all processors + Foam::sort(masterNames); + + if (syncPar && Pstream::parRun()) { - IOobjectList results(list.size()); + const wordList localNames(masterNames); + Pstream::scatter(masterNames); - forAllConstIters(list, iter) + if (localNames != masterNames) { - const word& key = iter.key(); - const IOobject* io = iter.object(); + FatalErrorInFunction + << "Objects not synchronised across processors." << nl + << "Master has " << flatOutput(masterNames) << nl + << "Processor " << Pstream::myProcNo() + << " has " << flatOutput(localNames) + << exit(FatalError); - if (matcher(key)) - { - if (IOobject::debug) - { - InfoInFunction << "Found " << key << endl; - } - - results.set(key, new IOobject(*io)); - } + return false; } - - return results; } - - // Templated implementation for lookupClass() - file-scope - template - static IOobjectList lookupClassImpl - ( - const IOobjectList& list, - const word& clsName, - const UnaryMatchPredicate& matcher - ) - { - IOobjectList results(list.size()); - - forAllConstIters(list, iter) - { - const word& key = iter.key(); - const IOobject* io = iter.object(); - - if (clsName == io->headerClassName() && matcher(key)) - { - if (IOobject::debug) - { - InfoInFunction << "Found " << key << endl; - } - - results.set(key, new IOobject(*io)); - } - } - - return results; - } - - - // Templated implementation for classes() - file-scope - template - static HashTable classesImpl - ( - const IOobjectList& list, - const UnaryMatchPredicate& matcher - ) - { - HashTable summary(2*list.size()); - - // Summary (key,val) = (class-name, object-names) - forAllConstIters(list, iter) - { - const word& key = iter.key(); - const IOobject* io = iter.object(); - - if (matcher(key)) - { - // Create entry (if needed) and insert - summary(io->headerClassName()).insert(key); - } - } - - return summary; - } - - - // Templated implementation for names(), sortedNames() - file-scope - template - static wordList namesImpl - ( - const IOobjectList& list, - const word& clsName, - const UnaryMatchPredicate& matcher, - const bool doSort - ) - { - wordList objNames(list.size()); - - label count = 0; - forAllConstIters(list, iter) - { - const word& key = iter.key(); - const IOobject* io = iter.object(); - - if (clsName == io->headerClassName() && matcher(key)) - { - objNames[count] = key; - ++count; - } - } - - objNames.resize(count); - - if (doSort) - { - Foam::sort(objNames); - } - - return objNames; - } - - - // With syncPar = true, check that object names are the same on - // all processors. Trigger FatalError if not. - // - // The object names are sorted as a side-effect, since this is - // required for consistent ordering across all processors. - static bool checkNames(wordList& masterNames, const bool syncPar) - { - Foam::sort(masterNames); - - if (syncPar && Pstream::parRun()) - { - const wordList localNames(masterNames); - Pstream::scatter(masterNames); - - if (localNames != masterNames) - { - FatalErrorInFunction - << "Objects not synchronised across processors." << nl - << "Master has " << flatOutput(masterNames) << nl - << "Processor " << Pstream::myProcNo() - << " has " << flatOutput(localNames) - << exit(FatalError); - - return false; - } - } - - return true; - } - -} // End namespace Foam + return true; +} // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -354,15 +222,17 @@ bool Foam::IOobjectList::remove(const IOobject& io) } -Foam::IOobject* Foam::IOobjectList::lookup(const word& name) const +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +Foam::IOobject* Foam::IOobjectList::findObject(const word& objName) const { - const_iterator iter = cfind(name); + const_iterator iter = cfind(objName); if (iter.found()) { if (IOobject::debug) { - InfoInFunction << "Found " << name << endl; + InfoInFunction << "Found " << objName << endl; } return const_cast(*iter); @@ -370,27 +240,30 @@ Foam::IOobject* Foam::IOobjectList::lookup(const word& name) const if (IOobject::debug) { - InfoInFunction << "Could not find " << name << endl; + InfoInFunction << "Could not find " << objName << endl; } return nullptr; } -Foam::IOobjectList Foam::IOobjectList::lookup(const wordRe& matcher) const +Foam::IOobjectList Foam::IOobjectList::lookup(const wordRe& matchName) const { - return lookupImpl(*this, matcher); + return lookupImpl(*this, matchName); } -Foam::IOobjectList Foam::IOobjectList::lookup(const wordRes& matcher) const +Foam::IOobjectList Foam::IOobjectList::lookup(const wordRes& matchName) const { - return lookupImpl(*this, matcher); + return lookupImpl(*this, matchName); } -Foam::IOobjectList Foam::IOobjectList::lookup(const wordHashSet& matcher) const +Foam::IOobjectList Foam::IOobjectList::lookup +( + const wordHashSet& matchName +) const { - return lookupImpl(*this, matcher); + return lookupImpl(*this, matchName); } @@ -407,23 +280,23 @@ Foam::HashTable Foam::IOobjectList::classes() const Foam::HashTable -Foam::IOobjectList::classes(const wordRe& matcher) const +Foam::IOobjectList::classes(const wordRe& matchName) const { - return classesImpl(*this, matcher); + return classesImpl(*this, matchName); } Foam::HashTable -Foam::IOobjectList::classes(const wordRes& matcher) const +Foam::IOobjectList::classes(const wordRes& matchName) const { - return classesImpl(*this, matcher); + return classesImpl(*this, matchName); } Foam::HashTable -Foam::IOobjectList::classes(const wordHashSet& matcher) const +Foam::IOobjectList::classes(const wordHashSet& matchName) const { - return classesImpl(*this, matcher); + return classesImpl(*this, matchName); } @@ -482,31 +355,31 @@ Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names ( const word& clsName, - const wordRe& matcher + const wordRe& matchName ) const { - return namesImpl(*this, clsName, matcher, false); + return namesImpl(*this, clsName, matchName, false); } Foam::wordList Foam::IOobjectList::sortedNames ( const word& clsName, - const wordRe& matcher + const wordRe& matchName ) const { - return namesImpl(*this, clsName, matcher, true); + return namesImpl(*this, clsName, matchName, true); } Foam::wordList Foam::IOobjectList::names ( const word& clsName, - const wordRe& matcher, + const wordRe& matchName, const bool syncPar ) const { - wordList objNames(namesImpl(*this, clsName, matcher, false)); + wordList objNames(namesImpl(*this, clsName, matchName, false)); checkNames(objNames, syncPar); return objNames; @@ -516,31 +389,31 @@ Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names ( const word& clsName, - const wordRes& matcher + const wordRes& matchName ) const { - return namesImpl(*this, clsName, matcher, false); + return namesImpl(*this, clsName, matchName, false); } Foam::wordList Foam::IOobjectList::sortedNames ( const word& clsName, - const wordRes& matcher + const wordRes& matchName ) const { - return namesImpl(*this, clsName, matcher, true); + return namesImpl(*this, clsName, matchName, true); } Foam::wordList Foam::IOobjectList::names ( const word& clsName, - const wordRes& matcher, + const wordRes& matchName, const bool syncPar ) const { - wordList objNames(namesImpl(*this, clsName, matcher, false)); + wordList objNames(namesImpl(*this, clsName, matchName, false)); checkNames(objNames, syncPar); return objNames; @@ -550,31 +423,31 @@ Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names ( const word& clsName, - const wordHashSet& matcher + const wordHashSet& matchName ) const { - return namesImpl(*this, clsName, matcher, false); + return namesImpl(*this, clsName, matchName, false); } Foam::wordList Foam::IOobjectList::sortedNames ( const word& clsName, - const wordHashSet& matcher + const wordHashSet& matchName ) const { - return namesImpl(*this, clsName, matcher, true); + return namesImpl(*this, clsName, matchName, true); } Foam::wordList Foam::IOobjectList::names ( const word& clsName, - const wordHashSet& matcher, + const wordHashSet& matchName, const bool syncPar ) const { - wordList objNames(namesImpl(*this, clsName, matcher, false)); + wordList objNames(namesImpl(*this, clsName, matchName, false)); checkNames(objNames, syncPar); return objNames; diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H index f3d206d850..f09d9e11f9 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H @@ -29,6 +29,7 @@ Description SourceFiles IOobjectList.C + IOobjectListTemplates.C \*---------------------------------------------------------------------------*/ @@ -45,11 +46,6 @@ SourceFiles namespace Foam { -// Forward declarations -class IOobjectList; -Ostream& operator<<(Ostream& os, const IOobjectList& list); - - /*---------------------------------------------------------------------------*\ Class IOobjectList Declaration \*---------------------------------------------------------------------------*/ @@ -58,6 +54,53 @@ class IOobjectList : public HashPtrTable { + // Private Member Functions + + //- Check name consistency on all processors + // + // With syncPar = true, check that object names are the same on + // all processors. Trigger FatalError if not. + // + // When syncPar is used, the object names are sorted as a side-effect, + // since this is required for consistent ordering across processors. + static bool checkNames(wordList& masterNames, const bool syncPar); + + //- Templated implementation for classes() + template + static HashTable classesImpl + ( + const IOobjectList& list, + const MatchPredicate& matchName + ); + + //- Templated implementation for names(), sortedNames() + template + static wordList namesImpl + ( + const IOobjectList& list, + const word& matchClass, + const MatchPredicate& matchName, + const bool doSort + ); + + //- Templated implementation for lookup() + template + static IOobjectList lookupImpl + ( + const IOobjectList& list, + const MatchPredicate& matchName + ); + + //- Templated implementation for lookupClass() + template + static IOobjectList lookupClassImpl + ( + const IOobjectList& list, + const word& matchClass, + const MatchPredicate& matchName + ); + + public: // Constructors @@ -90,7 +133,7 @@ public: ~IOobjectList() = default; - // Member functions + // Member Functions // Basic methods @@ -125,19 +168,23 @@ public: bool remove(const IOobject& io); - // Lookup + // Lookup single - //- Lookup a given name and return IOobject ptr if found else nullptr - IOobject* lookup(const word& name) const; + //- Lookup an object by name + // \return IOobject ptr if found else nullptr + IOobject* findObject(const word& objName) const; + + + // Lookup multiple //- The list of all IOobjects with matching names - IOobjectList lookup(const wordRe& matcher) const; + IOobjectList lookup(const wordRe& matchName) const; //- The list of all IOobjects with matching names - IOobjectList lookup(const wordRes& matcher) const; + IOobjectList lookup(const wordRes& matchName) const; //- The list of all IOobjects with matching names - IOobjectList lookup(const wordHashSet& matcher) const; + IOobjectList lookup(const wordHashSet& matchName) const; //- The list of all IOobjects with the given class name IOobjectList lookupClass(const word& clsName) const; @@ -212,40 +259,43 @@ public: // // Of course, there are many other ways to use and manipulate the // summary information. + // + // \return HashTable of class-names for keys and wordHashSet of + // of object-names for the values. HashTable classes() const; - //- A summary hash of classes used and their associated object names - // restricted to objects with names that satisfy the input matcher - HashTable classes(const wordRe& matcher) const; + //- A summary hash of classes used and their associated object names, + //- restricted to objects with names that satisfy the input matcher + HashTable classes(const wordRe& matchName) const; - //- A summary hash of classes used and their associated object names - // restricted to objects with names that satisfy the input matcher - HashTable classes(const wordRes& matcher) const; + //- A summary hash of classes used and their associated object names, + //- restricted to objects with names that satisfy the input matcher + HashTable classes(const wordRes& matchName) const; - //- A summary hash of classes used and their associated object names - // restricted to objects with names that satisfy the input matcher - HashTable classes(const wordHashSet& matcher) const; + //- A summary hash of classes used and their associated object names, + //- restricted to objects with names that satisfy the input matcher + HashTable classes(const wordHashSet& matchName) const; // Summary of names - //- A list of names of the IOobjects + //- The names of the IOobjects wordList names() const; - //- The names of IOobjects with the given class name + //- The names of IOobjects with the given class wordList names(const word& clsName) const; - //- The names of IOobjects with the given class name that also + //- The names of IOobjects with the given class that also //- have a name satisfying the input matcher - wordList names(const word& clsName, const wordRe& matcher) const; + wordList names(const word& clsName, const wordRe& matchName) const; - //- The names of IOobjects with the given class name that also + //- The names of IOobjects with the given class that also //- have a name satisfying the input matcher - wordList names(const word& clsName, const wordRes& matcher) const; + wordList names(const word& clsName, const wordRes& matchName) const; - //- The names of IOobjects with the given class name that also + //- The names of IOobjects with the given class that also //- have a name satisfying the input matcher - wordList names(const word& clsName, const wordHashSet& matcher) const; + wordList names(const word& clsName, const wordHashSet& matchName) const; //- A sorted list of names of the IOobjects. @@ -265,7 +315,7 @@ public: wordList names ( const word& clsName, - const wordRe& matcher, + const wordRe& matchName, const bool syncPar ) const; @@ -276,44 +326,52 @@ public: wordList names ( const word& clsName, - const wordRes& matcher, + const wordRes& matchName, const bool syncPar ) const; - //- The sorted names of IOobjects with the given class name that also + //- The sorted names of IOobjects with the given class that also //- have a name satisfying the input matcher // With syncPar = true, triggers FatalError if the names are // not consistent on all processors. wordList names ( const word& cls, - const wordHashSet& matcher, + const wordHashSet& matchName, const bool syncPar ) const; // Summary of names (sorted) - //- A sorted list of names of the IOobjects + //- The sorted names of the IOobjects wordList sortedNames() const; - //- The sorted names of IOobjects with the given class name + //- The sorted names of IOobjects with the given class wordList sortedNames(const word& clsName) const; - //- The sorted names of IOobjects with the given class name that also - //- have a name satisfying the input matcher - wordList sortedNames(const word& clsName, const wordRe& matcher) const; - - //- The sorted names of IOobjects with the given class name that also - //- have a name satisfying the input matcher - wordList sortedNames(const word& clsName, const wordRes& matcher) const; - - //- The sorted names of IOobjects with the given class name that also + //- The sorted names of IOobjects with the given class that also //- have a name satisfying the input matcher wordList sortedNames ( const word& clsName, - const wordHashSet& matcher + const wordRe& matchName + ) const; + + //- The sorted names of IOobjects with the given class that also + //- have a name satisfying the input matcher + wordList sortedNames + ( + const word& clsName, + const wordRes& matchName + ) const; + + //- The sorted names of IOobjects with the given class that also + //- have a name satisfying the input matcher + wordList sortedNames + ( + const word& clsName, + const wordHashSet& matchName ) const; @@ -326,18 +384,34 @@ public: void operator=(IOobjectList&& list); - // Ostream Operator + // Housekeeping - friend Ostream& operator<<(Ostream& os, const IOobjectList& list); + //- Deprecated(2018-11) Lookup an object by name + // \return IOobject ptr if found else nullptr + // \deprecated(2018-11) use findObject() for non-ambiguous resolution + IOobject* lookup(const word& objName) const + { + return findObject(objName); + } }; +// Ostream operator +Ostream& operator<<(Ostream& os, const IOobjectList& list); + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +#ifdef NoRepository + #include "IOobjectListTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + #endif // ************************************************************************* // diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C new file mode 100644 index 0000000000..d386462442 --- /dev/null +++ b/src/OpenFOAM/db/IOobjectList/IOobjectListTemplates.C @@ -0,0 +1,154 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2018 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "IOobjectList.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +// Templated implementation for classes() +template +Foam::HashTable Foam::IOobjectList::classesImpl +( + const IOobjectList& list, + const MatchPredicate& matchName +) +{ + HashTable summary(2*list.size()); + + // Summary (key,val) = (class-name, object-names) + forAllConstIters(list, iter) + { + const word& key = iter.key(); + const IOobject* io = iter.object(); + + if (matchName(key)) + { + // Create entry (if needed) and insert + summary(io->headerClassName()).insert(key); + } + } + + return summary; +} + + +// Templated implementation for names(), sortedNames() +template +Foam::wordList Foam::IOobjectList::namesImpl +( + const IOobjectList& list, + const word& matchClass, + const MatchPredicate& matchName, + const bool doSort +) +{ + wordList objNames(list.size()); + + label count = 0; + forAllConstIters(list, iter) + { + const word& key = iter.key(); + const IOobject* io = iter.object(); + + if (matchClass(io->headerClassName()) && matchName(key)) + { + objNames[count] = key; + ++count; + } + } + + objNames.setSize(count); + + if (doSort) + { + Foam::sort(objNames); + } + + return objNames; +} + + +// Templated implementation for lookup() +template +Foam::IOobjectList Foam::IOobjectList::lookupImpl +( + const IOobjectList& list, + const MatchPredicate& matchName +) +{ + IOobjectList results(list.size()); + + forAllConstIters(list, iter) + { + const word& key = iter.key(); + const IOobject* io = iter.object(); + + if (matchName(key)) + { + if (IOobject::debug) + { + InfoInFunction << "Found " << key << endl; + } + + results.set(key, new IOobject(*io)); + } + } + + return results; +} + + +// Templated implementation for lookupClass() +template +Foam::IOobjectList Foam::IOobjectList::lookupClassImpl +( + const IOobjectList& list, + const word& matchClass, + const MatchPredicate& matchName +) +{ + IOobjectList results(list.size()); + + forAllConstIters(list, iter) + { + const word& key = iter.key(); + const IOobject* io = iter.object(); + + if (matchClass(io->headerClassName()) && matchName(key)) + { + if (IOobject::debug) + { + InfoInFunction << "Found " << key << endl; + } + + results.set(key, new IOobject(*io)); + } + } + + return results; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index 003518aa97..b91a94d228 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -45,11 +45,7 @@ bool Foam::objectRegistry::parentNotTime() const // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::objectRegistry::objectRegistry -( - const Time& t, - const label nIoObjects -) +Foam::objectRegistry::objectRegistry(const Time& t, const label nObjects) : regIOobject ( @@ -64,7 +60,7 @@ Foam::objectRegistry::objectRegistry ), true // to flag that this is the top-level regIOobject ), - HashTable(nIoObjects), + HashTable(nObjects), time_(t), parent_(t), dbDir_(name()), @@ -72,14 +68,10 @@ Foam::objectRegistry::objectRegistry {} -Foam::objectRegistry::objectRegistry -( - const IOobject& io, - const label nIoObjects -) +Foam::objectRegistry::objectRegistry(const IOobject& io, const label nObjects) : regIOobject(io), - HashTable(nIoObjects), + HashTable(nObjects), time_(io.time()), parent_(io.db()), dbDir_(parent_.dbDir()/local()/name()), @@ -94,17 +86,17 @@ Foam::objectRegistry::objectRegistry Foam::objectRegistry::~objectRegistry() { List myObjects(size()); - label nMyObjects = 0; + label nObjects = 0; for (iterator iter = begin(); iter != end(); ++iter) { - if (iter()->ownedByRegistry()) + if (iter.object()->ownedByRegistry()) { - myObjects[nMyObjects++] = iter(); + myObjects[nObjects++] = iter.object(); } } - for (label i=0; i < nMyObjects; ++i) + for (label i=0; i < nObjects; ++i) { checkOut(*myObjects[i]); } @@ -120,23 +112,23 @@ Foam::HashTable Foam::objectRegistry::classes() const Foam::HashTable -Foam::objectRegistry::classes(const wordRe& matcher) const +Foam::objectRegistry::classes(const wordRe& matchName) const { - return classesImpl(*this, matcher); + return classesImpl(*this, matchName); } Foam::HashTable -Foam::objectRegistry::classes(const wordRes& matcher) const +Foam::objectRegistry::classes(const wordRes& matchName) const { - return classesImpl(*this, matcher); + return classesImpl(*this, matchName); } Foam::HashTable -Foam::objectRegistry::classes(const wordHashSet& matcher) const +Foam::objectRegistry::classes(const wordHashSet& matchName) const { - return classesImpl(*this, matcher); + return classesImpl(*this, matchName); } @@ -225,7 +217,6 @@ Foam::label Foam::objectRegistry::getEvent() const curEvent = 1; event_ = 2; - // No need to reset dependent objects; overflow is now handled // in regIOobject::upToDate } @@ -306,8 +297,8 @@ void Foam::objectRegistry::rename(const word& newName) { regIOobject::rename(newName); - // adjust dbDir_ as well - string::size_type i = dbDir_.rfind('/'); + // Adjust dbDir_ as well + const auto i = dbDir_.rfind('/'); if (i == string::npos) { @@ -322,7 +313,7 @@ void Foam::objectRegistry::rename(const word& newName) bool Foam::objectRegistry::modified() const { - forAllConstIter(HashTable, *this, iter) + for (const_iterator iter = cbegin(); iter != cend(); ++iter) { if (iter()->modified()) { @@ -367,7 +358,7 @@ bool Foam::objectRegistry::writeObject { bool ok = true; - forAllConstIter(HashTable, *this, iter) + for (const_iterator iter = cbegin(); iter != cend(); ++iter) { if (objectRegistry::debug) { diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.H b/src/OpenFOAM/db/objectRegistry/objectRegistry.H index e2d4f20f7d..8a92fbd26e 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.H +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.H @@ -41,6 +41,11 @@ SourceFiles #include "regIOobject.H" #include "wordRes.H" +// Historically included by objectRegistryTemplates (until NOV-2018), +// but not used by objectRegistry directly. +// Leave here for now to avoid a missing include in other bits of code. +#include "stringListOps.H" + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam @@ -77,19 +82,19 @@ class objectRegistry bool parentNotTime() const; //- Templated implementation for classes() - template + template static HashTable classesImpl ( const objectRegistry& list, - const UnaryMatchPredicate& matcher + const MatchPredicate& matchName ); - //- Templated implementation for names() - template - static wordList namesImpl + //- Templated implementation for names(), sortedNames() + template + static wordList namesTypeImpl ( const objectRegistry& list, - const UnaryMatchPredicate& matcher, + const MatchPredicate& matchName, const bool doSort ); @@ -109,28 +114,21 @@ public: // Constructors - //- Construct the time objectRegistry given an initial estimate - // for the number of entries - explicit objectRegistry - ( - const Time& db, - const label nIoObjects = 128 - ); + //- Construct the time objectRegistry, + //- with estimated table capacity (default: 128) + explicit objectRegistry(const Time& db, const label nObjects=128); - //- Construct a sub-registry given an IObject to describe the registry - // and an initial estimate for the number of entries - explicit objectRegistry - ( - const IOobject& io, - const label nIoObjects = 128 - ); + //- Construct sub-registry given an IObject to describe the registry, + //- with estimated table capacity (default: 128) + explicit objectRegistry(const IOobject& io, const label nObjects=128); - //- Destructor + //- Destructor, performs a checkOut() for all objects that are + //- ownedByRegistry virtual ~objectRegistry(); - // Member functions + // Member Functions // Access @@ -161,71 +159,88 @@ public: //- A summary hash of classes used and their associated object names //- restricted to objects with names that satisfy the input matcher - HashTable classes(const wordRe& matcher) const; + HashTable classes(const wordRe& matchName) const; //- A summary hash of classes used and their associated object names //- restricted to objects with names that satisfy the input matcher - HashTable classes(const wordRes& matcher) const; + HashTable classes(const wordRes& matchName) const; //- A summary hash of classes used and their associated object names //- restricted to objects with names that satisfy the input matcher - HashTable classes(const wordHashSet& matcher) const; + HashTable classes(const wordHashSet& matchName) const; // Summary of names - //- A list of names of the objects + //- The names of the objects wordList names() const; - //- The names of objects with the given class name + //- The names of objects with the given class wordList names(const word& clsName) const; - //- The names of objects with the given type + //- The names of objects with a class satisfying \c isA\. + // + // \note If \a Type is \c void, no isA check is used (always true). template wordList names() const; - //- The names of objects with the given type that also - //- have a name satisfying the input matcher + //- The names of objects with a class satisfying \c isA\ + //- that also have a name satisfying the input matcher + // + // \note If \a Type is \c void, no isA check is used (always true). template - wordList names(const wordRe& matcher) const; + wordList names(const wordRe& matchName) const; - //- The names of objects with the given type that also - //- have a name satisfying the input matcher + //- The names of objects with a class satisfying \c isA\ + //- that also have a name satisfying the input matcher + // + // \note If \a Type is \c void, no isA check is used (always true). template - wordList names(const wordRes& matcher) const; + wordList names(const wordRes& matchName) const; - //- The names of objects with the given type that also - //- have a name satisfying the input matcher + //- The names of objects with a class satisfying \c isA\ + //- that also have a name satisfying the input matcher + // + // \note If \a Type is \c void, no isA check is used (always true). template - wordList names(const wordHashSet& matcher) const; + wordList names(const wordHashSet& matchName) const; // Summary of names (sorted) - //- A sorted list of names of the objects + //- The sorted names of the objects wordList sortedNames() const; - //- The sorted names of objects with the given class name + //- The sorted names of objects with the given class wordList sortedNames(const word& clsName) const; - //- The sorted names of objects with the given type + //- The sorted names of objects with a class satisfying \c isA\ + //- that also have a name satisfying the input matcher + // + // \note If \a Type is \c void, no isA check is used (always true). template wordList sortedNames() const; - //- The sorted names of objects with the given type that also - //- have a name satisfying the input matcher + //- The sorted names of objects with a class satisfying \c isA\ + //- that also have a name satisfying the input matcher + // + // \note If \a Type is \c void, no isA check is used (always true). template - wordList sortedNames(const wordRe& matcher) const; + wordList sortedNames(const wordRe& matchName) const; - //- The sorted names of objects with the given type that also - //- have a name satisfying the input matcher + //- The sorted names of objects with a class satisfying \c isA\ + //- that also have a name satisfying the input matcher + // + // \note If \a Type is \c void, no isA check is used (always true). template - wordList sortedNames(const wordRes& matcher) const; + wordList sortedNames(const wordRes& matchName) const; - //- The sorted names of objects with the given type that also - //- have a name satisfying the input matcher + //- The sorted names of objects with a class satisfying \c isA\ + //- that also have a name satisfying the input matcher + // + // \note If \a Type is \c void, no isA check is used (always true). template - wordList sortedNames(const wordHashSet& matcher) const; + wordList sortedNames(const wordHashSet& matchName) const; // Lookup @@ -242,11 +257,15 @@ public: ) const; - //- Lookup and return all objects of the given Type + //- Return all objects with a class satisfying \c isA\ + // + // \param strict use \c isType\ instead of \c isA\ template HashTable lookupClass(const bool strict = false) const; - //- Lookup and return all objects of the given Type + //- Return all objects with a class satisfying \c isA\ + // + // \param strict use \c isType\ instead of \c isA\ template HashTable lookupClass(const bool strict = false); diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C index 6cc9eba56a..20740e07d0 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C @@ -24,17 +24,17 @@ License \*---------------------------------------------------------------------------*/ #include "objectRegistry.H" -#include "stringListOps.H" #include "predicates.H" +#include // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // Templated implementation for classes() -template +template Foam::HashTable Foam::objectRegistry::classesImpl ( const objectRegistry& list, - const UnaryMatchPredicate& matcher + const MatchPredicate& matchName ) { HashTable summary(2*list.size()); @@ -42,7 +42,7 @@ Foam::HashTable Foam::objectRegistry::classesImpl // Summary (key,val) = (class-name, object-names) forAllConstIters(list, iter) { - if (matcher(iter.key())) + if (matchName(iter.key())) { // Create entry (if needed) and insert summary(iter.object()->type()).insert(iter.key()); @@ -53,12 +53,12 @@ Foam::HashTable Foam::objectRegistry::classesImpl } -// Templated implementation for names() -template -Foam::wordList Foam::objectRegistry::namesImpl +// Templated implementation for names(), sortedNames() +template +Foam::wordList Foam::objectRegistry::namesTypeImpl ( const objectRegistry& list, - const UnaryMatchPredicate& matcher, + const MatchPredicate& matchName, const bool doSort ) { @@ -67,14 +67,20 @@ Foam::wordList Foam::objectRegistry::namesImpl label count = 0; forAllConstIters(list, iter) { - if (isA(*iter()) && matcher(iter()->name())) + const regIOobject* obj = iter.object(); + + if + ( + (std::is_void::value || isA(*obj)) + && matchName(obj->name()) + ) { objNames[count] = iter()->name(); ++count; } } - objNames.setSize(count); + objNames.resize(count); if (doSort) { @@ -90,59 +96,59 @@ Foam::wordList Foam::objectRegistry::namesImpl template Foam::wordList Foam::objectRegistry::names() const { - return namesImpl(*this, predicates::always(), false); + return namesTypeImpl(*this, predicates::always(), false); } template -Foam::wordList Foam::objectRegistry::names(const wordRe& matcher) const +Foam::wordList Foam::objectRegistry::names(const wordRe& matchName) const { - return namesImpl(*this, matcher, false); + return namesTypeImpl(*this, matchName, false); } template -Foam::wordList Foam::objectRegistry::names(const wordRes& matcher) const +Foam::wordList Foam::objectRegistry::names(const wordRes& matchName) const { - return namesImpl(*this, matcher, false); + return namesTypeImpl(*this, matchName, false); } template -Foam::wordList Foam::objectRegistry::names(const wordHashSet& matcher) const +Foam::wordList Foam::objectRegistry::names(const wordHashSet& matchName) const { - return namesImpl(*this, matcher, false); + return namesTypeImpl(*this, matchName, false); } template Foam::wordList Foam::objectRegistry::sortedNames() const { - return namesImpl(*this, predicates::always(), true); + return namesTypeImpl(*this, predicates::always(), true); } template -Foam::wordList Foam::objectRegistry::sortedNames(const wordRe& matcher) const +Foam::wordList Foam::objectRegistry::sortedNames(const wordRe& matchName) const { - return namesImpl(*this, matcher, true); + return namesTypeImpl(*this, matchName, true); } template -Foam::wordList Foam::objectRegistry::sortedNames(const wordRes& matcher) const +Foam::wordList Foam::objectRegistry::sortedNames(const wordRes& matchName) const { - return namesImpl(*this, matcher, true); + return namesTypeImpl(*this, matchName, true); } template Foam::wordList Foam::objectRegistry::sortedNames ( - const wordHashSet& matcher + const wordHashSet& matchName ) const { - return namesImpl(*this, matcher, true); + return namesTypeImpl(*this, matchName, true); } diff --git a/src/lumpedPointMotion/lumpedPointTools.C b/src/lumpedPointMotion/lumpedPointTools.C index 1b5a8e4b4f..8fa59c1aed 100644 --- a/src/lumpedPointMotion/lumpedPointTools.C +++ b/src/lumpedPointMotion/lumpedPointTools.C @@ -142,7 +142,7 @@ Foam::labelList Foam::lumpedPointTools::lumpedPointPatchList autoPtr displacePtr = loadPointField ( pMesh, - objects0.lookup("pointDisplacement") + objects0.findObject("pointDisplacement") ); if (!displacePtr.valid())