mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: additional variants of IOobjectList findObject()
- cfindObject() for const pointer access.
- getObject() for mutable non-const pointer access, similar to the
objectRegistry::getObjectPtr()
- cfindObject(), findObject(), getObject() with template type access
to also check the headerClassName.
For example,
cfindObject("U") -> good
cfindObject<volVectorField>("U") -> good
cfindObject<volScalarField>("U") -> nullptr
This allows inversion of looping logic.
1) Obtain the names for a particular Type
for (const word& objName : objs.sortedNames<Type>())
{
const IOobject* io = objs[objName];
...
}
2) Use previously obtained names and apply to a particular Type
for (const word& objName : someListOfNames)
{
const IOobject* io = objs.cfindObject<Type>(objName);
if (io)
{
...
}
}
This commit is contained in:
@ -52,7 +52,7 @@ void reportDetail(const IOobjectList& objects)
|
||||
for (const word& key : objects.sortedNames())
|
||||
{
|
||||
// Canonical method name (NOV-2018)
|
||||
IOobject* io = objects.findObject(key);
|
||||
const IOobject* io = objects.findObject(key);
|
||||
|
||||
label count = 0;
|
||||
|
||||
@ -82,6 +82,55 @@ void reportDetail(const IOobjectList& objects)
|
||||
}
|
||||
|
||||
|
||||
void printFound(const IOobject* ptr)
|
||||
{
|
||||
Info<< (ptr ? "found" : "not found") << nl;
|
||||
}
|
||||
|
||||
|
||||
void findObjectTest(const IOobjectList& objs)
|
||||
{
|
||||
Info<< "Test findObject()" << nl << nl;
|
||||
|
||||
const int oldDebug = IOobject::debug;
|
||||
IOobject::debug = 1;
|
||||
|
||||
{
|
||||
Info<< "cfindObject(U)" << nl;
|
||||
const IOobject* io = objs.cfindObject("U");
|
||||
printFound(io);
|
||||
}
|
||||
|
||||
{
|
||||
Info<< "getObject(U)" << nl;
|
||||
IOobject* io = objs.getObject("U");
|
||||
printFound(io);
|
||||
}
|
||||
|
||||
{
|
||||
Info<< "cfindObject<void>(U)" << nl;
|
||||
const IOobject* io = objs.cfindObject<void>("U");
|
||||
printFound(io);
|
||||
}
|
||||
|
||||
{
|
||||
Info<< "cfindObject<volScalarField>(U)" << nl;
|
||||
const IOobject* io = objs.cfindObject<volScalarField>("U");
|
||||
printFound(io);
|
||||
}
|
||||
|
||||
{
|
||||
Info<< "cfindObject<volVectorField>(U)" << nl;
|
||||
const IOobject* io = objs.cfindObject<volVectorField>("U");
|
||||
printFound(io);
|
||||
}
|
||||
|
||||
Info<< nl;
|
||||
|
||||
IOobject::debug = oldDebug;
|
||||
}
|
||||
|
||||
|
||||
template<class Type>
|
||||
void filterTest(const IOobjectList& objs, const wordRe& re)
|
||||
{
|
||||
@ -219,6 +268,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
report(objects);
|
||||
|
||||
findObjectTest(objects);
|
||||
|
||||
classes.filterKeys(subsetTypes);
|
||||
Info<<"only retain: " << flatOutput(subsetTypes) << nl;
|
||||
Info<<"Pruned: " << classes << nl;
|
||||
|
||||
Reference in New Issue
Block a user