From fd1bf6a6f846ae259d8a33dc7b4b3621dec60212 Mon Sep 17 00:00:00 2001 From: David Wells Date: Fri, 9 Feb 2018 19:17:52 -0500 Subject: [PATCH] Make Mapping::clone return a unique_ptr. 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. --- .../incompatibilities/20180208DavidWells | 3 ++ include/deal.II/fe/mapping.h | 3 +- include/deal.II/fe/mapping_c1.h | 2 +- include/deal.II/fe/mapping_cartesian.h | 2 +- include/deal.II/fe/mapping_fe_field.h | 2 +- include/deal.II/fe/mapping_manifold.h | 2 +- include/deal.II/fe/mapping_q.h | 2 +- include/deal.II/fe/mapping_q1.h | 2 +- include/deal.II/fe/mapping_q1_eulerian.h | 2 +- include/deal.II/fe/mapping_q_eulerian.h | 2 +- include/deal.II/fe/mapping_q_generic.h | 2 +- source/fe/mapping_c1.cc | 8 +++- source/fe/mapping_cartesian.cc | 6 +-- source/fe/mapping_fe_field.cc | 5 ++- source/fe/mapping_manifold.cc | 5 ++- source/fe/mapping_q.cc | 42 ++++++++++++------- source/fe/mapping_q1.cc | 5 ++- source/fe/mapping_q1_eulerian.cc | 6 ++- source/fe/mapping_q_eulerian.cc | 8 ++-- source/fe/mapping_q_generic.cc | 5 ++- tests/hp/mapping_collection_03.cc | 4 +- 21 files changed, 71 insertions(+), 47 deletions(-) create mode 100644 doc/news/changes/incompatibilities/20180208DavidWells diff --git a/doc/news/changes/incompatibilities/20180208DavidWells b/doc/news/changes/incompatibilities/20180208DavidWells new file mode 100644 index 0000000000..c66292b039 --- /dev/null +++ b/doc/news/changes/incompatibilities/20180208DavidWells @@ -0,0 +1,3 @@ +Changed: The function Mapping::clone (and all inheriting classes' implementations, such as MappingQ1Eulerian::clone) now return a unique_ptr>. +
+(David Wells, 2018/02/08) diff --git a/include/deal.II/fe/mapping.h b/include/deal.II/fe/mapping.h index 8ccc3d71cf..558ad22393 100644 --- a/include/deal.II/fe/mapping.h +++ b/include/deal.II/fe/mapping.h @@ -24,6 +24,7 @@ #include #include +#include DEAL_II_NAMESPACE_OPEN @@ -303,7 +304,7 @@ public: * This function is mainly used by the hp::MappingCollection class. */ virtual - Mapping *clone () const = 0; + std::unique_ptr> clone () const = 0; /** * Return the mapped vertices of a cell. diff --git a/include/deal.II/fe/mapping_c1.h b/include/deal.II/fe/mapping_c1.h index c240165b25..7406099ddd 100644 --- a/include/deal.II/fe/mapping_c1.h +++ b/include/deal.II/fe/mapping_c1.h @@ -50,7 +50,7 @@ public: * then assumes ownership of it. */ virtual - Mapping *clone () const; + std::unique_ptr> clone () const override; protected: diff --git a/include/deal.II/fe/mapping_cartesian.h b/include/deal.II/fe/mapping_cartesian.h index 5ef8c219ab..9cbdfd35ec 100644 --- a/include/deal.II/fe/mapping_cartesian.h +++ b/include/deal.II/fe/mapping_cartesian.h @@ -64,7 +64,7 @@ public: // for documentation, see the Mapping base class virtual - Mapping *clone () const; + std::unique_ptr> clone () const override; /** * Return @p true because MappingCartesian preserves vertex diff --git a/include/deal.II/fe/mapping_fe_field.h b/include/deal.II/fe/mapping_fe_field.h index 11ec2f41ce..fa9b9155d3 100644 --- a/include/deal.II/fe/mapping_fe_field.h +++ b/include/deal.II/fe/mapping_fe_field.h @@ -123,7 +123,7 @@ public: * then assumes ownership of it. */ virtual - Mapping *clone () const; + std::unique_ptr> clone () const override; diff --git a/include/deal.II/fe/mapping_manifold.h b/include/deal.II/fe/mapping_manifold.h index 443c5b4722..7d0b483c40 100644 --- a/include/deal.II/fe/mapping_manifold.h +++ b/include/deal.II/fe/mapping_manifold.h @@ -72,7 +72,7 @@ public: // for documentation, see the Mapping base class virtual - Mapping *clone () const; + std::unique_ptr> clone () const override; /** * Always returns @p true because this class assumes that the diff --git a/include/deal.II/fe/mapping_q.h b/include/deal.II/fe/mapping_q.h index c115f1e88d..1b09075a0a 100644 --- a/include/deal.II/fe/mapping_q.h +++ b/include/deal.II/fe/mapping_q.h @@ -201,7 +201,7 @@ public: * then assumes ownership of it. */ virtual - Mapping *clone () const; + std::unique_ptr> clone () const override; /** diff --git a/include/deal.II/fe/mapping_q1.h b/include/deal.II/fe/mapping_q1.h index 770aa5c66f..3faae61e43 100644 --- a/include/deal.II/fe/mapping_q1.h +++ b/include/deal.II/fe/mapping_q1.h @@ -63,7 +63,7 @@ public: // for documentation, see the Mapping base class virtual - MappingQ1 *clone () const; + std::unique_ptr> clone () const override; }; diff --git a/include/deal.II/fe/mapping_q1_eulerian.h b/include/deal.II/fe/mapping_q1_eulerian.h index faa5480f14..1675899df9 100644 --- a/include/deal.II/fe/mapping_q1_eulerian.h +++ b/include/deal.II/fe/mapping_q1_eulerian.h @@ -123,7 +123,7 @@ public: * then assumes ownership of it. */ virtual - MappingQ1Eulerian *clone () const; + std::unique_ptr> clone () const override; /** * Always returns @p false because MappingQ1Eulerian does not in general diff --git a/include/deal.II/fe/mapping_q_eulerian.h b/include/deal.II/fe/mapping_q_eulerian.h index 8216d645c3..98c20b21f2 100644 --- a/include/deal.II/fe/mapping_q_eulerian.h +++ b/include/deal.II/fe/mapping_q_eulerian.h @@ -126,7 +126,7 @@ public: * then assumes ownership of it. */ virtual - Mapping *clone () const; + std::unique_ptr> clone () const override; /** * Always return @p false because MappingQEulerian does not in general diff --git a/include/deal.II/fe/mapping_q_generic.h b/include/deal.II/fe/mapping_q_generic.h index 085985555a..1e3086c53d 100644 --- a/include/deal.II/fe/mapping_q_generic.h +++ b/include/deal.II/fe/mapping_q_generic.h @@ -142,7 +142,7 @@ public: // for documentation, see the Mapping base class virtual - Mapping *clone () const; + std::unique_ptr> clone () const override; /** * Return the degree of the mapping, i.e. the value which was passed to the diff --git a/source/fe/mapping_c1.cc b/source/fe/mapping_c1.cc index 5ff2ecdfb2..0532e863af 100644 --- a/source/fe/mapping_c1.cc +++ b/source/fe/mapping_c1.cc @@ -13,10 +13,14 @@ // // --------------------------------------------------------------------- +#include + #include #include #include + #include + #include DEAL_II_NAMESPACE_OPEN @@ -208,10 +212,10 @@ MappingC1::MappingC1Generic::add_quad_support_points (const typena template -Mapping * +std::unique_ptr > MappingC1::clone () const { - return new MappingC1(); + return std_cxx14::make_unique>(); } diff --git a/source/fe/mapping_cartesian.cc b/source/fe/mapping_cartesian.cc index 033e0de12c..3d70bfb63e 100644 --- a/source/fe/mapping_cartesian.cc +++ b/source/fe/mapping_cartesian.cc @@ -13,7 +13,7 @@ // // --------------------------------------------------------------------- - +#include #include #include #include @@ -1064,10 +1064,10 @@ MappingCartesian::transform_real_to_unit_cell ( template -Mapping * +std::unique_ptr > MappingCartesian::clone () const { - return new MappingCartesian(*this); + return std_cxx14::make_unique>(*this); } diff --git a/source/fe/mapping_fe_field.cc b/source/fe/mapping_fe_field.cc index 0cf9191b1b..b72b2ffdc4 100644 --- a/source/fe/mapping_fe_field.cc +++ b/source/fe/mapping_fe_field.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -1974,10 +1975,10 @@ MappingFEField::get_component_mask() con template -Mapping * +std::unique_ptr > MappingFEField::clone () const { - return new MappingFEField(*this); + return std_cxx14::make_unique>(*this); } diff --git a/source/fe/mapping_manifold.cc b/source/fe/mapping_manifold.cc index cfcc15b121..b5b7b422e6 100644 --- a/source/fe/mapping_manifold.cc +++ b/source/fe/mapping_manifold.cc @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -176,10 +177,10 @@ MappingManifold::MappingManifold (const MappingManifold -Mapping * +std::unique_ptr > MappingManifold::clone () const { - return new MappingManifold(*this); + return std_cxx14::make_unique>(*this); } diff --git a/source/fe/mapping_q.cc b/source/fe/mapping_q.cc index d924a86e27..25f641e662 100644 --- a/source/fe/mapping_q.cc +++ b/source/fe/mapping_q.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -77,7 +78,7 @@ MappingQ::MappingQ (const unsigned int degree, // created via the shared_ptr objects qp_mapping (this->polynomial_degree>1 ? - std::shared_ptr >(new MappingQGeneric(degree)) + std::make_shared>(degree) : q1_mapping) {} @@ -88,18 +89,27 @@ template MappingQ::MappingQ (const MappingQ &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*>(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 >(dynamic_cast*>(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> other_q1_map = mapping.q1_mapping->clone(); + q1_mapping = std::dynamic_pointer_cast>(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> other_qp_map = mapping.qp_mapping->clone(); + qp_mapping = std::dynamic_pointer_cast>(other_qp_map); + Assert(qp_mapping != nullptr, ExcInternalError()); + } +} @@ -502,11 +512,11 @@ transform_real_to_unit_cell (const typename Triangulation::cell_it template -Mapping * +std::unique_ptr > MappingQ::clone () const { - return new MappingQ(this->polynomial_degree, - this->use_mapping_q_on_all_cells); + return std_cxx14::make_unique>(this->polynomial_degree, + this->use_mapping_q_on_all_cells); } diff --git a/source/fe/mapping_q1.cc b/source/fe/mapping_q1.cc index c02161d39e..0261ceb3ca 100644 --- a/source/fe/mapping_q1.cc +++ b/source/fe/mapping_q1.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -49,10 +50,10 @@ MappingQ1::MappingQ1 () template -MappingQ1 * +std::unique_ptr > MappingQ1::clone () const { - return new MappingQ1(*this); + return std_cxx14::make_unique>(*this); } //--------------------------------------------------------------------------- diff --git a/source/fe/mapping_q1_eulerian.cc b/source/fe/mapping_q1_eulerian.cc index 4a9f487d52..c2cc75a909 100644 --- a/source/fe/mapping_q1_eulerian.cc +++ b/source/fe/mapping_q1_eulerian.cc @@ -13,6 +13,8 @@ // // --------------------------------------------------------------------- +#include + #include #include #include @@ -116,10 +118,10 @@ compute_mapping_support_points(const typename Triangulation::cell_ template -MappingQ1Eulerian * +std::unique_ptr > MappingQ1Eulerian::clone () const { - return new MappingQ1Eulerian(*this); + return std_cxx14::make_unique>(*this); } diff --git a/source/fe/mapping_q_eulerian.cc b/source/fe/mapping_q_eulerian.cc index 36732d36d3..f7ccc78f4a 100644 --- a/source/fe/mapping_q_eulerian.cc +++ b/source/fe/mapping_q_eulerian.cc @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -79,12 +80,11 @@ MappingQEulerian (const unsigned int degree, template -Mapping * +std::unique_ptr > MappingQEulerian::clone () const { - return new MappingQEulerian(this->get_degree(), - *euler_dof_handler, - *euler_vector); + return std_cxx14::make_unique> + (this->get_degree(), *euler_dof_handler, *euler_vector); } diff --git a/source/fe/mapping_q_generic.cc b/source/fe/mapping_q_generic.cc index 9c2647fd0d..f15410062a 100644 --- a/source/fe/mapping_q_generic.cc +++ b/source/fe/mapping_q_generic.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -2154,10 +2155,10 @@ MappingQGeneric::MappingQGeneric (const MappingQGeneric -Mapping * +std::unique_ptr > MappingQGeneric::clone () const { - return new MappingQGeneric(*this); + return std_cxx14::make_unique>(*this); } diff --git a/tests/hp/mapping_collection_03.cc b/tests/hp/mapping_collection_03.cc index a0a7caba34..09cb1f3afe 100644 --- a/tests/hp/mapping_collection_03.cc +++ b/tests/hp/mapping_collection_03.cc @@ -31,9 +31,9 @@ void test () { MappingQ mapping(2); deallog << "Cloning..." << std::endl; - Mapping *copy = mapping.clone(); + std::unique_ptr> copy = mapping.clone(); deallog << "Deleting clone..." << std::endl; - delete copy; + copy.reset(); deallog << "Destroying original..." << std::endl; } -- 2.39.5