1 | /****************************************************************************
|
---|
2 | ** $Id: qptrcollection.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of base class for all pointer based collection classes
|
---|
5 | **
|
---|
6 | ** Created : 920820
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the tools module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qptrcollection.h"
|
---|
39 |
|
---|
40 | /*!
|
---|
41 | \class QPtrCollection qptrcollection.h
|
---|
42 | \reentrant
|
---|
43 | \brief The QPtrCollection class is the base class of most pointer-based Qt collections.
|
---|
44 |
|
---|
45 | \ingroup collection
|
---|
46 | \ingroup tools
|
---|
47 |
|
---|
48 | The QPtrCollection class is an abstract base class for the Qt
|
---|
49 | \link collection.html collection classes\endlink QDict, QPtrList,
|
---|
50 | etc. Qt also includes value based collections, e.g. QValueList,
|
---|
51 | QMap, etc.
|
---|
52 |
|
---|
53 | A QPtrCollection only knows about the number of objects in the
|
---|
54 | collection and the deletion strategy (see setAutoDelete()).
|
---|
55 |
|
---|
56 | A collection is implemented using the \c Item (generic collection
|
---|
57 | item) type, which is a \c void*. The template classes that create
|
---|
58 | the real collections cast the \c Item to the required type.
|
---|
59 | */
|
---|
60 |
|
---|
61 |
|
---|
62 | /*!
|
---|
63 | \enum QPtrCollection::Item
|
---|
64 |
|
---|
65 | This type is the generic "item" in a QPtrCollection.
|
---|
66 | */
|
---|
67 |
|
---|
68 |
|
---|
69 | /*!
|
---|
70 | \fn QPtrCollection::QPtrCollection()
|
---|
71 |
|
---|
72 | Constructs a collection. The constructor is protected because
|
---|
73 | QPtrCollection is an abstract class.
|
---|
74 | */
|
---|
75 |
|
---|
76 | /*!
|
---|
77 | \fn QPtrCollection::QPtrCollection( const QPtrCollection & source )
|
---|
78 |
|
---|
79 | Constructs a copy of \a source with autoDelete() set to FALSE. The
|
---|
80 | constructor is protected because QPtrCollection is an abstract
|
---|
81 | class.
|
---|
82 |
|
---|
83 | Note that if \a source has autoDelete turned on, copying it will
|
---|
84 | risk memory leaks, reading freed memory, or both.
|
---|
85 | */
|
---|
86 |
|
---|
87 | /*!
|
---|
88 | \fn QPtrCollection::~QPtrCollection()
|
---|
89 |
|
---|
90 | Destroys the collection. The destructor is protected because
|
---|
91 | QPtrCollection is an abstract class.
|
---|
92 | */
|
---|
93 |
|
---|
94 |
|
---|
95 | /*!
|
---|
96 | \fn bool QPtrCollection::autoDelete() const
|
---|
97 |
|
---|
98 | Returns the setting of the auto-delete option. The default is FALSE.
|
---|
99 |
|
---|
100 | \sa setAutoDelete()
|
---|
101 | */
|
---|
102 |
|
---|
103 | /*!
|
---|
104 | \fn void QPtrCollection::setAutoDelete( bool enable )
|
---|
105 |
|
---|
106 | Sets the collection to auto-delete its contents if \a enable is
|
---|
107 | TRUE and to never delete them if \a enable is FALSE.
|
---|
108 |
|
---|
109 | If auto-deleting is turned on, all the items in a collection are
|
---|
110 | deleted when the collection itself is deleted. This is convenient
|
---|
111 | if the collection has the only pointer to the items.
|
---|
112 |
|
---|
113 | The default setting is FALSE, for safety. If you turn it on, be
|
---|
114 | careful about copying the collection - you might find yourself
|
---|
115 | with two collections deleting the same items.
|
---|
116 |
|
---|
117 | Note that the auto-delete setting may also affect other functions
|
---|
118 | in subclasses. For example, a subclass that has a remove()
|
---|
119 | function will remove the item from its data structure, and if
|
---|
120 | auto-delete is enabled, will also delete the item.
|
---|
121 |
|
---|
122 | \sa autoDelete()
|
---|
123 | */
|
---|
124 |
|
---|
125 |
|
---|
126 | /*!
|
---|
127 | \fn virtual uint QPtrCollection::count() const
|
---|
128 |
|
---|
129 | Returns the number of objects in the collection.
|
---|
130 | */
|
---|
131 |
|
---|
132 | /*!
|
---|
133 | \fn virtual void QPtrCollection::clear()
|
---|
134 |
|
---|
135 | Removes all objects from the collection. The objects will be
|
---|
136 | deleted if auto-delete has been enabled.
|
---|
137 |
|
---|
138 | \sa setAutoDelete()
|
---|
139 | */
|
---|
140 |
|
---|
141 | /*!
|
---|
142 | \fn void QPtrCollection::deleteItem( Item d )
|
---|
143 |
|
---|
144 | Reimplement this function if you want to be able to delete items.
|
---|
145 |
|
---|
146 | Deletes an item that is about to be removed from the collection.
|
---|
147 |
|
---|
148 | This function has to reimplemented in the collection template
|
---|
149 | classes, and should \e only delete item \a d if auto-delete has
|
---|
150 | been enabled.
|
---|
151 |
|
---|
152 | \warning If you reimplement this function you must also
|
---|
153 | reimplement the destructor and call the virtual function clear()
|
---|
154 | from your destructor. This is due to the way virtual functions and
|
---|
155 | destructors work in C++: Virtual functions in derived classes
|
---|
156 | cannot be called from a destructor. If you do not do this, your
|
---|
157 | deleteItem() function will not be called when the container is
|
---|
158 | destroyed.
|
---|
159 |
|
---|
160 | \sa newItem(), setAutoDelete()
|
---|
161 | */
|
---|
162 |
|
---|
163 | /*!
|
---|
164 | Virtual function that creates a copy of an object that is about to
|
---|
165 | be inserted into the collection.
|
---|
166 |
|
---|
167 | The default implementation returns the \a d pointer, i.e. no copy
|
---|
168 | is made.
|
---|
169 |
|
---|
170 | This function is seldom reimplemented in the collection template
|
---|
171 | classes. It is not common practice to make a copy of something
|
---|
172 | that is being inserted.
|
---|
173 |
|
---|
174 | \sa deleteItem()
|
---|
175 | */
|
---|
176 |
|
---|
177 | QPtrCollection::Item QPtrCollection::newItem( Item d )
|
---|
178 | {
|
---|
179 | return d; // just return reference
|
---|
180 | }
|
---|