| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qguardedptr.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Implementation of QGuardedPtr class
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 990929
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #include "qguardedptr.h"
|
|---|
| 39 |
|
|---|
| 40 | /*!
|
|---|
| 41 | \class QGuardedPtr qguardedptr.h
|
|---|
| 42 | \brief The QGuardedPtr class is a template class that provides guarded pointers to QObjects.
|
|---|
| 43 |
|
|---|
| 44 | \ingroup objectmodel
|
|---|
| 45 | \mainclass
|
|---|
| 46 |
|
|---|
| 47 | A guarded pointer, \c{QGuardedPtr<X>}, behaves like a normal C++
|
|---|
| 48 | pointer \c{X*}, except that it is automatically set to 0 when
|
|---|
| 49 | the referenced object is destroyed (unlike normal C++ pointers,
|
|---|
| 50 | which become "dangling pointers" in such cases). \c X must be a
|
|---|
| 51 | subclass of QObject.
|
|---|
| 52 |
|
|---|
| 53 | Guarded pointers are useful whenever you need to store a pointer
|
|---|
| 54 | to a QObject that is owned by someone else and therefore might be
|
|---|
| 55 | destroyed while you still hold a reference to it. You can safely
|
|---|
| 56 | test the pointer for validity.
|
|---|
| 57 |
|
|---|
| 58 | Example:
|
|---|
| 59 | \code
|
|---|
| 60 | QGuardedPtr<QLabel> label = new QLabel( 0, "label" );
|
|---|
| 61 | label->setText( "I like guarded pointers" );
|
|---|
| 62 |
|
|---|
| 63 | delete (QLabel*) label; // simulate somebody destroying the label
|
|---|
| 64 |
|
|---|
| 65 | if ( label)
|
|---|
| 66 | label->show();
|
|---|
| 67 | else
|
|---|
| 68 | qDebug("The label has been destroyed");
|
|---|
| 69 | \endcode
|
|---|
| 70 |
|
|---|
| 71 | The program will output \c{The label has been destroyed} rather
|
|---|
| 72 | than dereferencing an invalid address in \c label->show().
|
|---|
| 73 |
|
|---|
| 74 | The functions and operators available with a QGuardedPtr are the
|
|---|
| 75 | same as those available with a normal unguarded pointer, except
|
|---|
| 76 | the pointer arithmetic operators (++, --, -, and +), which are
|
|---|
| 77 | normally used only with arrays of objects. Use them like normal
|
|---|
| 78 | pointers and you will not need to read this class documentation.
|
|---|
| 79 |
|
|---|
| 80 | For creating guarded pointers, you can construct or assign to them
|
|---|
| 81 | from an X* or from another guarded pointer of the same type. You
|
|---|
| 82 | can compare them with each other using operator==() and
|
|---|
| 83 | operator!=(), or test for 0 with isNull(). And you can dereference
|
|---|
| 84 | them using either the \c *x or the \c x->member notation.
|
|---|
| 85 |
|
|---|
| 86 | A guarded pointer will automatically cast to an X*, so you can
|
|---|
| 87 | freely mix guarded and unguarded pointers. This means that if you
|
|---|
| 88 | have a QGuardedPtr<QWidget>, you can pass it to a function that
|
|---|
| 89 | requires a QWidget*. For this reason, it is of little value to
|
|---|
| 90 | declare functions to take a QGuardedPtr as a parameter; just use
|
|---|
| 91 | normal pointers. Use a QGuardedPtr when you are storing a pointer
|
|---|
| 92 | over time.
|
|---|
| 93 |
|
|---|
| 94 | Note again that class \e X must inherit QObject, or a compilation
|
|---|
| 95 | or link error will result.
|
|---|
| 96 | */
|
|---|
| 97 |
|
|---|
| 98 | /*!
|
|---|
| 99 | \fn QGuardedPtr::QGuardedPtr()
|
|---|
| 100 |
|
|---|
| 101 | Constructs a 0 guarded pointer.
|
|---|
| 102 |
|
|---|
| 103 | \sa isNull()
|
|---|
| 104 | */
|
|---|
| 105 |
|
|---|
| 106 | /*!
|
|---|
| 107 | \fn QGuardedPtr::QGuardedPtr( T* p )
|
|---|
| 108 |
|
|---|
| 109 | Constructs a guarded pointer that points to same object as \a p
|
|---|
| 110 | points to.
|
|---|
| 111 | */
|
|---|
| 112 |
|
|---|
| 113 | /*!
|
|---|
| 114 | \fn QGuardedPtr::QGuardedPtr(const QGuardedPtr<T> &p)
|
|---|
| 115 |
|
|---|
| 116 | Copy one guarded pointer from another. The constructed guarded
|
|---|
| 117 | pointer points to the same object that \a p points to (which may
|
|---|
| 118 | be 0).
|
|---|
| 119 | */
|
|---|
| 120 |
|
|---|
| 121 | /*!
|
|---|
| 122 | \fn QGuardedPtr::~QGuardedPtr()
|
|---|
| 123 |
|
|---|
| 124 | Destroys the guarded pointer. Just like a normal pointer,
|
|---|
| 125 | destroying a guarded pointer does \e not destroy the object being
|
|---|
| 126 | pointed to.
|
|---|
| 127 | */
|
|---|
| 128 |
|
|---|
| 129 | /*!
|
|---|
| 130 | \fn QGuardedPtr<T>& QGuardedPtr::operator=(const QGuardedPtr<T> &p)
|
|---|
| 131 |
|
|---|
| 132 | Assignment operator. This guarded pointer then points to the same
|
|---|
| 133 | object as \a p points to.
|
|---|
| 134 | */
|
|---|
| 135 |
|
|---|
| 136 | /*!
|
|---|
| 137 | \overload QGuardedPtr<T> & QGuardedPtr::operator=(T* p)
|
|---|
| 138 |
|
|---|
| 139 | Assignment operator. This guarded pointer then points to the same
|
|---|
| 140 | object as \a p points to.
|
|---|
| 141 | */
|
|---|
| 142 |
|
|---|
| 143 | /*!
|
|---|
| 144 | \fn bool QGuardedPtr::operator==( const QGuardedPtr<T> &p ) const
|
|---|
| 145 |
|
|---|
| 146 | Equality operator; implements traditional pointer semantics.
|
|---|
| 147 | Returns TRUE if both \a p and this guarded pointer are 0, or if
|
|---|
| 148 | both \a p and this pointer point to the same object; otherwise
|
|---|
| 149 | returns FALSE.
|
|---|
| 150 |
|
|---|
| 151 | \sa operator!=()
|
|---|
| 152 | */
|
|---|
| 153 |
|
|---|
| 154 | /*!
|
|---|
| 155 | \fn bool QGuardedPtr::operator!= ( const QGuardedPtr<T>& p ) const
|
|---|
| 156 |
|
|---|
| 157 | Inequality operator; implements pointer semantics, the negation of
|
|---|
| 158 | operator==(). Returns TRUE if \a p and this guarded pointer are
|
|---|
| 159 | not pointing to the same object; otherwise returns FALSE.
|
|---|
| 160 | */
|
|---|
| 161 |
|
|---|
| 162 | /*!
|
|---|
| 163 | \fn bool QGuardedPtr::isNull() const
|
|---|
| 164 |
|
|---|
| 165 | Returns \c TRUE if the referenced object has been destroyed or if
|
|---|
| 166 | there is no referenced object; otherwise returns FALSE.
|
|---|
| 167 | */
|
|---|
| 168 |
|
|---|
| 169 | /*!
|
|---|
| 170 | \fn T* QGuardedPtr::operator->() const
|
|---|
| 171 |
|
|---|
| 172 | Overloaded arrow operator; implements pointer semantics. Just use
|
|---|
| 173 | this operator as you would with a normal C++ pointer.
|
|---|
| 174 | */
|
|---|
| 175 |
|
|---|
| 176 | /*!
|
|---|
| 177 | \fn T& QGuardedPtr::operator*() const
|
|---|
| 178 |
|
|---|
| 179 | Dereference operator; implements pointer semantics. Just use this
|
|---|
| 180 | operator as you would with a normal C++ pointer.
|
|---|
| 181 | */
|
|---|
| 182 |
|
|---|
| 183 | /*!
|
|---|
| 184 | \fn QGuardedPtr::operator T*() const
|
|---|
| 185 |
|
|---|
| 186 | Cast operator; implements pointer semantics. Because of this
|
|---|
| 187 | function you can pass a QGuardedPtr\<X\> to a function where an X*
|
|---|
| 188 | is required.
|
|---|
| 189 | */
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | /* Internal classes */
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 | QGuardedPtrPrivate::QGuardedPtrPrivate( QObject* o)
|
|---|
| 196 | : QObject(0, "_ptrpriv" ), obj( o )
|
|---|
| 197 | {
|
|---|
| 198 | if ( obj )
|
|---|
| 199 | connect( obj, SIGNAL( destroyed() ), this, SLOT( objectDestroyed() ) );
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 | QGuardedPtrPrivate::~QGuardedPtrPrivate()
|
|---|
| 204 | {
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | void QGuardedPtrPrivate::reconnect( QObject *o )
|
|---|
| 208 | {
|
|---|
| 209 | if ( obj == o )
|
|---|
| 210 | return;
|
|---|
| 211 | if ( obj )
|
|---|
| 212 | disconnect( obj, SIGNAL( destroyed() ),
|
|---|
| 213 | this, SLOT( objectDestroyed() ) );
|
|---|
| 214 | obj = o;
|
|---|
| 215 | if ( obj )
|
|---|
| 216 | connect( obj, SIGNAL( destroyed() ),
|
|---|
| 217 | this, SLOT( objectDestroyed() ) );
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | void QGuardedPtrPrivate::objectDestroyed()
|
|---|
| 221 | {
|
|---|
| 222 | obj = 0;
|
|---|
| 223 | }
|
|---|