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:
Mark Olesen
2018-11-28 11:28:38 +01:00
parent 7477459186
commit b81420e524
7 changed files with 212 additions and 29 deletions

View File

@ -27,7 +27,6 @@ Application
Group
grpPostProcessingUtilitie
Description
Generates a VTK file of particle tracks for cases that were computed using
a steady-state cloud
@ -84,7 +83,7 @@ label validateFields
{
if (ok[i])
{
nOk++;
++nOk;
}
else
{

View File

@ -30,9 +30,7 @@ License
template<class Type>
bool Foam::fieldOk(const IOobjectList& cloudObjs, const word& name)
{
IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
return (objects.findObject(name) != nullptr);
return cloudObjs.cfindObject<IOField<Type>>(name) != nullptr;
}
@ -43,9 +41,7 @@ Foam::tmp<Foam::Field<Type>> Foam::readParticleField
const IOobjectList cloudObjs
)
{
IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
const IOobject* obj = objects.findObject(name);
const IOobject* obj = cloudObjs.cfindObject<IOField<Type>>(name);
if (obj != nullptr)
{
IOField<Type> newField(*obj);
@ -53,7 +49,8 @@ Foam::tmp<Foam::Field<Type>> Foam::readParticleField
}
FatalErrorInFunction
<< "error: cloud field name " << name << " not found"
<< "Error: cloud field name " << name
<< " not found or the wrong type"
<< abort(FatalError);
return Field<Type>::null();
@ -68,21 +65,21 @@ void Foam::readFields
const IOobjectList& cloudObjs
)
{
IOobjectList objects(cloudObjs.lookupClass(IOField<Type>::typeName));
forAll(fieldNames, j)
forAll(fieldNames, fieldi)
{
const IOobject* obj = objects.findObject(fieldNames[j]);
const word& fieldName = fieldNames[fieldi];
const IOobject* obj = cloudObjs.cfindObject<IOField<Type>>(fieldName);
if (obj != nullptr)
{
Info<< " reading field " << fieldNames[j] << endl;
Info<< " reading field " << fieldName << endl;
IOField<Type> newField(*obj);
values.set(j, new List<Type>(std::move(newField)));
values.set(fieldi, new List<Type>(std::move(newField)));
}
else
{
FatalErrorInFunction
<< "Unable to read field " << fieldNames[j]
<< "Unable to read field " << fieldName
<< abort(FatalError);
}
}
@ -158,7 +155,7 @@ void Foam::processFields
DynamicList<word> fieldNames(objects.size());
forAll(userFieldNames, i)
{
IOobject* obj = objects.findObject(userFieldNames[i]);
const IOobject* obj = objects.findObject(userFieldNames[i]);
if (obj != nullptr)
{
fieldNames.append(obj->name());