Commit faaa93fd authored by Mark Olesen's avatar Mark Olesen
Browse files

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..);
parent ffec4c6f
......@@ -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;
......
......@@ -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)
{
......
......@@ -32,7 +32,7 @@ bool Foam::fieldOk(const IOobjectList& cloudObjs, const word& name)
{
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));
const IOobject* obj = objects.lookup(name);
const IOobject* obj = objects.findObject(name);
if (obj != nullptr)
{
IOField<Type> 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<word> 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());
......
......@@ -141,7 +141,7 @@ int main(int argc, char *argv[])
// Pressure field
autoPtr<volScalarField> pField
= loadField<volScalarField>(mesh, objects.lookup("p"));
= loadField<volScalarField>(mesh, objects.findObject("p"));
// The forces per zone
if (movement().forcesAndMoments(mesh, forces, moments))
......
......@@ -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);
......
......@@ -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<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();
// Sort for consistent order on all processors
Foam::sort(masterNames);
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)
if (syncPar && Pstream::parRun())
{
Foam::sort(masterNames);
const wordList localNames(masterNames);
Pstream::scatter(masterNames);
if (syncPar && Pstream::parRun())
if (localNames != masterNames)
{
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;
}
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<IOobject*>(*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::wordHashSet> Foam::IOobjectList::classes() const
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::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::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;
......
......@@ -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<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:
// 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 an object by name
// \return IOobject ptr if found else nullptr
IOobject* findObject(const word& objName) const;
//- Lookup a given name and return IOobject ptr if found else nullptr
IOobject* lookup(const word& name) 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;