Commit 8fabc325 authored by Mark Olesen's avatar Mark Olesen

ENH: simplify objectRegistry access names (issue #322)

  New name:  findObject(), cfindObject()
  Old name:  lookupObjectPtr()

      Return a const pointer or nullptr on failure.

  New name:  findObject()
  Old name:  --

      Return a non-const pointer or nullptr on failure.

  New name:  getObjectPtr()
  Old name:  lookupObjectRefPtr()

      Return a non-const pointer or nullptr on failure.
      Can be called on a const object and it will perform a
      const_cast.

- use these updated names and functionality in more places

NB: The older methods names are deprecated, but continue to be defined.
parent e0255cff
......@@ -260,13 +260,7 @@ public:
virtual volScalarField& he()
{
NotImplemented;
return
(
const_cast<volScalarField&>
(
volScalarField::null()
)
);
return const_cast<volScalarField&>(volScalarField::null());
}
//- Return access to the inernal energy field [J/Kg]
......@@ -274,10 +268,7 @@ public:
virtual const volScalarField& he() const
{
NotImplemented;
return
(
volScalarField::null()
);
return volScalarField::null();
}
//- Enthalpy/Internal energy
......
......@@ -131,19 +131,15 @@ Foam::ThermalPhaseChangePhaseSystem<BasePhaseSystem>::heatTransfer() const
dimensionedScalar(dimensionSet(1,-1,-3,0,0), Zero)
);
if
(
otherPhase.mesh().foundObject<volScalarField>
const volScalarField* alphatPtr =
otherPhase.mesh().findObject<volScalarField>
(
"alphat." + otherPhase.name()
)
)
);
if (alphatPtr)
{
const volScalarField& alphat =
otherPhase.mesh().lookupObject<volScalarField>
(
"alphat." + otherPhase.name()
);
const volScalarField& alphat = *alphatPtr;
const fvPatchList& patches = this->mesh().boundary();
forAll(patches, patchi)
......@@ -427,19 +423,15 @@ void Foam::ThermalPhaseChangePhaseSystem<BasePhaseSystem>::correctThermo()
dimensionedScalar(dimDensity/dimTime, Zero)
);
if
(
phase2.mesh().foundObject<volScalarField>
const volScalarField* alphatPtr =
phase2.mesh().findObject<volScalarField>
(
"alphat." + phase2.name()
)
)
"alphat." + phase2.name()
);
if (alphatPtr)
{
const volScalarField& alphat =
phase2.mesh().lookupObject<volScalarField>
(
"alphat." + phase2.name()
);
const volScalarField& alphat = *alphatPtr;
const fvPatchList& patches = this->mesh().boundary();
forAll(patches, patchi)
......
......@@ -52,10 +52,8 @@ Foam::functionObjects::regionFunctionObject::whichSubRegistry
{
return obr.lookupObject<objectRegistry>(subName);
}
else
{
return obr;
}
return obr;
}
......@@ -71,22 +69,19 @@ bool Foam::functionObjects::regionFunctionObject::writeObject
const word& fieldName
)
{
const regIOobject* objPtr =
this->lookupObjectPtr<regIOobject>(fieldName);
const regIOobject* obj = this->findObject<regIOobject>(fieldName);
if (objPtr)
if (obj)
{
Log << " functionObjects::" << type() << " " << name()
<< " writing field: " << objPtr->name() << endl;
<< " writing field: " << obj->name() << endl;
objPtr->write();
obj->write();
return true;
}
else
{
return false;
}
return false;
}
......@@ -95,12 +90,14 @@ bool Foam::functionObjects::regionFunctionObject::clearObject
const word& fieldName
)
{
regIOobject* objPtr = lookupObjectRefPtr<regIOobject>(fieldName);
if (objPtr)
// Same as getObjectPtr, since the object is already non-const
regIOobject* obj = this->findObject<regIOobject>(fieldName);
if (obj)
{
if (objPtr->ownedByRegistry())
if (obj->ownedByRegistry())
{
return objPtr->checkOut();
return obj->checkOut();
}
else
{
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -94,25 +94,42 @@ protected:
template<class ObjectType>
bool foundObject(const word& fieldName) const;
//- Lookup and return object (eg, a field) from the (sub) objectRegistry
//- Return const pointer to the object (eg, a field) in the
//- (sub) objectRegistry.
//
// \return nullptr if the object was not found or had incorrect type.
template<class ObjectType>
const ObjectType& lookupObject(const word& fieldName) const;
const ObjectType* cfindObject(const word& fieldName) const;
//- Lookup and return object (eg, a field) from the (sub) objectRegistry
//- Return const pointer to the object (eg, a field) in the
//- (sub) objectRegistry.
//
// \return nullptr if the object was not found or had incorrect type.
template<class ObjectType>
ObjectType& lookupObjectRef(const word& fieldName) const;
const ObjectType* findObject(const word& fieldName) const;
//- Lookup and return pointer to the object,
// otherwise nullptr if the object was not found,
// or had the incorrect type.
//- Return non-const pointer to the object of the given Type,
//- (sub) objectRegistry.
//
// \return nullptr if the object was not found or had incorrect type.
template<class ObjectType>
ObjectType* findObject(const word& fieldName);
//- Return non-const pointer to the object of the given Type,
//- using a const-cast to have it behave like a mutable.
// Exercise caution when using.
//
// \return nullptr if the object was not found or had incorrect type.
template<class ObjectType>
ObjectType* getObjectPtr(const word& fieldName) const;
//- Lookup and return object (eg, a field) from the (sub) objectRegistry
template<class ObjectType>
const ObjectType* lookupObjectPtr(const word& fieldName) const;
const ObjectType& lookupObject(const word& fieldName) const;
//- Lookup and return non-const pointer to the object,
// otherwise nullptr if the object was not found,
// or had the incorrect type.
//- Lookup and return object (eg, a field) from the (sub) objectRegistry
template<class ObjectType>
ObjectType* lookupObjectRefPtr(const word& fieldName) const;
ObjectType& lookupObjectRef(const word& fieldName) const;
//- Store the field in the (sub) objectRegistry under the given name
// Note: sets the fieldName to tfield().name() if not already set
......@@ -177,6 +194,25 @@ public:
//- Read optional controls
virtual bool read(const dictionary& dict);
// Housekeeping
//- Same as findObject
// \deprecated use findObject (OCT-2018)
template<class ObjectType>
const ObjectType* lookupObjectPtr(const word& fieldName) const
{
return this->cfindObject<ObjectType>(fieldName);
}
//- Same as getObjectPtr
// \deprecated use getObjectPtr (OCT-2018)
template<class ObjectType>
ObjectType* lookupObjectRefPtr(const word& fieldName) const
{
return this->getObjectPtr<ObjectType>(fieldName);
}
};
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -39,42 +39,63 @@ bool Foam::functionObjects::regionFunctionObject::foundObject
template<class ObjectType>
const ObjectType& Foam::functionObjects::regionFunctionObject::lookupObject
const ObjectType* Foam::functionObjects::regionFunctionObject::cfindObject
(
const word& fieldName
) const
{
return obr().lookupObject<ObjectType>(fieldName);
return obr().cfindObject<ObjectType>(fieldName);
}
template<class ObjectType>
ObjectType& Foam::functionObjects::regionFunctionObject::lookupObjectRef
const ObjectType* Foam::functionObjects::regionFunctionObject::findObject
(
const word& fieldName
) const
{
return obr().lookupObjectRef<ObjectType>(fieldName);
return obr().findObject<ObjectType>(fieldName);
}
template<class ObjectType>
ObjectType* Foam::functionObjects::regionFunctionObject::findObject
(
const word& fieldName
)
{
// Need getObjectPtr to bypass const access on the objectRegistry
return obr().getObjectPtr<ObjectType>(fieldName);
}
template<class ObjectType>
const ObjectType* Foam::functionObjects::regionFunctionObject::lookupObjectPtr
ObjectType* Foam::functionObjects::regionFunctionObject::getObjectPtr
(
const word& fieldName
) const
{
return obr().lookupObjectPtr<ObjectType>(fieldName);
return obr().getObjectPtr<ObjectType>(fieldName);
}
template<class ObjectType>
ObjectType* Foam::functionObjects::regionFunctionObject::lookupObjectRefPtr
const ObjectType& Foam::functionObjects::regionFunctionObject::lookupObject
(
const word& fieldName
) const
{
return obr().lookupObject<ObjectType>(fieldName);
}
template<class ObjectType>
ObjectType& Foam::functionObjects::regionFunctionObject::lookupObjectRef
(
const word& fieldName
) const
{
return obr().lookupObjectRefPtr<ObjectType>(fieldName);
return obr().lookupObjectRef<ObjectType>(fieldName);
}
......
......@@ -231,8 +231,9 @@ public:
// Lookup
//- Lookup and return a const sub-objectRegistry.
// Optionally create it if it does not exist.
// If recursive, search parent registries.
//
// \param forceCreate create it if it does not exist.
// \param recursive search parent registries.
const objectRegistry& subRegistry
(
const word& name,
......@@ -250,7 +251,8 @@ public:
HashTable<Type*> lookupClass(const bool strict = false);
//- Is the named Type found?
// If recursive, search parent registries.
//
// \param recursive search parent registries
template<class Type>
bool foundObject
(
......@@ -258,43 +260,73 @@ public:
const bool recursive = false
) const;
//- Lookup and return the object of the given Type.
// If recursive, search parent registries.
//- Find const pointer to the object of the given Type.
//
// \param recursive search parent registries
//
// \return nullptr if the object was not found or had incorrect type.
template<class Type>
const Type& lookupObject
const Type* cfindObject
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return the object of the given Type.
// If recursive, search parent registries.
//- Return const pointer to the object of the given Type.
//
// \param recursive search parent registries
//
// \return nullptr if the object was not found or had incorrect type.
template<class Type>
Type& lookupObjectRef
const Type* findObject
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return pointer to the object of the given Type,
// otherwise nullptr if the object was not found,
// or had the incorrect type.
// If recursive, search parent registries.
//- Return non-const pointer to the object of the given Type.
//
// \param recursive search parent registries
//
// \return nullptr if the object was not found or had incorrect type.
template<class Type>
const Type* lookupObjectPtr
Type* findObject
(
const word& name,
const bool recursive = false
);
//- Return non-const pointer to the object of the given Type,
//- using a const-cast to have it behave like a mutable.
// Exercise caution when using.
//
// \param recursive search parent registries.
//
// \return nullptr if the object was not found or had incorrect type.
template<class Type>
Type* getObjectPtr
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return const reference to the object
//- of the given Type. Fatal if not found or the wrong type.
//
// \param recursive search parent registries.
template<class Type>
const Type& lookupObject
(
const word& name,
const bool recursive = false
) const;
//- Lookup and return non-const pointer to the object
// of the given Type,
// otherwise nullptr if the object was not found,
// or had the incorrect type.
// If recursive, search parent registries.
//- Lookup and return non-const reference to the object
//- of the given Type. Fatal if not found or the wrong type.
//
// \param recursive search parent registries.
template<class Type>
Type* lookupObjectRefPtr
Type& lookupObjectRef
(
const word& name,
const bool recursive = false
......@@ -349,6 +381,34 @@ public:
IOstream::compressionType cmp,
const bool valid
) const;
// Housekeeping
//- Same as findObject
// \deprecated use findObject (OCT-2018)
template<class Type>
const Type* lookupObjectPtr
(
const word& name,
bool recursive = false
) const
{
return this->cfindObject<Type>(name, recursive);
}
//- Same as getObjectPtr
//
// \deprecated use getObjectPtr (OCT-2018)
template<class Type>
Type* lookupObjectRefPtr
(
const word& name,
bool recursive = false
) const
{
return this->getObjectPtr<Type>(name, recursive);
}
};
......
......@@ -156,7 +156,7 @@ Foam::HashTable<const Type*> Foam::objectRegistry::lookupClass
forAllConstIters(*this, iter)
{
if (strict ? isType<Type>(*iter()) : isA<Type>(*iter()))
if (strict ? isType<Type>(*iter()) : bool(isA<Type>(*iter())))
{
objectsOfClass.insert
(
......@@ -180,7 +180,7 @@ Foam::HashTable<Type*> Foam::objectRegistry::lookupClass
forAllIters(*this, iter)
{
if (strict ? isType<Type>(*iter()) : isA<Type>(*iter()))
if (strict ? isType<Type>(*iter()) : bool(isA<Type>(*iter())))
{
objectsOfClass.insert
(
......@@ -201,25 +201,18 @@ bool Foam::objectRegistry::foundObject
const bool recursive
) const
{
const Type* ptr = this->lookupObjectPtr<Type>(name, recursive);
if (ptr)
{
return true;
}
return false;
return this->cfindObject<Type>(name, recursive);
}
template<class Type>
const Type& Foam::objectRegistry::lookupObject
const Type* Foam::objectRegistry::cfindObject
(
const word& name,
const bool recursive
) const
{
const_iterator iter = find(name);
const_iterator iter = cfind(name);
if (iter.found())
{
......@@ -227,57 +220,63 @@ const Type& Foam::objectRegistry::lookupObject
if (ptr)
{
return *ptr;
return ptr;
}
FatalErrorInFunction
<< nl
<< " lookup of " << name << " from objectRegistry "
<< this->name()
<< " successful\n but it is not a " << Type::typeName
<< ", it is a " << iter()->type()
<< abort(FatalError);
}
else if (recursive && this->parentNotTime())
{
return parent_.lookupObject<Type>(name, recursive);
return parent_.cfindObject<Type>(name, recursive);
}
FatalErrorInFunction
<< nl
<< " request for " << Type::typeName
<< " " << name << " from objectRegistry " << this->name()
<< " failed\n available objects of type " << Type::typeName
<< " are" << nl
<< names<Type>()
<< abort(FatalError);
return nullptr;
}
return NullObjectRef<Type>();
template<class Type>
const Type* Foam::objectRegistry::findObject
(
const word& name,
const bool recursive
) const
{
return this->cfindObject<Type>(name, recursive);
}
template<class Type>
Type& Foam::objectRegistry::lookupObjectRef
Type* Foam::objectRegistry::findObject
(
const word& name,
const bool recursive
)
{
const Type* ptr = this->cfindObject<Type>(name, recursive);
return const_cast<Type*>(ptr);
}
template<class Type>
Type* Foam::objectRegistry::getObjectPtr
(
const word& name,
const bool recursive
) const
{
const Type& ref = this->lookupObject<Type>(name, recursive);
<