From: Wolfgang Bangerth Date: Thu, 25 Jul 2024 23:51:57 +0000 (-0600) Subject: Make SmartPointer operations easier to read. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F17421%2Fhead;p=dealii.git Make SmartPointer operations easier to read. --- diff --git a/include/deal.II/base/smartpointer.h b/include/deal.II/base/smartpointer.h index 7511e386be..caa8fc3734 100644 --- a/include/deal.II/base/smartpointer.h +++ b/include/deal.II/base/smartpointer.h @@ -340,16 +340,19 @@ template inline SmartPointer & SmartPointer::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::operator=(const SmartPointer &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(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::operator=(const SmartPointer &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(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; }