]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make Mapping::clone return a unique_ptr<Mapping>.
authorDavid Wells <wellsd2@rpi.edu>
Sat, 10 Feb 2018 00:17:52 +0000 (19:17 -0500)
committerDavid Wells <wellsd2@rpi.edu>
Mon, 12 Feb 2018 16:19:40 +0000 (11:19 -0500)
This commit converts the current interface into one based on smart
pointers. It also gets rid of the use of a covariant return type in
MappingQ1::clone and MappingQ1Eulerian::clone.

21 files changed:
doc/news/changes/incompatibilities/20180208DavidWells [new file with mode: 0644]
include/deal.II/fe/mapping.h
include/deal.II/fe/mapping_c1.h
include/deal.II/fe/mapping_cartesian.h
include/deal.II/fe/mapping_fe_field.h
include/deal.II/fe/mapping_manifold.h
include/deal.II/fe/mapping_q.h
include/deal.II/fe/mapping_q1.h
include/deal.II/fe/mapping_q1_eulerian.h
include/deal.II/fe/mapping_q_eulerian.h
include/deal.II/fe/mapping_q_generic.h
source/fe/mapping_c1.cc
source/fe/mapping_cartesian.cc
source/fe/mapping_fe_field.cc
source/fe/mapping_manifold.cc
source/fe/mapping_q.cc
source/fe/mapping_q1.cc
source/fe/mapping_q1_eulerian.cc
source/fe/mapping_q_eulerian.cc
source/fe/mapping_q_generic.cc
tests/hp/mapping_collection_03.cc

diff --git a/doc/news/changes/incompatibilities/20180208DavidWells b/doc/news/changes/incompatibilities/20180208DavidWells
new file mode 100644 (file)
index 0000000..c66292b
--- /dev/null
@@ -0,0 +1,3 @@
+Changed: The function Mapping::clone (and all inheriting classes' implementations, such as MappingQ1Eulerian::clone) now return a <code>unique_ptr<Mapping<dim,spacedim>></code>.
+<br>
+(David Wells, 2018/02/08)
index 8ccc3d71cf50f267219d18646eebb9f620e1f5d7..558ad2239396da68125ebf7de6800c810621d536 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <array>
 #include <cmath>
+#include <memory>
 
 DEAL_II_NAMESPACE_OPEN
 
@@ -303,7 +304,7 @@ public:
    * This function is mainly used by the hp::MappingCollection class.
    */
   virtual
-  Mapping<dim,spacedim> *clone () const = 0;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const = 0;
 
   /**
    * Return the mapped vertices of a cell.
index c240165b25698bfbef87a18b8082d3ac79e79252..7406099ddd2a4dc9c16df765dee627678ae62449 100644 (file)
@@ -50,7 +50,7 @@ public:
    * then assumes ownership of it.
    */
   virtual
-  Mapping<dim,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 
 protected:
 
index 5ef8c219abf796533e410445ce8a4bac5f343eba..9cbdfd35ec25f2bf673bd4c260403a67523e9a76 100644 (file)
@@ -64,7 +64,7 @@ public:
 
   // for documentation, see the Mapping base class
   virtual
-  Mapping<dim, spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim, spacedim>> clone () const override;
 
   /**
    * Return @p true because MappingCartesian preserves vertex
index 11ec2f41ce182750e95fbe0f925af7865e60091b..fa9b9155d3a867e805236443cb8fc20ec386aef0 100644 (file)
@@ -123,7 +123,7 @@ public:
    * then assumes ownership of it.
    */
   virtual
-  Mapping<dim,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 
 
 
index 443c5b4722784cba7c61c5dcf0bc366f19af41e1..7d0b483c40b900aa434a650e97bc1fe406527f55 100644 (file)
@@ -72,7 +72,7 @@ public:
 
   // for documentation, see the Mapping base class
   virtual
-  Mapping<dim,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 
   /**
    * Always returns @p true because this class assumes that the
index c115f1e88d8e415613158f0fb21677b20fad0cc4..1b09075a0aac570f20d79b6f809d2c77bd174689 100644 (file)
@@ -201,7 +201,7 @@ public:
    * then assumes ownership of it.
    */
   virtual
-  Mapping<dim,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 
 
   /**
index 770aa5c66fe3a597b5fe65409dbf08b716c65271..3faae61e43fb2046e8583e414ffddcbbaaff63b7 100644 (file)
@@ -63,7 +63,7 @@ public:
 
   // for documentation, see the Mapping base class
   virtual
-  MappingQ1<dim,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 };
 
 
index faa5480f14b43bb97030a40a3706e3abec943084..1675899df9f3310ff59e0f0d01c25ed47e8712e3 100644 (file)
@@ -123,7 +123,7 @@ public:
    * then assumes ownership of it.
    */
   virtual
-  MappingQ1Eulerian<dim,VectorType,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 
   /**
    * Always returns @p false because MappingQ1Eulerian does not in general
index 8216d645c3cd5160cedad877b6acfdcfefd4055d..98c20b21f26a0cdf16c50057df135647ed805776 100644 (file)
@@ -126,7 +126,7 @@ public:
    * then assumes ownership of it.
    */
   virtual
-  Mapping<dim,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 
   /**
    * Always return @p false because MappingQEulerian does not in general
index 085985555a6fc82599880f932cd3159f8608a639..1e3086c53d040a8217b0c5e0f1eb012603b9d943 100644 (file)
@@ -142,7 +142,7 @@ public:
 
   // for documentation, see the Mapping base class
   virtual
-  Mapping<dim,spacedim> *clone () const;
+  std::unique_ptr<Mapping<dim,spacedim>> clone () const override;
 
   /**
    * Return the degree of the mapping, i.e. the value which was passed to the
index 5ff2ecdfb2545da660f75646a3678fae8089d332..0532e863afa380325df8d3089592754b0d64120a 100644 (file)
 //
 // ---------------------------------------------------------------------
 
+#include <deal.II/base/std_cxx14/memory.h>
+
 #include <deal.II/grid/tria_accessor.h>
 #include <deal.II/grid/tria_iterator.h>
 #include <deal.II/grid/tria_boundary.h>
+
 #include <deal.II/fe/mapping_c1.h>
+
 #include <cmath>
 
 DEAL_II_NAMESPACE_OPEN
@@ -208,10 +212,10 @@ MappingC1<dim,spacedim>::MappingC1Generic::add_quad_support_points (const typena
 
 
 template <int dim, int spacedim>
-Mapping<dim, spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingC1<dim,spacedim>::clone () const
 {
-  return new MappingC1<dim,spacedim>();
+  return std_cxx14::make_unique<MappingC1<dim,spacedim>>();
 }
 
 
index 033e0de12c93a1e1903dce9e7819182bfe5ff295..3d70bfb63ead87294d25db3cb2b074fa7df7731b 100644 (file)
@@ -13,7 +13,7 @@
 //
 // ---------------------------------------------------------------------
 
-
+#include <deal.II/base/std_cxx14/memory.h>
 #include <deal.II/base/array_view.h>
 #include <deal.II/base/tensor.h>
 #include <deal.II/base/quadrature.h>
@@ -1064,10 +1064,10 @@ MappingCartesian<dim, spacedim>::transform_real_to_unit_cell (
 
 
 template <int dim, int spacedim>
-Mapping<dim, spacedim> *
+std::unique_ptr<Mapping<dim, spacedim> >
 MappingCartesian<dim, spacedim>::clone () const
 {
-  return new MappingCartesian<dim, spacedim>(*this);
+  return std_cxx14::make_unique<MappingCartesian<dim,spacedim>>(*this);
 }
 
 
index 0cf9191b1b14ee46d708b827784a78a0909cafa0..b72b2ffdc4b2f803a5d58d99d5ace08cc3af011a 100644 (file)
@@ -20,6 +20,7 @@
 #include <deal.II/base/quadrature_lib.h>
 #include <deal.II/base/qprojector.h>
 #include <deal.II/base/memory_consumption.h>
+#include <deal.II/base/std_cxx14/memory.h>
 #include <deal.II/base/utilities.h>
 #include <deal.II/lac/full_matrix.h>
 #include <deal.II/lac/vector.h>
@@ -1974,10 +1975,10 @@ MappingFEField<dim,spacedim,VectorType,DoFHandlerType>::get_component_mask() con
 
 
 template <int dim, int spacedim, typename VectorType, typename DoFHandlerType>
-Mapping<dim,spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingFEField<dim,spacedim,VectorType,DoFHandlerType>::clone () const
 {
-  return new MappingFEField<dim,spacedim,VectorType,DoFHandlerType>(*this);
+  return std_cxx14::make_unique<MappingFEField<dim,spacedim,VectorType,DoFHandlerType>>(*this);
 }
 
 
index cfcc15b121c99eef9ab210df1a2f366857754328..b5b7b422e6aa14b81133145b9a1a6a251aec0532 100644 (file)
@@ -19,6 +19,7 @@
 #include <deal.II/base/quadrature.h>
 #include <deal.II/base/qprojector.h>
 #include <deal.II/base/quadrature_lib.h>
+#include <deal.II/base/std_cxx14/memory.h>
 #include <deal.II/base/tensor_product_polynomials.h>
 #include <deal.II/base/memory_consumption.h>
 #include <deal.II/lac/full_matrix.h>
@@ -176,10 +177,10 @@ MappingManifold<dim,spacedim>::MappingManifold (const MappingManifold<dim,spaced
 
 
 template <int dim, int spacedim>
-Mapping<dim,spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingManifold<dim,spacedim>::clone () const
 {
-  return new MappingManifold<dim,spacedim>(*this);
+  return std_cxx14::make_unique<MappingManifold<dim,spacedim>>(*this);
 }
 
 
index d924a86e27b0fc2b67e84c9e26aa85b2190c8b78..25f641e662b684bd2c3c3bb7bc8f7b2dd6b75c85 100644 (file)
@@ -18,6 +18,7 @@
 #include <deal.II/base/quadrature.h>
 #include <deal.II/base/quadrature_lib.h>
 #include <deal.II/base/memory_consumption.h>
+#include <deal.II/base/std_cxx14/memory.h>
 #include <deal.II/base/tensor_product_polynomials.h>
 #include <deal.II/base/utilities.h>
 #include <deal.II/lac/full_matrix.h>
@@ -77,7 +78,7 @@ MappingQ<dim,spacedim>::MappingQ (const unsigned int degree,
   // created via the shared_ptr objects
   qp_mapping (this->polynomial_degree>1
               ?
-              std::shared_ptr<const MappingQGeneric<dim,spacedim> >(new MappingQGeneric<dim,spacedim>(degree))
+              std::make_shared<const MappingQGeneric<dim,spacedim>>(degree)
               :
               q1_mapping)
 {}
@@ -88,18 +89,27 @@ template <int dim, int spacedim>
 MappingQ<dim,spacedim>::MappingQ (const MappingQ<dim,spacedim> &mapping)
   :
   polynomial_degree (mapping.polynomial_degree),
-  use_mapping_q_on_all_cells (mapping.use_mapping_q_on_all_cells),
-  // clone the Q1 mapping for use on interior cells (if necessary)
-  // or to create a good initial guess in transform_real_to_unit_cell()
-  q1_mapping (dynamic_cast<MappingQGeneric<dim,spacedim>*>(mapping.q1_mapping->clone())),
-  // create a Q_p mapping; if p=1, simply share the Q_1 mapping already
-  // created via the shared_ptr objects
-  qp_mapping (this->polynomial_degree>1
-              ?
-              std::shared_ptr<const MappingQGeneric<dim,spacedim> >(dynamic_cast<MappingQGeneric<dim,spacedim>*>(mapping.qp_mapping->clone()))
-              :
-              q1_mapping)
-{}
+  use_mapping_q_on_all_cells (mapping.use_mapping_q_on_all_cells)
+{
+  // Note that we really do have to use clone() here, since mapping.q1_mapping
+  // may be MappingQ1Eulerian and mapping.qp_mapping may be MappingQEulerian.
+  std::shared_ptr<const Mapping<dim,spacedim>> other_q1_map = mapping.q1_mapping->clone();
+  q1_mapping = std::dynamic_pointer_cast<const MappingQGeneric<dim,spacedim>>(other_q1_map);
+  Assert(q1_mapping != nullptr, ExcInternalError());
+  Assert(q1_mapping->get_degree() == 1, ExcInternalError());
+
+  // Same as the other constructor: if possible reuse the Q1 mapping
+  if (this->polynomial_degree == 1)
+    {
+      qp_mapping = q1_mapping;
+    }
+  else
+    {
+      std::shared_ptr<const Mapping<dim,spacedim>> other_qp_map = mapping.qp_mapping->clone();
+      qp_mapping = std::dynamic_pointer_cast<const MappingQGeneric<dim,spacedim>>(other_qp_map);
+      Assert(qp_mapping != nullptr, ExcInternalError());
+    }
+}
 
 
 
@@ -502,11 +512,11 @@ transform_real_to_unit_cell (const typename Triangulation<dim,spacedim>::cell_it
 
 
 template <int dim, int spacedim>
-Mapping<dim,spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingQ<dim,spacedim>::clone () const
 {
-  return new MappingQ<dim,spacedim>(this->polynomial_degree,
-                                    this->use_mapping_q_on_all_cells);
+  return std_cxx14::make_unique<MappingQ<dim,spacedim>>(this->polynomial_degree,
+                                                        this->use_mapping_q_on_all_cells);
 }
 
 
index c02161d39e849d417b12b2bad0edd765ba66f2fc..0261ceb3cad27042833b98588d6c4d192eacfd4c 100644 (file)
@@ -20,6 +20,7 @@
 #include <deal.II/base/quadrature_lib.h>
 #include <deal.II/base/tensor_product_polynomials.h>
 #include <deal.II/base/memory_consumption.h>
+#include <deal.II/base/std_cxx14/memory.h>
 #include <deal.II/lac/full_matrix.h>
 #include <deal.II/grid/tria.h>
 #include <deal.II/grid/tria_iterator.h>
@@ -49,10 +50,10 @@ MappingQ1<dim,spacedim>::MappingQ1 ()
 
 
 template <int dim, int spacedim>
-MappingQ1<dim,spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingQ1<dim,spacedim>::clone () const
 {
-  return new MappingQ1<dim,spacedim>(*this);
+  return std_cxx14::make_unique<MappingQ1<dim,spacedim>>(*this);
 }
 
 //---------------------------------------------------------------------------
index 4a9f487d52594afd53ddbd7c047b6268c77b1d6a..c2cc75a90915abae950278be5bc7c9b629104a86 100644 (file)
@@ -13,6 +13,8 @@
 //
 // ---------------------------------------------------------------------
 
+#include <deal.II/base/std_cxx14/memory.h>
+
 #include <deal.II/fe/mapping_q1_eulerian.h>
 #include <deal.II/lac/vector.h>
 #include <deal.II/lac/block_vector.h>
@@ -116,10 +118,10 @@ compute_mapping_support_points(const typename Triangulation<dim,spacedim>::cell_
 
 
 template <int dim, class VectorType, int spacedim>
-MappingQ1Eulerian<dim,VectorType,spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingQ1Eulerian<dim, VectorType, spacedim>::clone () const
 {
-  return new MappingQ1Eulerian<dim,VectorType,spacedim>(*this);
+  return std_cxx14::make_unique<MappingQ1Eulerian<dim,VectorType,spacedim>>(*this);
 }
 
 
index 36732d36d3a6af27f1cb28e25cdb0d97ddc733a0..f7ccc78f4a87c396aef0d9fc0eef96b8ce90d6c6 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <deal.II/base/utilities.h>
 #include <deal.II/base/quadrature_lib.h>
+#include <deal.II/base/std_cxx14/memory.h>
 #include <deal.II/lac/vector.h>
 #include <deal.II/lac/block_vector.h>
 #include <deal.II/lac/la_vector.h>
@@ -79,12 +80,11 @@ MappingQEulerian (const unsigned int              degree,
 
 
 template <int dim, class VectorType, int spacedim>
-Mapping<dim,spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingQEulerian<dim, VectorType, spacedim>::clone () const
 {
-  return new MappingQEulerian<dim,VectorType,spacedim>(this->get_degree(),
-                                                       *euler_dof_handler,
-                                                       *euler_vector);
+  return std_cxx14::make_unique<MappingQEulerian<dim,VectorType,spacedim>>
+         (this->get_degree(), *euler_dof_handler, *euler_vector);
 }
 
 
index 9c2647fd0db67ca0085cd117caef66c420c6c3e7..f15410062ae0fb5c7db24e26a446acf7573edc8c 100644 (file)
@@ -22,6 +22,7 @@
 #include <deal.II/base/table.h>
 #include <deal.II/base/tensor_product_polynomials.h>
 #include <deal.II/base/memory_consumption.h>
+#include <deal.II/base/std_cxx14/memory.h>
 #include <deal.II/lac/full_matrix.h>
 #include <deal.II/lac/tensor_product_matrix.h>
 #include <deal.II/grid/tria.h>
@@ -2154,10 +2155,10 @@ MappingQGeneric<dim,spacedim>::MappingQGeneric (const MappingQGeneric<dim,spaced
 
 
 template <int dim, int spacedim>
-Mapping<dim,spacedim> *
+std::unique_ptr<Mapping<dim,spacedim> >
 MappingQGeneric<dim,spacedim>::clone () const
 {
-  return new MappingQGeneric<dim,spacedim>(*this);
+  return std_cxx14::make_unique<MappingQGeneric<dim,spacedim>>(*this);
 }
 
 
index a0a7caba340561ca92664a0ce45981abba07fdec..09cb1f3afeced10cea57ce81a219ee175310566b 100644 (file)
@@ -31,9 +31,9 @@ void test ()
 {
   MappingQ<dim> mapping(2);
   deallog << "Cloning..." << std::endl;
-  Mapping<dim> *copy = mapping.clone();
+  std::unique_ptr<Mapping<dim>> copy = mapping.clone();
   deallog << "Deleting clone..." << std::endl;
-  delete copy;
+  copy.reset();
   deallog << "Destroying original..." << std::endl;
 }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.