]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Ensure SmartPointer gives useful compiler errors for types not derived from Subscriptor. 17679/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Fri, 6 Sep 2024 23:12:58 +0000 (17:12 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Mon, 9 Sep 2024 17:18:27 +0000 (11:18 -0600)
include/deal.II/base/smartpointer.h

index 50197b318f01030118d2fd0b515928b2b4a67ff5..23066f3946af4c4e9238b3bb8e1102abce1b9a7f 100644 (file)
@@ -19,6 +19,7 @@
 #include <deal.II/base/config.h>
 
 #include <deal.II/base/exceptions.h>
+#include <deal.II/base/subscriptor.h>
 
 #include <atomic>
 #include <typeinfo>
@@ -86,6 +87,8 @@ DEAL_II_NAMESPACE_OPEN
  *   to a constant object (disallowing write access when dereferenced), while
  *   `SmartPointer<T>` is a mutable pointer.
  *
+ * @dealiiConceptRequires{std::is_base_of_v<Subscriptor, T>}
+ *
  * @ingroup memory
  */
 template <typename T, typename P = void>
@@ -268,7 +271,11 @@ inline SmartPointer<T, P>::SmartPointer()
   : pointer(nullptr)
   , id(typeid(P).name())
   , pointed_to_object_is_alive(false)
-{}
+{
+  static_assert(std::is_base_of_v<Subscriptor, T>,
+                "This class can only be used if the first template argument "
+                "is a class derived from 'Subscriptor'.");
+}
 
 
 
@@ -278,6 +285,10 @@ inline SmartPointer<T, P>::SmartPointer(T *t)
   , id(typeid(P).name())
   , pointed_to_object_is_alive(false)
 {
+  static_assert(std::is_base_of_v<Subscriptor, T>,
+                "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<T, P>::SmartPointer(T *t, const std::string &id)
   , id(id)
   , pointed_to_object_is_alive(false)
 {
+  static_assert(std::is_base_of_v<Subscriptor, T>,
+                "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<T, P>::SmartPointer(const SmartPointer<T, Q> &other)
   , id(other.id)
   , pointed_to_object_is_alive(false)
 {
+  static_assert(std::is_base_of_v<Subscriptor, T>,
+                "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<T, P>::SmartPointer(const SmartPointer<T, P> &other)
   , id(other.id)
   , pointed_to_object_is_alive(false)
 {
+  static_assert(std::is_base_of_v<Subscriptor, T>,
+                "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<T, P>::SmartPointer(SmartPointer<T, P> &&other) noexcept
   , id(other.id)
   , pointed_to_object_is_alive(false)
 {
+  static_assert(std::is_base_of_v<Subscriptor, T>,
+                "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<T, P>::SmartPointer(SmartPointer<T, P> &&other) noexcept
 template <typename T, typename P>
 inline SmartPointer<T, P>::~SmartPointer()
 {
+  static_assert(std::is_base_of_v<Subscriptor, T>,
+                "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);
 }

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.