From f8c5a2cf74321fa5c810ead60412547e6d3b1b58 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Tue, 6 Aug 2024 10:27:40 -0600 Subject: [PATCH] Make code easier to read by moving a condition outward. --- include/deal.II/base/smartpointer.h | 39 +++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/include/deal.II/base/smartpointer.h b/include/deal.II/base/smartpointer.h index 4c097cedc8..34c74b126d 100644 --- a/include/deal.II/base/smartpointer.h +++ b/include/deal.II/base/smartpointer.h @@ -329,12 +329,18 @@ inline SmartPointer::SmartPointer(SmartPointer &&tt) noexcept , id(tt.id) , pointed_to_object_is_alive(false) { - if (tt.pointed_to_object_is_alive && t != nullptr) - t->subscribe(&pointed_to_object_is_alive, id); + if (tt != nullptr) + { + Assert(tt.pointed_to_object_is_alive, + ExcMessage("You can't move a smart pointer object that " + "is pointing to an object that is no longer alive.")); + + t->subscribe(&pointed_to_object_is_alive, id); - // Release the rhs object as if we had moved all members away from - // it directly: - tt = nullptr; + // Release the rhs object as if we had moved all members away from + // it directly: + tt = nullptr; + } } @@ -437,23 +443,24 @@ template inline SmartPointer & SmartPointer::operator=(SmartPointer &&tt) noexcept { + if (tt == nullptr) + { + *this = nullptr; + } // if objects on the left and right hand side of the operator= are // the same, then this is a no-op - if (&tt != this) + else if (&tt != this) { // Let us unsubscribe from the current object - if (pointed_to_object_is_alive && t != nullptr) + if (pointed_to_object_is_alive) t->unsubscribe(&pointed_to_object_is_alive, id); - // Then reset to the new object, and subscribe to it, assuming - // that the the rhs points to anything in the first place: - if (tt.pointed_to_object_is_alive && tt != nullptr) - { - t = tt.get(); - t->subscribe(&pointed_to_object_is_alive, id); - } - else - t = nullptr; + // Then reset to the new object, and subscribe to it: + Assert(tt.pointed_to_object_is_alive, + ExcMessage("You can't move a smart pointer object that " + "is pointing to an object that is no longer alive.")); + t = tt.get(); + t->subscribe(&pointed_to_object_is_alive, id); // Finally release the rhs object since we moved its contents tt = nullptr; -- 2.39.5