diff --git a/applications/test/IOobjectList/Make/files b/applications/test/IOobjectList/Make/files new file mode 100644 index 0000000000..c09b00ecaa --- /dev/null +++ b/applications/test/IOobjectList/Make/files @@ -0,0 +1,3 @@ +Test-IOobjectList.C + +EXE = $(FOAM_USER_APPBIN)/Test-IOobjectList diff --git a/applications/test/IOobjectList/Make/options b/applications/test/IOobjectList/Make/options new file mode 100644 index 0000000000..d9745f69a0 --- /dev/null +++ b/applications/test/IOobjectList/Make/options @@ -0,0 +1,3 @@ +EXE_INC = -I$(LIB_SRC)/finiteVolume/lnInclude + +EXE_LIBS = -lfiniteVolume diff --git a/applications/test/IOobjectList/Test-IOobjectList.C b/applications/test/IOobjectList/Test-IOobjectList.C new file mode 100644 index 0000000000..27bd495950 --- /dev/null +++ b/applications/test/IOobjectList/Test-IOobjectList.C @@ -0,0 +1,104 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2017 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 . + +Description + Basic tests of IOobjectList +\*---------------------------------------------------------------------------*/ + +#include "argList.H" +#include "Time.H" +#include "volFields.H" +#include "timeSelector.H" +#include "IOobjectList.H" +#include "hashedWordList.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noParallel(); + argList::addOption("re", "wordReList"); + + // timeSelector::addOptions(); + timeSelector::addOptions(true, true); + + #include "setRootCase.H" + #include "createTime.H" + + wordReList matcher; + if (args.optionFound("re")) + { + matcher = args.optionReadList("re"); + Info<<"limit names: " << matcher << nl; + + } + + const hashedWordList subsetTypes + { + volScalarField::typeName, + volScalarField::Internal::typeName, + volVectorField::typeName, + }; + + + instantList timeDirs = timeSelector::select0(runTime, args); + + forAll(timeDirs, timeI) + { + runTime.setTime(timeDirs[timeI], timeI); + + // Objects at this time + IOobjectList objects(runTime, runTime.timeName()); + HashTable classes = + ( + matcher.size() + ? objects.classes(matcher) + : objects.classes() + ); + + Info<< "Time: " << runTime.timeName() << nl; + + Info<<"Name: " << flatOutput(objects.sortedNames()) << nl + <<"Objects: " << objects << nl + <<"Classes: " << classes << nl; + + classes.filterKeys(subsetTypes); + Info<<"only retain: " << flatOutput(subsetTypes) << nl; + Info<<"Pruned: " << classes << nl; + + classes = objects.classes(); + classes.erase(subsetTypes); + Info<<"remove: " << flatOutput(subsetTypes) << nl; + Info<<"Pruned: " << classes << nl; + } + + Info<< "\nEnd\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.C b/src/OpenFOAM/db/IOobjectList/IOobjectList.C index 14c68e7b76..981e5ca2e5 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.C +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -27,7 +27,99 @@ License #include "Time.H" #include "OSspecific.H" #include "IOList.H" -#include "stringListOps.H" +#include "predicates.H" + +// * * * * * * * * * * * * * * Static Functions * * * * * * * * * * * * * // + +namespace Foam +{ + // Templated implementation for lookup() - file-scope + template + static IOobjectList lookupImpl + ( + const IOobjectList& list, + const UnaryMatchPredicate& matcher + ) + { + IOobjectList results(list.size()); + + forAllConstIters(list, iter) + { + if (matcher(iter.key())) + { + if (IOobject::debug) + { + InfoInFunction << "Found " << iter.key() << endl; + } + + results.insert + ( + iter.key(), + new IOobject(*(iter.object())) + ); + } + } + + 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) + { + if (matcher(iter.key())) + { + // Create entry (if needed) and insert + summary(iter.object()->headerClassName()).insert(iter.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) + { + if (iter()->headerClassName() == clsName && matcher(iter.key())) + { + objNames[count++] = iter.key(); + } + } + + objNames.setSize(count); + + if (doSort) + { + Foam::sort(objNames); + } + + return objNames; + } +} + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -62,14 +154,14 @@ Foam::IOobjectList::IOobjectList } // Create a list of file names in this directory - fileNameList ObjectNames = + fileNameList objNames = readDir(db.path(newInstance, db.dbDir()/local), fileName::FILE); - forAll(ObjectNames, i) + forAll(objNames, i) { IOobject* objectPtr = new IOobject ( - ObjectNames[i], + objNames[i], newInstance, local, db, @@ -81,7 +173,7 @@ Foam::IOobjectList::IOobjectList // Use object with local scope if (objectPtr->typeHeaderOk>(false)) { - insert(ObjectNames[i], objectPtr); + insert(objectPtr->name(), objectPtr); } else { @@ -119,7 +211,7 @@ bool Foam::IOobjectList::remove(IOobject& io) Foam::IOobject* Foam::IOobjectList::lookup(const word& name) const { - HashPtrTable::const_iterator iter = find(name); + const_iterator iter = find(name); if (iter.found()) { @@ -144,53 +236,13 @@ Foam::IOobject* Foam::IOobjectList::lookup(const word& name) const Foam::IOobjectList Foam::IOobjectList::lookup(const wordRe& matcher) const { - IOobjectList results(size()); - - forAllConstIters(*this, iter) - { - if (matcher.match(iter.key())) - { - if (IOobject::debug) - { - InfoInFunction << "Found " << iter.key() << endl; - } - - results.insert - ( - iter.key(), - new IOobject(*(iter.object())) - ); - } - } - - return results; + return lookupImpl(*this, matcher); } -Foam::IOobjectList Foam::IOobjectList::lookup(const wordReList& matcher) const +Foam::IOobjectList Foam::IOobjectList::lookup(const wordRes& matcher) const { - wordRes mat(matcher); - - IOobjectList results(size()); - - forAllConstIters(*this, iter) - { - if (mat.match(iter.key())) - { - if (IOobject::debug) - { - InfoInFunction << "Found " << iter.key() << endl; - } - - results.insert - ( - iter.key(), - new IOobject(*(iter.object())) - ); - } - } - - return results; + return lookupImpl(*this, matcher); } @@ -219,6 +271,26 @@ Foam::IOobjectList Foam::IOobjectList::lookupClass(const word& clsName) const } +Foam::HashTable Foam::IOobjectList::classes() const +{ + return classesImpl(*this, predicates::always()); +} + + +Foam::HashTable +Foam::IOobjectList::classes(const wordRe& matcher) const +{ + return classesImpl(*this, matcher); +} + + +Foam::HashTable +Foam::IOobjectList::classes(const wordRes& matcher) const +{ + return classesImpl(*this, matcher); +} + + Foam::wordList Foam::IOobjectList::names() const { return HashPtrTable::toc(); @@ -236,20 +308,7 @@ Foam::wordList Foam::IOobjectList::names const word& clsName ) const { - wordList objNames(size()); - - label count = 0; - forAllConstIters(*this, iter) - { - if (iter()->headerClassName() == clsName) - { - objNames[count++] = iter.key(); - } - } - - objNames.setSize(count); - - return objNames; + return namesImpl(*this, clsName, predicates::always(), false); } @@ -259,21 +318,17 @@ Foam::wordList Foam::IOobjectList::names const wordRe& matcher ) const { - wordList objNames = names(clsName); - - return wordList(objNames, findStrings(matcher, objNames)); + return namesImpl(*this, clsName, matcher, false); } Foam::wordList Foam::IOobjectList::names ( const word& clsName, - const wordReList& matcher + const wordRes& matcher ) const { - wordList objNames = names(clsName); - - return wordList(objNames, findStrings(matcher, objNames)); + return namesImpl(*this, clsName, matcher, false); } @@ -282,10 +337,7 @@ Foam::wordList Foam::IOobjectList::sortedNames const word& clsName ) const { - wordList sortedLst = names(clsName); - sort(sortedLst); - - return sortedLst; + return namesImpl(*this, clsName, predicates::always(), true); } @@ -295,23 +347,35 @@ Foam::wordList Foam::IOobjectList::sortedNames const wordRe& matcher ) const { - wordList sortedLst = names(clsName, matcher); - sort(sortedLst); - - return sortedLst; + return namesImpl(*this, clsName, matcher, true); } Foam::wordList Foam::IOobjectList::sortedNames ( const word& clsName, - const wordReList& matcher + const wordRes& matcher ) const { - wordList sortedLst = names(clsName, matcher); - sort(sortedLst); + return namesImpl(*this, clsName, matcher, true); +} - return sortedLst; + +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +Foam::Ostream& Foam::operator<<(Ostream& os, const IOobjectList& list) +{ + os << nl << list.size() << nl << token::BEGIN_LIST << nl; + + forAllConstIters(list, it) + { + os << it.key() << token::SPACE << it.object()->headerClassName() << nl; + } + + os << token::END_LIST; + os.check(FUNCTION_NAME); + + return os; } diff --git a/src/OpenFOAM/db/IOobjectList/IOobjectList.H b/src/OpenFOAM/db/IOobjectList/IOobjectList.H index 17f75233ed..0976e1540f 100644 --- a/src/OpenFOAM/db/IOobjectList/IOobjectList.H +++ b/src/OpenFOAM/db/IOobjectList/IOobjectList.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,14 +36,20 @@ SourceFiles #define IOobjectList_H #include "HashPtrTable.H" +#include "HashSet.H" #include "IOobject.H" -#include "wordReList.H" +#include "wordRes.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +// Forward declaration of friend functions and operators +class IOobjectList; +Ostream& operator<<(Ostream& os, const IOobjectList& list); + + /*---------------------------------------------------------------------------*\ Class IOobjectList Declaration \*---------------------------------------------------------------------------*/ @@ -86,12 +92,17 @@ public: // Member functions + // Basic methods + //- Add an IOobject to the list bool add(IOobject& io); //- Remove an IOobject from the list bool remove(IOobject& io); + + // Lookup + //- Lookup a given name and return IOobject ptr if found else nullptr IOobject* lookup(const word& name) const; @@ -99,12 +110,94 @@ public: IOobjectList lookup(const wordRe& matcher) const; //- The list of all IOobjects with matching names - IOobjectList lookup(const wordReList& matcher) const; + IOobjectList lookup(const wordRes& matcher) const; //- The list of all IOobjects with the given class name IOobjectList lookupClass(const word& clsName) const; + // Summary of classes + + //- A summary hash of classes used and their associated object names. + // The HashTable representation allows us to leverage various + // HashTable methods. + // This hashed summary view can be useful when querying particular + // aspects. For example, + // + // \code + // IOobjectList objects(runTime, runTime.timeName()); + // HashTable classes = objects.classes(); + // + // // How many volScalarField? + // word checkType = volScalarField::typeName; + // + // Info<< checkType << "=" + // << (classes.found(checkType) ? classes[checkType].size() : 0) + // << nl; + // \endcode + // Using the two-parameter HashTable::lookup method lets us avoid + // the \c '?' ternary, but still looks fairly ugly: + // \code + // Info<< checkType << "=" + // << classes.lookup(checkType, wordHashSet()).size() << nl; + // \endcode + // + // If we have non-const access to the hash table, and don't mind + // incidentally creating empty entries, + // we can use the HashTable::operator() directly: + // \code + // Info<< checkType << "=" << classes(checkType).size() << nl; + // \endcode + // + // Of course, for a single query it would have been easier + // and simpler to have used a direct query of the names: + // \code + // Info<< checkType << "=" << objects.names(checkType).size() << nl; + // \endcode + // + // The summary hash, however, becomes most useful when reducing + // the objects in consideration to a particular subset. For example, + // \code + // const wordHashSet interestingTypes + // { + // volScalarField::typeName, + // volVectorField::typeName + // }; + // + // classes.retain(interestingTypes); + // \endcode + // Or do just the opposite: + // \code + // classes.erase(unsupportedTypes); + // \endcode + // This also works with a hashedWordList, since it provides the + // expected '()' operator. But in this case the more general + // HashTable::filterKeys is required: + // \code + // const hashedWordList interestingTypes + // { + // volScalarField::typeName, + // volVectorField::typeName + // }; + // + // classes.filterKeys(interestingTypes); + // \endcode + // + // Of course, there are many other ways to use and manipulate the + // summary information. + 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 wordRes& matcher) const; + + + // Summary of names + //- A list of names of the IOobjects wordList names() const; @@ -117,9 +210,11 @@ public: //- The names of IOobjects with the given class name that also // have a name satisfying the input matcher - wordList names(const word& clsName, const wordReList& matcher) const; + wordList names(const word& clsName, const wordRes& matcher) const; + // Summary of names (sorted) + //- A sorted list of names of the IOobjects wordList sortedNames() const; @@ -132,11 +227,12 @@ public: //- 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 wordReList& matcher - ) const; + wordList sortedNames(const word& clsName, const wordRes& matcher) const; + + + // Ostream Operator + + friend Ostream& operator<<(Ostream& os, const IOobjectList& list); }; diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.C b/src/OpenFOAM/db/objectRegistry/objectRegistry.C index d72efda11c..2f048ea71c 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2015-2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,6 +25,7 @@ License #include "objectRegistry.H" #include "Time.H" +#include "predicates.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // @@ -103,7 +104,7 @@ Foam::objectRegistry::~objectRegistry() } } - for (label i=0; i < nMyObjects; i++) + for (label i=0; i < nMyObjects; ++i) { checkOut(*myObjects[i]); } @@ -112,6 +113,26 @@ Foam::objectRegistry::~objectRegistry() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +Foam::HashTable Foam::objectRegistry::classes() const +{ + return classesImpl(*this, predicates::always()); +} + + +Foam::HashTable +Foam::objectRegistry::classes(const wordRe& matcher) const +{ + return classesImpl(*this, matcher); +} + + +Foam::HashTable +Foam::objectRegistry::classes(const wordRes& matcher) const +{ + return classesImpl(*this, matcher); +} + + Foam::wordList Foam::objectRegistry::names() const { return HashTable::toc(); @@ -124,31 +145,31 @@ Foam::wordList Foam::objectRegistry::sortedNames() const } -Foam::wordList Foam::objectRegistry::names(const word& ClassName) const +Foam::wordList Foam::objectRegistry::names(const word& clsName) const { - wordList objectNames(size()); + wordList objNames(size()); label count=0; for (const_iterator iter = cbegin(); iter != cend(); ++iter) { - if (iter()->type() == ClassName) + if (iter()->type() == clsName) { - objectNames[count++] = iter.key(); + objNames[count++] = iter.key(); } } - objectNames.setSize(count); + objNames.setSize(count); - return objectNames; + return objNames; } -Foam::wordList Foam::objectRegistry::sortedNames(const word& ClassName) const +Foam::wordList Foam::objectRegistry::sortedNames(const word& clsName) const { - wordList sortedLst = names(ClassName); - sort(sortedLst); + wordList objNames = names(clsName); + Foam::sort(objNames); - return sortedLst; + return objNames; } @@ -224,7 +245,7 @@ bool Foam::objectRegistry::checkOut(regIOobject& io) const { iterator iter = const_cast(*this).find(io.name()); - if (iter != end()) + if (iter.found()) { if (objectRegistry::debug) { diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistry.H b/src/OpenFOAM/db/objectRegistry/objectRegistry.H index 16dc0e1778..77d390754f 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistry.H +++ b/src/OpenFOAM/db/objectRegistry/objectRegistry.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -37,8 +37,9 @@ SourceFiles #define objectRegistry_H #include "HashTable.H" +#include "HashSet.H" #include "regIOobject.H" -#include "wordReList.H" +#include "wordRes.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -75,6 +76,24 @@ class objectRegistry // Used to terminate searching within the ancestors bool parentNotTime() const; + //- Templated implementation for classes() + template + static HashTable classesImpl + ( + const objectRegistry& list, + const UnaryMatchPredicate& matcher + ); + + //- Templated implementation for names() + template + static wordList namesImpl + ( + const objectRegistry& list, + const UnaryMatchPredicate& matcher, + const bool doSort + ); + + //- Disallow Copy constructor objectRegistry(const objectRegistry&) = delete; @@ -113,184 +132,209 @@ public: // Member functions - // Access + // Access - //- Return time - const Time& time() const - { - return time_; - } + //- Return time + const Time& time() const + { + return time_; + } - //- Return the parent objectRegistry - const objectRegistry& parent() const - { - return parent_; - } + //- Return the parent objectRegistry + const objectRegistry& parent() const + { + return parent_; + } - //- Local directory path of this objectRegistry relative to the time - virtual const fileName& dbDir() const - { - return dbDir_; - } - - //- A list of names of the objects - wordList names() const; - - //- A sorted list of names of the objects - wordList sortedNames() const; - - //- A list of names of objects that have the given class name - wordList names(const word& className) const; - - //- A sorted list of names of objects that have the given class name - wordList sortedNames(const word& className) const; - - //- A list of names of objects that have the given type - template - wordList names() const; - - //- A list of names of objects that have the given type, - // and that also satisfy the input matcher - template - wordList names(const wordRe&) const; - - //- A list of names for objects that have the given type, - // and that also satisfy the input matchers - template - wordList names(const wordReList&) const; - - //- A sorted list of names of objects that have the given type - template - wordList sortedNames() const; - - //- A sorted list of names of objects that have the given type, - // and that also satisfy the input matcher - template - wordList sortedNames(const wordRe&) const; - - //- A sorted list of names of objects that have the given type, - // and that also satisfy the input matchers - template - wordList sortedNames(const wordReList&) const; + //- Local directory path of this objectRegistry relative to the time + virtual const fileName& dbDir() const + { + return dbDir_; + } - //- Lookup and return a const sub-objectRegistry. - // Optionally create it if it does not exist. - // If recursive, search parent registries. - const objectRegistry& subRegistry - ( - const word& name, - const bool forceCreate = false, - const bool recursive = false - ) const; + // Summary of classes + + //- A summary hash of classes used and their associated object names. + // Behaviour and usage as per IOobjectList::classes + 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 wordRes& matcher) const; - //- Lookup and return all objects of the given Type - template - HashTable lookupClass(const bool strict = false) const; + // Summary of names - //- Lookup and return all objects of the given Type - template - HashTable lookupClass(const bool strict = false); + //- A list of names of the objects + wordList names() const; - //- Is the named Type found? - // If recursive, search parent registries. - template - bool foundObject - ( - const word& name, - const bool recursive = false - ) const; + //- The names of objects with the given class name + wordList names(const word& clsName) const; - //- Lookup and return the object of the given Type. - // If recursive, search parent registries. - template - const Type& lookupObject - ( - const word& name, - const bool recursive = false - ) const; + //- The names of objects with the given type + template + wordList names() const; - //- Lookup and return the object of the given Type. - // If recursive, search parent registries. - template - Type& lookupObjectRef - ( - const word& name, - const bool recursive = false - ) const; + //- The names of objects with the given type that also + // have a name satisfying the input matcher + template + wordList names(const wordRe& matcher) const; - //- Lookup and return pointer to the object of the given Type, - // otherwise nullptr if the object was not found, - // or had the incorrect type. - // If recursive, search parent registries. - template - const Type* lookupObjectPtr - ( - const word& name, - const bool recursive = false - ) const; + //- The names of objects with the given type that also + // have a name satisfying the input matcher + template + wordList names(const wordRes& matcher) const; - //- Lookup and return non-const pointer to the object - // of the given Type, - // otherwise nullptr if the object was not found, - // or had the incorrect type. - // If recursive, search parent registries. - template - Type* lookupObjectRefPtr - ( - const word& name, - const bool recursive = false - ) const; + // Summary of names (sorted) + + //- A sorted list of names of the objects + wordList sortedNames() const; + + //- The sorted names of objects with the given class name + wordList sortedNames(const word& clsName) const; + + //- The sorted names of objects with the given type + template + wordList sortedNames() const; + + //- The sorted names of objects with the given type that also + // have a name satisfying the input matcher + template + wordList sortedNames(const wordRe& matcher) const; + + //- The sorted names of objects with the given type that also + // have a name satisfying the input matcher + template + wordList sortedNames(const wordRes& matcher) const; - //- Return new event number. - label getEvent() const; + // Lookup + + //- Lookup and return a const sub-objectRegistry. + // Optionally create it if it does not exist. + // If recursive, search parent registries. + const objectRegistry& subRegistry + ( + const word& name, + const bool forceCreate = false, + const bool recursive = false + ) const; - // Edit + //- Lookup and return all objects of the given Type + template + HashTable lookupClass(const bool strict = false) const; - //- Rename - virtual void rename(const word& newName); + //- Lookup and return all objects of the given Type + template + HashTable lookupClass(const bool strict = false); - //- Add an regIOobject to registry - bool checkIn(regIOobject&) const; + //- Is the named Type found? + // If recursive, search parent registries. + template + bool foundObject + ( + const word& name, + const bool recursive = false + ) const; - //- Remove an regIOobject from registry - bool checkOut(regIOobject&) const; + //- Lookup and return the object of the given Type. + // If recursive, search parent registries. + template + const Type& lookupObject + ( + const word& name, + const bool recursive = false + ) const; + + //- Lookup and return the object of the given Type. + // If recursive, search parent registries. + template + Type& lookupObjectRef + ( + const word& name, + const bool recursive = false + ) const; + + //- Lookup and return pointer to the object of the given Type, + // otherwise nullptr if the object was not found, + // or had the incorrect type. + // If recursive, search parent registries. + template + const Type* lookupObjectPtr + ( + const word& name, + const bool recursive = false + ) const; - // Reading - - //- Return true if any of the object's files have been modified - virtual bool modified() const; - - //- Read the objects that have been modified - void readModifiedObjects(); - - //- Read object if modified - virtual bool readIfModified(); + //- Lookup and return non-const pointer to the object + // of the given Type, + // otherwise nullptr if the object was not found, + // or had the incorrect type. + // If recursive, search parent registries. + template + Type* lookupObjectRefPtr + ( + const word& name, + const bool recursive = false + ) const; - // Writing + // Events - //- writeData function required by regIOobject but not used - // for this class, write is used instead - virtual bool writeData(Ostream&) const - { - NotImplemented; + //- Return new event number. + label getEvent() const; - return false; - } - //- Write the objects - virtual bool writeObject - ( - IOstream::streamFormat fmt, - IOstream::versionNumber ver, - IOstream::compressionType cmp - ) const; + // Edit + + //- Rename + virtual void rename(const word& newName); + + //- Add an regIOobject to registry + bool checkIn(regIOobject& io) const; + + //- Remove an regIOobject from registry + bool checkOut(regIOobject& io) const; + + + // Reading + + //- Return true if any of the object's files have been modified + virtual bool modified() const; + + //- Read the objects that have been modified + void readModifiedObjects(); + + //- Read object if modified + virtual bool readIfModified(); + + + // Writing + + //- writeData function required by regIOobject but not used + // for this class, write is used instead + virtual bool writeData(Ostream&) const + { + NotImplemented; + + return false; + } + + //- Write the objects + virtual bool writeObject + ( + IOstream::streamFormat fmt, + IOstream::versionNumber ver, + IOstream::compressionType cmp + ) const; }; diff --git a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C index c5c583ad26..040b05f6a3 100644 --- a/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C +++ b/src/OpenFOAM/db/objectRegistry/objectRegistryTemplates.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -25,95 +25,115 @@ License #include "objectRegistry.H" #include "stringListOps.H" +#include "predicates.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +// Templated implementation for classes() +template +Foam::HashTable Foam::objectRegistry::classesImpl +( + const objectRegistry& list, + const UnaryMatchPredicate& matcher +) +{ + HashTable summary(2*list.size()); + + // Summary (key,val) = (class-name, object-names) + forAllConstIters(list, iter) + { + if (matcher(iter.key())) + { + // Create entry (if needed) and insert + summary(iter.object()->type()).insert(iter.key()); + } + } + + return summary; +} + + +// Templated implementation for names() +template +Foam::wordList Foam::objectRegistry::namesImpl +( + const objectRegistry& list, + const UnaryMatchPredicate& matcher, + const bool doSort +) +{ + wordList objNames(list.size()); + + label count = 0; + forAllConstIters(list, iter) + { + if (isA(*iter()) && matcher(iter()->name())) + { + objNames[count++] = iter()->name(); + } + } + + objNames.setSize(count); + + if (doSort) + { + Foam::sort(objNames); + } + + return objNames; +} + // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template Foam::wordList Foam::objectRegistry::names() const { - wordList objectNames(size()); - - label count=0; - forAllConstIter(HashTable, *this, iter) - { - if (isA(*iter())) - { - objectNames[count++] = iter()->name(); - } - } - - objectNames.setSize(count); - - return objectNames; + return namesImpl(*this, predicates::always(), false); } template Foam::wordList Foam::objectRegistry::names(const wordRe& matcher) const { - wordList objectNames(size()); - - label count = 0; - forAllConstIter(HashTable, *this, iter) - { - if (isA(*iter())) - { - const word& objectName = iter()->name(); - - if (matcher.match(objectName)) - { - objectNames[count++] = objectName; - } - } - } - - objectNames.setSize(count); - - return objectNames; + return namesImpl(*this, matcher, false); } template -Foam::wordList Foam::objectRegistry::names(const wordReList& matcher) const +Foam::wordList Foam::objectRegistry::names +( + const wordRes& matcher +) const { - wordList names(this->names()); - - return wordList(names, findStrings(matcher, names)); + return namesImpl(*this, matcher, false); } template Foam::wordList Foam::objectRegistry::sortedNames() const { - wordList sorted(this->names()); - sort(sorted); - - return sorted; -} - -template -Foam::wordList Foam::objectRegistry::sortedNames -( - const wordRe& match -) const -{ - wordList sorted(this->names(match)); - sort(sorted); - - return sorted; + return namesImpl(*this, predicates::always(), true); } template Foam::wordList Foam::objectRegistry::sortedNames ( - const wordReList& matcher + const wordRe& matcher ) const { - wordList sorted(this->names(matcher)); - sort(sorted); + return namesImpl(*this, matcher, true); +} - return sorted; + +template +Foam::wordList Foam::objectRegistry::sortedNames +( + const wordRes& matcher +) const +{ + return namesImpl(*this, matcher, true); } @@ -125,7 +145,7 @@ Foam::HashTable Foam::objectRegistry::lookupClass { HashTable objectsOfClass(size()); - forAllConstIter(HashTable, *this, iter) + forAllConstIters(*this, iter) { if (strict ? isType(*iter()) : isA(*iter())) { @@ -149,7 +169,7 @@ Foam::HashTable Foam::objectRegistry::lookupClass { HashTable objectsOfClass(size()); - forAllIter(HashTable, *this, iter) + forAllIters(*this, iter) { if (strict ? isType(*iter()) : isA(*iter())) { @@ -194,7 +214,7 @@ const Type& Foam::objectRegistry::lookupObject { const_iterator iter = find(name); - if (iter != end()) + if (iter.found()) { const Type* ptr = dynamic_cast(iter()); @@ -252,7 +272,7 @@ const Type* Foam::objectRegistry::lookupObjectPtr { const_iterator iter = find(name); - if (iter != end()) + if (iter.found()) { const Type* ptr = dynamic_cast(iter()); diff --git a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C index 94f1c23a75..066c9c268f 100644 --- a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -47,23 +47,23 @@ void Foam::ZoneMesh::calcZoneMap() const // Count number of objects in all zones label nObjects = 0; - forAll(*this, zoneI) + forAll(*this, zonei) { - nObjects += this->operator[](zoneI).size(); + nObjects += this->operator[](zonei).size(); } zoneMapPtr_ = new Map