Warning: That file was not part of the compilation database. It may have many parsing errors.

1/*
2 This file is part of the kcal library.
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., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21/**
22 @file
23 This file is part of the API for handling calendar data and
24 defines the ListBase class.
25
26 @author Cornelius Schumacher \<schumacher@kde.org\>
27*/
28
29#ifndef KCAL_LISTBASE_H
30#define KCAL_LISTBASE_H
31
32#include "kcal_export.h"
33#include <QtCore/QList>
34
35namespace KCal {
36
37/**
38 @brief
39 This class provides a template for lists of pointers.
40
41 It extends QList<T *> with an "auto-delete" functionality.
42*/
43template<class T>
44class ListBase : public QList<T *>
45{
46 public:
47 /**
48 Constructor.
49 */
50 ListBase()
51 : QList<T *>(), mAutoDelete( false )
52 {
53 }
54
55 /**
56 Copy constructor.
57 @param other is the ListBase to copy.
58 */
59 ListBase( const ListBase &other )
60 : QList<T *>( other ), mAutoDelete( false )
61 {
62 }
63
64 /**
65 Destructor.
66 */
67 ~ListBase()
68 {
69 if ( mAutoDelete ) {
70 qDeleteAll( *this );
71 }
72 }
73
74 /**
75 Assigns @p l to this listbase.
76 @param l is the ListBase to copy.
77 */
78 ListBase &operator=( const ListBase &l )
79 {
80 if ( this == &l ) {
81 return *this;
82 }
83 QList<T *>::operator=( l );
84 return *this;
85 }
86
87 /**
88 Sets this list to operate in "auto-delete" mode.
89 This mode deletes the memory pointed at by all members of the list
90 in the destructor.
91 @param autoDelete if true, puts the list into "auto-delete" mode.
92 */
93 void setAutoDelete( bool autoDelete )
94 {
95 mAutoDelete = autoDelete;
96 }
97
98 /**
99 Clears the list.
100 Memory is also freed if the list is set to "auto-delete" mode.
101 */
102 void clearAll()
103 {
104 if ( mAutoDelete ) {
105 qDeleteAll( *this );
106 }
107 QList<T*>::clear();
108 }
109
110 /**
111 Removes all the members from the list with the specified address.
112 Memory is also freed if the list is set to "auto-delete" mode.
113 @param t is the pointer to remove from the list.
114 @return true if successful; otherwise false (no such address @p t found).
115 */
116 bool removeRef( T *t )
117 {
118 if ( !this->contains( t ) ) {
119 return false;
120 } else {
121 if ( mAutoDelete ) {
122 delete t;
123 }
124 this->removeAll( t );
125 return true;
126 }
127 }
128
129 /**
130 Removes the specified member from the list.
131 Memory is also freed if the list is set to "auto-delete" mode.
132 @param it the iterator to remove from the list.
133 */
134 void removeRef( typename QList<T*>::iterator it )
135 {
136 if ( mAutoDelete ) {
137 delete *it;
138 }
139 QList<T*>::erase( it );
140 }
141
142 bool operator==( const ListBase &l2 )
143 {
144 int sz = QList<T*>::size();
145
146 if ( sz != l2.size() ) {
147 return false;
148 } else {
149 for ( int i=0; i<sz; ++i ) {
150 if ( !( *QList<T*>::value( i ) == *l2.value( i ) ) ) {
151 return false;
152 }
153 }
154 }
155 return true;
156 }
157
158 private:
159 //@cond PRIVATE
160 bool mAutoDelete;
161 //@endcond
162};
163
164}
165
166#endif
167

Warning: That file was not part of the compilation database. It may have many parsing errors.