| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qguardedptr.h 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Definition 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 | #ifndef QGUARDEDPTR_H
|
|---|
| 39 | #define QGUARDEDPTR_H
|
|---|
| 40 |
|
|---|
| 41 | #ifndef QT_H
|
|---|
| 42 | #include "qobject.h"
|
|---|
| 43 | #endif // QT_H
|
|---|
| 44 |
|
|---|
| 45 | // ### 4.0: rename to something without Private in it. Not really internal.
|
|---|
| 46 | class Q_EXPORT QGuardedPtrPrivate : public QObject, public QShared
|
|---|
| 47 | {
|
|---|
| 48 | Q_OBJECT
|
|---|
| 49 | public:
|
|---|
| 50 | QGuardedPtrPrivate( QObject* );
|
|---|
| 51 | ~QGuardedPtrPrivate();
|
|---|
| 52 |
|
|---|
| 53 | QObject* object() const;
|
|---|
| 54 | void reconnect( QObject* );
|
|---|
| 55 |
|
|---|
| 56 | private slots:
|
|---|
| 57 | void objectDestroyed();
|
|---|
| 58 |
|
|---|
| 59 | private:
|
|---|
| 60 | QObject* obj;
|
|---|
| 61 | #if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator=
|
|---|
| 62 | QGuardedPtrPrivate( const QGuardedPtrPrivate & );
|
|---|
| 63 | QGuardedPtrPrivate &operator=( const QGuardedPtrPrivate & );
|
|---|
| 64 | #endif
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | template <class T>
|
|---|
| 68 | class QGuardedPtr
|
|---|
| 69 | {
|
|---|
| 70 | public:
|
|---|
| 71 | QGuardedPtr() : priv( new QGuardedPtrPrivate( 0 ) ) {}
|
|---|
| 72 |
|
|---|
| 73 | QGuardedPtr( T* o) {
|
|---|
| 74 | priv = new QGuardedPtrPrivate( (QObject*)o );
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | QGuardedPtr(const QGuardedPtr<T> &p) {
|
|---|
| 78 | priv = p.priv;
|
|---|
| 79 | ref();
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | ~QGuardedPtr() { deref(); }
|
|---|
| 83 |
|
|---|
| 84 | QGuardedPtr<T> &operator=(const QGuardedPtr<T> &p) {
|
|---|
| 85 | if ( priv != p.priv ) {
|
|---|
| 86 | deref();
|
|---|
| 87 | priv = p.priv;
|
|---|
| 88 | ref();
|
|---|
| 89 | }
|
|---|
| 90 | return *this;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | QGuardedPtr<T> &operator=(T* o) {
|
|---|
| 94 | if ( priv && priv->count == 1 ) {
|
|---|
| 95 | priv->reconnect( (QObject*)o );
|
|---|
| 96 | } else {
|
|---|
| 97 | deref();
|
|---|
| 98 | priv = new QGuardedPtrPrivate( (QObject*)o );
|
|---|
| 99 | }
|
|---|
| 100 | return *this;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | bool operator==( const QGuardedPtr<T> &p ) const {
|
|---|
| 104 | return (T*)(*this) == (T*) p;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | bool operator!= ( const QGuardedPtr<T>& p ) const {
|
|---|
| 108 | return !( *this == p );
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | bool isNull() const { return !priv || !priv->object(); }
|
|---|
| 112 |
|
|---|
| 113 | T* operator->() const { return (T*)(priv?priv->object():0); }
|
|---|
| 114 |
|
|---|
| 115 | T& operator*() const { return *((T*)(priv?priv->object():0)); }
|
|---|
| 116 |
|
|---|
| 117 | operator T*() const { return (T*)(priv?priv->object():0); }
|
|---|
| 118 |
|
|---|
| 119 | private:
|
|---|
| 120 |
|
|---|
| 121 | void ref() { if (priv) priv->ref(); }
|
|---|
| 122 |
|
|---|
| 123 | void deref() {
|
|---|
| 124 | if ( priv && priv->deref() )
|
|---|
| 125 | delete priv;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | QGuardedPtrPrivate* priv;
|
|---|
| 129 | };
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 | inline QObject* QGuardedPtrPrivate::object() const
|
|---|
| 135 | {
|
|---|
| 136 | return obj;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | #define Q_DEFINED_QGUARDEDPTR
|
|---|
| 140 | #include "qwinexport.h"
|
|---|
| 141 | #endif
|
|---|