From 8cdc64083823b4ee6e715bb5da8918d5f1448523 Mon Sep 17 00:00:00 2001 From: Marco Thaller Date: Fri, 8 Dec 2023 20:42:38 +0100 Subject: [PATCH] Add signal to indicate destruction of Object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 'destroyed' signal is emitted when Object::~Object is called before any of the Object's children are destroyed. Change-Id: Ida5783cc515d28efc75d7d9a060b1b3aea4e4cea Reviewed-on: https://codereview.kdab.com/c/kdab/kdutils/+/135487 Reviewed-by: Paul Lemire Tested-by: Continuous Integration Reviewed-by: MiƂosz Kosobucki --- src/KDFoundation/object.cpp | 2 ++ src/KDFoundation/object.h | 1 + tests/auto/foundation/object/tst_object.cpp | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/src/KDFoundation/object.cpp b/src/KDFoundation/object.cpp index e7ee8af..d8adc0b 100644 --- a/src/KDFoundation/object.cpp +++ b/src/KDFoundation/object.cpp @@ -25,6 +25,8 @@ Object::Object() Object::~Object() { + destroyed.emit(this); + // Destroy the children in LIFO to be more like the stack while (!m_children.empty()) { childRemoved.emit(this, m_children.back().get()); diff --git a/src/KDFoundation/object.h b/src/KDFoundation/object.h index b4f77bf..e75e3ef 100644 --- a/src/KDFoundation/object.h +++ b/src/KDFoundation/object.h @@ -97,6 +97,7 @@ class KDFOUNDATION_API Object : public EventReceiver KDBindings::Signal parentChanged; KDBindings::Signal childAdded; KDBindings::Signal childRemoved; + KDBindings::Signal destroyed; protected: virtual void timerEvent(TimerEvent *ev); diff --git a/tests/auto/foundation/object/tst_object.cpp b/tests/auto/foundation/object/tst_object.cpp index ea4fba2..7b77c16 100644 --- a/tests/auto/foundation/object/tst_object.cpp +++ b/tests/auto/foundation/object/tst_object.cpp @@ -290,6 +290,14 @@ TEST_CASE("Object destruction") REQUIRE(expectedDestructionOrder[i] == destructionOrder[i]); } } + + SUBCASE("destroyed signal is emitted on object destruction") + { + auto obj = new Object(); + SignalSpy objectDestroyedSpy(obj->destroyed); + delete obj; + REQUIRE(objectDestroyedSpy.count() == 1); + } } TEST_CASE("Event delivery")