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