-rw-r--r-- | libkcal/listbase.h | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/libkcal/listbase.h b/libkcal/listbase.h new file mode 100644 index 0000000..085b13d --- a/dev/null +++ b/libkcal/listbase.h | |||
@@ -0,0 +1,97 @@ | |||
1 | /* | ||
2 | This file is part of libkcal. | ||
3 | |||
4 | Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | ||
15 | |||
16 | You should have received a copy of the GNU Library General Public License | ||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | #ifndef KCAL_LISTBASE_H | ||
22 | #define KCAL_LISTBASE_H | ||
23 | |||
24 | #include <qvaluelist.h> | ||
25 | |||
26 | namespace KCal { | ||
27 | class Event; | ||
28 | class Todo; | ||
29 | /** | ||
30 | This class provides a template for lists of pointers. It extends QValueList<T | ||
31 | *> by auto delete funtionality known from QPtrList. | ||
32 | */ | ||
33 | template<class T> | ||
34 | class ListBase : public QValueList<T *> | ||
35 | { | ||
36 | public: | ||
37 | ListBase() | ||
38 | : QValueList<T *>(), mAutoDelete( false ) | ||
39 | { | ||
40 | } | ||
41 | |||
42 | ListBase( const ListBase &l ) | ||
43 | : QValueList<T *>( l ), mAutoDelete( false ) | ||
44 | { | ||
45 | } | ||
46 | |||
47 | ~ListBase() | ||
48 | { | ||
49 | if ( mAutoDelete ) { | ||
50 | QValueListIterator<T *> it; | ||
51 | for( it = QValueList<T*>::begin(); it != QValueList<T*>::end(); ++it ) { | ||
52 | delete *it; | ||
53 | } | ||
54 | } | ||
55 | } | ||
56 | |||
57 | ListBase &operator=( const ListBase &l ) | ||
58 | { | ||
59 | if ( this == &l ) return *this; | ||
60 | QValueList<T *>::operator=( l ); | ||
61 | return *this; | ||
62 | } | ||
63 | |||
64 | void setAutoDelete( bool autoDelete ) | ||
65 | { | ||
66 | mAutoDelete = autoDelete; | ||
67 | } | ||
68 | |||
69 | bool removeRef( T *t ) | ||
70 | { | ||
71 | QValueListIterator<T *> it = find( t ); | ||
72 | if ( it == QValueList<T*>::end() ) { | ||
73 | return false; | ||
74 | } else { | ||
75 | if ( mAutoDelete ) delete t; | ||
76 | remove( it ); | ||
77 | return true; | ||
78 | } | ||
79 | } | ||
80 | void fill ( QPtrList<T> list ) { | ||
81 | QPtrListIterator<T> it (list); | ||
82 | T *item; | ||
83 | while ( (item = it.current()) != 0 ) { | ||
84 | append( item ); | ||
85 | ++it; | ||
86 | } | ||
87 | |||
88 | } | ||
89 | |||
90 | |||
91 | private: | ||
92 | bool mAutoDelete; | ||
93 | }; | ||
94 | |||
95 | } | ||
96 | |||
97 | #endif | ||