Commit 5c26f993 authored by Oliver Sander's avatar Oliver Sander

Let GridFactory::createGrid return ToUniquePtr

Until now, the method GridFactory::createGrid returned the grid object
it created as a plain C pointer.  That way, it was not obvious from
the API who was to keep the responsibility for eventually deleting
the object: the GridFactory or the calling code.  At the 2017 Dune
Dev Meeting in Heidelberg it was decided that the method should
really return a std::unique_ptr instead, to make it clear that the
calling code gets the ownership of the new object.  To ease the
transition towards the new interface, the class ToUniquePtr was
introduced in dune-common.  Objects of that type are std::unique_ptr
objects, but they also cast to C pointers.  That way, code that calls
createGrid can still store the return value in a C pointer (but will
trigger a deprecation warning).

However, there does not seem to be a way to ensure similar compatibility
for implementors of third-party grid implementations.  Those will have
to adapt their implementations of GridFactory::createGrid to the new
return value.
parent 11db7687
Pipeline #10604 passed with stage
in 36 minutes and 43 seconds
......@@ -27,6 +27,17 @@
can still store the return value in a C pointer, but that possibility
will go away.
- Likewise, the return type of the `GridFactory::createGrid`method has been changed
from a plain C pointer to the custom pointer class `ToUniquePtr<Grid>`.
In the long run, the method is planned to return objects of type
`std::unique_ptr`, to make it obvious that the calling code receives
the ownership of the grid object. For the time being the calling code
can still store the return value in a C pointer, but that possibility
will go away. While this procedure allows full backward compatibility
for code that calls `GridFactory::createGrid`, implementors or third-party
grid implementations will need to update their implementations of
`GridFactory::createGrid`.
# Release 2.6
- The deprecated `EntityPointer` has been removed completely and `EntityIterator`
......
......@@ -9,12 +9,14 @@
using the GridFactory.
*/
#include <dune/common/to_unique_ptr.hh>
#include <dune/grid/common/gridfactory.hh>
namespace Dune {
template <class GridType>
GridType* make2DHybridTestGrid()
ToUniquePtr<GridType> make2DHybridTestGrid()
{
static_assert(GridType::dimension == 2,
"Instantiate make2dHybridTestGrid only for 2d grids!");
......@@ -87,7 +89,7 @@ namespace Dune {
template <class GridType>
GridType* make3DHybridTestGrid()
ToUniquePtr<GridType> make3DHybridTestGrid()
{
static_assert(GridType::dimension == 3,
"Instantiate make3DHybridTestGrid only for 3d grids!");
......
......@@ -15,6 +15,8 @@
#include <map>
#include <memory>
#include <dune/common/to_unique_ptr.hh>
#include <dune/geometry/referenceelements.hh>
#include <dune/grid/common/gridfactory.hh>
......@@ -288,7 +290,7 @@ namespace Dune
* \note ALBERTA's grid factory provides a static method for freeing the
* grid (destroyGrid).
*/
Grid *createGrid ()
ToUniquePtr<Grid> createGrid ()
{
macroData_.finalize();
if( macroData_.elementCount() == 0 )
......
......@@ -12,6 +12,7 @@
#include <dune/common/function.hh>
#include <dune/common/fvector.hh>
#include <dune/common/to_unique_ptr.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/geometry/type.hh>
......@@ -153,7 +154,7 @@ namespace Dune
The receiver takes responsibility of the memory allocated for the grid
*/
virtual GridType* createGrid() = 0;
virtual ToUniquePtr<GridType> createGrid() = 0;
/** \brief obtain an element's insertion index
*
......@@ -318,7 +319,7 @@ namespace Dune
The receiver takes responsibility of the memory allocated for the grid
*/
virtual GridType* createGrid() {
virtual ToUniquePtr<GridType> createGrid() {
DUNE_THROW(GridError, "There is no grid factory for this grid type!");
}
......
......@@ -100,7 +100,7 @@ insertionIndex(const typename OneDGrid::LeafIntersection& intersection) const
return insertionIdx;
}
Dune::OneDGrid* Dune::GridFactory<Dune::OneDGrid>::
ToUniquePtr<OneDGrid> Dune::GridFactory<Dune::OneDGrid>::
createGrid()
{
// Prevent a crash when this method is called twice in a row
......
......@@ -18,6 +18,7 @@
#include <vector>
#include <dune/common/fvector.hh>
#include <dune/common/to_unique_ptr.hh>
#include <dune/grid/common/gridfactory.hh>
#include <dune/grid/onedgrid.hh>
......@@ -84,7 +85,7 @@ namespace Dune {
/** \brief Finalize grid creation and hand over the grid
The receiver takes responsibility of the memory allocated for the grid
*/
virtual OneDGrid* createGrid();
virtual ToUniquePtr<OneDGrid> createGrid();
private:
......
......@@ -26,11 +26,9 @@ void checkAlbertaReader ()
reader.readGrid( filename.str(), factory );
// create grid and just check the macro grid
Grid *grid = factory.createGrid();
auto grid = factory.createGrid();
grid->globalRefine( 2 );
gridcheck( *grid );
//GridFactory< Grid >::destroyGrid( grid );
delete grid;
}
#else
......
......@@ -106,13 +106,12 @@ void checkProjectedUnitCube ()
addToGridFactory( gridFactory, Dune::Dim< Grid::dimensionworld > () );
gridFactory.insertBoundaryProjection( new Projection );
gridFactory.markLongestEdge();
Grid *grid = gridFactory.createGrid();
auto grid = gridFactory.createGrid();
for( int i = 0; i < 2; ++i )
{
grid->globalRefine( Grid::dimension );
gridcheck( *grid );
}
delete grid;
}
......
......@@ -98,7 +98,14 @@ void makeHalfCircleQuad(Dune::UGGrid<2>& grid, bool boundarySegments, bool param
// //////////////////////////////////////
// Finish initialization
// //////////////////////////////////////
factory.createGrid();
// The factory returns the grid object we have given it as a std::unique_ptr.
// Release it, the keep the std::unique_ptr destructor from killing it at
// the end of this very method.
// Yes, that is a bit weird. But in particular the calling method tests whether
// the GridFactory can reuse an existing UGGrid object, and I would like to keep
// that for the time being.
factory.createGrid().release();
}
......
......@@ -216,7 +216,7 @@ insertBoundarySegment(const std::vector<unsigned int>& vertices,
}
template <int dimworld>
UGGrid<dimworld>* GridFactory<UGGrid<dimworld> >::
ToUniquePtr<UGGrid<dimworld>> GridFactory<UGGrid<dimworld> >::
createGrid()
{
// Prevent a crash when this method is called twice in a row
......
......@@ -14,6 +14,7 @@
#include <vector>
#include <dune/common/fvector.hh>
#include <dune/common/to_unique_ptr.hh>
#include <dune/grid/common/boundarysegment.hh>
#include <dune/grid/common/gridfactory.hh>
......@@ -216,7 +217,7 @@ namespace Dune {
The receiver takes responsibility of the memory allocated for the grid
*/
virtual UGGrid<dimworld>* createGrid();
virtual ToUniquePtr<UGGrid<dimworld>> createGrid();
static const int dimension = UGGrid<dimworld>::dimension;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment