src/corelib/kernel/qobjectcleanuphandler.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    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