]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: list.h | |
e54c96f1 | 3 | // Purpose: interface of wxList<T> |
23324ae1 FM |
4 | // Author: wxWidgets team |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
6a93e794 | 10 | @class wxList |
7c913512 | 11 | |
6a93e794 FM |
12 | The wxList<T> class provides linked list functionality. |
13 | ||
14 | This class has been rewritten to be type safe and to provide the full API of | |
15 | the STL std::list container and should be used like it. | |
16 | The exception is that wxList<T> actually stores pointers and therefore its | |
17 | iterators return pointers and not references to the actual objets in the list | |
18 | (see example below) and @e value_type is defined as @e T*. | |
19 | wxList<T> destroys an object after removing it only if wxList::DeleteContents | |
20 | has been called. | |
7c913512 | 21 | |
5cb947fd | 22 | wxList<T> is not a real template and it requires that you declare and define |
23324ae1 | 23 | each wxListT class in your program. This is done with @e WX_DECLARE_LIST |
6a93e794 FM |
24 | and @e WX_DEFINE_LIST macros (see example). We hope that we'll be able to |
25 | provide a proper template class providing both the STL std::list and the old | |
26 | wxList API in the future. | |
7c913512 | 27 | |
6a93e794 FM |
28 | Please refer to the STL std::list documentation for further information on how |
29 | to use the class. Below we documented both the supported STL and the legacy API | |
30 | that originated from the old wxList class and which can still be used alternatively | |
31 | for the the same class. | |
7c913512 FM |
32 | |
33 | Note that if you compile wxWidgets in STL mode (wxUSE_STL defined as 1) | |
5cb947fd | 34 | then wxList<T> will actually derive from std::list and just add a legacy |
23324ae1 | 35 | compatibility layer for the old wxList class. |
7c913512 | 36 | |
73473b3e RR |
37 | @code |
38 | // this part might be in a header or source (.cpp) file | |
39 | class MyListElement | |
40 | { | |
41 | ... // whatever | |
42 | }; | |
43 | ||
44 | // this macro declares and partly implements MyList class | |
45 | WX_DECLARE_LIST(MyListElement, MyList); | |
46 | ||
47 | ... | |
48 | ||
49 | // the only requirement for the rest is to be AFTER the full declaration of | |
50 | // MyListElement (for WX_DECLARE_LIST forward declaration is enough), but | |
51 | // usually it will be found in the source file and not in the header | |
52 | ||
53 | #include <wx/listimpl.cpp> | |
54 | WX_DEFINE_LIST(MyList); | |
55 | ||
56 | ||
57 | MyList list; | |
58 | MyListElement element; | |
59 | list.Append(&element); // ok | |
60 | list.Append(17); // error: incorrect type | |
61 | ||
62 | // let's iterate over the list in STL syntax | |
63 | MyList::iterator iter; | |
64 | for (iter = list.begin(); iter != list.end(); ++iter) | |
65 | { | |
66 | MyListElement *current = *iter; | |
67 | ||
68 | ...process the current element... | |
69 | } | |
70 | ||
71 | // the same with the legacy API from the old wxList class | |
72 | MyList::compatibility_iterator node = list.GetFirst(); | |
73 | while (node) | |
74 | { | |
75 | MyListElement *current = node->GetData(); | |
76 | ||
77 | ...process the current element... | |
320ab87c | 78 | |
73473b3e RR |
79 | node = node->GetNext(); |
80 | } | |
81 | @endcode | |
82 | ||
6a93e794 FM |
83 | For compatibility with previous versions wxList and wxStringList classes are |
84 | still defined, but their usage is deprecated and they will disappear in the | |
85 | future versions completely. | |
86 | The use of the latter is especially discouraged as it is not only unsafe but | |
87 | is also much less efficient than wxArrayString class. | |
73473b3e | 88 | |
23324ae1 | 89 | @library{wxbase} |
320ab87c | 90 | @category{data} |
7c913512 | 91 | |
73473b3e | 92 | @see wxArray<T>, wxVector<T> |
23324ae1 | 93 | */ |
7c913512 | 94 | class wxList<T> |
23324ae1 FM |
95 | { |
96 | public: | |
23324ae1 | 97 | /** |
c6714d38 | 98 | Default constructor. |
23324ae1 | 99 | */ |
73473b3e | 100 | wxList<T>(); |
320ab87c | 101 | |
c6714d38 | 102 | /** |
4050e98d | 103 | Constructor which initialized the list with an array of @a count elements. |
c6714d38 | 104 | */ |
73473b3e | 105 | wxList<T>(size_t count, T* elements[]); |
23324ae1 FM |
106 | |
107 | /** | |
108 | Destroys the list, but does not delete the objects stored in the list | |
109 | unless you called DeleteContents(@true ). | |
110 | */ | |
73473b3e | 111 | ~wxList<T>(); |
23324ae1 FM |
112 | |
113 | /** | |
4cc4bfaf | 114 | Appends the pointer to @a object to the list. |
23324ae1 | 115 | */ |
5cb947fd | 116 | wxList<T>::compatibility_iterator Append(T* object); |
23324ae1 FM |
117 | |
118 | /** | |
320ab87c | 119 | Clears the list. |
c6714d38 | 120 | Deletes the actual objects if DeleteContents( @true ) was called previously. |
23324ae1 FM |
121 | */ |
122 | void Clear(); | |
123 | ||
124 | /** | |
4cc4bfaf | 125 | If @a destroy is @true, instructs the list to call @e delete |
23324ae1 FM |
126 | on objects stored in the list whenever they are removed. |
127 | The default is @false. | |
128 | */ | |
129 | void DeleteContents(bool destroy); | |
130 | ||
131 | /** | |
c6714d38 RR |
132 | Deletes the given element refered to by @a iter from the list |
133 | if @a iter is a valid iterator. Returns @true if successful. | |
320ab87c | 134 | |
c6714d38 | 135 | Deletes the actual object if DeleteContents( @true ) was called previously. |
23324ae1 FM |
136 | */ |
137 | bool DeleteNode(const compatibility_iterator& iter); | |
138 | ||
139 | /** | |
4cc4bfaf | 140 | Finds the given @a object and removes it from the list, returning |
320ab87c FM |
141 | @true if successful. |
142 | ||
c6714d38 | 143 | Deletes @a object if DeleteContents( @true ) was called previously. |
23324ae1 | 144 | */ |
4cc4bfaf | 145 | bool DeleteObject(T* object); |
23324ae1 FM |
146 | |
147 | /** | |
c6714d38 | 148 | Removes element refered to be @a iter. |
320ab87c | 149 | |
c6714d38 | 150 | Deletes the actualy object if DeleteContents( @true ) was called previously. |
23324ae1 FM |
151 | */ |
152 | void Erase(const compatibility_iterator& iter); | |
153 | ||
154 | /** | |
4cc4bfaf | 155 | Returns the iterator refering to @a object or @NULL if none found. |
23324ae1 | 156 | */ |
5cb947fd | 157 | wxList<T>::compatibility_iterator Find(T* object) const; |
23324ae1 FM |
158 | |
159 | /** | |
160 | Returns the number of elements in the list. | |
161 | */ | |
328f5751 | 162 | size_t GetCount() const; |
23324ae1 FM |
163 | |
164 | /** | |
165 | Returns the first iterator in the list (@NULL if the list is empty). | |
166 | */ | |
5cb947fd | 167 | wxList<T>::compatibility_iterator GetFirst() const; |
23324ae1 FM |
168 | |
169 | /** | |
170 | Returns the last iterator in the list (@NULL if the list is empty). | |
171 | */ | |
5cb947fd | 172 | wxList<T>::compatibility_iterator GetLast() const; |
23324ae1 FM |
173 | |
174 | /** | |
4cc4bfaf FM |
175 | Returns the index of @a obj within the list or @c wxNOT_FOUND if |
176 | @a obj is not found in the list. | |
23324ae1 | 177 | */ |
328f5751 | 178 | int IndexOf(T* obj) const; |
23324ae1 | 179 | |
23324ae1 | 180 | /** |
c6714d38 | 181 | Inserts @a object at the beginning of the list. |
23324ae1 | 182 | */ |
5cb947fd | 183 | wxList<T>::compatibility_iterator Insert(T* object); |
c6714d38 RR |
184 | |
185 | /** | |
186 | Inserts @a object at @a position. | |
187 | */ | |
5cb947fd | 188 | wxList<T>::compatibility_iterator Insert(size_t position, |
4cc4bfaf | 189 | T* object); |
c6714d38 RR |
190 | |
191 | /** | |
192 | Inserts @a object before the object refered to be @a iter. | |
193 | */ | |
5cb947fd | 194 | wxList<T>::compatibility_iterator Insert(compatibility_iterator iter, |
4cc4bfaf | 195 | T* object); |
23324ae1 FM |
196 | |
197 | /** | |
198 | Returns @true if the list is empty, @false otherwise. | |
199 | */ | |
328f5751 | 200 | bool IsEmpty() const; |
23324ae1 FM |
201 | |
202 | /** | |
203 | Returns the iterator refering to the object at the given | |
c6714d38 | 204 | @a index in the list. |
23324ae1 | 205 | */ |
5cb947fd | 206 | wxList<T>::compatibility_iterator Item(size_t index) const; |
23324ae1 FM |
207 | |
208 | /** | |
6a93e794 | 209 | @deprecated This function is deprecated, use Find() instead. |
23324ae1 | 210 | */ |
5cb947fd | 211 | wxList<T>::compatibility_iterator Member(T* object) const; |
23324ae1 FM |
212 | |
213 | /** | |
6a93e794 | 214 | @deprecated This function is deprecated, use Item() instead. |
23324ae1 | 215 | */ |
5cb947fd | 216 | wxList<T>::compatibility_iterator Nth(int n) const; |
23324ae1 FM |
217 | |
218 | /** | |
6a93e794 | 219 | @deprecated This function is deprecated, use wxList::GetCount instead. |
23324ae1 FM |
220 | Returns the number of elements in the list. |
221 | */ | |
328f5751 | 222 | int Number() const; |
23324ae1 FM |
223 | |
224 | /** | |
225 | Allows the sorting of arbitrary lists by giving a function to compare | |
226 | two list elements. We use the system @b qsort function for the actual | |
227 | sorting process. | |
228 | */ | |
229 | void Sort(wxSortCompareFunction compfunc); | |
230 | ||
23324ae1 | 231 | /** |
c6714d38 | 232 | Clears the list and item from @a first to @a last from another list to it. |
23324ae1 FM |
233 | */ |
234 | void assign(const_iterator first, const const_iterator& last); | |
c6714d38 RR |
235 | |
236 | /** | |
237 | Clears the list and adds @a n items with value @a v to it. | |
238 | */ | |
78e37b46 | 239 | void assign(size_type n, const_reference v = value_type()); |
23324ae1 | 240 | |
23324ae1 FM |
241 | /** |
242 | Returns the last item of the list. | |
243 | */ | |
682f6de6 | 244 | reference back(); |
d38e30d1 RR |
245 | |
246 | /** | |
247 | Returns the last item of the list as a const reference. | |
248 | */ | |
328f5751 | 249 | const_reference back() const; |
23324ae1 | 250 | |
23324ae1 | 251 | /** |
d38e30d1 | 252 | Returns an iterator pointing to the beginning of the list. |
23324ae1 | 253 | */ |
682f6de6 | 254 | iterator begin(); |
d38e30d1 RR |
255 | |
256 | /** | |
257 | Returns a const iterator pointing to the beginning of the list. | |
258 | */ | |
328f5751 | 259 | const_iterator begin() const; |
23324ae1 FM |
260 | |
261 | /** | |
262 | Removes all items from the list. | |
263 | */ | |
264 | void clear(); | |
265 | ||
266 | /** | |
267 | Returns @e @true if the list is empty. | |
268 | */ | |
328f5751 | 269 | bool empty() const; |
23324ae1 | 270 | |
23324ae1 | 271 | /** |
d38e30d1 | 272 | Returns a const iterator pointing at the end of the list. |
23324ae1 | 273 | */ |
328f5751 | 274 | const_iterator end() const; |
23324ae1 | 275 | |
23324ae1 | 276 | /** |
d38e30d1 RR |
277 | Returns a iterator pointing at the end of the list. |
278 | */ | |
279 | iterator end() const; | |
280 | ||
281 | /** | |
282 | Erases the given item | |
23324ae1 FM |
283 | */ |
284 | iterator erase(const iterator& it); | |
d38e30d1 RR |
285 | |
286 | /** | |
287 | Erases the items from @e first to @e last. | |
288 | */ | |
7c913512 FM |
289 | iterator erase(const iterator& first, |
290 | const iterator& last); | |
23324ae1 | 291 | |
23324ae1 FM |
292 | /** |
293 | Returns the first item in the list. | |
294 | */ | |
328f5751 | 295 | reference front() const; |
d38e30d1 RR |
296 | |
297 | /** | |
298 | Returns the first item in the list as a const reference. | |
299 | */ | |
328f5751 | 300 | const_reference front() const; |
23324ae1 | 301 | |
23324ae1 | 302 | /** |
d38e30d1 | 303 | Inserts an item at the head of the list |
23324ae1 FM |
304 | */ |
305 | iterator insert(const iterator& it); | |
d38e30d1 RR |
306 | |
307 | /** | |
308 | Inserts an item at the given position | |
309 | */ | |
7c913512 | 310 | void insert(const iterator& it, size_type n); |
d38e30d1 RR |
311 | |
312 | /** | |
313 | Inserts several items at the given position. | |
314 | */ | |
7c913512 FM |
315 | void insert(const iterator& it, const_iterator first, |
316 | const const_iterator& last); | |
23324ae1 FM |
317 | |
318 | /** | |
319 | Returns the largest possible size of the list. | |
320 | */ | |
328f5751 | 321 | size_type max_size() const; |
23324ae1 FM |
322 | |
323 | /** | |
324 | Removes the last item from the list. | |
325 | */ | |
326 | void pop_back(); | |
327 | ||
328 | /** | |
329 | Removes the first item from the list. | |
330 | */ | |
331 | void pop_front(); | |
332 | ||
333 | /** | |
23324ae1 FM |
334 | Adds an item to end of the list. |
335 | */ | |
682f6de6 | 336 | void push_back(const_reference v = value_type()); |
23324ae1 FM |
337 | |
338 | /** | |
23324ae1 FM |
339 | Adds an item to the front of the list. |
340 | */ | |
682f6de6 | 341 | void push_front(const_reference v = value_type()); |
23324ae1 | 342 | |
23324ae1 | 343 | /** |
d38e30d1 | 344 | Returns a reverse iterator pointing to the beginning of the |
23324ae1 FM |
345 | reversed list. |
346 | */ | |
682f6de6 | 347 | reverse_iterator rbegin(); |
d38e30d1 RR |
348 | |
349 | /** | |
350 | Returns a const reverse iterator pointing to the beginning of the | |
351 | reversed list. | |
352 | */ | |
328f5751 | 353 | const_reverse_iterator rbegin() const; |
23324ae1 FM |
354 | |
355 | /** | |
356 | Removes an item from the list. | |
357 | */ | |
358 | void remove(const_reference v); | |
359 | ||
23324ae1 | 360 | /** |
6a93e794 | 361 | Returns a reverse iterator pointing to the end of the reversed list. |
23324ae1 | 362 | */ |
682f6de6 | 363 | reverse_iterator rend(); |
d38e30d1 RR |
364 | |
365 | /** | |
6a93e794 | 366 | Returns a const reverse iterator pointing to the end of the reversed list. |
d38e30d1 | 367 | */ |
328f5751 | 368 | const_reverse_iterator rend() const; |
23324ae1 FM |
369 | |
370 | /** | |
6a93e794 | 371 | Resizes the list. |
e3d1fc26 FM |
372 | |
373 | If the list is longer than @a n, then items are removed until the list | |
374 | becomes long @a n. | |
375 | If the list is shorter than @a n items with the value @a v are appended | |
376 | to the list until the list becomes long @a n. | |
23324ae1 | 377 | */ |
e3d1fc26 | 378 | void resize(size_type n, value_type v = value_type()); |
23324ae1 FM |
379 | |
380 | /** | |
381 | Reverses the list. | |
382 | */ | |
383 | void reverse(); | |
384 | ||
385 | /** | |
386 | Returns the size of the list. | |
387 | */ | |
328f5751 | 388 | size_type size() const; |
23324ae1 FM |
389 | }; |
390 | ||
391 | ||
e54c96f1 | 392 | |
23324ae1 FM |
393 | /** |
394 | @class wxNode | |
7c913512 | 395 | |
6a93e794 FM |
396 | wxNodeBase is the node structure used in linked lists (see wxList) and derived |
397 | classes. You should never use wxNodeBase class directly, however, because it | |
398 | works with untyped (@c void *) data and this is unsafe. | |
399 | Use wxNodeBase-derived classes which are automatically defined by WX_DECLARE_LIST | |
400 | and WX_DEFINE_LIST macros instead as described in wxList documentation | |
401 | (see example there). | |
402 | ||
403 | Also note that although there is a class called wxNode, it is defined for backwards | |
23324ae1 | 404 | compatibility only and usage of this class is strongly deprecated. |
7c913512 | 405 | |
23324ae1 | 406 | In the documentation below, the type @c T should be thought of as a |
cdbcf4c2 | 407 | "template" parameter: this is the type of data stored in the linked list or, |
23324ae1 FM |
408 | in other words, the first argument of WX_DECLARE_LIST macro. Also, wxNode is |
409 | written as wxNodeT even though it isn't really a template class -- but it | |
410 | helps to think of it as if it were. | |
7c913512 | 411 | |
23324ae1 | 412 | @library{wxbase} |
6a93e794 | 413 | @category{data} |
7c913512 | 414 | |
73473b3e | 415 | @see wxList<T>, wxHashTable |
23324ae1 | 416 | */ |
73473b3e | 417 | class wxNode<T> |
23324ae1 FM |
418 | { |
419 | public: | |
420 | /** | |
421 | Retrieves the client data pointer associated with the node. | |
422 | */ | |
328f5751 | 423 | T* GetData() const; |
23324ae1 FM |
424 | |
425 | /** | |
426 | Retrieves the next node or @NULL if this node is the last one. | |
427 | */ | |
5cb947fd | 428 | wxNode<T>* GetNext() const; |
23324ae1 FM |
429 | |
430 | /** | |
431 | Retrieves the previous node or @NULL if this node is the first one in the list. | |
432 | */ | |
5cb947fd | 433 | wxNode<T>* GetPrevious(); |
23324ae1 FM |
434 | |
435 | /** | |
436 | Returns the zero-based index of this node within the list. The return value | |
437 | will be @c wxNOT_FOUND if the node has not been added to a list yet. | |
438 | */ | |
439 | int IndexOf(); | |
440 | ||
441 | /** | |
442 | Sets the data associated with the node (usually the pointer will have been | |
443 | set when the node was created). | |
444 | */ | |
4cc4bfaf | 445 | void SetData(T* data); |
23324ae1 | 446 | }; |
e54c96f1 | 447 |