1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qobjectcleanuphandler.h" |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | /*! |
45 | \class QObjectCleanupHandler |
46 | \inmodule QtCore |
47 | \brief The QObjectCleanupHandler class watches the lifetime of multiple QObjects. |
48 | |
49 | \ingroup objectmodel |
50 | |
51 | A QObjectCleanupHandler is useful whenever you need to know when a |
52 | number of \l{QObject}s that are owned by someone else have been |
53 | deleted. This is important, for example, when referencing memory |
54 | in an application that has been allocated in a shared library. |
55 | |
56 | To keep track of some \l{QObject}s, create a |
57 | QObjectCleanupHandler, and add() the objects you are interested |
58 | in. If you are no longer interested in tracking a particular |
59 | object, use remove() to remove it from the cleanup handler. If an |
60 | object being tracked by the cleanup handler gets deleted by |
61 | someone else it will automatically be removed from the cleanup |
62 | handler. You can delete all the objects in the cleanup handler |
63 | with clear(), or by destroying the cleanup handler. isEmpty() |
64 | returns \c true if the QObjectCleanupHandler has no objects to keep |
65 | track of. |
66 | |
67 | \sa QPointer |
68 | */ |
69 | |
70 | /*! |
71 | Constructs an empty QObjectCleanupHandler. |
72 | */ |
73 | QObjectCleanupHandler::QObjectCleanupHandler() |
74 | { |
75 | } |
76 | |
77 | /*! |
78 | Destroys the cleanup handler. All objects in this cleanup handler |
79 | will be deleted. |
80 | |
81 | \sa clear() |
82 | */ |
83 | QObjectCleanupHandler::~QObjectCleanupHandler() |
84 | { |
85 | clear(); |
86 | } |
87 | |
88 | /*! |
89 | Adds \a object to this cleanup handler and returns the pointer to |
90 | the object. |
91 | |
92 | \sa remove() |
93 | */ |
94 | QObject *QObjectCleanupHandler::add(QObject* object) |
95 | { |
96 | if (!object) |
97 | return nullptr; |
98 | |
99 | connect(sender: object, SIGNAL(destroyed(QObject*)), receiver: this, SLOT(objectDestroyed(QObject*))); |
100 | cleanupObjects.insert(i: 0, t: object); |
101 | return object; |
102 | } |
103 | |
104 | /*! |
105 | Removes the \a object from this cleanup handler. The object will |
106 | not be destroyed. |
107 | |
108 | \sa add() |
109 | */ |
110 | void QObjectCleanupHandler::remove(QObject *object) |
111 | { |
112 | int index; |
113 | if ((index = cleanupObjects.indexOf(t: object)) != -1) { |
114 | cleanupObjects.removeAt(i: index); |
115 | disconnect(sender: object, SIGNAL(destroyed(QObject*)), receiver: this, SLOT(objectDestroyed(QObject*))); |
116 | } |
117 | } |
118 | |
119 | /*! |
120 | Returns \c true if this cleanup handler is empty or if all objects in |
121 | this cleanup handler have been destroyed; otherwise return false. |
122 | |
123 | \sa add(), remove(), clear() |
124 | */ |
125 | bool QObjectCleanupHandler::isEmpty() const |
126 | { |
127 | return cleanupObjects.isEmpty(); |
128 | } |
129 | |
130 | /*! |
131 | Deletes all objects in this cleanup handler. The cleanup handler |
132 | becomes empty. |
133 | |
134 | \sa isEmpty() |
135 | */ |
136 | void QObjectCleanupHandler::clear() |
137 | { |
138 | while (!cleanupObjects.isEmpty()) |
139 | delete cleanupObjects.takeFirst(); |
140 | } |
141 | |
142 | void QObjectCleanupHandler::objectDestroyed(QObject *object) |
143 | { |
144 | remove(object); |
145 | } |
146 | |
147 | QT_END_NAMESPACE |
148 | |
149 | #include "moc_qobjectcleanuphandler.cpp" |
150 | |