ENH: simplify/extend objectRegistry code with templated predicates

- replace explicit use of wordRe, wordRes, wordHashSet as filters
  with a MatchPredicate.

- support filtering by class based on <Type> or predicates
This commit is contained in:
Mark Olesen
2018-11-10 17:20:32 +01:00
parent f44e59fb55
commit 4965ea4988
4 changed files with 166 additions and 157 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenCFD Ltd. \\ / A nd | Copyright (C) 2016-2018 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -34,8 +34,8 @@ Description
#include "Time.H" #include "Time.H"
#include "polyMesh.H" #include "polyMesh.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "FlatOutput.H"
#include "objectRegistry.H" #include "objectRegistry.H"
#include "hashedWordList.H"
using namespace Foam; using namespace Foam;
@ -45,22 +45,6 @@ using namespace Foam;
bool recursive = false; bool recursive = false;
template<class Type>
Foam::Ostream& printList(Foam::Ostream& os, const UList<Type>& list)
{
// list with out any linebreaks
os << '(';
forAll(list, i)
{
if (i) os << ' ';
os << list[i];
}
os << ')';
return os;
}
void printRegistry void printRegistry
( (
Foam::Ostream& os, Foam::Ostream& os,
@ -76,8 +60,8 @@ void printRegistry
Foam::label indent Foam::label indent
) )
{ {
wordList names = obr.sortedNames(); wordList names(obr.sortedNames());
hashedWordList regs = obr.sortedNames<objectRegistry>(); wordList regs(obr.sortedNames<objectRegistry>());
std::string prefix; std::string prefix;
for (label i=indent; i; --i) for (label i=indent; i; --i)
@ -88,23 +72,13 @@ void printRegistry
os << '#' << prefix.c_str() << obr.name() os << '#' << prefix.c_str() << obr.name()
<< " parent:" << obr.parent().name() << nl; << " parent:" << obr.parent().name() << nl;
// all names os << ' ' << prefix.c_str() << "objects: " << flatOutput(names) << nl;
{ os << ' ' << prefix.c_str() << "registries: " << flatOutput(regs) << nl;
os << ' ' << prefix.c_str() << "objects: ";
printList(os, names) << nl;
}
// sub-registry names
{
os << ' ' << prefix.c_str() << "registries: ";
printList(os, regs) << nl;
}
// Print, but skip expansion of sub-registries for now // Print, but skip expansion of sub-registries for now
forAll(names, i) for (const word& name : names)
{ {
const word& name = names[i];
os << (regs.found(name) ? '-' : ' ') os << (regs.found(name) ? '-' : ' ')
<< prefix.c_str() << name << " => " << obr[name]->type() << nl; << prefix.c_str() << name << " => " << obr[name]->type() << nl;
} }
@ -115,9 +89,8 @@ void printRegistry
os << '\n'; os << '\n';
// Now descend into the sub-registries // Now descend into the sub-registries
forAll(regs, i) for (const word& name : regs)
{ {
const word& name = regs[i];
const objectRegistry& next = obr.lookupObject<objectRegistry> const objectRegistry& next = obr.lookupObject<objectRegistry>
( (
name, name,
@ -240,8 +213,8 @@ int main(int argc, char *argv[])
); );
} }
Info<< "after adding some entries, top-level now contains: "; Info<< "after adding some entries, top-level now contains: "
printList(Info, db.names()) << endl; << flatOutput(db.names()) << endl;
Info<<"## Now attempt to add a few more entries ##" << nl; Info<<"## Now attempt to add a few more entries ##" << nl;

View File

@ -111,27 +111,6 @@ Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classes() const
} }
Foam::HashTable<Foam::wordHashSet>
Foam::objectRegistry::classes(const wordRe& matchName) const
{
return classesImpl(*this, matchName);
}
Foam::HashTable<Foam::wordHashSet>
Foam::objectRegistry::classes(const wordRes& matchName) const
{
return classesImpl(*this, matchName);
}
Foam::HashTable<Foam::wordHashSet>
Foam::objectRegistry::classes(const wordHashSet& matchName) const
{
return classesImpl(*this, matchName);
}
Foam::wordList Foam::objectRegistry::names() const Foam::wordList Foam::objectRegistry::names() const
{ {
return HashTable<regIOobject*>::toc(); return HashTable<regIOobject*>::toc();
@ -144,31 +123,17 @@ Foam::wordList Foam::objectRegistry::sortedNames() const
} }
Foam::wordList Foam::objectRegistry::names(const word& clsName) const Foam::wordList Foam::objectRegistry::names(const char* clsName) const
{ {
wordList objNames(size()); // No nullptr check - only called with string literals
return names(static_cast<word>(clsName));
label count=0;
for (const_iterator iter = cbegin(); iter != cend(); ++iter)
{
if (iter()->type() == clsName)
{
objNames[count++] = iter.key();
}
}
objNames.setSize(count);
return objNames;
} }
Foam::wordList Foam::objectRegistry::sortedNames(const word& clsName) const Foam::wordList Foam::objectRegistry::sortedNames(const char* clsName) const
{ {
wordList objNames = names(clsName); // No nullptr check - only called with string literals
Foam::sort(objNames); return sortedNames(static_cast<word>(clsName));
return objNames;
} }

View File

@ -89,6 +89,16 @@ class objectRegistry
const MatchPredicate& matchName const MatchPredicate& matchName
); );
//- Templated implementation for names(), sortedNames()
template<class MatchPredicate1, class MatchPredicate2>
static wordList namesImpl
(
const objectRegistry& list,
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool doSort
);
//- Templated implementation for names(), sortedNames() //- Templated implementation for names(), sortedNames()
template<class Type, class MatchPredicate> template<class Type, class MatchPredicate>
static wordList namesTypeImpl static wordList namesTypeImpl
@ -157,26 +167,35 @@ public:
// Behaviour and usage as per IOobjectList::classes // Behaviour and usage as per IOobjectList::classes
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 that have a matching object name.
HashTable<wordHashSet> classes(const wordRe& matchName) const; template<class MatchPredicate>
HashTable<wordHashSet> classes(const MatchPredicate& matchName) const;
//- A summary hash of classes used and their associated object names
//- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordRes& matchName) const;
//- A summary hash of classes used and their associated object names
//- restricted to objects with names that satisfy the input matcher
HashTable<wordHashSet> classes(const wordHashSet& matchName) const;
// Summary of names // Summary of names
//- The names of the objects //- The names of all objects
wordList names() const; wordList names() const;
//- The names of objects with the given class //- The names of objects with the given class name.
wordList names(const word& clsName) const; // \note uses the class type() method
wordList names(const char* clsName) const;
//- The names of objects with a matching class name
// \note uses the class type() method
template<class MatchPredicate>
wordList names(const MatchPredicate& matchClass) const;
//- The names of objects with a matching class name
//- that also have a matching object name.
// \note uses the class type() method
template<class MatchPredicate1, class MatchPredicate2>
wordList names
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const;
//- The names of objects with a class satisfying \c isA\<Type\>. //- The names of objects with a class satisfying \c isA\<Type\>.
// //
@ -185,62 +204,49 @@ public:
wordList names() const; wordList names() const;
//- The names of objects with a class satisfying \c isA\<Type\> //- The names of objects with a class satisfying \c isA\<Type\>
//- that also have a name satisfying the input matcher //- that also have a matching object name.
// //
// \note If \a Type is \c void, no isA check is used (always true). // \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type, class MatchPredicate>
wordList names(const wordRe& matchName) const; wordList names(const MatchPredicate& matchName) const;
//- The 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>
wordList names(const wordRes& matchName) const;
//- The 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>
wordList names(const wordHashSet& matchName) const;
// Summary of names (sorted) // Summary of names (sorted)
//- The sorted names of the objects //- The sorted names of all objects
wordList sortedNames() const; wordList sortedNames() const;
//- The sorted names of objects with the given class //- The sorted names of objects with the given class name.
wordList sortedNames(const word& clsName) const; // \note uses the class type() method
wordList sortedNames(const char* clsName) const;
//- The sorted names objects with a matching class name
// \note uses the class type() method
template<class MatchPredicate>
wordList sortedNames(const MatchPredicate& matchClass) const;
//- The sorted names of objects with a matching class name
//- that also have a matching object name.
// \note uses the class type() method
template<class MatchPredicate1, class MatchPredicate2>
wordList sortedNames
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const;
//- The sorted names of objects with a class satisfying \c isA\<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). // \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 a class satisfying \c isA\<Type\> //- The sorted names of objects with a class satisfying \c isA\<Type\>
//- that also have a name satisfying the input matcher //- that also have a matching object name.
// //
// \note If \a Type is \c void, no isA check is used (always true). // \note If \a Type is \c void, no isA check is used (always true).
template<class Type> template<class Type, class MatchPredicate>
wordList sortedNames(const wordRe& matchName) const; wordList sortedNames(const MatchPredicate& matchName) const;
//- 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>
wordList sortedNames(const wordRes& matchName) const;
//- 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>
wordList sortedNames(const wordHashSet& matchName) const;
// Lookup // Lookup

View File

@ -42,10 +42,12 @@ 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 (matchName(iter.key())) const regIOobject* obj = iter.object();
if (matchName(obj->name()))
{ {
// Create entry (if needed) and insert // Create entry (if needed) and insert
summary(iter.object()->type()).insert(iter.key()); summary(iter.object()->type()).insert(obj->name());
} }
} }
@ -53,6 +55,41 @@ Foam::HashTable<Foam::wordHashSet> Foam::objectRegistry::classesImpl
} }
// Templated implementation for names(), sortedNames()
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::objectRegistry::namesImpl
(
const objectRegistry& list,
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName,
const bool doSort
)
{
wordList objNames(list.size());
label count=0;
forAllConstIters(list, iter)
{
const regIOobject* obj = iter.object();
if (matchClass(obj->type()) && matchName(obj->name()))
{
objNames[count] = obj->name();
++count;
}
}
objNames.resize(count);
if (doSort)
{
Foam::sort(objNames);
}
return objNames;
}
// Templated implementation for names(), sortedNames() // Templated implementation for names(), sortedNames()
template<class Type, class MatchPredicate> template<class Type, class MatchPredicate>
Foam::wordList Foam::objectRegistry::namesTypeImpl Foam::wordList Foam::objectRegistry::namesTypeImpl
@ -75,7 +112,7 @@ Foam::wordList Foam::objectRegistry::namesTypeImpl
&& matchName(obj->name()) && matchName(obj->name())
) )
{ {
objNames[count] = iter()->name(); objNames[count] = obj->name();
++count; ++count;
} }
} }
@ -93,6 +130,38 @@ Foam::wordList Foam::objectRegistry::namesTypeImpl
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class MatchPredicate>
Foam::HashTable<Foam::wordHashSet>
Foam::objectRegistry::classes
(
const MatchPredicate& matchName
) const
{
return classesImpl(*this, matchName);
}
template<class MatchPredicate>
Foam::wordList Foam::objectRegistry::names
(
const MatchPredicate& matchClass
) const
{
return namesImpl(*this, matchClass, predicates::always(), false);
}
template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::objectRegistry::names
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const
{
return namesImpl(*this, matchClass, matchName, false);
}
template<class Type> template<class Type>
Foam::wordList Foam::objectRegistry::names() const Foam::wordList Foam::objectRegistry::names() const
{ {
@ -100,24 +169,34 @@ Foam::wordList Foam::objectRegistry::names() const
} }
template<class Type> template<class Type, class MatchPredicate>
Foam::wordList Foam::objectRegistry::names(const wordRe& matchName) const Foam::wordList Foam::objectRegistry::names
(
const MatchPredicate& matchName
) const
{ {
return namesTypeImpl<Type>(*this, matchName, false); return namesTypeImpl<Type>(*this, matchName, false);
} }
template<class Type> template<class MatchPredicate>
Foam::wordList Foam::objectRegistry::names(const wordRes& matchName) const Foam::wordList Foam::objectRegistry::sortedNames
(
const MatchPredicate& matchClass
) const
{ {
return namesTypeImpl<Type>(*this, matchName, false); return namesImpl(*this, matchClass, predicates::always(), true);
} }
template<class Type> template<class MatchPredicate1, class MatchPredicate2>
Foam::wordList Foam::objectRegistry::names(const wordHashSet& matchName) const Foam::wordList Foam::objectRegistry::sortedNames
(
const MatchPredicate1& matchClass,
const MatchPredicate2& matchName
) const
{ {
return namesTypeImpl<Type>(*this, matchName, false); return namesImpl(*this, matchClass, matchName, true);
} }
@ -128,24 +207,10 @@ Foam::wordList Foam::objectRegistry::sortedNames() const
} }
template<class Type> template<class Type, class MatchPredicate>
Foam::wordList Foam::objectRegistry::sortedNames(const wordRe& matchName) const
{
return namesTypeImpl<Type>(*this, matchName, true);
}
template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames(const wordRes& matchName) const
{
return namesTypeImpl<Type>(*this, matchName, true);
}
template<class Type>
Foam::wordList Foam::objectRegistry::sortedNames Foam::wordList Foam::objectRegistry::sortedNames
( (
const wordHashSet& matchName const MatchPredicate& matchName
) const ) const
{ {
return namesTypeImpl<Type>(*this, matchName, true); return namesTypeImpl<Type>(*this, matchName, true);