]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/list.tex
First step in background erase optimization
[wxWidgets.git] / docs / latex / wx / list.tex
... / ...
CommitLineData
1\section{\class{wxList}}\label{wxlist}
2
3wxList classes provide linked list functionality for wxWidgets, and for an
4application if it wishes. Depending on the form of constructor used, a list
5can be keyed on integer or string keys to provide a primitive look-up ability,
6but please note that this feature is {\bf deprecated}.
7See \helpref{wxHashMap}{wxhashmap}\rtfsp for a faster method of storage
8when random access is required.
9
10While wxList class in the previous versions of wxWidgets only could contain
11elements of type wxObject and had essentially untyped interface (thus allowing
12you to put apples in the list and read back oranges from it), the new wxList
13classes family may contain elements of any type and has much more strict type
14checking. Unfortunately, it also requires an additional line to be inserted in
15your program for each list class you use (which is the only solution short of
16using templates which is not done in wxWidgets because of portability issues).
17
18The general idea is to have the base class wxListBase working with {\it void *}
19data but make all of its dangerous (because untyped) functions protected, so
20that they can only be used from derived classes which, in turn, expose a type
21safe interface. With this approach a new wxList-like class must be defined for
22each list type (i.e. list of ints, of wxStrings or of MyObjects). This is done
23with {\it WX\_DECLARE\_LIST} and {\it WX\_DEFINE\_LIST} macros like this
24(notice the similarity with WX\_DECLARE\_OBJARRAY and WX\_IMPLEMENT\_OBJARRAY
25macros):
26
27\wxheading{Example}
28
29\begin{verbatim}
30 // this part might be in a header or source (.cpp) file
31 class MyListElement
32 {
33 ... // whatever
34 };
35
36 // declare our list class: this macro declares and partly implements MyList
37 // class (which derives from wxListBase)
38 WX_DECLARE_LIST(MyListElement, MyList);
39
40 ...
41
42 // the only requirement for the rest is to be AFTER the full declaration of
43 // MyListElement (for WX_DECLARE_LIST forward declaration is enough), but
44 // usually it will be found in the source file and not in the header
45
46 #include <wx/listimpl.cpp>
47 WX_DEFINE_LIST(MyList);
48
49 // now MyList class may be used as a usual wxList, but all of its methods
50 // will take/return the objects of the right (i.e. MyListElement) type. You
51 // also have MyList::Node type which is the type-safe version of wxNode.
52 MyList list;
53 MyListElement element;
54 list.Append(element); // ok
55 list.Append(17); // error: incorrect type
56
57 // let's iterate over the list
58 for ( MyList::Node *node = list.GetFirst(); node; node = node->GetNext() )
59 {
60 MyListElement *current = node->GetData();
61
62 ...process the current element...
63 }
64\end{verbatim}
65
66For compatibility with previous versions wxList and wxStringList classes are
67still defined, but their usage is deprecated and they will disappear in the
68future versions completely. The use of the latter is especially discouraged as
69it is not only unsafe but is also much less efficient than
70\helpref{wxArrayString}{wxarraystring} class.
71
72In the documentation of the list classes below, the template notations are
73used even though these classes are not really templates at all -- but it helps
74to think about them as if they were. You should replace wxNode<T> with
75wxListName::Node and T itself with the list element type (i.e. the first
76parameter of WX\_DECLARE\_LIST).
77
78\wxheading{Derived from}
79
80\helpref{wxObject}{wxobject}
81
82\wxheading{Include files}
83
84<wx/list.h>
85
86\wxheading{Example}
87
88It is very common to iterate on a list as follows:
89
90\begin{verbatim}
91 ...
92 wxWindow *win1 = new wxWindow(...);
93 wxWindow *win2 = new wxWindow(...);
94
95 wxList SomeList;
96 SomeList.Append(win1);
97 SomeList.Append(win2);
98
99 ...
100
101 wxNode *node = SomeList.GetFirst();
102 while (node)
103 {
104 wxWindow *win = node->GetData();
105 ...
106 node = node->GetNext();
107 }
108\end{verbatim}
109
110To delete nodes in a list as the list is being traversed, replace
111
112\begin{verbatim}
113 ...
114 node = node->GetNext();
115 ...
116\end{verbatim}
117
118with
119
120\begin{verbatim}
121 ...
122 delete win;
123 delete node;
124 node = SomeList.GetFirst();
125 ...
126\end{verbatim}
127
128See \helpref{wxNode}{wxnode} for members that retrieve the data associated with a node, and
129members for getting to the next or previous node.
130
131\wxheading{See also}
132
133\helpref{wxNode}{wxnode}, \helpref{wxStringList}{wxstringlist},
134\helpref{wxArray}{wxarray}
135
136\latexignore{\rtfignore{\wxheading{Members}}}
137
138\membersection{wxList::wxList}
139
140\func{}{wxList}{\void}
141
142\func{}{wxList}{\param{int}{ n}, \param{T *}{objects[]}}
143
144\func{}{wxList}{\param{T *}{object}, ...}
145
146{\bf Note}: keyed lists are deprecated and should not be used in new code.
147
148\func{}{wxList}{\param{unsigned int}{ key\_type}}
149
150Constructors. {\it key\_type} is one of wxKEY\_NONE, wxKEY\_INTEGER, or wxKEY\_STRING,
151and indicates what sort of keying is required (if any).
152
153{\it objects} is an array of {\it n} objects with which to initialize the list.
154
155The variable-length argument list constructor must be supplied with a
156terminating NULL.
157
158\membersection{wxList::\destruct{wxList}}
159
160\func{}{\destruct{wxList}}{\void}
161
162Destroys the list. Also destroys any remaining nodes, but does not destroy
163client data held in the nodes.
164
165\membersection{wxList::Append}\label{wxlistappend}
166
167\func{wxNode<T> *}{Append}{\param{T *}{object}}
168
169{\bf Note}: keyed lists are deprecated and should not be used in new code.
170
171\func{wxNode<T> *}{Append}{\param{long}{ key}, \param{T *}{object}}
172
173\func{wxNode<T> *}{Append}{\param{const wxString\& }{key}, \param{T *}{object}}
174
175Appends a new \helpref{wxNode}{wxnode} to the end of the list and puts a
176pointer to the \rtfsp{\it object} in the node. The last two forms store a key
177with the object for later retrieval using the key. The new node is returned in
178each case.
179
180The key string is copied and stored by the list implementation.
181
182\membersection{wxList::Clear}\label{wxlistclear}
183
184\func{void}{Clear}{\void}
185
186Clears the list (but does not delete the client data stored with each node
187unless you called DeleteContents({\tt true}), in which case it deletes data).
188
189\membersection{wxList::DeleteContents}\label{wxlistdeletecontents}
190
191\func{void}{DeleteContents}{\param{bool}{ destroy}}
192
193If {\it destroy} is {\tt true}, instructs the list to call {\it delete} on the client contents of
194a node whenever the node is destroyed. The default is {\tt false}.
195
196\membersection{wxList::DeleteNode}\label{wxlistdeletenode}
197
198\func{bool}{DeleteNode}{\param{wxNode<T> *}{node}}
199
200Deletes the given node from the list, returning {\tt true} if successful.
201
202\membersection{wxList::DeleteObject}\label{wxlistdeleteobject}
203
204\func{bool}{DeleteObject}{\param{T *}{object}}
205
206Finds the given client {\it object} and deletes the appropriate node from the list, returning
207{\tt true} if successful. The application must delete the actual object separately.
208
209\membersection{wxList::Find}\label{wxlistfind}
210
211\func{wxNode<T> *}{Find}{\param{T *}{ object}}
212
213Returns the node whose client date is {\it object} or NULL if none found.
214
215{\bf Note}: keyed lists are deprecated and should not be used in new code.
216
217\func{wxNode<T> *}{Find}{\param{long}{ key}}
218
219\func{wxNode<T> *}{Find}{\param{const wxString\& }{key}}
220
221Returns the node whose stored key matches {\it key}. Use on a keyed list only.
222
223\membersection{wxList::GetCount}\label{wxlistgetcount}
224
225\constfunc{size\_t}{GetCount}{\void}
226
227Returns the number of elements in the list.
228
229\membersection{wxList::GetFirst}\label{wxlistgetfirst}
230
231\func{wxNode<T> *}{GetFirst}{\void}
232
233Returns the first node in the list (NULL if the list is empty).
234
235\membersection{wxList::GetLast}\label{wxlistgetlast}
236
237\func{wxNode<T> *}{GetLast}{\void}
238
239Returns the last node in the list (NULL if the list is empty).
240
241\membersection{wxList::IndexOf}\label{wxlistindexof}
242
243\func{int}{IndexOf}{\param{T*}{ obj }}
244
245Returns the index of {\it obj} within the list or wxNOT\_FOUND if {\it obj}
246is not found in the list.
247
248\membersection{wxList::Insert}\label{wxlistinsert}
249
250\func{wxNode<T> *}{Insert}{\param{T *}{object}}
251
252Insert object at front of list.
253
254\func{wxNode<T> *}{Insert}{\param{size\_t }{position}, \param{T *}{object}}
255
256Insert object before {\it position}, i.e. the index of the new item in the
257list will be equal to {\it position}. {\it position} should be less than or
258equal to \helpref{GetCount}{wxlistgetcount}; if it is equal to it, this is the
259same as calling \helpref{Append}{wxlistappend}.
260
261\func{wxNode<T> *}{Insert}{\param{wxNode<T> *}{node}, \param{T *}{object}}
262
263Inserts the object before the given {\it node}.
264
265\membersection{wxList::IsEmpty}\label{wxlistisempty}
266
267\constfunc{bool}{IsEmpty}{\void}
268
269Returns {\tt true} if the list is empty, {\tt false} otherwise.
270
271% Use different label name to avoid clashing with wxListItem label
272\membersection{wxList::Item}\label{wxlistitemfunc}
273
274\constfunc{wxNode<T> *}{Item}{\param{size\_t }{index}}
275
276Returns the node at given position in the list.
277
278\membersection{wxList::Member}\label{wxlistmember}
279
280\func{wxNode<T> *}{Member}{\param{T *}{object}}
281
282{\bf NB:} This function is deprecated, use \helpref{Find}{wxlistfind} instead.
283
284Returns the node associated with {\it object} if it is in the list, NULL otherwise.
285
286\membersection{wxList::Nth}\label{wxlistnth}
287
288\func{wxNode<T> *}{Nth}{\param{int}{ n}}
289
290{\bf NB:} This function is deprecated, use \helpref{Item}{wxlistitemfunc} instead.
291
292Returns the {\it nth} node in the list, indexing from zero (NULL if the list is empty
293or the nth node could not be found).
294
295\membersection{wxList::Number}\label{wxlistnumber}
296
297\func{int}{Number}{\void}
298
299{\bf NB:} This function is deprecated, use \helpref{GetCount}{wxlistgetcount} instead.
300
301Returns the number of elements in the list.
302
303\membersection{wxList::Sort}\label{wxlistsort}
304
305\func{void}{Sort}{\param{wxSortCompareFunction}{ compfunc}}
306
307\begin{verbatim}
308 // Type of compare function for list sort operation (as in 'qsort')
309 typedef int (*wxSortCompareFunction)(const void *elem1, const void *elem2);
310\end{verbatim}
311
312Allows the sorting of arbitrary lists by giving
313a function to compare two list elements. We use the system {\bf qsort} function
314for the actual sorting process.
315
316If you use untyped wxList the sort function receives pointers to wxObject
317pointers (wxObject **), so be careful to dereference appropriately - but,
318of course, a better solution is to use list of appropriate type defined with
319{\tt WX\_DECLARE\_LIST}.
320
321Example:
322
323\begin{verbatim}
324 int listcompare(const void *arg1, const void *arg2)
325 {
326 return(compare(**(wxString **)arg1, // use the wxString 'compare'
327 **(wxString **)arg2)); // function
328 }
329
330 void main()
331 {
332 wxList list;
333
334 list.Append(new wxString("DEF"));
335 list.Append(new wxString("GHI"));
336 list.Append(new wxString("ABC"));
337 list.Sort(listcompare);
338 }
339\end{verbatim}
340