00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef QCLEANUPHANDLER_H
00022 #define QCLEANUPHANDLER_H
00023
00024 #ifndef QT_H
00025 #include <qlist.h>
00026 #endif // QT_H
00027
00028 template<class Type>
00029 class Q_EXPORT QCleanupHandler
00030 {
00031 public:
00032 QCleanupHandler() : cleanupObjects( 0 ) {}
00033 ~QCleanupHandler() { clear(); }
00034
00035 Type* add( Type **object ) {
00036 if ( !cleanupObjects )
00037 cleanupObjects = new QList<Type*>;
00038 cleanupObjects->insert( 0, object );
00039 return *object;
00040 }
00041
00042 void remove( Type **object ) {
00043 if ( !cleanupObjects )
00044 return;
00045 if ( cleanupObjects->findRef( object ) >= 0 )
00046 (void) cleanupObjects->take();
00047 }
00048
00049 bool isEmpty() const {
00050 return cleanupObjects ? cleanupObjects->isEmpty() : TRUE;
00051 }
00052
00053 void clear() {
00054 if ( !cleanupObjects )
00055 return;
00056 QListIterator<Type*> it( *cleanupObjects );
00057 Type **object;
00058 while ( ( object = it.current() ) ) {
00059 delete *object;
00060 *object = 0;
00061 cleanupObjects->remove( object );
00062 }
00063 delete cleanupObjects;
00064 cleanupObjects = 0;
00065 }
00066
00067 private:
00068 QList<Type*> *cleanupObjects;
00069 };
00070
00071 #endif //QCLEANUPHANDLER_H