home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qguardedptr.h < prev    next >
C/C++ Source or Header  |  2001-10-11  |  3KB  |  136 lines

  1. /****************************************************************************
  2. ** $Id$
  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. class Q_EXPORT QGuardedPtrPrivate : public QObject, public QShared
  46. {
  47.     Q_OBJECT
  48. public:
  49.     QGuardedPtrPrivate( QObject* );
  50.     ~QGuardedPtrPrivate();
  51.  
  52.     QObject* object() const;
  53.     void reconnect( QObject* );
  54.  
  55. private slots:
  56.     void objectDestroyed();
  57.  
  58. private:
  59.     QObject* obj;
  60. };
  61.  
  62.  
  63. template <class T> class Q_EXPORT QGuardedPtr
  64. {
  65. public:
  66.     QGuardedPtr() : priv( new QGuardedPtrPrivate( 0 ) ) {}
  67.  
  68.     QGuardedPtr( T* o) {
  69.     priv = new QGuardedPtrPrivate( (QObject*)o );
  70.     }
  71.  
  72.     QGuardedPtr(const QGuardedPtr<T> &p) {
  73.     priv = p.priv;
  74.     ref();
  75.     }
  76.  
  77.     ~QGuardedPtr() { deref(); }
  78.  
  79.     QGuardedPtr<T> &operator=(const QGuardedPtr<T> &p) {
  80.     if ( priv != p.priv ) {
  81.         deref();
  82.         priv = p.priv;
  83.         ref();
  84.     }
  85.     return *this;
  86.     }
  87.  
  88.     QGuardedPtr<T> &operator=(T* o) {
  89.     if ( priv->count == 1 ) {
  90.         priv->reconnect( (QObject*)o );
  91.     } else {
  92.         deref();
  93.         priv = new QGuardedPtrPrivate( (QObject*)o );
  94.     }
  95.     return *this;
  96.     }
  97.  
  98.     bool operator==( const QGuardedPtr<T> &p ) const {
  99.     return priv->object() == p.priv->object();
  100.     }
  101.  
  102.     bool operator!= ( const QGuardedPtr<T>& p ) const {
  103.     return !( *this == p );
  104.     }
  105.  
  106.     bool isNull() const { return !priv->object(); }
  107.  
  108.     T* operator->() const { return (T*) priv->object(); }
  109.  
  110.     T& operator*() const { return *( (T*)priv->object() ); }
  111.  
  112.     operator T*() const { return (T*) priv->object(); }
  113.  
  114. private:
  115.  
  116.     void ref() { priv->ref(); }
  117.  
  118.     void deref() {
  119.     if ( priv->deref() )
  120.         delete priv;
  121.     }
  122.  
  123.     QGuardedPtrPrivate* priv;
  124. };
  125.  
  126.  
  127.  
  128.  
  129. inline QObject* QGuardedPtrPrivate::object() const
  130. {
  131.     return obj;
  132. }
  133.  
  134.  
  135. #endif
  136.