]>
Commit | Line | Data |
---|---|---|
1ac74d83 WS |
1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
2 | %% Name: list.tex | |
3 | %% Purpose: wxList | |
4 | %% Author: wxWidgets Team | |
5 | %% Modified by: | |
6 | %% Created: | |
7 | %% RCS-ID: $Id$ | |
8 | %% Copyright: (c) wxWidgets Team | |
9 | %% License: wxWindows license | |
10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
11 | ||
83e51c48 RR |
12 | \section{\class{wxList<T>}}\label{wxlist} |
13 | ||
14 | The wxList<T> class provides linked list functionality. It has been written | |
15 | to be type safe and to provide the full API of the STL std::list container and | |
16 | should be used like it. The exception is that wxList<T> actually stores | |
17 | pointers and therefore its iterators return pointers and not references | |
18 | to the actual objets in the list (see example below). Unfortunately, the | |
19 | new wxList<T> class requires that you declare and define each wxList<T> | |
20 | class in your program. This is done with {\it WX\_DECLARE\_LIST} and | |
21 | {\it WX\_DEFINE\_LIST} macros (see example). We hope that we'll be able | |
22 | to provide a proper template class providing both the STL std::list | |
23 | and the old wxList API in the future. | |
24 | ||
25 | Please refer to the STL std::list documentation for further | |
26 | information on how to use the class. Below we documented the legacy | |
27 | API that originated from the old wxList class and which can still | |
28 | be used alternatively for the the same class. | |
29 | ||
30 | Note that if you compile wxWidgets in STL mode (wxUSE_STL defined as 1) | |
31 | then wxList<T> will actually derive from std::list and just add a legacy | |
32 | compatibility layer for the old wxList class. | |
33 | ||
34 | each list type (i.e. list of ints, of wxStrings or of MyObjects). | |
6e6110ee VZ |
35 | |
36 | \wxheading{Example} | |
37 | ||
6e6110ee VZ |
38 | \begin{verbatim} |
39 | // this part might be in a header or source (.cpp) file | |
40 | class MyListElement | |
41 | { | |
42 | ... // whatever | |
43 | }; | |
44 | ||
83e51c48 | 45 | // this macro declares and partly implements MyList class |
f776e250 | 46 | WX_DECLARE_LIST(MyListElement, MyList); |
6e6110ee VZ |
47 | |
48 | ... | |
49 | ||
2edb0bde | 50 | // the only requirement for the rest is to be AFTER the full declaration of |
6e6110ee VZ |
51 | // MyListElement (for WX_DECLARE_LIST forward declaration is enough), but |
52 | // usually it will be found in the source file and not in the header | |
53 | ||
54 | #include <wx/listimpl.cpp> | |
f776e250 | 55 | WX_DEFINE_LIST(MyList); |
6e6110ee | 56 | |
83e51c48 RR |
57 | |
58 | ||
6e6110ee VZ |
59 | MyList list; |
60 | MyListElement element; | |
1ac74d83 | 61 | list.Append(&element); // ok |
bb250157 | 62 | list.Append(17); // error: incorrect type |
6e6110ee | 63 | |
83e51c48 RR |
64 | // let's iterate over the list in STL syntax |
65 | MyList::iterator iter; | |
66 | for (iter = list.begin(); iter != list.end(); ++iter) | |
67 | { | |
68 | MyListElement *current = *iter; | |
69 | ||
70 | ...process the current element... | |
71 | } | |
72 | ||
73 | // the same with the legacy API from the old wxList class | |
74 | MyList::compatibility_iterator node = list.GetFirst(); | |
75 | while (node) | |
6e6110ee VZ |
76 | { |
77 | MyListElement *current = node->GetData(); | |
78 | ||
79 | ...process the current element... | |
83e51c48 RR |
80 | |
81 | node = node->GetNext(); | |
6e6110ee | 82 | } |
83e51c48 | 83 | |
6e6110ee | 84 | \end{verbatim} |
6e6110ee VZ |
85 | |
86 | For compatibility with previous versions wxList and wxStringList classes are | |
87 | still defined, but their usage is deprecated and they will disappear in the | |
703f03c3 | 88 | future versions completely. The use of the latter is especially discouraged as |
1ac74d83 | 89 | it is not only unsafe but is also much less efficient than |
35d367d8 | 90 | \helpref{wxArrayString}{wxarraystring} class. |
a660d684 | 91 | |
954b8ae6 JS |
92 | \wxheading{Include files} |
93 | ||
94 | <wx/list.h> | |
95 | ||
a660d684 KB |
96 | \wxheading{See also} |
97 | ||
6e6110ee | 98 | \helpref{wxArray}{wxarray} |
a660d684 KB |
99 | |
100 | \latexignore{\rtfignore{\wxheading{Members}}} | |
101 | ||
83e51c48 | 102 | \membersection{wxList<T>::wxList<T>}\label{wxlistctor} |
2bbd97f4 | 103 | |
83e51c48 | 104 | \func{}{wxList<T>}{\void} |
a660d684 | 105 | |
83e51c48 | 106 | \func{}{wxList<T>}{\param{size_t}{ count}, \param{T *}{elements[]}} |
a660d684 | 107 | |
83e51c48 | 108 | Constructors. |
a660d684 | 109 | |
83e51c48 | 110 | \membersection{wxList<T>::\destruct{wxList<T>}}\label{wxlistdtor} |
a660d684 | 111 | |
83e51c48 | 112 | \func{}{\destruct{wxList<T>}}{\void} |
a660d684 | 113 | |
83e51c48 RR |
114 | Destroys the list, but does not delete the objects stored in the list |
115 | unless you called DeleteContents({\tt true} ). | |
a660d684 | 116 | |
83e51c48 | 117 | \membersection{wxList<T>::Append}\label{wxlistappend} |
a660d684 | 118 | |
83e51c48 | 119 | \func{wxList<T>::compatibility_iterator }{Append}{\param{T *}{object}} |
a660d684 | 120 | |
83e51c48 | 121 | Appends the pointer to \rtfsp{\it object} to the list. |
2bbd97f4 | 122 | |
83e51c48 | 123 | \membersection{wxList<T>::Clear}\label{wxlistclear} |
a660d684 KB |
124 | |
125 | \func{void}{Clear}{\void} | |
126 | ||
83e51c48 RR |
127 | Clears the list, but does not delete the objects stored in the list |
128 | unless you called DeleteContents({\tt true} ). | |
a660d684 | 129 | |
83e51c48 | 130 | \membersection{wxList<T>::DeleteContents}\label{wxlistdeletecontents} |
a660d684 KB |
131 | |
132 | \func{void}{DeleteContents}{\param{bool}{ destroy}} | |
133 | ||
83e51c48 RR |
134 | If {\it destroy} is {\tt true}, instructs the list to call {\it delete} |
135 | on objects stored in the list whenever they are removed. | |
136 | The default is {\tt false}. | |
a660d684 | 137 | |
83e51c48 | 138 | \membersection{wxList<T>::DeleteNode}\label{wxlistdeletenode} |
a660d684 | 139 | |
83e51c48 | 140 | \func{bool}{DeleteNode}{\param{const compatibility_iterator &}{iter}} |
a660d684 | 141 | |
83e51c48 RR |
142 | Deletes the given element refered to by {\tt iter} from the list, |
143 | returning {\tt true} if successful. | |
a660d684 | 144 | |
83e51c48 | 145 | \membersection{wxList<T>::DeleteObject}\label{wxlistdeleteobject} |
a660d684 | 146 | |
2b5f62a0 | 147 | \func{bool}{DeleteObject}{\param{T *}{object}} |
a660d684 | 148 | |
83e51c48 RR |
149 | Finds the given {\it object} and removes it from the list, returning |
150 | {\tt true} if successful. The application must delete the actual object | |
151 | separately. | |
e0ad14ca | 152 | |
83e51c48 | 153 | \membersection{wxList<T>::Erase}\label{wxlisterase} |
a660d684 | 154 | |
83e51c48 | 155 | \func{void}{Erase}{\param{const compatibility_iterator &}{iter}} |
a660d684 | 156 | |
83e51c48 | 157 | Removes element refered to be {\tt iter}. |
2b5f62a0 | 158 | |
83e51c48 | 159 | \membersection{wxList<T>::Find}\label{wxlistfind} |
2bbd97f4 | 160 | |
83e51c48 | 161 | \constfunc{wxList<T>::compatibility_iterator}{Find}{\param{T *}{ object}} |
2b5f62a0 | 162 | |
83e51c48 | 163 | Returns the iterator refering to {\it object} or NULL if none found. |
a660d684 | 164 | |
83e51c48 | 165 | \membersection{wxList<T>::GetCount}\label{wxlistgetcount} |
d8996187 VZ |
166 | |
167 | \constfunc{size\_t}{GetCount}{\void} | |
168 | ||
169 | Returns the number of elements in the list. | |
170 | ||
83e51c48 | 171 | \membersection{wxList<T>::GetFirst}\label{wxlistgetfirst} |
a660d684 | 172 | |
83e51c48 | 173 | \constfunc{wxList<T>::compatibility_iterator}{GetFirst}{\void} |
a660d684 | 174 | |
83e51c48 | 175 | Returns the first iterator in the list (NULL if the list is empty). |
a660d684 | 176 | |
83e51c48 | 177 | \membersection{wxList<T>::GetLast}\label{wxlistgetlast} |
d8996187 | 178 | |
83e51c48 | 179 | \constfunc{wxList<T>::compatibility_iterator}{GetLast}{\void} |
d8996187 | 180 | |
83e51c48 | 181 | Returns the last iterator in the list (NULL if the list is empty). |
d8996187 | 182 | |
83e51c48 | 183 | \membersection{wxList<T>::IndexOf}\label{wxlistindexof} |
77c5eefb | 184 | |
83e51c48 | 185 | \constfunc{int}{IndexOf}{\param{T*}{ obj }} |
77c5eefb | 186 | |
83e51c48 RR |
187 | Returns the index of {\it obj} within the list or {\tt wxNOT\_FOUND} if |
188 | {\it obj} is not found in the list. | |
77c5eefb | 189 | |
83e51c48 | 190 | \membersection{wxList<T>::Insert}\label{wxlistinsert} |
a660d684 | 191 | |
83e51c48 | 192 | \func{wxList<T>::compatibility_iterator}{Insert}{\param{T *}{object}} |
a660d684 | 193 | |
83e51c48 | 194 | Insert object at the front of list. |
a660d684 | 195 | |
83e51c48 | 196 | \func{wxList<T>::compatibility_iterator}{Insert}{\param{size\_t }{position}, \param{T *}{object}} |
a660d684 | 197 | |
d8996187 VZ |
198 | Insert object before {\it position}, i.e. the index of the new item in the |
199 | list will be equal to {\it position}. {\it position} should be less than or | |
200 | equal to \helpref{GetCount}{wxlistgetcount}; if it is equal to it, this is the | |
201 | same as calling \helpref{Append}{wxlistappend}. | |
a660d684 | 202 | |
83e51c48 | 203 | \func{wxList<T>::compatibility_iterator}{Insert}{\param{compatibility_iterator}{iter}, \param{T *}{object}} |
a660d684 | 204 | |
83e51c48 | 205 | Inserts the object before the object refered to be {\it iter}. |
a660d684 | 206 | |
83e51c48 | 207 | \membersection{wxList<T>::IsEmpty}\label{wxlistisempty} |
b79a8705 VZ |
208 | |
209 | \constfunc{bool}{IsEmpty}{\void} | |
210 | ||
cc81d32f | 211 | Returns {\tt true} if the list is empty, {\tt false} otherwise. |
b79a8705 | 212 | |
0b0625e9 | 213 | % Use different label name to avoid clashing with wxListItem label |
83e51c48 | 214 | \membersection{wxList<T>::Item}\label{wxlistitemfunc} |
a660d684 | 215 | |
83e51c48 | 216 | \constfunc{wxList<T>::compatibility_iterator}{Item}{\param{size\_t }{index}} |
d8996187 | 217 | |
83e51c48 RR |
218 | Returns the iterator refering to the object at the given |
219 | {\tt index} in the list. | |
a660d684 | 220 | |
83e51c48 | 221 | \membersection{wxList<T>::Member}\label{wxlistmember} |
a660d684 | 222 | |
83e51c48 | 223 | \constfunc{wxList<T>::compatibility_iterator}{Member}{\param{T *}{ object}} |
a660d684 | 224 | |
d8996187 VZ |
225 | {\bf NB:} This function is deprecated, use \helpref{Find}{wxlistfind} instead. |
226 | ||
83e51c48 | 227 | \membersection{wxList<T>::Nth}\label{wxlistnth} |
a660d684 | 228 | |
83e51c48 | 229 | \constfunc{wxList<T>::compatibility_iterator}{Nth}{\param{int }{n}} |
a660d684 | 230 | |
0b0625e9 | 231 | {\bf NB:} This function is deprecated, use \helpref{Item}{wxlistitemfunc} instead. |
d8996187 | 232 | |
a660d684 KB |
233 | Returns the {\it nth} node in the list, indexing from zero (NULL if the list is empty |
234 | or the nth node could not be found). | |
235 | ||
83e51c48 | 236 | \membersection{wxList<T>::Number}\label{wxlistnumber} |
a660d684 | 237 | |
83e51c48 | 238 | \constfunc{int}{Number}{\void} |
a660d684 | 239 | |
d8996187 VZ |
240 | {\bf NB:} This function is deprecated, use \helpref{GetCount}{wxlistgetcount} instead. |
241 | ||
a660d684 KB |
242 | Returns the number of elements in the list. |
243 | ||
83e51c48 | 244 | \membersection{wxList<T>::Sort}\label{wxlistsort} |
a660d684 KB |
245 | |
246 | \func{void}{Sort}{\param{wxSortCompareFunction}{ compfunc}} | |
247 | ||
248 | \begin{verbatim} | |
249 | // Type of compare function for list sort operation (as in 'qsort') | |
250 | typedef int (*wxSortCompareFunction)(const void *elem1, const void *elem2); | |
251 | \end{verbatim} | |
252 | ||
83e51c48 RR |
253 | Allows the sorting of arbitrary lists by giving a function to compare |
254 | two list elements. We use the system {\bf qsort} function for the actual | |
255 | sorting process. | |
a660d684 | 256 |