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