home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qcleanuphandler.h < prev    next >
C/C++ Source or Header  |  2001-06-01  |  1KB  |  52 lines

  1. #ifndef QCLEANUPHANDLER_H
  2. #define QCLEANUPHANDLER_H
  3.  
  4. #ifndef QT_H
  5. #include <qptrlist.h>
  6. #endif // QT_H
  7.  
  8. template<class Type>
  9. class Q_EXPORT QCleanupHandler
  10. {
  11. public:
  12.     QCleanupHandler() : cleanupObjects( 0 ) {}
  13.     ~QCleanupHandler() { clear(); }
  14.  
  15.     Type* add( Type **object ) {
  16.     if ( !cleanupObjects )
  17.         cleanupObjects = new QPtrList<Type*>;
  18.     cleanupObjects->insert( 0, object );
  19.     return *object;
  20.     }
  21.  
  22.     void remove( Type **object ) {
  23.     if ( !cleanupObjects )
  24.         return;
  25.     if ( cleanupObjects->findRef( object ) >= 0 )
  26.         (void) cleanupObjects->take();
  27.     }
  28.  
  29.     bool isEmpty() const {
  30.     return cleanupObjects ? cleanupObjects->isEmpty() : TRUE;
  31.     }
  32.  
  33.     void clear() {
  34.     if ( !cleanupObjects )
  35.         return;
  36.     QPtrListIterator<Type*> it( *cleanupObjects );
  37.     Type **object;
  38.     while ( ( object = it.current() ) ) {
  39.         delete *object;
  40.         *object = 0;
  41.         cleanupObjects->remove( object );
  42.     }
  43.     delete cleanupObjects;
  44.     cleanupObjects = 0;
  45.     }
  46.  
  47. private:
  48.     QPtrList<Type*> *cleanupObjects;
  49. };
  50.  
  51. #endif //QCLEANUPHANDLER_H
  52.