From: Peter Munch Date: Sat, 6 Feb 2021 13:38:35 +0000 (+0100) Subject: Accept unique_ptr in different MG-related classes X-Git-Tag: v9.3.0-rc1~471^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F11698%2Fhead;p=dealii.git Accept unique_ptr in different MG-related classes --- diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index a7fda7316a..dbd88258c1 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -763,20 +763,42 @@ namespace Utilities */ template std::unique_ptr - dynamic_unique_cast(std::unique_ptr &&p) - { - // Let's see if we can cast from 'From' to 'To'. If so, do the cast, - // and then release the pointer from the old - // owner - if (To *cast = dynamic_cast(p.get())) - { - std::unique_ptr result(cast); - p.release(); - return result; - } - else - throw std::bad_cast(); - } + dynamic_unique_cast(std::unique_ptr &&p); + + /** + * Return underlying value. Default: return input. + */ + template + T & + get_underlying_value(T &p); + + /** + * Return underlying value. Specialization for std::shared_ptr. + */ + template + T & + get_underlying_value(std::shared_ptr &p); + + /** + * Return underlying value. Specialization for const std::shared_ptr. + */ + template + T & + get_underlying_value(const std::shared_ptr &p); + + /** + * Return underlying value. Specialization for std::unique_ptr. + */ + template + T & + get_underlying_value(std::unique_ptr &p); + + /** + * Return underlying value. Specialization for const std::unique_ptr. + */ + template + T & + get_underlying_value(const std::unique_ptr &p); /** * A namespace for utility functions that probe system properties. @@ -1376,6 +1398,70 @@ namespace Utilities + template + inline std::unique_ptr + dynamic_unique_cast(std::unique_ptr &&p) + { + // Let's see if we can cast from 'From' to 'To'. If so, do the cast, + // and then release the pointer from the old + // owner + if (To *cast = dynamic_cast(p.get())) + { + std::unique_ptr result(cast); + p.release(); + return result; + } + else + throw std::bad_cast(); + } + + + + template + inline T & + get_underlying_value(T &p) + { + return p; + } + + + + template + inline T & + get_underlying_value(std::shared_ptr &p) + { + return *p; + } + + + + template + inline T & + get_underlying_value(const std::shared_ptr &p) + { + return *p; + } + + + + template + inline T & + get_underlying_value(std::unique_ptr &p) + { + return *p; + } + + + + template + inline T & + get_underlying_value(const std::unique_ptr &p) + { + return *p; + } + + + template std::vector reverse_permutation(const std::vector &permutation) diff --git a/include/deal.II/multigrid/mg_matrix.h b/include/deal.II/multigrid/mg_matrix.h index a09354708a..ce98fa3cae 100644 --- a/include/deal.II/multigrid/mg_matrix.h +++ b/include/deal.II/multigrid/mg_matrix.h @@ -208,7 +208,9 @@ namespace mg // rich enough interface to populate reinit_(domain|range)_vector. // Thus, apply an empty LinearOperator exemplar. matrices[level] = - linear_operator(LinearOperator(), p[level]); + linear_operator(LinearOperator(), + Utilities::get_underlying_value( + p[level])); } } diff --git a/include/deal.II/multigrid/mg_smoother.h b/include/deal.II/multigrid/mg_smoother.h index acb6fd2ec0..d8f898983c 100644 --- a/include/deal.II/multigrid/mg_smoother.h +++ b/include/deal.II/multigrid/mg_smoother.h @@ -1062,8 +1062,9 @@ MGSmootherPrecondition::initialize( // enough interface to populate reinit_(domain|range)_vector. Thus, // apply an empty LinearOperator exemplar. matrices[i] = - linear_operator(LinearOperator(), m[i]); - smoothers[i].initialize(m[i], data[i]); + linear_operator(LinearOperator(), + Utilities::get_underlying_value(m[i])); + smoothers[i].initialize(Utilities::get_underlying_value(m[i]), data[i]); } }