source: trunk/src/corelib/kernel/qobjectcleanuphandler.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qobjectcleanuphandler.h"
43
44QT_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*/
74QObjectCleanupHandler::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*/
84QObjectCleanupHandler::~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*/
95QObject *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*/
111void 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*/
126bool 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*/
137void QObjectCleanupHandler::clear()
138{
139 while (!cleanupObjects.isEmpty())
140 delete cleanupObjects.takeFirst();
141}
142
143void QObjectCleanupHandler::objectDestroyed(QObject *object)
144{
145 remove(object);
146}
147
148QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.