1 | /****************************************************************************
|
---|
2 | ** $Id: qobjectcleanuphandler.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QObjectCleanupHandler class
|
---|
5 | **
|
---|
6 | ** Copyright (C) 2000-2001 Trolltech AS. All rights reserved.
|
---|
7 | **
|
---|
8 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
---|
9 | **
|
---|
10 | ** This file may be distributed under the terms of the Q Public License
|
---|
11 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
12 | ** LICENSE.QPL included in the packaging of this file.
|
---|
13 | **
|
---|
14 | ** This file may be distributed and/or modified under the terms of the
|
---|
15 | ** GNU General Public License version 2 as published by the Free Software
|
---|
16 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
17 | ** packaging of this file.
|
---|
18 | **
|
---|
19 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
20 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
21 | ** Agreement provided with the Software.
|
---|
22 | **
|
---|
23 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
24 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
25 | **
|
---|
26 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
27 | ** information about Qt Commercial License Agreements.
|
---|
28 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
29 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
30 | **
|
---|
31 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
32 | ** not clear to you.
|
---|
33 | **
|
---|
34 | **********************************************************************/
|
---|
35 |
|
---|
36 | #include "qobjectcleanuphandler.h"
|
---|
37 | #include "qobjectlist.h"
|
---|
38 |
|
---|
39 | /*!
|
---|
40 | \class QObjectCleanupHandler qobjectcleanuphandler.h
|
---|
41 | \brief The QObjectCleanupHandler class watches the lifetime of multiple QObjects.
|
---|
42 |
|
---|
43 | \ingroup objectmodel
|
---|
44 |
|
---|
45 | A QObjectCleanupHandler is useful whenever you need to know when a
|
---|
46 | number of \l{QObject}s that are owned by someone else have been
|
---|
47 | deleted. This is important, for example, when referencing memory
|
---|
48 | in an application that has been allocated in a shared library.
|
---|
49 |
|
---|
50 | Example:
|
---|
51 |
|
---|
52 | \code
|
---|
53 | class FactoryComponent : public FactoryInterface, public QLibraryInterface
|
---|
54 | {
|
---|
55 | public:
|
---|
56 | ...
|
---|
57 |
|
---|
58 | QObject *createObject();
|
---|
59 |
|
---|
60 | bool init();
|
---|
61 | void cleanup();
|
---|
62 | bool canUnload() const;
|
---|
63 |
|
---|
64 | private:
|
---|
65 | QObjectCleanupHandler objects;
|
---|
66 | };
|
---|
67 |
|
---|
68 | // allocate a new object, and add it to the cleanup handler
|
---|
69 | QObject *FactoryComponent::createObject()
|
---|
70 | {
|
---|
71 | return objects.add( new QObject() );
|
---|
72 | }
|
---|
73 |
|
---|
74 | // QLibraryInterface implementation
|
---|
75 | bool FactoryComponent::init()
|
---|
76 | {
|
---|
77 | return TRUE;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void FactoryComponent::cleanup()
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | // it is only safe to unload the library when all QObject's have been destroyed
|
---|
85 | bool FactoryComponent::canUnload() const
|
---|
86 | {
|
---|
87 | return objects.isEmpty();
|
---|
88 | }
|
---|
89 | \endcode
|
---|
90 | */
|
---|
91 |
|
---|
92 | /*!
|
---|
93 | Constructs an empty QObjectCleanupHandler.
|
---|
94 | */
|
---|
95 | QObjectCleanupHandler::QObjectCleanupHandler()
|
---|
96 | : QObject(), cleanupObjects( 0 )
|
---|
97 | {
|
---|
98 | }
|
---|
99 |
|
---|
100 | /*!
|
---|
101 | Destroys the cleanup handler. All objects in this cleanup handler
|
---|
102 | will be deleted.
|
---|
103 | */
|
---|
104 | QObjectCleanupHandler::~QObjectCleanupHandler()
|
---|
105 | {
|
---|
106 | clear();
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*!
|
---|
110 | Adds \a object to this cleanup handler and returns the pointer to
|
---|
111 | the object.
|
---|
112 | */
|
---|
113 | QObject* QObjectCleanupHandler::add( QObject* object )
|
---|
114 | {
|
---|
115 | if ( !object )
|
---|
116 | return 0;
|
---|
117 |
|
---|
118 | if ( !cleanupObjects ) {
|
---|
119 | cleanupObjects = new QObjectList;
|
---|
120 | cleanupObjects->setAutoDelete( TRUE );
|
---|
121 | }
|
---|
122 | connect( object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)) );
|
---|
123 | cleanupObjects->insert( 0, object );
|
---|
124 | return object;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*!
|
---|
128 | Removes the \a object from this cleanup handler. The object will
|
---|
129 | not be destroyed.
|
---|
130 | */
|
---|
131 | void QObjectCleanupHandler::remove( QObject *object )
|
---|
132 | {
|
---|
133 | if ( !cleanupObjects )
|
---|
134 | return;
|
---|
135 | if ( cleanupObjects->findRef( object ) >= 0 ) {
|
---|
136 | (void) cleanupObjects->take();
|
---|
137 | disconnect( object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)) );
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*!
|
---|
142 | Returns TRUE if this cleanup handler is empty or if all objects in
|
---|
143 | this cleanup handler have been destroyed; otherwise return FALSE.
|
---|
144 | */
|
---|
145 | bool QObjectCleanupHandler::isEmpty() const
|
---|
146 | {
|
---|
147 | return cleanupObjects ? cleanupObjects->isEmpty() : TRUE;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /*!
|
---|
151 | Deletes all objects in this cleanup handler. The cleanup handler
|
---|
152 | becomes empty.
|
---|
153 | */
|
---|
154 | void QObjectCleanupHandler::clear()
|
---|
155 | {
|
---|
156 | delete cleanupObjects;
|
---|
157 | cleanupObjects = 0;
|
---|
158 | }
|
---|
159 |
|
---|
160 | void QObjectCleanupHandler::objectDestroyed( QObject*object )
|
---|
161 | {
|
---|
162 | if ( cleanupObjects )
|
---|
163 | cleanupObjects->setAutoDelete( FALSE );
|
---|
164 |
|
---|
165 | remove( object );
|
---|
166 |
|
---|
167 | if ( cleanupObjects )
|
---|
168 | cleanupObjects->setAutoDelete( TRUE );
|
---|
169 | }
|
---|