]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make SmartPointer operations easier to read. 17421/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 25 Jul 2024 23:51:57 +0000 (17:51 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Fri, 2 Aug 2024 02:17:12 +0000 (20:17 -0600)
include/deal.II/base/smartpointer.h

index 7511e386be7fab93e6bc4111aeefd7397e81f731..caa8fc37347bb0d9119eaac58cac720a04673a6a 100644 (file)
@@ -340,16 +340,19 @@ template <typename T, typename P>
 inline SmartPointer<T, P> &
 SmartPointer<T, P>::operator=(T *tt)
 {
-  // optimize if no real action is
-  // requested
+  // optimize if no real action is requested
   if (t == tt)
     return *this;
 
+  // Let us unsubscribe from the current object
   if (pointed_to_object_is_alive && t != nullptr)
     t->unsubscribe(&pointed_to_object_is_alive, id);
+
+  // Then reset to the new object, and subscribe to it
   t = tt;
   if (tt != nullptr)
-    tt->subscribe(&pointed_to_object_is_alive, id);
+    t->subscribe(&pointed_to_object_is_alive, id);
+
   return *this;
 }
 
@@ -366,11 +369,14 @@ SmartPointer<T, P>::operator=(const SmartPointer<T, Q> &tt)
   if (&tt == this)
     return *this;
 
+  // Let us unsubscribe from the current object
   if (pointed_to_object_is_alive && t != nullptr)
     t->unsubscribe(&pointed_to_object_is_alive, id);
-  t = static_cast<T *>(tt);
+
+  // Then reset to the new object, and subscribe to it
+  t = (tt != nullptr ? tt.get() : nullptr);
   if (tt.pointed_to_object_is_alive && tt != nullptr)
-    tt->subscribe(&pointed_to_object_is_alive, id);
+    t->subscribe(&pointed_to_object_is_alive, id);
   return *this;
 }
 
@@ -386,11 +392,15 @@ SmartPointer<T, P>::operator=(const SmartPointer<T, P> &tt)
   if (&tt == this)
     return *this;
 
+  // Let us unsubscribe from the current object
   if (pointed_to_object_is_alive && t != nullptr)
     t->unsubscribe(&pointed_to_object_is_alive, id);
-  t = static_cast<T *>(tt);
+
+  // Then reset to the new object, and subscribe to it
+  t = (tt != nullptr ? tt.get() : nullptr);
   if (tt.pointed_to_object_is_alive && tt != nullptr)
-    tt->subscribe(&pointed_to_object_is_alive, id);
+    t->subscribe(&pointed_to_object_is_alive, id);
+
   return *this;
 }
 

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.