1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxList<T>
4 // Author: wxWidgets team
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
12 The wxList<T> class provides linked list functionality.
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
22 wxList<T> is not a real template and it requires that you declare and define
23 each wxListT class in your program. This is done with @e WX_DECLARE_LIST
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.
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.
33 Note that if you compile wxWidgets in STL mode (wxUSE_STL defined as 1)
34 then wxList<T> will actually derive from std::list and just add a legacy
35 compatibility layer for the old wxList class.
38 // this part might be in a header or source (.cpp) file
44 // this macro declares and partly implements MyList class
45 WX_DECLARE_LIST(MyListElement, MyList);
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
53 #include <wx/listimpl.cpp>
54 WX_DEFINE_LIST(MyList);
58 MyListElement element;
59 list.Append(&element); // ok
60 list.Append(17); // error: incorrect type
62 // let's iterate over the list in STL syntax
63 MyList::iterator iter;
64 for (iter = list.begin(); iter != list.end(); ++iter)
66 MyListElement *current = *iter;
68 ...process the current element...
71 // the same with the legacy API from the old wxList class
72 MyList::compatibility_iterator node = list.GetFirst();
75 MyListElement *current = node->GetData();
77 ...process the current element...
79 node = node->GetNext();
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.
92 @see wxArray<T>, wxVector<T>
103 Constructor which initialized the list with an array of @a count elements.
105 wxList
<T
>(size_t count
, T
* elements
[]);
108 Destroys the list, but does not delete the objects stored in the list
109 unless you called DeleteContents(@true ).
114 Appends the pointer to @a object to the list.
116 wxList
<T
>::compatibility_iterator
Append(T
* object
);
120 Deletes the actual objects if DeleteContents( @true ) was called previously.
125 If @a destroy is @true, instructs the list to call @e delete
126 on objects stored in the list whenever they are removed.
127 The default is @false.
129 void DeleteContents(bool destroy
);
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.
135 Deletes the actual object if DeleteContents( @true ) was called previously.
137 bool DeleteNode(const compatibility_iterator
& iter
);
140 Finds the given @a object and removes it from the list, returning
143 Deletes @a object if DeleteContents( @true ) was called previously.
145 bool DeleteObject(T
* object
);
148 Removes element refered to be @a iter.
150 Deletes the actualy object if DeleteContents( @true ) was called previously.
152 void Erase(const compatibility_iterator
& iter
);
155 Returns the iterator refering to @a object or @NULL if none found.
157 wxList
<T
>::compatibility_iterator
Find(T
* object
) const;
160 Returns the number of elements in the list.
162 size_t GetCount() const;
165 Returns the first iterator in the list (@NULL if the list is empty).
167 wxList
<T
>::compatibility_iterator
GetFirst() const;
170 Returns the last iterator in the list (@NULL if the list is empty).
172 wxList
<T
>::compatibility_iterator
GetLast() const;
175 Returns the index of @a obj within the list or @c wxNOT_FOUND if
176 @a obj is not found in the list.
178 int IndexOf(T
* obj
) const;
181 Inserts @a object at the beginning of the list.
183 wxList
<T
>::compatibility_iterator
Insert(T
* object
);
186 Inserts @a object at @a position.
188 wxList
<T
>::compatibility_iterator
Insert(size_t position
,
192 Inserts @a object before the object refered to be @a iter.
194 wxList
<T
>::compatibility_iterator
Insert(compatibility_iterator iter
,
198 Returns @true if the list is empty, @false otherwise.
200 bool IsEmpty() const;
203 Returns the iterator refering to the object at the given
204 @a index in the list.
206 wxList
<T
>::compatibility_iterator
Item(size_t index
) const;
209 @deprecated This function is deprecated, use Find() instead.
211 wxList
<T
>::compatibility_iterator
Member(T
* object
) const;
214 @deprecated This function is deprecated, use Item() instead.
216 wxList
<T
>::compatibility_iterator
Nth(int n
) const;
219 @deprecated This function is deprecated, use wxList::GetCount instead.
220 Returns the number of elements in the list.
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
229 void Sort(wxSortCompareFunction compfunc
);
232 Clears the list and item from @a first to @a last from another list to it.
234 void assign(const_iterator first
, const const_iterator
& last
);
237 Clears the list and adds @a n items with value @a v to it.
239 void assign(size_type n
, const_reference v
= value_type());
242 Returns the last item of the list.
247 Returns the last item of the list as a const reference.
249 const_reference
back() const;
252 Returns an iterator pointing to the beginning of the list.
257 Returns a const iterator pointing to the beginning of the list.
259 const_iterator
begin() const;
262 Removes all items from the list.
267 Returns @e @true if the list is empty.
272 Returns a const iterator pointing at the end of the list.
274 const_iterator
end() const;
277 Returns a iterator pointing at the end of the list.
279 iterator
end() const;
282 Erases the given item
284 iterator
erase(const iterator
& it
);
287 Erases the items from @e first to @e last.
289 iterator
erase(const iterator
& first
,
290 const iterator
& last
);
293 Returns the first item in the list.
295 reference
front() const;
298 Returns the first item in the list as a const reference.
300 const_reference
front() const;
303 Inserts an item at the head of the list
305 iterator
insert(const iterator
& it
);
308 Inserts an item at the given position
310 void insert(const iterator
& it
, size_type n
);
313 Inserts several items at the given position.
315 void insert(const iterator
& it
, const_iterator first
,
316 const const_iterator
& last
);
319 Returns the largest possible size of the list.
321 size_type
max_size() const;
324 Removes the last item from the list.
329 Removes the first item from the list.
334 Adds an item to end of the list.
336 void push_back(const_reference v
= value_type());
339 Adds an item to the front of the list.
341 void push_front(const_reference v
= value_type());
344 Returns a reverse iterator pointing to the beginning of the
347 reverse_iterator
rbegin();
350 Returns a const reverse iterator pointing to the beginning of the
353 const_reverse_iterator
rbegin() const;
356 Removes an item from the list.
358 void remove(const_reference v
);
361 Returns a reverse iterator pointing to the end of the reversed list.
363 reverse_iterator
rend();
366 Returns a const reverse iterator pointing to the end of the reversed list.
368 const_reverse_iterator
rend() const;
372 If the the list is enlarges items with the value @e v are appended to the list.
374 void resize(size_type n
);
382 Returns the size of the list.
384 size_type
size() const;
392 wxNodeBase is the node structure used in linked lists (see wxList) and derived
393 classes. You should never use wxNodeBase class directly, however, because it
394 works with untyped (@c void *) data and this is unsafe.
395 Use wxNodeBase-derived classes which are automatically defined by WX_DECLARE_LIST
396 and WX_DEFINE_LIST macros instead as described in wxList documentation
399 Also note that although there is a class called wxNode, it is defined for backwards
400 compatibility only and usage of this class is strongly deprecated.
402 In the documentation below, the type @c T should be thought of as a
403 "template" parameter: this is the type of data stored in the linked list or,
404 in other words, the first argument of WX_DECLARE_LIST macro. Also, wxNode is
405 written as wxNodeT even though it isn't really a template class -- but it
406 helps to think of it as if it were.
411 @see wxList<T>, wxHashTable
417 Retrieves the client data pointer associated with the node.
422 Retrieves the next node or @NULL if this node is the last one.
424 wxNode
<T
>* GetNext() const;
427 Retrieves the previous node or @NULL if this node is the first one in the list.
429 wxNode
<T
>* GetPrevious();
432 Returns the zero-based index of this node within the list. The return value
433 will be @c wxNOT_FOUND if the node has not been added to a list yet.
438 Sets the data associated with the node (usually the pointer will have been
439 set when the node was created).
441 void SetData(T
* data
);