summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-write/qcleanuphandler.h
Unidiff
Diffstat (limited to 'noncore/apps/opie-write/qcleanuphandler.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-write/qcleanuphandler.h139
1 files changed, 139 insertions, 0 deletions
diff --git a/noncore/apps/opie-write/qcleanuphandler.h b/noncore/apps/opie-write/qcleanuphandler.h
new file mode 100644
index 0000000..5c5bf16
--- a/dev/null
+++ b/noncore/apps/opie-write/qcleanuphandler.h
@@ -0,0 +1,139 @@
1/****************************************************************************
2** $Id$
3**
4** ...
5**
6** Copyright (C) 2001-2002 Trolltech AS. All rights reserved.
7**
8** This file is part of the tools module of the Qt GUI Toolkit.
9**
10** This file may be distributed under the terms of the Q Public License
11** as defined by Trolltech AS of Norway and appearing in the file
12** LICENSE.QPL included in the packaging of this file.
13**
14** This file may be distributed and/or modified under the terms of the
15** GNU General Public License version 2 as published by the Free Software
16** Foundation and appearing in the file LICENSE.GPL included in the
17** packaging of this file.
18**
19** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
20** licenses may use this file in accordance with the Qt Commercial License
21** Agreement provided with the Software.
22**
23** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25**
26** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27** information about Qt Commercial License Agreements.
28** See http://www.trolltech.com/qpl/ for QPL licensing information.
29** See http://www.trolltech.com/gpl/ for GPL licensing information.
30**
31** Contact info@trolltech.com if any conditions of this licensing are
32** not clear to you.
33**
34**********************************************************************/
35
36#ifndef QCLEANUPHANDLER_H
37#define QCLEANUPHANDLER_H
38
39#ifndef QT_H
40#include <qlist.h>
41#endif // QT_H
42
43template<class Type>
44#ifdef Q_NO_TEMPLATE_EXPORT
45class QCleanupHandler
46#else
47class Q_EXPORT QCleanupHandler
48#endif
49{
50public:
51 QCleanupHandler() : cleanupObjects( 0 ) {}
52 ~QCleanupHandler() { clear(); }
53
54 Type* add( Type **object ) {
55 if ( !cleanupObjects )
56 cleanupObjects = new QPtrList<Type*>;
57 cleanupObjects->insert( 0, object );
58 return *object;
59 }
60
61 void remove( Type **object ) {
62 if ( !cleanupObjects )
63 return;
64 if ( cleanupObjects->findRef( object ) >= 0 )
65 (void) cleanupObjects->take();
66 }
67
68 bool isEmpty() const {
69 return cleanupObjects ? cleanupObjects->isEmpty() : TRUE;
70 }
71
72 void clear() {
73 if ( !cleanupObjects )
74 return;
75 QPtrListIterator<Type*> it( *cleanupObjects );
76 Type **object;
77 while ( ( object = it.current() ) ) {
78 delete *object;
79 *object = 0;
80 cleanupObjects->remove( object );
81 }
82 delete cleanupObjects;
83 cleanupObjects = 0;
84 }
85
86private:
87 QPtrList<Type*> *cleanupObjects;
88};
89
90template<class Type>
91#ifdef Q_NO_TEMPLATE_EXPORT
92class QSingleCleanupHandler
93#else
94class Q_EXPORT QSingleCleanupHandler
95#endif
96{
97public:
98 QSingleCleanupHandler() : object( 0 ) {}
99 ~QSingleCleanupHandler() {
100 if ( object ) {
101 delete *object;
102 *object = 0;
103 }
104 }
105 Type* set( Type **o ) {
106 object = o;
107 return *object;
108 }
109 void reset() { object = 0; }
110private:
111 Type **object;
112};
113
114template<class Type>
115#ifdef Q_NO_TEMPLATE_EXPORT
116class QSharedCleanupHandler
117#else
118class Q_EXPORT QSharedCleanupHandler
119#endif
120{
121public:
122 QSharedCleanupHandler() : object( 0 ) {}
123 ~QSharedCleanupHandler() {
124 if ( object ) {
125 if ( (*object)->deref() )
126 delete *object;
127 *object = 0;
128 }
129 }
130 Type* set( Type **o ) {
131 object = o;
132 return *object;
133 }
134 void reset() { object = 0; }
135private:
136 Type **object;
137};
138
139#endif //QCLEANUPHANDLER_H