From ea5f8b0a2c84ade656a2816ef28ec3568cc7f762 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Thu, 25 Jul 2024 17:51:57 -0600 Subject: [PATCH] Make SmartPointer operations easier to read. --- include/deal.II/base/smartpointer.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) 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; } -- 2.39.5