Skip to content

Commit

Permalink
Add signal to indicate destruction of Object
Browse files Browse the repository at this point in the history
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 <[email protected]>
Tested-by: Continuous Integration <[email protected]>
Reviewed-by: Miłosz Kosobucki <[email protected]>
  • Loading branch information
marcothaller committed Dec 20, 2023
1 parent 662918f commit 8cdc640
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/KDFoundation/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions src/KDFoundation/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class KDFOUNDATION_API Object : public EventReceiver
KDBindings::Signal<Object *, Object *> parentChanged;
KDBindings::Signal<Object *, Object *> childAdded;
KDBindings::Signal<Object *, Object *> childRemoved;
KDBindings::Signal<Object *> destroyed;

protected:
virtual void timerEvent(TimerEvent *ev);
Expand Down
8 changes: 8 additions & 0 deletions tests/auto/foundation/object/tst_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8cdc640

Please sign in to comment.