| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). |
| 4 |
** All rights reserved. |
| 5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
| 6 |
** |
| 7 |
** This file is part of the QtCore module of the Qt Toolkit. |
| 8 |
** |
| 9 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 10 |
** GNU Lesser General Public License Usage |
| 11 |
** This file may be used under the terms of the GNU Lesser General Public |
| 12 |
** License version 2.1 as published by the Free Software Foundation and |
| 13 |
** appearing in the file LICENSE.LGPL included in the packaging of this |
| 14 |
** file. Please review the following information to ensure the GNU Lesser |
| 15 |
** General Public License version 2.1 requirements will be met: |
| 16 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 17 |
** |
| 18 |
** In addition, as a special exception, Nokia gives you certain additional |
| 19 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 20 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 21 |
** |
| 22 |
** GNU General Public License Usage |
| 23 |
** Alternatively, this file may be used under the terms of the GNU General |
| 24 |
** Public License version 3.0 as published by the Free Software Foundation |
| 25 |
** and appearing in the file LICENSE.GPL included in the packaging of this |
| 26 |
** file. Please review the following information to ensure the GNU General |
| 27 |
** Public License version 3.0 requirements will be met: |
| 28 |
** http://www.gnu.org/copyleft/gpl.html. |
| 29 |
** |
| 30 |
** Other Usage |
| 31 |
** Alternatively, this file may be used in accordance with the terms and |
| 32 |
** conditions contained in a signed written agreement between you and Nokia. |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 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 |
*/ |
| 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 |
*/ |
| 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 |
*/ |
| 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 |
*/ |
| 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 |
*/ |
| 126 |
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 |
*/ |
| 137 |
void QObjectCleanupHandler::clear() |
| 138 |
{ |
| 139 |
while (!cleanupObjects.isEmpty()) |
| 140 |
delete cleanupObjects.takeFirst(); |
| 141 |
} |
| 142 |
|
| 143 |
void QObjectCleanupHandler::objectDestroyed(QObject *object) |
| 144 |
{ |
| 145 |
remove(object); |
| 146 |
} |
| 147 |
|
| 148 |
QT_END_NAMESPACE |