ENH: add IOobjectList::findObject() method

- naming similar to objectRegistry, with unambiguous resolution.
  The lookup() methods have different return types depending on the
  calling parameter.

STYLE: use IOobjectListTemplates.C for implementations

- previously included as local definition within IOobjectList.C,
  but will be adding more templated methods soon.

- adjust parameters (eg, matchName instead of matcher) to show their
  function

ENH: handle objectRegistry::names<void>(...)

- this is equivalent to no Type restriction, and can be used when
  filtering names. Eg,

     obr.names<void>(wordRe..);
This commit is contained in:
Mark Olesen
2018-11-09 21:57:55 +01:00
parent ffec4c6fa7
commit faaa93fdb5
12 changed files with 481 additions and 343 deletions

View File

@ -49,10 +49,31 @@ void reportDetail(const IOobjectList& objects)
for (const word& key : objects.sortedNames()) 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() Info<< key << " (" << io->headerClassName()
<< ") = addr " << long(io) << nl; << ") = addr " << long(io) << nl;
if (count != 1)
{
Warning
<< key << " had incorrect lookup?" << nl;
}
} }
Info<<"====" << nl; Info<<"====" << nl;

View File

@ -412,7 +412,7 @@ int main(int argc, char *argv[])
const word& fieldName = fieldIter.key(); const word& fieldName = fieldIter.key();
const word& fieldType = fieldIter.object(); const word& fieldType = fieldIter.object();
IOobject *fieldObject = cloudObjs.lookup(fieldName); IOobject *fieldObject = cloudObjs.findObject(fieldName);
if (!fieldObject) if (!fieldObject)
{ {

View File

@ -32,7 +32,7 @@ bool Foam::fieldOk(const IOobjectList& cloudObjs, const word& name)
{ {
IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName)); IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
return (objects.lookup(name) != nullptr); return (objects.findObject(name) != nullptr);
} }
@ -45,7 +45,7 @@ Foam::tmp<Foam::Field<Type>> Foam::readParticleField
{ {
IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName)); IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
const IOobject* obj = objects.lookup(name); const IOobject* obj = objects.findObject(name);
if (obj != nullptr) if (obj != nullptr)
{ {
IOField<Type> newField(*obj); IOField<Type> newField(*obj);
@ -72,7 +72,7 @@ void Foam::readFields
forAll(fieldNames, j) forAll(fieldNames, j)
{ {
const IOobject* obj = objects.lookup(fieldNames[j]); const IOobject* obj = objects.findObject(fieldNames[j]);
if (obj != nullptr) if (obj != nullptr)
{ {
Info<< " reading field " << fieldNames[j] << endl; Info<< " reading field " << fieldNames[j] << endl;
@ -158,7 +158,7 @@ void Foam::processFields
DynamicList<word> fieldNames(objects.size()); DynamicList<word> fieldNames(objects.size());
forAll(userFieldNames, i) forAll(userFieldNames, i)
{ {
IOobject* obj = objects.lookup(userFieldNames[i]); IOobject* obj = objects.findObject(userFieldNames[i]);
if (obj != nullptr) if (obj != nullptr)
{ {
fieldNames.append(obj->name()); fieldNames.append(obj->name());

View File

@ -141,7 +141,7 @@ int main(int argc, char *argv[])
// Pressure field // Pressure field
autoPtr<volScalarField> pField autoPtr<volScalarField> pField
= loadField<volScalarField>(mesh, objects.lookup("p")); = loadField<volScalarField>(mesh, objects.findObject("p"));
// The forces per zone // The forces per zone
if (movement().forcesAndMoments(mesh, forces, moments)) if (movement().forcesAndMoments(mesh, forces, moments))

View File

@ -173,7 +173,7 @@ int main(int argc, char *argv[])
"uniform" "uniform"
); );
IOobject* ioptr = objects.lookup(profilingFileName); IOobject* ioptr = objects.findObject(profilingFileName);
if (ioptr) if (ioptr)
{ {
IOdictionary dict(*ioptr); IOdictionary dict(*ioptr);

View File

@ -25,145 +25,15 @@ License
#include "IOobjectList.H" #include "IOobjectList.H"
#include "Time.H" #include "Time.H"
#include "OSspecific.H"
#include "IOList.H" #include "IOList.H"
#include "predicates.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<class UnaryMatchPredicate>
static IOobjectList lookupImpl
(
const IOobjectList& list,
const UnaryMatchPredicate& matcher
)
{
IOobjectList results(list.size());
forAllConstIters(list, iter)
{
const word& key = iter.key();
const IOobject* io = iter.object();
if (matcher(key))
{
if (IOobject::debug)
{
InfoInFunction << "Found " << key << endl;
}
results.set(key, new IOobject(*io));
}
}
return results;
}
// Templated implementation for lookupClass() - file-scope
template<class UnaryMatchPredicate>
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<class UnaryMatchPredicate>
static HashTable<wordHashSet> classesImpl
(
const IOobjectList& list,
const UnaryMatchPredicate& matcher
)
{
HashTable<wordHashSet> 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<class UnaryMatchPredicate>
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)
{ {
// Sort for consistent order on all processors
Foam::sort(masterNames); Foam::sort(masterNames);
if (syncPar && Pstream::parRun()) if (syncPar && Pstream::parRun())
@ -187,8 +57,6 @@ namespace Foam
return true; return true;
} }
} // End namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * 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 (iter.found())
{ {
if (IOobject::debug) if (IOobject::debug)
{ {
InfoInFunction << "Found " << name << endl; InfoInFunction << "Found " << objName << endl;
} }
return const_cast<IOobject*>(*iter); return const_cast<IOobject*>(*iter);
@ -370,27 +240,30 @@ Foam::IOobject* Foam::IOobjectList::lookup(const word& name) const
if (IOobject::debug) if (IOobject::debug)
{ {
InfoInFunction << "Could not find " << name << endl; InfoInFunction << "Could not find " << objName << endl;
} }
return nullptr; 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::wordHashSet> Foam::IOobjectList::classes() const
Foam::HashTable<Foam::wordHashSet> Foam::HashTable<Foam::wordHashSet>
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::wordHashSet> Foam::HashTable<Foam::wordHashSet>
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::wordHashSet> Foam::HashTable<Foam::wordHashSet>
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 Foam::wordList Foam::IOobjectList::names
( (
const word& clsName, const word& clsName,
const wordRe& matcher const wordRe& matchName
) const ) const
{ {
return namesImpl(*this, clsName, matcher, false); return namesImpl(*this, clsName, matchName, false);
} }
Foam::wordList Foam::IOobjectList::sortedNames Foam::wordList Foam::IOobjectList::sortedNames
( (
const word& clsName, const word& clsName,
const wordRe& matcher const wordRe& matchName
) const ) const
{ {
return namesImpl(*this, clsName, matcher, true); return namesImpl(*this, clsName, matchName, true);
} }
Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names
( (
const word& clsName, const word& clsName,
const wordRe& matcher, const wordRe& matchName,
const bool syncPar const bool syncPar
) const ) const
{ {
wordList objNames(namesImpl(*this, clsName, matcher, false)); wordList objNames(namesImpl(*this, clsName, matchName, false));
checkNames(objNames, syncPar); checkNames(objNames, syncPar);
return objNames; return objNames;
@ -516,31 +389,31 @@ Foam::wordList Foam::IOobjectList::names
Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names
( (
const word& clsName, const word& clsName,
const wordRes& matcher const wordRes& matchName
) const ) const
{ {
return namesImpl(*this, clsName, matcher, false); return namesImpl(*this, clsName, matchName, false);
} }
Foam::wordList Foam::IOobjectList::sortedNames Foam::wordList Foam::IOobjectList::sortedNames
( (
const word& clsName, const word& clsName,
const wordRes& matcher const wordRes& matchName
) const ) const
{ {
return namesImpl(*this, clsName, matcher, true); return namesImpl(*this, clsName, matchName, true);
} }
Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names
( (
const word& clsName, const word& clsName,
const wordRes& matcher, const wordRes& matchName,
const bool syncPar const bool syncPar
) const ) const
{ {
wordList objNames(namesImpl(*this, clsName, matcher, false)); wordList objNames(namesImpl(*this, clsName, matchName, false));
checkNames(objNames, syncPar); checkNames(objNames, syncPar);
return objNames; return objNames;
@ -550,31 +423,31 @@ Foam::wordList Foam::IOobjectList::names
Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names
( (
const word& clsName, const word& clsName,
const wordHashSet& matcher const wordHashSet& matchName
) const ) const
{ {
return namesImpl(*this, clsName, matcher, false); return namesImpl(*this, clsName, matchName, false);
} }
Foam::wordList Foam::IOobjectList::sortedNames Foam::wordList Foam::IOobjectList::sortedNames
( (
const word& clsName, const word& clsName,
const wordHashSet& matcher const wordHashSet& matchName
) const ) const
{ {
return namesImpl(*this, clsName, matcher, true); return namesImpl(*this, clsName, matchName, true);
} }
Foam::wordList Foam::IOobjectList::names Foam::wordList Foam::IOobjectList::names
( (
const word& clsName, const word& clsName,
const wordHashSet& matcher, const wordHashSet& matchName,
const bool syncPar const bool syncPar
) const ) const
{ {
wordList objNames(namesImpl(*this, clsName, matcher, false)); wordList objNames(namesImpl(*this, clsName, matchName, false));
checkNames(objNames, syncPar); checkNames(objNames, syncPar);
return objNames; return objNames;

View File

@ -29,6 +29,7 @@ Description
SourceFiles SourceFiles
IOobjectList.C IOobjectList.C
IOobjectListTemplates.C
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -45,11 +46,6 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Forward declarations
class IOobjectList;
Ostream& operator<<(Ostream& os, const IOobjectList& list);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class IOobjectList Declaration Class IOobjectList Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
@ -58,6 +54,53 @@ class IOobjectList
: :
public HashPtrTable<IOobject> public HashPtrTable<IOobject>
{ {
// 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<class MatchPredicate>
static HashTable<wordHashSet> classesImpl
(
const IOobjectList& list,
const MatchPredicate& matchName
);
//- Templated implementation for names(), sortedNames()
template<class MatchPredicate>
static wordList namesImpl
(
const IOobjectList& list,
const word& matchClass,
const MatchPredicate& matchName,
const bool doSort
);
//- Templated implementation for lookup()
template<class MatchPredicate>
static IOobjectList lookupImpl
(
const IOobjectList& list,
const MatchPredicate& matchName
);
//- Templated implementation for lookupClass()
template<class MatchPredicate>
static IOobjectList lookupClassImpl
(
const IOobjectList& list,
const word& matchClass,
const MatchPredicate& matchName
);
public: public:
// Constructors // Constructors
@ -90,7 +133,7 @@ public:
~IOobjectList() = default; ~IOobjectList() = default;
// Member functions // Member Functions
// Basic methods // Basic methods
@ -125,19 +168,23 @@ public:
bool remove(const IOobject& io); bool remove(const IOobject& io);
// Lookup // Lookup single
//- Lookup a given name and return IOobject ptr if found else nullptr //- Lookup an object by name
IOobject* lookup(const word& name) const; // \return IOobject ptr if found else nullptr
IOobject* findObject(const word& objName) const;
// Lookup multiple
//- The list of all IOobjects with matching names //- 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 //- 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 //- 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 //- The list of all IOobjects with the given class name
IOobjectList lookupClass(const word& clsName) const; IOobjectList lookupClass(const word& clsName) const;
@ -212,40 +259,43 @@ public:
// //
// Of course, there are many other ways to use and manipulate the // Of course, there are many other ways to use and manipulate the
// summary information. // summary information.
//
// \return HashTable of class-names for keys and wordHashSet of
// of object-names for the values.
HashTable<wordHashSet> classes() const; HashTable<wordHashSet> classes() const;
//- A summary hash of classes used and their associated object names //- A summary hash of classes used and their associated object names,
// restricted to objects with names that satisfy the input matcher //- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordRe& matcher) const; HashTable<wordHashSet> classes(const wordRe& matchName) const;
//- A summary hash of classes used and their associated object names //- A summary hash of classes used and their associated object names,
// restricted to objects with names that satisfy the input matcher //- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordRes& matcher) const; HashTable<wordHashSet> classes(const wordRes& matchName) const;
//- A summary hash of classes used and their associated object names //- A summary hash of classes used and their associated object names,
// restricted to objects with names that satisfy the input matcher //- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordHashSet& matcher) const; HashTable<wordHashSet> classes(const wordHashSet& matchName) const;
// Summary of names // Summary of names
//- A list of names of the IOobjects //- The names of the IOobjects
wordList names() const; 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; 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 //- 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 //- 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 //- 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. //- A sorted list of names of the IOobjects.
@ -265,7 +315,7 @@ public:
wordList names wordList names
( (
const word& clsName, const word& clsName,
const wordRe& matcher, const wordRe& matchName,
const bool syncPar const bool syncPar
) const; ) const;
@ -276,44 +326,52 @@ public:
wordList names wordList names
( (
const word& clsName, const word& clsName,
const wordRes& matcher, const wordRes& matchName,
const bool syncPar const bool syncPar
) const; ) 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 //- have a name satisfying the input matcher
// With syncPar = true, triggers FatalError if the names are // With syncPar = true, triggers FatalError if the names are
// not consistent on all processors. // not consistent on all processors.
wordList names wordList names
( (
const word& cls, const word& cls,
const wordHashSet& matcher, const wordHashSet& matchName,
const bool syncPar const bool syncPar
) const; ) const;
// Summary of names (sorted) // Summary of names (sorted)
//- A sorted list of names of the IOobjects //- The sorted names of the IOobjects
wordList sortedNames() const; 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; wordList sortedNames(const word& clsName) 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 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
//- have a name satisfying the input matcher //- have a name satisfying the input matcher
wordList sortedNames wordList sortedNames
( (
const word& clsName, 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; ) const;
@ -326,18 +384,34 @@ public:
void operator=(IOobjectList&& list); 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 } // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "IOobjectListTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif #endif
// ************************************************************************* // // ************************************************************************* //

View File

@ -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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "IOobjectList.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// Templated implementation for classes()
template<class MatchPredicate>
Foam::HashTable<Foam::wordHashSet> Foam::IOobjectList::classesImpl
(
const IOobjectList& list,
const MatchPredicate& matchName
)
{
HashTable<wordHashSet> 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<class MatchPredicate>
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<class MatchPredicate>
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<class MatchPredicate>
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;
}
// ************************************************************************* //

View File

@ -45,11 +45,7 @@ bool Foam::objectRegistry::parentNotTime() const
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::objectRegistry::objectRegistry Foam::objectRegistry::objectRegistry(const Time& t, const label nObjects)
(
const Time& t,
const label nIoObjects
)
: :
regIOobject regIOobject
( (
@ -64,7 +60,7 @@ Foam::objectRegistry::objectRegistry
), ),
true // to flag that this is the top-level regIOobject true // to flag that this is the top-level regIOobject
), ),
HashTable<regIOobject*>(nIoObjects), HashTable<regIOobject*>(nObjects),
time_(t), time_(t),
parent_(t), parent_(t),
dbDir_(name()), dbDir_(name()),
@ -72,14 +68,10 @@ Foam::objectRegistry::objectRegistry
{} {}
Foam::objectRegistry::objectRegistry Foam::objectRegistry::objectRegistry(const IOobject& io, const label nObjects)
(
const IOobject& io,
const label nIoObjects
)
: :
regIOobject(io), regIOobject(io),
HashTable<regIOobject*>(nIoObjects), HashTable<regIOobject*>(nObjects),
time_(io.time()), time_(io.time()),
parent_(io.db()), parent_(io.db()),
dbDir_(parent_.dbDir()/local()/name()), dbDir_(parent_.dbDir()/local()/name()),
@ -94,17 +86,17 @@ Foam::objectRegistry::objectRegistry
Foam::objectRegistry::~objectRegistry() Foam::objectRegistry::~objectRegistry()
{ {
List<regIOobject*> myObjects(size()); List<regIOobject*> myObjects(size());
label nMyObjects = 0; label nObjects = 0;
for (iterator iter = begin(); iter != end(); ++iter) 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]); checkOut(*myObjects[i]);
} }
@ -120,23 +112,23 @@ Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classes() const
Foam::HashTable<Foam::wordHashSet> Foam::HashTable<Foam::wordHashSet>
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::wordHashSet> Foam::HashTable<Foam::wordHashSet>
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::wordHashSet> Foam::HashTable<Foam::wordHashSet>
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; curEvent = 1;
event_ = 2; event_ = 2;
// No need to reset dependent objects; overflow is now handled // No need to reset dependent objects; overflow is now handled
// in regIOobject::upToDate // in regIOobject::upToDate
} }
@ -306,8 +297,8 @@ void Foam::objectRegistry::rename(const word& newName)
{ {
regIOobject::rename(newName); regIOobject::rename(newName);
// adjust dbDir_ as well // Adjust dbDir_ as well
string::size_type i = dbDir_.rfind('/'); const auto i = dbDir_.rfind('/');
if (i == string::npos) if (i == string::npos)
{ {
@ -322,7 +313,7 @@ void Foam::objectRegistry::rename(const word& newName)
bool Foam::objectRegistry::modified() const bool Foam::objectRegistry::modified() const
{ {
forAllConstIter(HashTable<regIOobject*>, *this, iter) for (const_iterator iter = cbegin(); iter != cend(); ++iter)
{ {
if (iter()->modified()) if (iter()->modified())
{ {
@ -367,7 +358,7 @@ bool Foam::objectRegistry::writeObject
{ {
bool ok = true; bool ok = true;
forAllConstIter(HashTable<regIOobject*>, *this, iter) for (const_iterator iter = cbegin(); iter != cend(); ++iter)
{ {
if (objectRegistry::debug) if (objectRegistry::debug)
{ {

View File

@ -41,6 +41,11 @@ SourceFiles
#include "regIOobject.H" #include "regIOobject.H"
#include "wordRes.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 namespace Foam
@ -77,19 +82,19 @@ class objectRegistry
bool parentNotTime() const; bool parentNotTime() const;
//- Templated implementation for classes() //- Templated implementation for classes()
template<class UnaryMatchPredicate> template<class MatchPredicate>
static HashTable<wordHashSet> classesImpl static HashTable<wordHashSet> classesImpl
( (
const objectRegistry& list, const objectRegistry& list,
const UnaryMatchPredicate& matcher const MatchPredicate& matchName
); );
//- Templated implementation for names() //- Templated implementation for names(), sortedNames()
template<class Type, class UnaryMatchPredicate> template<class Type, class MatchPredicate>
static wordList namesImpl static wordList namesTypeImpl
( (
const objectRegistry& list, const objectRegistry& list,
const UnaryMatchPredicate& matcher, const MatchPredicate& matchName,
const bool doSort const bool doSort
); );
@ -109,28 +114,21 @@ public:
// Constructors // Constructors
//- Construct the time objectRegistry given an initial estimate //- Construct the time objectRegistry,
// for the number of entries //- with estimated table capacity (default: 128)
explicit objectRegistry explicit objectRegistry(const Time& db, const label nObjects=128);
(
const Time& db,
const label nIoObjects = 128
);
//- Construct a sub-registry given an IObject to describe the registry //- Construct sub-registry given an IObject to describe the registry,
// and an initial estimate for the number of entries //- with estimated table capacity (default: 128)
explicit objectRegistry explicit objectRegistry(const IOobject& io, const label nObjects=128);
(
const IOobject& io,
const label nIoObjects = 128
);
//- Destructor //- Destructor, performs a checkOut() for all objects that are
//- ownedByRegistry
virtual ~objectRegistry(); virtual ~objectRegistry();
// Member functions // Member Functions
// Access // Access
@ -161,71 +159,88 @@ public:
//- A summary hash of classes used and their associated object names //- A summary hash of classes used and their associated object names
//- restricted to objects with names that satisfy the input matcher //- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordRe& matcher) const; HashTable<wordHashSet> classes(const wordRe& matchName) const;
//- A summary hash of classes used and their associated object names //- A summary hash of classes used and their associated object names
//- restricted to objects with names that satisfy the input matcher //- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordRes& matcher) const; HashTable<wordHashSet> classes(const wordRes& matchName) const;
//- A summary hash of classes used and their associated object names //- A summary hash of classes used and their associated object names
//- restricted to objects with names that satisfy the input matcher //- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordHashSet& matcher) const; HashTable<wordHashSet> classes(const wordHashSet& matchName) const;
// Summary of names // Summary of names
//- A list of names of the objects //- The names of the objects
wordList names() const; 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; wordList names(const word& clsName) const;
//- The names of objects with the given type //- The names of objects with a class satisfying \c isA\<Type\>.
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList names() const; wordList names() const;
//- The names of objects with the given type that also //- The names of objects with a class satisfying \c isA\<Type\>
//- have a name satisfying the input matcher //- that also have a name satisfying the input matcher
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList names(const wordRe& matcher) const; wordList names(const wordRe& matchName) const;
//- The names of objects with the given type that also //- The names of objects with a class satisfying \c isA\<Type\>
//- have a name satisfying the input matcher //- that also have a name satisfying the input matcher
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList names(const wordRes& matcher) const; wordList names(const wordRes& matchName) const;
//- The names of objects with the given type that also //- The names of objects with a class satisfying \c isA\<Type\>
//- have a name satisfying the input matcher //- that also have a name satisfying the input matcher
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList names(const wordHashSet& matcher) const; wordList names(const wordHashSet& matchName) const;
// Summary of names (sorted) // Summary of names (sorted)
//- A sorted list of names of the objects //- The sorted names of the objects
wordList sortedNames() const; 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; 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\<Type\>
//- that also have a name satisfying the input matcher
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList sortedNames() const; wordList sortedNames() const;
//- The sorted names of objects with the given type that also //- The sorted names of objects with a class satisfying \c isA\<Type\>
//- have a name satisfying the input matcher //- that also have a name satisfying the input matcher
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList sortedNames(const wordRe& matcher) const; wordList sortedNames(const wordRe& matchName) const;
//- The sorted names of objects with the given type that also //- The sorted names of objects with a class satisfying \c isA\<Type\>
//- have a name satisfying the input matcher //- that also have a name satisfying the input matcher
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList sortedNames(const wordRes& matcher) const; wordList sortedNames(const wordRes& matchName) const;
//- The sorted names of objects with the given type that also //- The sorted names of objects with a class satisfying \c isA\<Type\>
//- have a name satisfying the input matcher //- that also have a name satisfying the input matcher
//
// \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type>
wordList sortedNames(const wordHashSet& matcher) const; wordList sortedNames(const wordHashSet& matchName) const;
// Lookup // Lookup
@ -242,11 +257,15 @@ public:
) const; ) const;
//- Lookup and return all objects of the given Type //- Return all objects with a class satisfying \c isA\<Type\>
//
// \param strict use \c isType\<Type\> instead of \c isA\<Type\>
template<class Type> template<class Type>
HashTable<const Type*> lookupClass(const bool strict = false) const; HashTable<const Type*> lookupClass(const bool strict = false) const;
//- Lookup and return all objects of the given Type //- Return all objects with a class satisfying \c isA\<Type\>
//
// \param strict use \c isType\<Type\> instead of \c isA\<Type\>
template<class Type> template<class Type>
HashTable<Type*> lookupClass(const bool strict = false); HashTable<Type*> lookupClass(const bool strict = false);

View File

@ -24,17 +24,17 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "objectRegistry.H" #include "objectRegistry.H"
#include "stringListOps.H"
#include "predicates.H" #include "predicates.H"
#include <type_traits>
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
// Templated implementation for classes() // Templated implementation for classes()
template<class UnaryMatchPredicate> template<class MatchPredicate>
Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
( (
const objectRegistry& list, const objectRegistry& list,
const UnaryMatchPredicate& matcher const MatchPredicate& matchName
) )
{ {
HashTable<wordHashSet> summary(2*list.size()); HashTable<wordHashSet> summary(2*list.size());
@ -42,7 +42,7 @@ Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
// Summary (key,val) = (class-name, object-names) // Summary (key,val) = (class-name, object-names)
forAllConstIters(list, iter) forAllConstIters(list, iter)
{ {
if (matcher(iter.key())) if (matchName(iter.key()))
{ {
// Create entry (if needed) and insert // Create entry (if needed) and insert
summary(iter.object()->type()).insert(iter.key()); summary(iter.object()->type()).insert(iter.key());
@ -53,12 +53,12 @@ Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
} }
// Templated implementation for names() // Templated implementation for names(), sortedNames()
template<class Type, class UnaryMatchPredicate> template<class Type, class MatchPredicate>
Foam::wordList Foam::objectRegistry::namesImpl Foam::wordList Foam::objectRegistry::namesTypeImpl
( (
const objectRegistry& list, const objectRegistry& list,
const UnaryMatchPredicate& matcher, const MatchPredicate& matchName,
const bool doSort const bool doSort
) )
{ {
@ -67,14 +67,20 @@ Foam::wordList Foam::objectRegistry::namesImpl
label count = 0; label count = 0;
forAllConstIters(list, iter) forAllConstIters(list, iter)
{ {
if (isA<Type>(*iter()) && matcher(iter()->name())) const regIOobject* obj = iter.object();
if
(
(std::is_void<Type>::value || isA<Type>(*obj))
&& matchName(obj->name())
)
{ {
objNames[count] = iter()->name(); objNames[count] = iter()->name();
++count; ++count;
} }
} }
objNames.setSize(count); objNames.resize(count);
if (doSort) if (doSort)
{ {
@ -90,59 +96,59 @@ Foam::wordList Foam::objectRegistry::namesImpl
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::names() const Foam::wordList Foam::objectRegistry::names() const
{ {
return namesImpl<Type>(*this, predicates::always(), false); return namesTypeImpl<Type>(*this, predicates::always(), false);
} }
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::names(const wordRe& matcher) const Foam::wordList Foam::objectRegistry::names(const wordRe& matchName) const
{ {
return namesImpl<Type>(*this, matcher, false); return namesTypeImpl<Type>(*this, matchName, false);
} }
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::names(const wordRes& matcher) const Foam::wordList Foam::objectRegistry::names(const wordRes& matchName) const
{ {
return namesImpl<Type>(*this, matcher, false); return namesTypeImpl<Type>(*this, matchName, false);
} }
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::names(const wordHashSet& matcher) const Foam::wordList Foam::objectRegistry::names(const wordHashSet& matchName) const
{ {
return namesImpl<Type>(*this, matcher, false); return namesTypeImpl<Type>(*this, matchName, false);
} }
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames() const Foam::wordList Foam::objectRegistry::sortedNames() const
{ {
return namesImpl<Type>(*this, predicates::always(), true); return namesTypeImpl<Type>(*this, predicates::always(), true);
} }
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames(const wordRe& matcher) const Foam::wordList Foam::objectRegistry::sortedNames(const wordRe& matchName) const
{ {
return namesImpl<Type>(*this, matcher, true); return namesTypeImpl<Type>(*this, matchName, true);
} }
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames(const wordRes& matcher) const Foam::wordList Foam::objectRegistry::sortedNames(const wordRes& matchName) const
{ {
return namesImpl<Type>(*this, matcher, true); return namesTypeImpl<Type>(*this, matchName, true);
} }
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames Foam::wordList Foam::objectRegistry::sortedNames
( (
const wordHashSet& matcher const wordHashSet& matchName
) const ) const
{ {
return namesImpl<Type>(*this, matcher, true); return namesTypeImpl<Type>(*this, matchName, true);
} }

View File

@ -142,7 +142,7 @@ Foam::labelList Foam::lumpedPointTools::lumpedPointPatchList
autoPtr<pointVectorField> displacePtr = loadPointField<pointVectorField> autoPtr<pointVectorField> displacePtr = loadPointField<pointVectorField>
( (
pMesh, pMesh,
objects0.lookup("pointDisplacement") objects0.findObject("pointDisplacement")
); );
if (!displacePtr.valid()) if (!displacePtr.valid())