diff options
Diffstat (limited to 'qtinterface/tqcleanuphandler.h')
-rw-r--r-- | qtinterface/tqcleanuphandler.h | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/qtinterface/tqcleanuphandler.h b/qtinterface/tqcleanuphandler.h index 1a5b6e8..faabf35 100644 --- a/qtinterface/tqcleanuphandler.h +++ b/qtinterface/tqcleanuphandler.h @@ -40,6 +40,92 @@ Boston, MA 02110-1301, USA. //#include <Qt/qcleanuphandler.h> +#include "tqptrlist.h" + +template<class Type> +class TQCleanupHandler +{ +public: + TQCleanupHandler() : cleanupObjects( 0 ) {} + ~TQCleanupHandler() { clear(); } + + Type* add( Type **object ) { + if ( !cleanupObjects ) + cleanupObjects = new Q3PtrList<Type*>; + cleanupObjects->insert( 0, object ); + return *object; + } + + void remove( Type **object ) { + if ( !cleanupObjects ) + return; + if ( cleanupObjects->findRef( object ) >= 0 ) + (void) cleanupObjects->take(); + } + + bool isEmpty() const { + return cleanupObjects ? cleanupObjects->isEmpty() : TRUE; + } + + void clear() { + if ( !cleanupObjects ) + return; + Q3PtrListIterator<Type*> it( *cleanupObjects ); + Type **object; + while ( ( object = it.current() ) ) { + delete *object; + *object = 0; + cleanupObjects->remove( object ); + } + delete cleanupObjects; + cleanupObjects = 0; + } + +private: + Q3PtrList<Type*> *cleanupObjects; +}; + +template<class Type> +class TQSingleCleanupHandler +{ +public: + TQSingleCleanupHandler() : object( 0 ) {} + ~TQSingleCleanupHandler() { + if ( object ) { + delete *object; + *object = 0; + } + } + Type* set( Type **o ) { + object = o; + return *object; + } + void reset() { object = 0; } +private: + Type **object; +}; + +template<class Type> +class TQSharedCleanupHandler +{ +public: + TQSharedCleanupHandler() : object( 0 ) {} + ~TQSharedCleanupHandler() { + if ( object ) { + if ( (*object)->deref() ) + delete *object; + *object = 0; + } + } + Type* set( Type **o ) { + object = o; + return *object; + } + void reset() { object = 0; } +private: + Type **object; +}; + #endif // USE_QT4 #endif /* TQCLEANUPHANDLER_H */
\ No newline at end of file |