| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qobjectcleanuphandler.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 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:LGPL21$ | - | ||||||
| 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 | ** $QT_END_LICENSE$ | - | ||||||
| 31 | ** | - | ||||||
| 32 | ****************************************************************************/ | - | ||||||
| 33 | - | |||||||
| 34 | #include "qobjectcleanuphandler.h" | - | ||||||
| 35 | - | |||||||
| 36 | QT_BEGIN_NAMESPACE | - | ||||||
| 37 | - | |||||||
| 38 | /*! | - | ||||||
| 39 | \class QObjectCleanupHandler | - | ||||||
| 40 | \inmodule QtCore | - | ||||||
| 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 | To keep track of some \l{QObject}s, create a | - | ||||||
| 51 | QObjectCleanupHandler, and add() the objects you are interested | - | ||||||
| 52 | in. If you are no longer interested in tracking a particular | - | ||||||
| 53 | object, use remove() to remove it from the cleanup handler. If an | - | ||||||
| 54 | object being tracked by the cleanup handler gets deleted by | - | ||||||
| 55 | someone else it will automatically be removed from the cleanup | - | ||||||
| 56 | handler. You can delete all the objects in the cleanup handler | - | ||||||
| 57 | with clear(), or by destroying the cleanup handler. isEmpty() | - | ||||||
| 58 | returns \c true if the QObjectCleanupHandler has no objects to keep | - | ||||||
| 59 | track of. | - | ||||||
| 60 | - | |||||||
| 61 | \sa QPointer | - | ||||||
| 62 | */ | - | ||||||
| 63 | - | |||||||
| 64 | /*! | - | ||||||
| 65 | Constructs an empty QObjectCleanupHandler. | - | ||||||
| 66 | */ | - | ||||||
| 67 | QObjectCleanupHandler::QObjectCleanupHandler() | - | ||||||
| 68 | { | - | ||||||
| 69 | } | - | ||||||
| 70 | - | |||||||
| 71 | /*! | - | ||||||
| 72 | Destroys the cleanup handler. All objects in this cleanup handler | - | ||||||
| 73 | will be deleted. | - | ||||||
| 74 | - | |||||||
| 75 | \sa clear() | - | ||||||
| 76 | */ | - | ||||||
| 77 | QObjectCleanupHandler::~QObjectCleanupHandler() | - | ||||||
| 78 | { | - | ||||||
| 79 | clear(); | - | ||||||
| 80 | } never executed: end of block | 0 | ||||||
| 81 | - | |||||||
| 82 | /*! | - | ||||||
| 83 | Adds \a object to this cleanup handler and returns the pointer to | - | ||||||
| 84 | the object. | - | ||||||
| 85 | - | |||||||
| 86 | \sa remove() | - | ||||||
| 87 | */ | - | ||||||
| 88 | QObject *QObjectCleanupHandler::add(QObject* object) | - | ||||||
| 89 | { | - | ||||||
| 90 | if (!object)
| 0 | ||||||
| 91 | return 0; never executed: return 0; | 0 | ||||||
| 92 | - | |||||||
| 93 | connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*))); | - | ||||||
| 94 | cleanupObjects.insert(0, object); | - | ||||||
| 95 | return object; never executed: return object; | 0 | ||||||
| 96 | } | - | ||||||
| 97 | - | |||||||
| 98 | /*! | - | ||||||
| 99 | Removes the \a object from this cleanup handler. The object will | - | ||||||
| 100 | not be destroyed. | - | ||||||
| 101 | - | |||||||
| 102 | \sa add() | - | ||||||
| 103 | */ | - | ||||||
| 104 | void QObjectCleanupHandler::remove(QObject *object) | - | ||||||
| 105 | { | - | ||||||
| 106 | int index; | - | ||||||
| 107 | if ((index = cleanupObjects.indexOf(object)) != -1) {
| 0 | ||||||
| 108 | cleanupObjects.removeAt(index); | - | ||||||
| 109 | disconnect(object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*))); | - | ||||||
| 110 | } never executed: end of block | 0 | ||||||
| 111 | } never executed: end of block | 0 | ||||||
| 112 | - | |||||||
| 113 | /*! | - | ||||||
| 114 | Returns \c true if this cleanup handler is empty or if all objects in | - | ||||||
| 115 | this cleanup handler have been destroyed; otherwise return false. | - | ||||||
| 116 | - | |||||||
| 117 | \sa add(), remove(), clear() | - | ||||||
| 118 | */ | - | ||||||
| 119 | bool QObjectCleanupHandler::isEmpty() const | - | ||||||
| 120 | { | - | ||||||
| 121 | return cleanupObjects.isEmpty(); never executed: return cleanupObjects.isEmpty(); | 0 | ||||||
| 122 | } | - | ||||||
| 123 | - | |||||||
| 124 | /*! | - | ||||||
| 125 | Deletes all objects in this cleanup handler. The cleanup handler | - | ||||||
| 126 | becomes empty. | - | ||||||
| 127 | - | |||||||
| 128 | \sa isEmpty() | - | ||||||
| 129 | */ | - | ||||||
| 130 | void QObjectCleanupHandler::clear() | - | ||||||
| 131 | { | - | ||||||
| 132 | while (!cleanupObjects.isEmpty())
| 0 | ||||||
| 133 | delete cleanupObjects.takeFirst(); never executed: delete cleanupObjects.takeFirst(); | 0 | ||||||
| 134 | } never executed: end of block | 0 | ||||||
| 135 | - | |||||||
| 136 | void QObjectCleanupHandler::objectDestroyed(QObject *object) | - | ||||||
| 137 | { | - | ||||||
| 138 | remove(object); | - | ||||||
| 139 | } never executed: end of block | 0 | ||||||
| 140 | - | |||||||
| 141 | QT_END_NAMESPACE | - | ||||||
| Source code | Switch to Preprocessed file |