From 3772ad279e59f8ce068cdde8fdd4bb2a22892ad5 Mon Sep 17 00:00:00 2001 From: Max Kahnt <max.kahnt@fu-berlin.de> Date: Thu, 27 Apr 2017 13:13:53 +0200 Subject: [PATCH] Pass grid ownership with unique_ptr for clarity. Grid creation methods hand over the ownership. --- dune/fufem/makehalfcircle.hh | 4 +- dune/fufem/makering.hh | 49 +++++++++++++----------- dune/fufem/makesphere.hh | 12 +++--- dune/fufem/test/adolctest.cc | 2 +- dune/fufem/test/gridconstructiontest.cc | 20 +++------- dune/fufem/utilities/gridconstruction.hh | 26 +++++++------ 6 files changed, 57 insertions(+), 56 deletions(-) diff --git a/dune/fufem/makehalfcircle.hh b/dune/fufem/makehalfcircle.hh index afda135f..93e9402b 100644 --- a/dune/fufem/makehalfcircle.hh +++ b/dune/fufem/makehalfcircle.hh @@ -13,7 +13,7 @@ #include "arcofcircle.hh" template<class GridType> -GridType* createCircle(const Dune::FieldVector<double,2>& center, double r) +std::unique_ptr<GridType> createCircle(const Dune::FieldVector<double,2>& center, double r) { static_assert(GridType::dimension==2, "createCircle can only be instantiated for 2d grids"); @@ -91,7 +91,7 @@ GridType* createCircle(const Dune::FieldVector<double,2>& center, double r) // ////////////////////////////////////// // Finish initialization // ////////////////////////////////////// - return factory.createGrid(); + return std::unique_ptr<GridType>(factory.createGrid()); } diff --git a/dune/fufem/makering.hh b/dune/fufem/makering.hh index 219b322d..9afe6427 100644 --- a/dune/fufem/makering.hh +++ b/dune/fufem/makering.hh @@ -3,6 +3,7 @@ #ifndef MAKE_RING_HH #define MAKE_RING_HH +#include <memory> #include <vector> #include <dune/common/shared_ptr.hh> @@ -250,16 +251,18 @@ void makeRingSegment2D(const Dune::FieldVector<typename GridType::ctype,2>& cent * \param outerBoundary - If given, make a level 0 boundary patch containing the outer nodes * */ -template<class GridType> -GridType* makeTubeSegment3D(const Dune::FieldVector<typename GridType::ctype,3>& center, - const typename GridType::ctype thickness, const typename GridType::ctype length, - const typename GridType::ctype innerRadius, - const typename GridType::ctype fromAngle, const typename GridType::ctype toAngle, - unsigned int nElementRing, unsigned int nElementLength, bool closeTube, - unsigned int axis = 0, - bool tetrahedra=true, bool parametrizedBoundary=true, - BoundaryPatch<typename GridType::LevelGridView>* innerBoundary = NULL, - BoundaryPatch<typename GridType::LevelGridView>* outerBoundary = NULL) +template <class GridType> +std::unique_ptr<GridType> makeTubeSegment3D( + const Dune::FieldVector<typename GridType::ctype, 3>& center, + const typename GridType::ctype thickness, + const typename GridType::ctype length, + const typename GridType::ctype innerRadius, + const typename GridType::ctype fromAngle, + const typename GridType::ctype toAngle, unsigned int nElementRing, + unsigned int nElementLength, bool closeTube, unsigned int axis = 0, + bool tetrahedra = true, bool parametrizedBoundary = true, + BoundaryPatch<typename GridType::LevelGridView> * innerBoundary = NULL, + BoundaryPatch<typename GridType::LevelGridView> * outerBoundary = NULL) { static_assert(GridType::dimension==3, "makeTubeSegment3D can only be called for 3-dimensional grids"); typedef typename GridType::ctype ctype; @@ -570,7 +573,7 @@ GridType* makeTubeSegment3D(const Dune::FieldVector<typename GridType::ctype,3>& // ////////////////////////////////////// // Finish initialization // ////////////////////////////////////// - GridType* grid = factory.createGrid(); + std::unique_ptr<GridType> grid = std::unique_ptr<GridType>(factory.createGrid()); // create boundarypatches if desired if (innerBoundary) { @@ -618,17 +621,19 @@ GridType* makeTubeSegment3D(const Dune::FieldVector<typename GridType::ctype,3>& * \param outerBoundary - If given, make a level 0 boundary patch containing the outer nodes * */ -template<class GridType> -GridType* makeTorus(const Dune::FieldVector<typename GridType::ctype,3>& center, - const std::array<Dune::FieldVector<typename GridType::ctype,3>,3 > plane, - const typename GridType::ctype thickness, - const typename GridType::ctype horizontalRadius, - const typename GridType::ctype verticalRadius, - unsigned int nElementHorizontal, unsigned int nElementVertical, - BoundaryPatch<typename GridType::LevelGridView>* innerBoundary = NULL, - BoundaryPatch<typename GridType::LevelGridView>* outerBoundary = NULL) +template <class GridType> +std::unique_ptr<GridType> makeTorus( + const Dune::FieldVector<typename GridType::ctype, 3>& center, + const std::array<Dune::FieldVector<typename GridType::ctype, 3>, 3> plane, + const typename GridType::ctype thickness, + const typename GridType::ctype horizontalRadius, + const typename GridType::ctype verticalRadius, + unsigned int nElementHorizontal, unsigned int nElementVertical, + BoundaryPatch<typename GridType::LevelGridView>* innerBoundary = NULL, + BoundaryPatch<typename GridType::LevelGridView> * outerBoundary = NULL) { - static_assert(GridType::dimension==3, "makeTorus can only be called for 3-dimensional grids"); + static_assert(GridType::dimension == 3, + "makeTorus can only be called for 3-dimensional grids"); using ctype = typename GridType::ctype; using FV = Dune::FieldVector<ctype,3>; @@ -797,7 +802,7 @@ GridType* makeTorus(const Dune::FieldVector<typename GridType::ctype,3>& center, // ////////////////////////////////////// // Finish initialization // ////////////////////////////////////// - GridType* grid = factory.createGrid(); + std::unique_ptr<GridType> grid = std::unique_ptr<GridType>(factory.createGrid()); // create boundarypatches if desired if (innerBoundary) { diff --git a/dune/fufem/makesphere.hh b/dune/fufem/makesphere.hh index f6affb35..2abf563b 100644 --- a/dune/fufem/makesphere.hh +++ b/dune/fufem/makesphere.hh @@ -84,8 +84,10 @@ public: //! Create sphere grid from a cube and parameterised boundaries template <class GridType, class field_type = double> -GridType* makeSphere(const Dune::FieldVector<field_type, 3>& center, - field_type radius) { +std::unique_ptr<GridType> makeSphere( + const Dune::FieldVector<field_type, 3>& center, + field_type radius) +{ using namespace Dune; Dune::GridFactory<GridType> factory; @@ -132,12 +134,12 @@ GridType* makeSphere(const Dune::FieldVector<field_type, 3>& center, factory.insertElement(GeometryType(GeometryType::simplex,3), cornerIDs); } - return factory.createGrid(); + return std::unique_ptr<GridType>(factory.createGrid()); } //! Create sphere grid from an octahedron and parameterised boundaries template <class GridType, class field_type = double> -GridType* makeSphereOnOctahedron(const Dune::FieldVector<field_type, 3>& center, +std::unique_ptr<GridType> makeSphereOnOctahedron(const Dune::FieldVector<field_type, 3>& center, field_type radius) { using namespace Dune; @@ -182,7 +184,7 @@ GridType* makeSphereOnOctahedron(const Dune::FieldVector<field_type, 3>& center, factory.insertElement(GeometryType(GeometryType::simplex,3), cornerIDs); } - return factory.createGrid(); + return std::unique_ptr<GridType>(factory.createGrid()); } #endif diff --git a/dune/fufem/test/adolctest.cc b/dune/fufem/test/adolctest.cc index ccee4ceb..fa8c9ed0 100644 --- a/dune/fufem/test/adolctest.cc +++ b/dune/fufem/test/adolctest.cc @@ -168,7 +168,7 @@ int main (int argc, char *argv[]) // /////////////////////////////////////// typedef UGGrid<dim> GridType; - GridType* grid = makeSphere<GridType>(FVector(0),0.2); + std::unique_ptr<GridType> grid = makeSphere<GridType>(FVector(0),0.2); grid->globalRefine(3); typedef GridType::LeafGridView GridView; GridView gridView = grid->leafGridView(); diff --git a/dune/fufem/test/gridconstructiontest.cc b/dune/fufem/test/gridconstructiontest.cc index e33e8d15..127022db 100644 --- a/dune/fufem/test/gridconstructiontest.cc +++ b/dune/fufem/test/gridconstructiontest.cc @@ -23,20 +23,15 @@ struct GridConstructionTest { template <class Grid> bool test1D() const { using C = Dune::GridConstruction<Grid, 1>; - return test([this]() { - Grid* interval = C::createGrid(getIntervalConfig()); - delete (interval); - }); + return test([this]() { C::createGrid(getIntervalConfig()); }); } template <class Grid> bool test2D() const { using C = Dune::GridConstruction<Grid, 2>; return test([this]() { - Grid* rectangle = C::createGrid(getRectangleConfig()); - delete (rectangle); - Grid* circle = C::createGrid(getCircleConfig()); - delete (circle); + C::createGrid(getRectangleConfig()); + C::createGrid(getCircleConfig()); }); } @@ -44,12 +39,9 @@ struct GridConstructionTest { bool test3D() const { using C = Dune::GridConstruction<Grid, 3>; return test([this]() { - Grid* tubeSegment = C::createGrid(getTubeSegmentConfig()); - delete (tubeSegment); - Grid* sphere = C::createGrid(getSphereConfig()); - delete (sphere); - Grid* cuboid = C::createGrid(getCuboidConfig()); - delete (cuboid); + C::createGrid(getTubeSegmentConfig()); + C::createGrid(getSphereConfig()); + C::createGrid(getCuboidConfig()); }); } diff --git a/dune/fufem/utilities/gridconstruction.hh b/dune/fufem/utilities/gridconstruction.hh index 567f3438..a0053a07 100644 --- a/dune/fufem/utilities/gridconstruction.hh +++ b/dune/fufem/utilities/gridconstruction.hh @@ -3,6 +3,8 @@ #ifndef DUNE_FUFEM_UTILITIES_GRIDCONSTRUCTION_HH #define DUNE_FUFEM_UTILITIES_GRIDCONSTRUCTION_HH +#include <memory> + #include <dune/common/fvector.hh> #include <dune/common/parametertree.hh> #include <dune/grid/common/gridfactory.hh> @@ -32,7 +34,7 @@ struct GridConstruction<GridType,1> { using ctype = typename GridType::ctype; using FV = FieldVector<ctype,dim>; - static GridType* createGrid(const ParameterTree& config) { + static std::unique_ptr<GridType> createGrid(const ParameterTree& config) { auto type = config.get<std::string>("type"); if (type == "interval") return createInterval(config); @@ -40,7 +42,7 @@ struct GridConstruction<GridType,1> { DUNE_THROW(Exception,"There is no factory yet for dimension and type "+type); } - static GridType* createInterval(const ParameterTree& config) { + static std::unique_ptr<GridType> createInterval(const ParameterTree& config) { FV lower = config.get<FV>("lowerCorner"); FV upper = config.get<FV>("upperCorner"); @@ -65,7 +67,7 @@ struct GridConstruction<GridType,1> { factory.insertElement(gt, E({i, i+1})); } - return factory.createGrid(); + return std::unique_ptr<GridType>(factory.createGrid()); } }; @@ -83,7 +85,7 @@ public: * * \param config - Parameter file specifying additional parameter */ - static GridType* createGrid(const ParameterTree& config) { + static std::unique_ptr<GridType> createGrid(const ParameterTree& config) { auto type = config.get<std::string>("type"); if (type == "rectangle") @@ -95,7 +97,7 @@ public: } /** \brief Create a rectangular grid.*/ - static GridType* createRectangle(const ParameterTree& config) { + static std::unique_ptr<GridType> createRectangle(const ParameterTree& config) { FV lower = config.get<FV>("lowerCorner"); FV upper = config.get<FV>("upperCorner"); @@ -138,11 +140,11 @@ public: } } - return factory.createGrid(); + return std::unique_ptr<GridType>(factory.createGrid()); } /** \brief Create a circular grid.*/ - static GridType* createCircle(const ParameterTree& config) { + static std::unique_ptr<GridType> createCircle(const ParameterTree& config) { return ::createCircle<GridType>(config.get<FV>("center"), config.get<ctype>("radius")); } }; @@ -162,7 +164,7 @@ public: * \param config - Parameter file specifying additional parameter * \param */ - static GridType* createGrid(const ParameterTree& config) { + static std::unique_ptr<GridType> createGrid(const ParameterTree& config) { auto type = config.get<std::string>("type"); if (type == "tubeSegment") @@ -175,7 +177,7 @@ public: DUNE_THROW(Exception,"There is no factory for dimension and type "+type); } - static GridType* createTubeSegment(const ParameterTree& config) { + static std::unique_ptr<GridType> createTubeSegment(const ParameterTree& config) { return makeTubeSegment3D<GridType>(config.get<FV>("center"), config.get<ctype>("thickness"), config.get<ctype>("length"), config.get<ctype>("innerRadius"), @@ -189,7 +191,7 @@ public: * * */ - static GridType* createSphere(const ParameterTree& config) { + static std::unique_ptr<GridType> createSphere(const ParameterTree& config) { return makeSphereOnOctahedron<GridType>(config.get<FV>("center"), config.get<ctype>("radius")); } @@ -198,7 +200,7 @@ public: * * */ - static GridType* createCuboid(const ParameterTree& config) { + static std::unique_ptr<GridType> createCuboid(const ParameterTree& config) { FV lower = config.get<FV>("lowerCorner"); FV upper = config.get<FV>("upperCorner"); @@ -258,7 +260,7 @@ public: } } } - return factory.createGrid(); + return std::unique_ptr<GridType>(factory.createGrid()); } }; -- GitLab