lib Library API Documentation

qcleanuphandler.h

00001 #ifndef QCLEANUPHANDLER_H
00002 #define QCLEANUPHANDLER_H
00003 
00004 #ifndef QT_H
00005 #include <qptrlist.h>
00006 #include <qguardedptr.h>
00007 #endif // QT_H
00008 
00009 template<class Type>
00010 class Q_EXPORT QGuardedCleanupHandler
00011 {
00012 public:
00013     ~QGuardedCleanupHandler() { clear(); }
00014 
00015     void add( Type* object )
00016     {
00017     cleanupObjects.insert( 0, new QGuardedPtr<Type>(object) );
00018     }
00019 
00020     void remove( Type *object )
00021     {
00022     QPtrListIterator<QGuardedPtr<Type> > it( cleanupObjects );
00023     while ( it.current() ) {
00024         QGuardedPtr<Type>* guard = it.current();
00025         ++it;
00026         if ( (Type *)guard == object ) {
00027         cleanupObjects.removeRef( guard );
00028         delete guard;
00029         break;
00030         }
00031     }
00032     }
00033 
00034     bool isEmpty() const
00035     {
00036     QPtrListIterator<QGuardedPtr<Type> > it( cleanupObjects );
00037     while ( it.current() ) {
00038         QGuardedPtr<Type>* guard = it.current();
00039         ++it;
00040         if ( (Type*)*guard )
00041         return FALSE;
00042     }
00043     return TRUE;
00044     }
00045 
00046     void clear() {
00047     QPtrListIterator<QGuardedPtr<Type> > it( cleanupObjects );
00048     it.toLast();
00049     while ( it.current() ) {
00050         QGuardedPtr<Type>* guard = it.current();
00051         --it;
00052         cleanupObjects.removeRef( guard );
00053         delete (Type*)*guard;
00054         delete guard;
00055     }
00056     }
00057 
00058 private:
00059     QPtrList<QGuardedPtr<Type> > cleanupObjects;
00060 };
00061 
00062 template<class Type>
00063 class Q_EXPORT QCleanupHandler
00064 {
00065 public:
00066     QCleanupHandler() : cleanupObjects( 0 )
00067     {}
00068     ~QCleanupHandler() { clear(); }
00069 
00070     void add( Type* object )
00071     {
00072     if ( !cleanupObjects ) {
00073         cleanupObjects = new QPtrList<Type>;
00074     }
00075     cleanupObjects->insert( 0, object );
00076     }
00077 
00078     void remove( Type *object )
00079     {
00080     if ( !cleanupObjects )
00081         return;
00082     if ( object )
00083         cleanupObjects->removeRef( object );
00084     }
00085 
00086     bool isEmpty() const
00087     {
00088     return cleanupObjects ? cleanupObjects->isEmpty() : TRUE;
00089     }
00090 
00091     void clear()
00092     {
00093     if ( !cleanupObjects )
00094         return;
00095 
00096     QPtrListIterator<Type> it( *cleanupObjects );
00097     it.toLast();
00098     while ( it.current() ) {
00099         Type* object = it.current();
00100         --it;
00101         cleanupObjects->removeRef( object );
00102         delete object;
00103     }
00104 
00105     delete cleanupObjects;
00106     cleanupObjects = 0;
00107     }
00108 
00109 private:
00110     QPtrList<Type> *cleanupObjects;
00111 };
00112 
00113 #endif //QCLEANUPHANDLER_H
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 13:19:27 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003