| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtCore module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at [email protected].
|
|---|
| 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
|
|---|