From c78e3428d193d967073b86dc1a064d9d8d0272ae Mon Sep 17 00:00:00 2001 From: Sebastian Eibl <sebastian.eibl@fau.de> Date: Wed, 13 Jun 2018 10:56:22 +0200 Subject: [PATCH] replaced memory allocation in factories with smart pointers --- .../pe/rigid_body/ConvexPolyhedronFactory.cpp | 21 +++++------ src/pe/rigidbody/BoxFactory.cpp | 21 +++++------ src/pe/rigidbody/CapsuleFactory.cpp | 21 +++++------ .../rigidbody/CylindricalBoundaryFactory.cpp | 4 +- src/pe/rigidbody/EllipsoidFactory.cpp | 37 +++++++++---------- src/pe/rigidbody/PlaneFactory.cpp | 5 +-- src/pe/rigidbody/SphereFactory.cpp | 21 +++++------ src/pe/rigidbody/SquirmerFactory.cpp | 21 +++++------ src/pe/rigidbody/UnionFactory.h | 21 +++++------ 9 files changed, 82 insertions(+), 90 deletions(-) diff --git a/src/mesh/pe/rigid_body/ConvexPolyhedronFactory.cpp b/src/mesh/pe/rigid_body/ConvexPolyhedronFactory.cpp index 9919e2f6a..63c268ae6 100644 --- a/src/mesh/pe/rigid_body/ConvexPolyhedronFactory.cpp +++ b/src/mesh/pe/rigid_body/ConvexPolyhedronFactory.cpp @@ -65,7 +65,7 @@ ConvexPolyhedronID createConvexPolyhedron( BodyStorage& globalStorage, BlockStor { WALBERLA_ASSERT_UNEQUAL( ConvexPolyhedron::getStaticTypeID(), std::numeric_limits<id_t>::max(), "ConvexPolyhedron TypeID not initalized!"); - ConvexPolyhedronID poly = NULL; + ConvexPolyhedronID poly = nullptr; Vec3 centroid = toWalberla( computeCentroid( mesh ) ); translate( mesh, -centroid ); @@ -78,25 +78,24 @@ ConvexPolyhedronID createConvexPolyhedron( BodyStorage& globalStorage, BlockStor WALBERLA_CHECK_EQUAL(communicating, false, "Global bodies can not be communicating!" ); WALBERLA_CHECK_EQUAL(infiniteMass, true, "Global bodies must have infinite mass!" ); - poly = new ConvexPolyhedron(sid, uid, gpos, Vec3(0,0,0), Quat(), mesh, material, global, false, true); - globalStorage.add(poly); + auto cp = std::make_unique<ConvexPolyhedron>(sid, uid, gpos, Vec3(0,0,0), Quat(), mesh, material, global, false, true); + poly = static_cast<ConvexPolyhedronID>(&globalStorage.add(std::move(cp))); } else { - for (auto it = blocks.begin(); it != blocks.end(); ++it){ - IBlock* block = (&(*it)); - if (block->getAABB().contains(gpos)) + for (auto& block : blocks){ + if (block.getAABB().contains(gpos)) { const id_t sid( UniqueID<RigidBody>::create() ); - Storage* bs = block->getData<Storage>(storageID); - poly = new ConvexPolyhedron(sid, uid, gpos, Vec3(0,0,0), Quat(), mesh, material, global, communicating, infiniteMass); - poly->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block->getId().getID())); - (*bs)[0].add(poly); + BodyStorage& bs = (*block.getData<Storage>(storageID))[0]; + auto cp = std::make_unique<ConvexPolyhedron>(sid, uid, gpos, Vec3(0,0,0), Quat(), mesh, material, global, communicating, infiniteMass); + cp->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block.getId().getID())); + poly = static_cast<ConvexPolyhedronID>(&bs.add( std::move(cp) ) ); } } } - if (poly != NULL) + if (poly != nullptr) { // Logging the successful creation of the box WALBERLA_LOG_DETAIL( diff --git a/src/pe/rigidbody/BoxFactory.cpp b/src/pe/rigidbody/BoxFactory.cpp index 7ef2109e4..6f471ba55 100644 --- a/src/pe/rigidbody/BoxFactory.cpp +++ b/src/pe/rigidbody/BoxFactory.cpp @@ -43,32 +43,31 @@ BoxID createBox( BodyStorage& globalStorage, BlockStorage& blocks, BlockDa if( lengths[0] <= real_t(0) || lengths[1] <= real_t(0) || lengths[2] <= real_t(0) ) throw std::invalid_argument( "Invalid side length" ); - BoxID box = NULL; + BoxID box = nullptr; if (global) { const id_t sid = UniqueID<RigidBody>::createGlobal(); WALBERLA_ASSERT_EQUAL(communicating, false); WALBERLA_ASSERT_EQUAL(infiniteMass, true); - box = new Box(sid, uid, gpos, Vec3(0,0,0), Quat(), lengths, material, global, false, true); - globalStorage.add(box); + BoxPtr bx = std::make_unique<Box>(sid, uid, gpos, Vec3(0,0,0), Quat(), lengths, material, global, false, true); + box = static_cast<BoxID>(&globalStorage.add(std::move(bx))); } else { - for (auto it = blocks.begin(); it != blocks.end(); ++it){ - IBlock* block = (&(*it)); - if (block->getAABB().contains(gpos)) + for (auto& block : blocks){ + if (block.getAABB().contains(gpos)) { const id_t sid( UniqueID<RigidBody>::create() ); - Storage* bs = block->getData<Storage>(storageID); - box = new Box(sid, uid, gpos, Vec3(0,0,0), Quat(), lengths, material, global, communicating, infiniteMass); - box->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block->getId().getID())); - (*bs)[0].add(box); + BodyStorage& bs = (*block.getData<Storage>(storageID))[0]; + BoxPtr bx = std::make_unique<Box>(sid, uid, gpos, Vec3(0,0,0), Quat(), lengths, material, global, communicating, infiniteMass); + bx->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block.getId().getID())); + box = static_cast<BoxID>(&bs.add(std::move(bx))); } } } - if (box != NULL) + if (box != nullptr) { // Logging the successful creation of the box WALBERLA_LOG_DETAIL( diff --git a/src/pe/rigidbody/CapsuleFactory.cpp b/src/pe/rigidbody/CapsuleFactory.cpp index 57805661d..325af9701 100644 --- a/src/pe/rigidbody/CapsuleFactory.cpp +++ b/src/pe/rigidbody/CapsuleFactory.cpp @@ -43,32 +43,31 @@ CapsuleID createCapsule( BodyStorage& globalStorage, BlockStorage& blocks, Blo WALBERLA_ASSERT_GREATER( radius, real_t(0), "Invalid capsule radius" ); WALBERLA_ASSERT_GREATER( length, real_t(0), "Invalid capsule length" ); - CapsuleID capsule = NULL; + CapsuleID capsule = nullptr; if (global) { const id_t sid = UniqueID<RigidBody>::createGlobal(); WALBERLA_ASSERT_EQUAL(communicating, false); WALBERLA_ASSERT_EQUAL(infiniteMass, true); - capsule = new Capsule(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, length, material, global, false, true); - globalStorage.add(capsule); + CapsulePtr cp = std::make_unique<Capsule>(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, length, material, global, false, true); + capsule = static_cast<CapsuleID>(&globalStorage.add(std::move(cp))); } else { - for (auto it = blocks.begin(); it != blocks.end(); ++it){ - IBlock* block = (&(*it)); - if (block->getAABB().contains(gpos)) + for (auto& block : blocks){ + if (block.getAABB().contains(gpos)) { const id_t sid( UniqueID<RigidBody>::create() ); - Storage* bs = block->getData<Storage>(storageID); - capsule = new Capsule(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, length, material, global, communicating, infiniteMass); - capsule->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block->getId().getID())); - (*bs)[0].add(capsule); + BodyStorage& bs = (*block.getData<Storage>(storageID))[0]; + CapsulePtr cp = std::make_unique<Capsule>(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, length, material, global, communicating, infiniteMass); + cp->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block.getId().getID())); + capsule = static_cast<CapsuleID>(&bs.add( std::move(cp) )); } } } - if (capsule != NULL) + if (capsule != nullptr) { // Logging the successful creation of the box WALBERLA_LOG_DETAIL( diff --git a/src/pe/rigidbody/CylindricalBoundaryFactory.cpp b/src/pe/rigidbody/CylindricalBoundaryFactory.cpp index e43c94ecd..6cbf752cb 100644 --- a/src/pe/rigidbody/CylindricalBoundaryFactory.cpp +++ b/src/pe/rigidbody/CylindricalBoundaryFactory.cpp @@ -38,9 +38,9 @@ CylindricalBoundaryID createCylindricalBoundary( BodyStorage& globalStorage, const id_t sid( UniqueID<RigidBody>::createGlobal() ); - CylindricalBoundaryID cb = new CylindricalBoundary( sid, uid, gpos, radius, material ); + CylindricalBoundaryPtr cbPtr = std::make_unique<CylindricalBoundary>( sid, uid, gpos, radius, material ); - globalStorage.add(cb); + CylindricalBoundaryID cb = static_cast<CylindricalBoundaryID>(&globalStorage.add(std::move(cbPtr))); // Logging the successful creation of the plane WALBERLA_LOG_DETAIL( "Created cylindrical boundary " << sid << "\n" << cb); diff --git a/src/pe/rigidbody/EllipsoidFactory.cpp b/src/pe/rigidbody/EllipsoidFactory.cpp index 47fae3c94..8e8835b23 100644 --- a/src/pe/rigidbody/EllipsoidFactory.cpp +++ b/src/pe/rigidbody/EllipsoidFactory.cpp @@ -32,36 +32,35 @@ namespace walberla { namespace pe { EllipsoidID createEllipsoid( BodyStorage& globalStorage, BlockStorage& blocks, BlockDataID storageID, - id_t uid, const Vec3& gpos, const Vec3& semiAxes, - MaterialID material, - bool global, bool communicating, bool infiniteMass ) + id_t uid, const Vec3& gpos, const Vec3& semiAxes, + MaterialID material, + bool global, bool communicating, bool infiniteMass ) { WALBERLA_ASSERT_UNEQUAL( Ellipsoid::getStaticTypeID(), std::numeric_limits<id_t>::max(), "Ellipsoid TypeID not initalized!"); // Checking the semiAxes if( semiAxes[0] <= real_c(0) || semiAxes[1] <= real_c(0) || semiAxes[2] <= real_c(0) ) throw std::invalid_argument( "Invalid Ellipsoid semi-axes" ); - EllipsoidID ellipsoid = NULL; + EllipsoidID ellipsoid = nullptr; if (global) { const id_t sid = UniqueID<RigidBody>::createGlobal(); WALBERLA_ASSERT_EQUAL(communicating, false); WALBERLA_ASSERT_EQUAL(infiniteMass, true); - ellipsoid = new Ellipsoid(sid, uid, gpos, Vec3(0,0,0), Quat(), semiAxes, material, global, false, true); - globalStorage.add(ellipsoid); + EllipsoidPtr el = std::make_unique<Ellipsoid>(sid, uid, gpos, Vec3(0,0,0), Quat(), semiAxes, material, global, false, true); + ellipsoid = static_cast<EllipsoidID>(&globalStorage.add(std::move(el))); } else { - for (auto it = blocks.begin(); it != blocks.end(); ++it){ - IBlock* block = (&(*it)); - if (block->getAABB().contains(gpos)) + for (auto& block : blocks){ + if (block.getAABB().contains(gpos)) { const id_t sid( UniqueID<RigidBody>::create() ); - Storage* bs = block->getData<Storage>(storageID); - ellipsoid = new Ellipsoid(sid, uid, gpos, Vec3(0,0,0), Quat(), semiAxes, material, global, communicating, infiniteMass); - ellipsoid->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block->getId().getID())); - (*bs)[0].add(ellipsoid); + BodyStorage& bs = (*block.getData<Storage>(storageID))[0]; + EllipsoidPtr el = std::make_unique<Ellipsoid>(sid, uid, gpos, Vec3(0,0,0), Quat(), semiAxes, material, global, communicating, infiniteMass); + el->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block.getId().getID())); + ellipsoid = static_cast<EllipsoidID>(&bs.add(std::move(el))); } } } @@ -70,12 +69,12 @@ EllipsoidID createEllipsoid( BodyStorage& globalStorage, BlockStorage& blocks, B { // Logging the successful creation of the Ellipsoid WALBERLA_LOG_DETAIL( - "Created Ellipsoid " << ellipsoid->getSystemID() << "\n" - << " User-ID = " << uid << "\n" - << " Global position = " << gpos << "\n" - << " Semi-axes = " << semiAxes << "\n" - << " LinVel = " << ellipsoid->getLinearVel() << "\n" - << " Material = " << Material::getName( material ) + "Created Ellipsoid " << ellipsoid->getSystemID() << "\n" + << " User-ID = " << uid << "\n" + << " Global position = " << gpos << "\n" + << " Semi-axes = " << semiAxes << "\n" + << " LinVel = " << ellipsoid->getLinearVel() << "\n" + << " Material = " << Material::getName( material ) ); } diff --git a/src/pe/rigidbody/PlaneFactory.cpp b/src/pe/rigidbody/PlaneFactory.cpp index bb35cd1e7..650054ee0 100644 --- a/src/pe/rigidbody/PlaneFactory.cpp +++ b/src/pe/rigidbody/PlaneFactory.cpp @@ -45,9 +45,8 @@ PlaneID createPlane( BodyStorage& globalStorage, id_t uid, Vec3 normal, const Ve const id_t sid( UniqueID<RigidBody>::createGlobal() ); - PlaneID plane = new Plane( sid, uid, gpos, normal, normal*gpos, material ); - - globalStorage.add(plane); + PlanePtr pl = std::make_unique<Plane>( sid, uid, gpos, normal, normal*gpos, material ); + PlaneID plane = static_cast<PlaneID>(&globalStorage.add(std::move(pl))); // Logging the successful creation of the plane WALBERLA_LOG_DETAIL( "Created plane " << sid << "\n" diff --git a/src/pe/rigidbody/SphereFactory.cpp b/src/pe/rigidbody/SphereFactory.cpp index e32305773..d88774c45 100644 --- a/src/pe/rigidbody/SphereFactory.cpp +++ b/src/pe/rigidbody/SphereFactory.cpp @@ -42,32 +42,31 @@ SphereID createSphere( BodyStorage& globalStorage, BlockStorage& blocks, BlockDa if( radius <= real_c(0) ) throw std::invalid_argument( "Invalid sphere radius" ); - SphereID sphere = NULL; + SphereID sphere = nullptr; if (global) { const id_t sid = UniqueID<RigidBody>::createGlobal(); WALBERLA_ASSERT_EQUAL(communicating, false); WALBERLA_ASSERT_EQUAL(infiniteMass, true); - sphere = new Sphere(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, material, global, false, true); - globalStorage.add(sphere); + SpherePtr sp = std::make_unique<Sphere>(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, material, global, false, true); + sphere = static_cast<SphereID>(&globalStorage.add( std::move(sp) )); } else { - for (auto it = blocks.begin(); it != blocks.end(); ++it){ - IBlock* block = (&(*it)); - if (block->getAABB().contains(gpos)) + for (auto& block : blocks){ + if (block.getAABB().contains(gpos)) { const id_t sid( UniqueID<RigidBody>::create() ); - Storage* bs = block->getData<Storage>(storageID); - sphere = new Sphere(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, material, global, communicating, infiniteMass); - sphere->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block->getId().getID())); - (*bs)[0].add(sphere); + BodyStorage& bs = (*block.getData<Storage>(storageID))[0]; + SpherePtr sp = std::make_unique<Sphere>(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, material, global, communicating, infiniteMass); + sp->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block.getId().getID())); + sphere = static_cast<SphereID>(&bs.add( std::move(sp) )); } } } - if (sphere != NULL) + if (sphere != nullptr) { // Logging the successful creation of the sphere WALBERLA_LOG_DETAIL( diff --git a/src/pe/rigidbody/SquirmerFactory.cpp b/src/pe/rigidbody/SquirmerFactory.cpp index 7b74f622c..7160ff2d0 100644 --- a/src/pe/rigidbody/SquirmerFactory.cpp +++ b/src/pe/rigidbody/SquirmerFactory.cpp @@ -41,32 +41,31 @@ SquirmerID createSquirmer( BodyStorage& globalStorage, BlockStorage& blocks, Blo if( radius <= real_c(0) ) throw std::invalid_argument( "Invalid squirmer radius" ); - SquirmerID squirmer = NULL; + SquirmerID squirmer = nullptr; if (global) { const id_t sid = UniqueID<RigidBody>::createGlobal(); WALBERLA_ASSERT_EQUAL(communicating, false); WALBERLA_ASSERT_EQUAL(infiniteMass, true); - squirmer = new Squirmer(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, squirmerVelocity, squirmerBeta, material, global, false, true); - globalStorage.add(squirmer); + SquirmerPtr sq = std::make_unique<Squirmer>(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, squirmerVelocity, squirmerBeta, material, global, false, true); + squirmer = static_cast<SquirmerID>(&globalStorage.add(std::move(sq))); } else { - for (auto it = blocks.begin(); it != blocks.end(); ++it){ - IBlock* block = (&(*it)); - if (block->getAABB().contains(gpos)) + for (auto& block : blocks){ + if (block.getAABB().contains(gpos)) { const id_t sid( UniqueID<RigidBody>::create() ); - Storage* bs = block->getData<Storage>(storageID); - squirmer = new Squirmer(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, squirmerVelocity, squirmerBeta, material, global, communicating, infiniteMass); - squirmer->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block->getId().getID())); - (*bs)[0].add(squirmer); + BodyStorage& bs = (*block.getData<Storage>(storageID))[0]; + SquirmerPtr sq = std::make_unique<Squirmer>(sid, uid, gpos, Vec3(0,0,0), Quat(), radius, squirmerVelocity, squirmerBeta, material, global, communicating, infiniteMass); + sq->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block.getId().getID())); + squirmer = static_cast<SquirmerID>(&bs.add( std::move(sq) )); } } } - if (squirmer != NULL) + if (squirmer != nullptr) { // Logging the successful creation of the squirmer WALBERLA_LOG_DETAIL( diff --git a/src/pe/rigidbody/UnionFactory.h b/src/pe/rigidbody/UnionFactory.h index 45c01d4a4..eeff2ffe8 100644 --- a/src/pe/rigidbody/UnionFactory.h +++ b/src/pe/rigidbody/UnionFactory.h @@ -75,32 +75,31 @@ Union<BodyTypeTuple>* createUnion( BodyStorage& globalStorage, BlockStorage& b if (Union<BodyTypeTuple>::getStaticTypeID() == std::numeric_limits<id_t>::max()) throw std::runtime_error("Union TypeID not initalized!"); - Union<BodyTypeTuple>* bd = NULL; + Union<BodyTypeTuple>* bd = nullptr; if (global) { const id_t sid = UniqueID<RigidBody>::createGlobal(); WALBERLA_ASSERT_EQUAL(communicating, false); WALBERLA_ASSERT_EQUAL(infiniteMass, true); - bd = new Union<BodyTypeTuple>(sid, uid, gpos, Vec3(0,0,0), Quat(), global, false, true); - globalStorage.add(bd); + auto un = std::make_unique<Union<BodyTypeTuple>>(sid, uid, gpos, Vec3(0,0,0), Quat(), global, false, true); + bd = static_cast<Union<BodyTypeTuple>*>(&globalStorage.add(std::move(un))); } else { - for (auto it = blocks.begin(); it != blocks.end(); ++it){ - IBlock* block = (&(*it)); - if (block->getAABB().contains(gpos)) + for (auto& block : blocks){ + if (block.getAABB().contains(gpos)) { const id_t sid( UniqueID<RigidBody>::create() ); - Storage* bs = block->getData<Storage>(storageID); - bd = new Union<BodyTypeTuple>(sid, uid, gpos, Vec3(0,0,0), Quat(), global, communicating, infiniteMass); - bd->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block->getId().getID())); - (*bs)[0].add(bd); + BodyStorage& bs = (*block.getData<Storage>(storageID))[0]; + auto un = std::make_unique<Union<BodyTypeTuple>>(sid, uid, gpos, Vec3(0,0,0), Quat(), global, communicating, infiniteMass); + un->MPITrait.setOwner(Owner(MPIManager::instance()->rank(), block.getId().getID())); + bd = static_cast<Union<BodyTypeTuple>*>(&bs.add(std::move(un))); } } } - if (bd != NULL) + if (bd != nullptr) { // Logging the successful creation of the box WALBERLA_LOG_DETAIL( -- GitLab