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