From c3b75cb61119472904bf47b06472633b14c06212 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Fri, 6 Sep 2024 17:12:58 -0600 Subject: [PATCH] Ensure SmartPointer gives useful compiler errors for types not derived from Subscriptor. --- include/deal.II/base/smartpointer.h | 33 ++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/include/deal.II/base/smartpointer.h b/include/deal.II/base/smartpointer.h index 50197b318f..23066f3946 100644 --- a/include/deal.II/base/smartpointer.h +++ b/include/deal.II/base/smartpointer.h @@ -19,6 +19,7 @@ #include #include +#include #include #include @@ -86,6 +87,8 @@ DEAL_II_NAMESPACE_OPEN * to a constant object (disallowing write access when dereferenced), while * `SmartPointer` is a mutable pointer. * + * @dealiiConceptRequires{std::is_base_of_v} + * * @ingroup memory */ template @@ -268,7 +271,11 @@ inline SmartPointer::SmartPointer() : pointer(nullptr) , id(typeid(P).name()) , pointed_to_object_is_alive(false) -{} +{ + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); +} @@ -278,6 +285,10 @@ inline SmartPointer::SmartPointer(T *t) , id(typeid(P).name()) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (t != nullptr) t->subscribe(&pointed_to_object_is_alive, id); } @@ -290,6 +301,10 @@ inline SmartPointer::SmartPointer(T *t, const std::string &id) , id(id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (pointer != nullptr) pointer->subscribe(&pointed_to_object_is_alive, id); } @@ -303,6 +318,10 @@ inline SmartPointer::SmartPointer(const SmartPointer &other) , id(other.id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (other != nullptr) { Assert(other.pointed_to_object_is_alive, @@ -320,6 +339,10 @@ inline SmartPointer::SmartPointer(const SmartPointer &other) , id(other.id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (other != nullptr) { Assert(other.pointed_to_object_is_alive, @@ -337,6 +360,10 @@ inline SmartPointer::SmartPointer(SmartPointer &&other) noexcept , id(other.id) , pointed_to_object_is_alive(false) { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (other != nullptr) { Assert(other.pointed_to_object_is_alive, @@ -367,6 +394,10 @@ inline SmartPointer::SmartPointer(SmartPointer &&other) noexcept template inline SmartPointer::~SmartPointer() { + static_assert(std::is_base_of_v, + "This class can only be used if the first template argument " + "is a class derived from 'Subscriptor'."); + if (pointed_to_object_is_alive && pointer != nullptr) pointer->unsubscribe(&pointed_to_object_is_alive, id); } -- 2.39.5