Eneboo - Documentación para desarrolladores
src/qt/src/kernel/qguardedptr.h
Ir a la documentación de este archivo.
00001 /****************************************************************************
00002 ** $Id: qt/qguardedptr.h   3.3.8   edited Jan 11 14:38 $
00003 **
00004 ** Definition of QGuardedPtr class
00005 **
00006 ** Created : 990929
00007 **
00008 ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
00009 **
00010 ** This file is part of the kernel module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech ASA of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #ifndef QGUARDEDPTR_H
00039 #define QGUARDEDPTR_H
00040 
00041 #ifndef QT_H
00042 #include "qobject.h"
00043 #endif // QT_H
00044 
00045 // ### 4.0: rename to something without Private in it. Not really internal.
00046 class Q_EXPORT QGuardedPtrPrivate : public QObject, public QShared
00047 {
00048     Q_OBJECT
00049 public:
00050     QGuardedPtrPrivate( QObject* );
00051     ~QGuardedPtrPrivate();
00052 
00053     QObject* object() const;
00054     void reconnect( QObject* );
00055 
00056 private slots:
00057     void objectDestroyed();
00058 
00059 private:
00060     QObject* obj;
00061 #if defined(Q_DISABLE_COPY) // Disabled copy constructor and operator=
00062     QGuardedPtrPrivate( const QGuardedPtrPrivate & );
00063     QGuardedPtrPrivate &operator=( const QGuardedPtrPrivate & );
00064 #endif
00065 };
00066 
00067 template <class T>
00068 class QGuardedPtr
00069 {
00070 public:
00071     QGuardedPtr() : priv( new QGuardedPtrPrivate( 0 ) ) {}
00072 
00073     QGuardedPtr( T* o) {
00074         priv = new QGuardedPtrPrivate( (QObject*)o );
00075     }
00076 
00077     QGuardedPtr(const QGuardedPtr<T> &p) {
00078         priv = p.priv;
00079         ref();
00080     }
00081 
00082     ~QGuardedPtr() { deref(); }
00083 
00084     QGuardedPtr<T> &operator=(const QGuardedPtr<T> &p) {
00085         if ( priv != p.priv ) {
00086             deref();
00087             priv = p.priv;
00088             ref();
00089         }
00090         return *this;
00091     }
00092 
00093     QGuardedPtr<T> &operator=(T* o) {
00094         if ( priv && priv->count == 1 ) {
00095             priv->reconnect( (QObject*)o );
00096         } else {
00097             deref();
00098             priv = new QGuardedPtrPrivate( (QObject*)o );
00099         }
00100         return *this;
00101     }
00102 
00103     bool operator==( const QGuardedPtr<T> &p ) const {
00104         return (T*)(*this) == (T*) p;
00105     }
00106 
00107     bool operator!= ( const QGuardedPtr<T>& p ) const {
00108         return !( *this == p );
00109     }
00110 
00111     bool isNull() const { return !priv || !priv->object(); }
00112 
00113     T* operator->() const { return (T*)(priv?priv->object():0); }
00114 
00115     T& operator*() const { return *((T*)(priv?priv->object():0)); }
00116 
00117     operator T*() const { return (T*)(priv?priv->object():0); }
00118 
00119 private:
00120 
00121     void ref() { if (priv) priv->ref(); }
00122 
00123     void deref() {
00124         if ( priv && priv->deref() )
00125             delete priv;
00126     }
00127 
00128     QGuardedPtrPrivate* priv;
00129 };
00130 
00131 
00132 
00133 
00134 inline QObject* QGuardedPtrPrivate::object() const
00135 {
00136     return obj;
00137 }
00138 
00139 #define Q_DEFINED_QGUARDEDPTR
00140 #include "qwinexport.h"
00141 #endif
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'