1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qobjectcleanuphandler.h"
5
6QT_BEGIN_NAMESPACE
7
8/*!
9 \class QObjectCleanupHandler
10 \inmodule QtCore
11 \brief The QObjectCleanupHandler class watches the lifetime of multiple QObjects.
12
13 \ingroup objectmodel
14
15 A QObjectCleanupHandler is useful whenever you need to know when a
16 number of \l{QObject}s that are owned by someone else have been
17 deleted. This is important, for example, when referencing memory
18 in an application that has been allocated in a shared library.
19
20 To keep track of some \l{QObject}s, create a
21 QObjectCleanupHandler, and add() the objects you are interested
22 in. If you are no longer interested in tracking a particular
23 object, use remove() to remove it from the cleanup handler. If an
24 object being tracked by the cleanup handler gets deleted by
25 someone else it will automatically be removed from the cleanup
26 handler. You can delete all the objects in the cleanup handler
27 with clear(), or by destroying the cleanup handler. isEmpty()
28 returns \c true if the QObjectCleanupHandler has no objects to keep
29 track of.
30
31 \sa QPointer
32*/
33
34/*!
35 Constructs an empty QObjectCleanupHandler.
36*/
37QObjectCleanupHandler::QObjectCleanupHandler()
38{
39}
40
41/*!
42 Destroys the cleanup handler. All objects in this cleanup handler
43 will be deleted.
44
45 \sa clear()
46*/
47QObjectCleanupHandler::~QObjectCleanupHandler()
48{
49 clear();
50}
51
52/*!
53 Adds \a object to this cleanup handler and returns the pointer to
54 the object.
55
56 \sa remove()
57*/
58QObject *QObjectCleanupHandler::add(QObject *object)
59{
60 if (!object)
61 return nullptr;
62
63 connect(sender: object, SIGNAL(destroyed(QObject*)), receiver: this, SLOT(objectDestroyed(QObject*)));
64 cleanupObjects.insert(i: 0, t: object);
65 return object;
66}
67
68/*!
69 Removes the \a object from this cleanup handler. The object will
70 not be destroyed.
71
72 \sa add()
73*/
74void QObjectCleanupHandler::remove(QObject *object)
75{
76 int index;
77 if ((index = cleanupObjects.indexOf(t: object)) != -1) {
78 cleanupObjects.removeAt(i: index);
79 disconnect(sender: object, SIGNAL(destroyed(QObject*)), receiver: this, SLOT(objectDestroyed(QObject*)));
80 }
81}
82
83/*!
84 Returns \c true if this cleanup handler is empty or if all objects in
85 this cleanup handler have been destroyed; otherwise return false.
86
87 \sa add(), remove(), clear()
88*/
89bool QObjectCleanupHandler::isEmpty() const
90{
91 return cleanupObjects.isEmpty();
92}
93
94/*!
95 Deletes all objects in this cleanup handler. The cleanup handler
96 becomes empty.
97
98 \sa isEmpty()
99*/
100void QObjectCleanupHandler::clear()
101{
102 while (!cleanupObjects.isEmpty())
103 delete cleanupObjects.takeFirst();
104}
105
106void QObjectCleanupHandler::objectDestroyed(QObject *object)
107{
108 remove(object);
109}
110
111QT_END_NAMESPACE
112
113#include "moc_qobjectcleanuphandler.cpp"
114

source code of qtbase/src/corelib/kernel/qobjectcleanuphandler.cpp