ENH: code reduction in MeshObject (#1071)

- use forwarding templates for the factory method

- avoid double use of dynamic_cast.
  Don't need implicit use in isA<>, can use result directly

STYLE: updated iteration over HashTable of mesh objects
This commit is contained in:
Mark Olesen
2018-11-15 13:06:12 +01:00
parent f269371dbc
commit c599810533
2 changed files with 47 additions and 225 deletions

View File

@ -40,181 +40,36 @@ Foam::MeshObject<Mesh, MeshObjectType, Type>::MeshObject(const Mesh& mesh)
// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
template<class Mesh, template<class> class MeshObjectType, class Type> template<class Mesh, template<class> class MeshObjectType, class Type>
const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New template<class... Args>
(
const Mesh& mesh
)
{
const Type* ptr = mesh.thisDb().objectRegistry::template cfindObject<Type>
(
Type::typeName
);
if (ptr)
{
return *ptr;
}
else
{
if (meshObject::debug)
{
Pout<< "MeshObject::New(const " << Mesh::typeName
<< "&) : constructing " << Type::typeName
<< " for region " << mesh.name() << endl;
}
Type* objectPtr = new Type(mesh);
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
return *objectPtr;
}
}
template<class Mesh, template<class> class MeshObjectType, class Type>
template<class Data1>
const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
( (
const Mesh& mesh, const Mesh& mesh,
const Data1& d Args&&... args
) )
{ {
const Type* ptr = mesh.thisDb().objectRegistry::template cfindObject<Type> const Type* ptr =
( mesh.thisDb().objectRegistry::template cfindObject<Type>
Type::typeName (
); Type::typeName
);
if (ptr) if (ptr)
{ {
return *ptr; return *ptr;
} }
else
if (meshObject::debug)
{ {
if (meshObject::debug) Pout<< "MeshObject::New(const " << Mesh::typeName
{ << "&, ...) : constructing " << Type::typeName
Pout<< "MeshObject::New(const " << Mesh::typeName << " for region " << mesh.name() << endl;
<< "&, const Data1&) : constructing " << Type::typeName
<< " for region " << mesh.name() << endl;
}
Type* objectPtr = new Type(mesh, d);
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
return *objectPtr;
} }
}
Type* objectPtr = new Type(mesh, std::forward<Args>(args)...);
template<class Mesh, template<class> class MeshObjectType, class Type> regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
template<class Data1, class Data2>
const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
(
const Mesh& mesh,
const Data1& d1,
const Data2& d2
)
{
const Type* ptr = mesh.thisDb().objectRegistry::template cfindObject<Type>
(
Type::typeName
);
if (ptr) return *objectPtr;
{
return *ptr;
}
else
{
if (meshObject::debug)
{
Pout<< "MeshObject::New(const " << Mesh::typeName
<< "&, const Data[1-2]&) : constructing " << Type::typeName
<< " for region " << mesh.name() << endl;
}
Type* objectPtr = new Type(mesh, d1, d2);
// Make sure to register the top level regIOobject for if Type itself
// is a regIOobject
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
return *objectPtr;
}
}
template<class Mesh, template<class> class MeshObjectType, class Type>
template<class Data1, class Data2, class Data3>
const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
(
const Mesh& mesh,
const Data1& d1,
const Data2& d2,
const Data3& d3
)
{
const Type* ptr = mesh.thisDb().objectRegistry::template cfindObject<Type>
(
Type::typeName
);
if (ptr)
{
return *ptr;
}
else
{
if (meshObject::debug)
{
Pout<< "MeshObject::New(const " << Mesh::typeName
<< "&, const Data[1-3]&) : constructing " << Type::typeName
<< " for region " << mesh.name() << endl;
}
Type* objectPtr = new Type(mesh, d1, d2, d3);
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
return *objectPtr;
}
}
template<class Mesh, template<class> class MeshObjectType, class Type>
template<class Data1, class Data2, class Data3, class Data4>
const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
(
const Mesh& mesh,
const Data1& d1,
const Data2& d2,
const Data3& d3,
const Data4& d4
)
{
const Type* ptr = mesh.thisDb().objectRegistry::template cfindObject<Type>
(
Type::typeName
);
if (ptr)
{
return *ptr;
}
else
{
if (meshObject::debug)
{
Pout<< "MeshObject::New(const " << Mesh::typeName
<< "&, const Data[1-4]&) : constructing " << Type::typeName
<< " for region " << mesh.name() << endl;
}
Type* objectPtr = new Type(mesh, d1, d2, d3, d4);
regIOobject::store(static_cast<MeshObjectType<Mesh>*>(objectPtr));
return *objectPtr;
}
} }
@ -223,10 +78,11 @@ const Type& Foam::MeshObject<Mesh, MeshObjectType, Type>::New
template<class Mesh, template<class> class MeshObjectType, class Type> template<class Mesh, template<class> class MeshObjectType, class Type>
bool Foam::MeshObject<Mesh, MeshObjectType, Type>::Delete(const Mesh& mesh) bool Foam::MeshObject<Mesh, MeshObjectType, Type>::Delete(const Mesh& mesh)
{ {
const Type* ptr = mesh.thisDb().objectRegistry::template findObject<Type> Type* ptr =
( mesh.thisDb().objectRegistry::template getObjectPtr<Type>
Type::typeName (
); Type::typeName
);
if (ptr) if (ptr)
{ {
@ -236,12 +92,10 @@ bool Foam::MeshObject<Mesh, MeshObjectType, Type>::Delete(const Mesh& mesh)
<< Type::typeName << endl; << Type::typeName << endl;
} }
return mesh.thisDb().checkOut(const_cast<Type&>(*ptr)); return mesh.thisDb().checkOut(*ptr);
}
else
{
return false;
} }
return false;
} }
@ -267,20 +121,19 @@ void Foam::meshObject::movePoints(objectRegistry& obr)
<< " meshObjects for region " << obr.name() << endl; << " meshObjects for region " << obr.name() << endl;
} }
forAllIter forAllIters(meshObjects, iter)
(
typename HashTable<GeometricMeshObject<Mesh>*>,
meshObjects,
iter
)
{ {
if (isA<MoveableMeshObject<Mesh>>(*iter())) // Same as (isA<MoveableMeshObject<Mesh>>(*iter()))
auto* objectPtr = dynamic_cast<MoveableMeshObject<Mesh>*>(iter());
if (objectPtr)
{ {
if (meshObject::debug) if (meshObject::debug)
{ {
Pout<< " Moving " << iter()->name() << endl; Pout<< " Moving " << iter()->name() << endl;
} }
dynamic_cast<MoveableMeshObject<Mesh>*>(iter())->movePoints(); objectPtr->movePoints();
} }
else else
{ {
@ -309,20 +162,19 @@ void Foam::meshObject::updateMesh(objectRegistry& obr, const mapPolyMesh& mpm)
<< " meshObjects for region " << obr.name() << endl; << " meshObjects for region " << obr.name() << endl;
} }
forAllIter forAllIters(meshObjects, iter)
(
typename HashTable<GeometricMeshObject<Mesh>*>,
meshObjects,
iter
)
{ {
if (isA<UpdateableMeshObject<Mesh>>(*iter())) // Same as (isA<UpdateableMeshObject<Mesh>>(*iter()))
auto* objectPtr = dynamic_cast<UpdateableMeshObject<Mesh>*>(iter());
if (objectPtr)
{ {
if (meshObject::debug) if (meshObject::debug)
{ {
Pout<< " Updating " << iter()->name() << endl; Pout<< " Updating " << iter()->name() << endl;
} }
dynamic_cast<UpdateableMeshObject<Mesh>*>(iter())->updateMesh(mpm); objectPtr->updateMesh(mpm);
} }
else else
{ {
@ -351,7 +203,7 @@ void Foam::meshObject::clear(objectRegistry& obr)
<< " meshObjects for region " << obr.name() << endl; << " meshObjects for region " << obr.name() << endl;
} }
forAllIter(typename HashTable<MeshObjectType<Mesh>*>, meshObjects, iter) forAllIters(meshObjects, iter)
{ {
if (meshObject::debug) if (meshObject::debug)
{ {
@ -382,7 +234,7 @@ void Foam::meshObject::clearUpto(objectRegistry& obr)
<< " meshObjects for region " << obr.name() << endl; << " meshObjects for region " << obr.name() << endl;
} }
forAllIter(typename HashTable<FromType<Mesh>*>, meshObjects, iter) forAllIters(meshObjects, iter)
{ {
if (!isA<ToType<Mesh>>(*iter())) if (!isA<ToType<Mesh>>(*iter()))
{ {

View File

@ -3,7 +3,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) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -100,47 +100,17 @@ public:
explicit MeshObject(const Mesh& mesh); explicit MeshObject(const Mesh& mesh);
static const Type& New(const Mesh& mesh); // Selectors
template<class Data1> //- Get existing or create a new MeshObject
static const Type& New template<class... Args>
( static const Type& New(const Mesh& mesh, Args&&... args);
const Mesh& mesh,
const Data1& d
);
template<class Data1, class Data2>
static const Type& New
(
const Mesh& mesh,
const Data1&,
const Data2&
);
template<class Data1, class Data2, class Data3>
static const Type& New
(
const Mesh& mesh,
const Data1&,
const Data2&,
const Data3&
);
template<class Data1, class Data2, class Data3, class Data4>
static const Type& New
(
const Mesh& mesh,
const Data1&,
const Data2&,
const Data3&,
const Data4&
);
// Destructor //- Destructor
virtual ~MeshObject();
virtual ~MeshObject();
//- Static destructor
static bool Delete(const Mesh& mesh); static bool Delete(const Mesh& mesh);