]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: list.h | |
3 | // Purpose: wxList, wxStringList classes | |
4 | // Author: Julian Smart | |
fd3f686c | 5 | // Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added |
c801d85f KB |
6 | // Created: 29/01/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Julian Smart | |
371a5b4e | 9 | // Licence: wxWindows licence |
c801d85f KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
fd3f686c VZ |
12 | /* |
13 | All this is quite ugly but serves two purposes: | |
14 | 1. Be almost 100% compatible with old, untyped, wxList class | |
15 | 2. Ensure compile-time type checking for the linked lists | |
16 | ||
17 | The idea is to have one base class (wxListBase) working with "void *" data, | |
18 | but to hide these untyped functions - i.e. make them protected, so they | |
19 | can only be used from derived classes which have inline member functions | |
20 | working with right types. This achieves the 2nd goal. As for the first one, | |
21 | we provide a special derivation of wxListBase called wxList which looks just | |
22 | like the old class. | |
23 | */ | |
24 | ||
34138703 JS |
25 | #ifndef _WX_LISTH__ |
26 | #define _WX_LISTH__ | |
c801d85f | 27 | |
df5168c4 MB |
28 | #if defined(__GNUG__) && !defined(__APPLE__) && \ |
29 | !(defined(__MINGW32__) && __GNUC__ == 3 && __GNUC_MINOR__ == 2) | |
0d3820b3 | 30 | #pragma interface "list.h" |
c801d85f KB |
31 | #endif |
32 | ||
fd3f686c VZ |
33 | // ----------------------------------------------------------------------------- |
34 | // headers | |
35 | // ----------------------------------------------------------------------------- | |
36 | ||
c801d85f KB |
37 | #include "wx/defs.h" |
38 | #include "wx/object.h" | |
3bef6c4c | 39 | #include "wx/string.h" |
c801d85f | 40 | |
df5168c4 MB |
41 | #if wxUSE_STL |
42 | #include "wx/beforestd.h" | |
43 | #include <list> | |
44 | #include "wx/afterstd.h" | |
df5168c4 MB |
45 | #endif |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // types | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // type of compare function for list sort operation (as in 'qsort'): it should | |
52 | // return a negative value, 0 or positive value if the first element is less | |
53 | // than, equal or greater than the second | |
5e967044 | 54 | |
df5168c4 MB |
55 | extern "C" |
56 | { | |
57 | typedef int (* LINKAGEMODE wxSortCompareFunction)(const void *elem1, const void *elem2); | |
58 | } | |
59 | ||
bddd7a8d | 60 | class WXDLLIMPEXP_BASE wxObjectListNode; |
fd3f686c VZ |
61 | typedef wxObjectListNode wxNode; |
62 | ||
df5168c4 MB |
63 | // |
64 | typedef int (* LINKAGEMODE wxListIterateFunction)(void *current); | |
c801d85f | 65 | |
df5168c4 | 66 | // ---------------------------------------------------------------------------- |
fd3f686c | 67 | // constants |
df5168c4 MB |
68 | // ---------------------------------------------------------------------------- |
69 | ||
70 | #if !defined(wxENUM_KEY_TYPE_DEFINED) | |
71 | #define wxENUM_KEY_TYPE_DEFINED | |
72 | ||
fd3f686c VZ |
73 | enum wxKeyType |
74 | { | |
75 | wxKEY_NONE, | |
76 | wxKEY_INTEGER, | |
77 | wxKEY_STRING | |
78 | }; | |
79 | ||
df5168c4 | 80 | #endif |
fd3f686c | 81 | |
df5168c4 | 82 | #if wxUSE_STL |
fd3f686c | 83 | |
df5168c4 MB |
84 | #define wxLIST_COMPATIBILITY |
85 | ||
86 | #define WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl) \ | |
87 | WX_DECLARE_LIST_X(elT, liT, decl) | |
6de44038 VZ |
88 | #define WX_DECLARE_LIST_PTR_3(elT, dummy1, liT, dummy2, decl) \ |
89 | WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl) | |
df5168c4 MB |
90 | |
91 | #define WX_DECLARE_LIST_2(elT, liT, dummy, decl) \ | |
92 | WX_DECLARE_LIST_X(elT, liT, decl) | |
6de44038 VZ |
93 | #define WX_DECLARE_LIST_PTR_2(elT, liT, dummy, decl) \ |
94 | WX_DECLARE_LIST_2(elT, liT, dummy, decl) \ | |
df5168c4 MB |
95 | |
96 | #define WX_DECLARE_LIST_X(elT, liT, decl) \ | |
97 | WX_DECLARE_LIST_XO(elT*, liT, decl) | |
98 | ||
99 | #define WX_DECLARE_LIST_XO(elT, liT, decl) \ | |
100 | decl liT : public std::list<elT> \ | |
101 | { \ | |
102 | public: \ | |
103 | class dummy; \ | |
104 | \ | |
105 | struct compatibility_iterator \ | |
106 | { \ | |
107 | typedef std::list<elT>::iterator iterator; \ | |
108 | iterator m_iter; \ | |
109 | liT * m_list; \ | |
110 | public: \ | |
111 | operator bool() const \ | |
112 | { return m_list && m_iter != m_list->end(); } \ | |
113 | bool operator !() const \ | |
114 | { return !m_list || m_iter == m_list->end(); } \ | |
115 | compatibility_iterator( const liT* li, iterator it ) \ | |
116 | : m_iter( it ), m_list( (liT*)li ) {} \ | |
117 | compatibility_iterator( liT* li, iterator it ) \ | |
118 | : m_iter( it ), m_list( li ) {} \ | |
119 | compatibility_iterator() : m_list( NULL ) { } \ | |
120 | dummy* operator->() { return (dummy*)this; } \ | |
121 | const dummy* operator->() const { return (const dummy*)this; } \ | |
122 | }; \ | |
123 | typedef struct compatibility_iterator citer; \ | |
124 | \ | |
125 | class dummy \ | |
126 | { \ | |
127 | typedef std::list<elT>::iterator it; \ | |
128 | typedef compatibility_iterator citer; \ | |
129 | public: \ | |
130 | elT GetData() const \ | |
131 | { \ | |
132 | citer* i = (citer*)this; \ | |
133 | return *(i->m_iter); \ | |
134 | } \ | |
135 | citer GetNext() const \ | |
136 | { \ | |
137 | citer* i = (citer*)this; \ | |
138 | it lit = i->m_iter; \ | |
139 | return citer( i->m_list, ++lit ); \ | |
140 | } \ | |
141 | citer GetPrevious() const \ | |
142 | { \ | |
143 | citer* i = (citer*)this; \ | |
144 | it lit = i->m_iter; \ | |
145 | return citer( i->m_list, ++lit ); \ | |
146 | } \ | |
147 | void SetData( elT e ) \ | |
148 | { \ | |
149 | citer* i = (citer*)this; \ | |
150 | *(i->m_iter) = e; \ | |
151 | } \ | |
152 | private: \ | |
153 | dummy(); \ | |
154 | }; \ | |
155 | protected: \ | |
156 | iterator find( elT e ) \ | |
157 | { \ | |
158 | iterator it, en; \ | |
159 | for( it = begin(), en = end(); it != en; ++it ) \ | |
160 | if( *it == e ) \ | |
161 | return it; \ | |
162 | return it; \ | |
163 | } \ | |
164 | public: \ | |
165 | liT() {}; \ | |
166 | \ | |
167 | citer Append( elT e ) { push_back( e ); return GetLast(); } \ | |
168 | void Clear() { clear(); } \ | |
169 | size_t GetCount() const { return size(); } \ | |
170 | citer GetFirst() const { return citer( this, ((liT*)this)->begin() ); } \ | |
171 | citer GetLast() const { return citer( this, --(((liT*)this)->end()) ); } \ | |
172 | bool IsEmpty() const { return empty(); } \ | |
173 | bool DeleteObject( elT e ) \ | |
174 | { \ | |
175 | iterator it = find( e ); \ | |
176 | if( it != end() ) \ | |
177 | { \ | |
178 | erase( it ); \ | |
179 | return true; \ | |
180 | } \ | |
181 | return false; \ | |
182 | } \ | |
183 | void Erase( const compatibility_iterator& it ) \ | |
184 | { \ | |
185 | erase( it.m_iter ); \ | |
186 | } \ | |
187 | citer Find( elT e ) const { return citer( this, ((liT*)this)->find( e ) ); } \ | |
188 | citer Member( elT e ) const { return Find( e ); } \ | |
189 | citer Insert( elT e ) \ | |
190 | { push_front( e ); return citer( this, begin() ); } \ | |
191 | citer Insert( size_t idx, elT e ) \ | |
192 | { return Insert( Item( idx ), e ); } \ | |
193 | citer Insert( citer idx, elT e ) \ | |
194 | { return citer( this, insert( idx.m_iter, e ) ); } \ | |
195 | citer Item( size_t idx ) const \ | |
196 | { \ | |
197 | iterator it; \ | |
198 | for( it = ((liT*)this)->begin(); idx; --idx ) \ | |
199 | ++it; \ | |
200 | return citer( this, it ); \ | |
201 | } \ | |
202 | int IndexOf( elT e ) const \ | |
203 | { \ | |
204 | const_iterator it, en; \ | |
205 | int idx; \ | |
206 | for( idx = 0, it = begin(), en = end(); it != en; ++it, ++idx ) \ | |
207 | if( *it == e ) \ | |
208 | return idx; \ | |
209 | return wxNOT_FOUND; \ | |
210 | } \ | |
211 | } | |
212 | ||
213 | #define WX_DECLARE_LIST(elementtype, listname) \ | |
214 | WX_DECLARE_LIST_X(elementtype, listname, class) | |
6de44038 VZ |
215 | #define WX_DECLARE_LIST_PTR(elementtype, listname) \ |
216 | WX_DECLARE_LIST(elementtype, listname) | |
df5168c4 MB |
217 | |
218 | #define WX_DECLARE_EXPORTED_LIST(elementtype, listname) \ | |
219 | WX_DECLARE_LIST_X(elementtype, listname, class WXDLLEXPORT) | |
6de44038 VZ |
220 | #define WX_DECLARE_EXPORTED_LIST_PTR(elementtype, listname) \ |
221 | WX_DECLARE_EXPORTED_LIST(elementtype, listname) | |
df5168c4 MB |
222 | |
223 | #define WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) \ | |
224 | WX_DECLARE_LIST_X(elementtype, listname, class usergoo) | |
6de44038 VZ |
225 | #define WX_DECLARE_USER_EXPORTED_LIST_PTR(elementtype, listname, usergoo) \ |
226 | WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) | |
df5168c4 MB |
227 | |
228 | // this macro must be inserted in your program after | |
229 | // #include <wx/listimpl.cpp> | |
230 | #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!" | |
231 | ||
232 | #define WX_DEFINE_EXPORTED_LIST(name) WX_DEFINE_LIST(name) | |
233 | #define WX_DEFINE_USER_EXPORTED_LIST(name) WX_DEFINE_LIST(name) | |
234 | ||
235 | #else // if !wxUSE_STL | |
236 | ||
237 | // due to circular header dependencies this function has to be declared here | |
238 | // (normally it's found in utils.h which includes itself list.h...) | |
ff98bd68 | 239 | extern WXDLLIMPEXP_BASE wxChar* copystring(const wxChar *s); |
df5168c4 MB |
240 | |
241 | class WXDLLEXPORT wxObjectListNode; | |
242 | typedef wxObjectListNode wxNode; | |
243 | ||
244 | // undef it to get rid of old, deprecated functions | |
245 | #define wxLIST_COMPATIBILITY | |
fd3f686c VZ |
246 | |
247 | // ----------------------------------------------------------------------------- | |
248 | // key stuff: a list may be optionally keyed on integer or string key | |
249 | // ----------------------------------------------------------------------------- | |
250 | ||
251 | union wxListKeyValue | |
c801d85f | 252 | { |
c801d85f | 253 | long integer; |
9d2f3c71 | 254 | wxChar *string; |
c801d85f KB |
255 | }; |
256 | ||
fd3f686c VZ |
257 | // a struct which may contain both types of keys |
258 | // | |
259 | // implementation note: on one hand, this class allows to have only one function | |
260 | // for any keyed operation instead of 2 almost equivalent. OTOH, it's needed to | |
261 | // resolve ambiguity which we would otherwise have with wxStringList::Find() and | |
262 | // wxList::Find(const char *). | |
bddd7a8d | 263 | class WXDLLIMPEXP_BASE wxListKey |
fd3f686c VZ |
264 | { |
265 | public: | |
266 | // implicit ctors | |
f2af4afb GD |
267 | wxListKey() : m_keyType(wxKEY_NONE) |
268 | { } | |
269 | wxListKey(long i) : m_keyType(wxKEY_INTEGER) | |
270 | { m_key.integer = i; } | |
271 | wxListKey(const wxChar *s) : m_keyType(wxKEY_STRING) | |
272 | { m_key.string = wxStrdup(s); } | |
273 | wxListKey(const wxString& s) : m_keyType(wxKEY_STRING) | |
274 | { m_key.string = wxStrdup(s.c_str()); } | |
fd3f686c VZ |
275 | |
276 | // accessors | |
277 | wxKeyType GetKeyType() const { return m_keyType; } | |
9d2f3c71 | 278 | const wxChar *GetString() const |
fd3f686c VZ |
279 | { wxASSERT( m_keyType == wxKEY_STRING ); return m_key.string; } |
280 | long GetNumber() const | |
281 | { wxASSERT( m_keyType == wxKEY_INTEGER ); return m_key.integer; } | |
282 | ||
6164d85e JS |
283 | // comparison |
284 | // Note: implementation moved to list.cpp to prevent BC++ inline | |
285 | // expansion warning. | |
286 | bool operator==(wxListKeyValue value) const ; | |
fd3f686c VZ |
287 | |
288 | // dtor | |
289 | ~wxListKey() | |
290 | { | |
291 | if ( m_keyType == wxKEY_STRING ) | |
292 | free(m_key.string); | |
293 | } | |
294 | ||
295 | private: | |
296 | wxKeyType m_keyType; | |
297 | wxListKeyValue m_key; | |
298 | }; | |
299 | ||
300 | // ----------------------------------------------------------------------------- | |
301 | // wxNodeBase class is a (base for) node in a double linked list | |
302 | // ----------------------------------------------------------------------------- | |
303 | ||
bddd7a8d | 304 | WXDLLIMPEXP_DATA_BASE(extern wxListKey) wxDefaultListKey; |
2432b92d | 305 | |
bddd7a8d | 306 | class WXDLLIMPEXP_BASE wxListBase; |
fbfb3fb3 | 307 | |
bddd7a8d | 308 | class WXDLLIMPEXP_BASE wxNodeBase |
fd3f686c VZ |
309 | { |
310 | friend class wxListBase; | |
311 | public: | |
312 | // ctor | |
313 | wxNodeBase(wxListBase *list = (wxListBase *)NULL, | |
314 | wxNodeBase *previous = (wxNodeBase *)NULL, | |
315 | wxNodeBase *next = (wxNodeBase *)NULL, | |
316 | void *data = NULL, | |
2432b92d | 317 | const wxListKey& key = wxDefaultListKey); |
fd3f686c VZ |
318 | |
319 | virtual ~wxNodeBase(); | |
320 | ||
f03fc89f | 321 | // FIXME no check is done that the list is really keyed on strings |
9d2f3c71 | 322 | const wxChar *GetKeyString() const { return m_key.string; } |
74e34480 | 323 | long GetKeyInteger() const { return m_key.integer; } |
fd3f686c | 324 | |
4fabb575 | 325 | // Necessary for some existing code |
9d2f3c71 | 326 | void SetKeyString(wxChar* s) { m_key.string = s; } |
4fabb575 JS |
327 | void SetKeyInteger(long i) { m_key.integer = i; } |
328 | ||
fd3f686c | 329 | #ifdef wxLIST_COMPATIBILITY |
b1d4dd7a RL |
330 | // compatibility methods, use Get* instead. |
331 | wxDEPRECATED( wxNode *Next() const ); | |
332 | wxDEPRECATED( wxNode *Previous() const ); | |
333 | wxDEPRECATED( wxObject *Data() const ); | |
fd3f686c VZ |
334 | #endif // wxLIST_COMPATIBILITY |
335 | ||
336 | protected: | |
337 | // all these are going to be "overloaded" in the derived classes | |
338 | wxNodeBase *GetNext() const { return m_next; } | |
339 | wxNodeBase *GetPrevious() const { return m_previous; } | |
340 | ||
341 | void *GetData() const { return m_data; } | |
342 | void SetData(void *data) { m_data = data; } | |
343 | ||
3c67202d | 344 | // get 0-based index of this node within the list or wxNOT_FOUND |
77c5eefb VZ |
345 | int IndexOf() const; |
346 | ||
fd3f686c | 347 | virtual void DeleteData() { } |
df5168c4 MB |
348 | public: |
349 | // for wxList::iterator | |
350 | void** GetDataPtr() const { return &(((wxNodeBase*)this)->m_data); } | |
fd3f686c VZ |
351 | private: |
352 | // optional key stuff | |
353 | wxListKeyValue m_key; | |
354 | ||
355 | void *m_data; // user data | |
356 | wxNodeBase *m_next, // next and previous nodes in the list | |
357 | *m_previous; | |
358 | ||
359 | wxListBase *m_list; // list we belong to | |
f2af4afb GD |
360 | |
361 | DECLARE_NO_COPY_CLASS(wxNodeBase) | |
fd3f686c VZ |
362 | }; |
363 | ||
364 | // ----------------------------------------------------------------------------- | |
365 | // a double-linked list class | |
366 | // ----------------------------------------------------------------------------- | |
f98bd52a | 367 | |
b1d4dd7a RL |
368 | class wxList; |
369 | ||
bddd7a8d | 370 | class WXDLLIMPEXP_BASE wxListBase : public wxObject |
fd3f686c | 371 | { |
bddd7a8d | 372 | friend class WXDLLIMPEXP_BASE wxNodeBase; // should be able to call DetachNode() |
bcaa23de | 373 | friend class wxHashTableBase; // should be able to call untyped Find() |
788722ac JS |
374 | private: |
375 | // common part of all ctors | |
376 | void Init(wxKeyType keyType = wxKEY_NONE); // Must be declared before it's used (for VC++ 1.5) | |
fd3f686c VZ |
377 | public: |
378 | // default ctor & dtor | |
f2af4afb GD |
379 | wxListBase(wxKeyType keyType = wxKEY_NONE) |
380 | { Init(keyType); } | |
fd3f686c VZ |
381 | virtual ~wxListBase(); |
382 | ||
383 | // accessors | |
384 | // count of items in the list | |
385 | size_t GetCount() const { return m_count; } | |
c801d85f | 386 | |
b79a8705 VZ |
387 | // return TRUE if this list is empty |
388 | bool IsEmpty() const { return m_count == 0; } | |
389 | ||
fd3f686c | 390 | // operations |
e146b8c8 | 391 | |
fd3f686c | 392 | // delete all nodes |
db9504c5 | 393 | void Clear(); |
e146b8c8 | 394 | |
fd3f686c VZ |
395 | // instruct it to destroy user data when deleting nodes |
396 | void DeleteContents(bool destroy) { m_destroy = destroy; } | |
397 | ||
907789a0 RR |
398 | // query if to delete |
399 | bool GetDeleteContents() const | |
400 | { return m_destroy; } | |
e146b8c8 | 401 | |
907789a0 RR |
402 | // get the keytype |
403 | wxKeyType GetKeyType() const | |
404 | { return m_keyType; } | |
405 | ||
406 | // set the keytype (required by the serial code) | |
407 | void SetKeyType(wxKeyType keyType) | |
408 | { wxASSERT( m_count==0 ); m_keyType = keyType; } | |
409 | ||
e146b8c8 | 410 | #ifdef wxLIST_COMPATIBILITY |
b1d4dd7a RL |
411 | // compatibility methods from old wxList |
412 | wxDEPRECATED( int Number() const ); // use GetCount instead. | |
413 | wxDEPRECATED( wxNode *First() const ); // use GetFirst | |
414 | wxDEPRECATED( wxNode *Last() const ); // use GetLast | |
415 | wxDEPRECATED( wxNode *Nth(size_t n) const ); // use Item | |
416 | ||
417 | // kludge for typesafe list migration in core classes. | |
418 | wxDEPRECATED( operator wxList&() const ); | |
e146b8c8 VZ |
419 | #endif // wxLIST_COMPATIBILITY |
420 | ||
fd3f686c | 421 | protected: |
a3ef5bf5 | 422 | |
fd3f686c VZ |
423 | // all methods here are "overloaded" in derived classes to provide compile |
424 | // time type checking | |
425 | ||
426 | // create a node for the list of this type | |
427 | virtual wxNodeBase *CreateNode(wxNodeBase *prev, wxNodeBase *next, | |
428 | void *data, | |
2432b92d | 429 | const wxListKey& key = wxDefaultListKey) = 0; |
fd3f686c | 430 | |
a3ef5bf5 JS |
431 | // Can't access these from derived classes otherwise (bug in Salford C++?) |
432 | #ifdef __SALFORDC__ | |
433 | public: | |
434 | #endif | |
435 | ||
fd3f686c VZ |
436 | // ctors |
437 | // from an array | |
438 | wxListBase(size_t count, void *elements[]); | |
439 | // from a sequence of objects | |
440 | wxListBase(void *object, ... /* terminate with NULL */); | |
441 | ||
a3ef5bf5 | 442 | protected: |
fd3f686c | 443 | // copy ctor and assignment operator |
6788ecb8 | 444 | wxListBase(const wxListBase& list) : wxObject() |
6108b321 | 445 | { Init(); DoCopy(list); } |
fd3f686c VZ |
446 | wxListBase& operator=(const wxListBase& list) |
447 | { Clear(); DoCopy(list); return *this; } | |
448 | ||
449 | // get list head/tail | |
450 | wxNodeBase *GetFirst() const { return m_nodeFirst; } | |
451 | wxNodeBase *GetLast() const { return m_nodeLast; } | |
452 | ||
453 | // by (0-based) index | |
454 | wxNodeBase *Item(size_t index) const; | |
455 | ||
456 | // get the list item's data | |
2b5f62a0 VZ |
457 | void *operator[](size_t n) const |
458 | { | |
459 | wxNodeBase *node = Item(n); | |
460 | ||
461 | return node ? node->GetData() : (wxNodeBase *)NULL; | |
462 | } | |
fd3f686c VZ |
463 | |
464 | // operations | |
465 | // append to end of list | |
890f8a7c RR |
466 | wxNodeBase *Prepend(void *object) |
467 | { return (wxNodeBase *)wxListBase::Insert(object); } | |
468 | // append to beginning of list | |
fd3f686c VZ |
469 | wxNodeBase *Append(void *object); |
470 | // insert a new item at the beginning of the list | |
a802c3a1 | 471 | wxNodeBase *Insert(void *object) { return Insert( (wxNodeBase*)NULL, object); } |
d8996187 VZ |
472 | // insert a new item at the given position |
473 | wxNodeBase *Insert(size_t pos, void *object) | |
474 | { return pos == GetCount() ? Append(object) | |
475 | : Insert(Item(pos), object); } | |
fd3f686c VZ |
476 | // insert before given node or at front of list if prev == NULL |
477 | wxNodeBase *Insert(wxNodeBase *prev, void *object); | |
478 | ||
479 | // keyed append | |
480 | wxNodeBase *Append(long key, void *object); | |
9d2f3c71 | 481 | wxNodeBase *Append(const wxChar *key, void *object); |
fd3f686c VZ |
482 | |
483 | // removes node from the list but doesn't delete it (returns pointer | |
484 | // to the node or NULL if it wasn't found in the list) | |
485 | wxNodeBase *DetachNode(wxNodeBase *node); | |
486 | // delete element from list, returns FALSE if node not found | |
487 | bool DeleteNode(wxNodeBase *node); | |
488 | // finds object pointer and deletes node (and object if DeleteContents | |
489 | // is on), returns FALSE if object not found | |
77c5eefb | 490 | bool DeleteObject(void *object); |
fd3f686c VZ |
491 | |
492 | // search (all return NULL if item not found) | |
493 | // by data | |
494 | wxNodeBase *Find(void *object) const; | |
495 | ||
496 | // by key | |
497 | wxNodeBase *Find(const wxListKey& key) const; | |
498 | ||
3c67202d | 499 | // get 0-based index of object or wxNOT_FOUND |
77c5eefb VZ |
500 | int IndexOf( void *object ) const; |
501 | ||
fd3f686c VZ |
502 | // this function allows the sorting of arbitrary lists by giving |
503 | // a function to compare two list elements. The list is sorted in place. | |
6164d85e | 504 | void Sort(const wxSortCompareFunction compfunc); |
fd3f686c VZ |
505 | |
506 | // functions for iterating over the list | |
507 | void *FirstThat(wxListIterateFunction func); | |
508 | void ForEach(wxListIterateFunction func); | |
509 | void *LastThat(wxListIterateFunction func); | |
e146b8c8 | 510 | |
df5168c4 MB |
511 | // for STL interface, "last" points to one after the last node |
512 | // of the controlled sequence (NULL for the end of the list) | |
513 | void Reverse(); | |
514 | void DeleteNodes(wxNodeBase* first, wxNodeBase* last); | |
fd3f686c VZ |
515 | private: |
516 | // helpers | |
fd3f686c VZ |
517 | // common part of copy ctor and assignment operator |
518 | void DoCopy(const wxListBase& list); | |
519 | // common part of all Append()s | |
520 | wxNodeBase *AppendCommon(wxNodeBase *node); | |
521 | // free node's data and node itself | |
522 | void DoDeleteNode(wxNodeBase *node); | |
523 | ||
524 | size_t m_count; // number of elements in the list | |
525 | bool m_destroy; // destroy user data when deleting list items? | |
526 | wxNodeBase *m_nodeFirst, // pointers to the head and tail of the list | |
527 | *m_nodeLast; | |
528 | ||
529 | wxKeyType m_keyType; // type of our keys (may be wxKEY_NONE) | |
530 | }; | |
531 | ||
532 | // ----------------------------------------------------------------------------- | |
533 | // macros for definition of "template" list type | |
534 | // ----------------------------------------------------------------------------- | |
535 | ||
536 | // and now some heavy magic... | |
537 | ||
538 | // declare a list type named 'name' and containing elements of type 'T *' | |
539 | // (as a by product of macro expansion you also get wx##name##Node | |
ce3ed50d | 540 | // wxNode-derived type) |
fd3f686c VZ |
541 | // |
542 | // implementation details: | |
ce3ed50d | 543 | // 1. We define _WX_LIST_ITEM_TYPE_##name typedef to save in it the item type |
fd3f686c VZ |
544 | // for the list of given type - this allows us to pass only the list name |
545 | // to WX_DEFINE_LIST() even if it needs both the name and the type | |
546 | // | |
ce3ed50d JS |
547 | // 2. We redefine all non-type-safe wxList functions with type-safe versions |
548 | // which don't take any space (everything is inline), but bring compile | |
fd3f686c | 549 | // time error checking. |
f03fc89f VZ |
550 | // |
551 | // 3. The macro which is usually used (WX_DECLARE_LIST) is defined in terms of | |
552 | // a more generic WX_DECLARE_LIST_2 macro which, in turn, uses the most | |
553 | // generic WX_DECLARE_LIST_3 one. The last macro adds a sometimes | |
554 | // interesting capability to store polymorphic objects in the list and is | |
555 | // particularly useful with, for example, "wxWindow *" list where the | |
556 | // wxWindowBase pointers are put into the list, but wxWindow pointers are | |
557 | // retrieved from it. | |
6de44038 VZ |
558 | // |
559 | // 4. final hack is that WX_DECLARE_LIST_3 is defined in terms of | |
560 | // WX_DECLARE_LIST_4 to allow defining classes without operator->() as | |
561 | // it results in compiler warnings when this operator doesn't make sense | |
562 | // (i.e. stored elements are not pointers) | |
f03fc89f | 563 | |
6de44038 VZ |
564 | // common part of WX_DECLARE_LIST_3 and WX_DECLARE_LIST_PTR_3 |
565 | #define WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, ptrop) \ | |
ba059d80 | 566 | typedef int (*wxSortFuncFor_##name)(const T **, const T **); \ |
fd3f686c | 567 | \ |
f6bcfd97 | 568 | classexp nodetype : public wxNodeBase \ |
fd3f686c VZ |
569 | { \ |
570 | public: \ | |
571 | nodetype(wxListBase *list = (wxListBase *)NULL, \ | |
572 | nodetype *previous = (nodetype *)NULL, \ | |
573 | nodetype *next = (nodetype *)NULL, \ | |
7f985bd3 | 574 | T *data = (T *)NULL, \ |
e146b8c8 | 575 | const wxListKey& key = wxDefaultListKey) \ |
fd3f686c VZ |
576 | : wxNodeBase(list, previous, next, data, key) { } \ |
577 | \ | |
578 | nodetype *GetNext() const \ | |
579 | { return (nodetype *)wxNodeBase::GetNext(); } \ | |
580 | nodetype *GetPrevious() const \ | |
581 | { return (nodetype *)wxNodeBase::GetPrevious(); } \ | |
582 | \ | |
583 | T *GetData() const \ | |
584 | { return (T *)wxNodeBase::GetData(); } \ | |
585 | void SetData(T *data) \ | |
586 | { wxNodeBase::SetData(data); } \ | |
587 | \ | |
588 | virtual void DeleteData(); \ | |
cd989b24 VZ |
589 | \ |
590 | DECLARE_NO_COPY_CLASS(nodetype) \ | |
fd3f686c VZ |
591 | }; \ |
592 | \ | |
f6bcfd97 | 593 | classexp name : public wxListBase \ |
fd3f686c VZ |
594 | { \ |
595 | public: \ | |
e2a3cc0c | 596 | typedef nodetype Node; \ |
df5168c4 | 597 | typedef Node* compatibility_iterator; \ |
e2a3cc0c | 598 | \ |
fd3f686c VZ |
599 | name(wxKeyType keyType = wxKEY_NONE) : wxListBase(keyType) \ |
600 | { } \ | |
601 | name(size_t count, T *elements[]) \ | |
602 | : wxListBase(count, (void **)elements) { } \ | |
603 | \ | |
f6bcfd97 | 604 | name& operator=(const name& list) \ |
f2af4afb | 605 | { (void) wxListBase::operator=(list); return *this; } \ |
f6bcfd97 | 606 | \ |
fd3f686c VZ |
607 | nodetype *GetFirst() const \ |
608 | { return (nodetype *)wxListBase::GetFirst(); } \ | |
609 | nodetype *GetLast() const \ | |
610 | { return (nodetype *)wxListBase::GetLast(); } \ | |
611 | \ | |
612 | nodetype *Item(size_t index) const \ | |
613 | { return (nodetype *)wxListBase::Item(index); } \ | |
614 | \ | |
615 | T *operator[](size_t index) const \ | |
616 | { \ | |
617 | nodetype *node = Item(index); \ | |
7f985bd3 | 618 | return node ? (T*)(node->GetData()) : (T*)NULL; \ |
fd3f686c VZ |
619 | } \ |
620 | \ | |
f03fc89f | 621 | nodetype *Append(Tbase *object) \ |
fd3f686c | 622 | { return (nodetype *)wxListBase::Append(object); } \ |
f03fc89f | 623 | nodetype *Insert(Tbase *object) \ |
a802c3a1 | 624 | { return (nodetype *)Insert((nodetype*)NULL, object); } \ |
d8996187 VZ |
625 | nodetype *Insert(size_t pos, Tbase *object) \ |
626 | { return (nodetype *)wxListBase::Insert(pos, object); } \ | |
f03fc89f | 627 | nodetype *Insert(nodetype *prev, Tbase *object) \ |
fd3f686c VZ |
628 | { return (nodetype *)wxListBase::Insert(prev, object); } \ |
629 | \ | |
630 | nodetype *Append(long key, void *object) \ | |
631 | { return (nodetype *)wxListBase::Append(key, object); } \ | |
9d2f3c71 | 632 | nodetype *Append(const wxChar *key, void *object) \ |
fd3f686c VZ |
633 | { return (nodetype *)wxListBase::Append(key, object); } \ |
634 | \ | |
635 | nodetype *DetachNode(nodetype *node) \ | |
636 | { return (nodetype *)wxListBase::DetachNode(node); } \ | |
637 | bool DeleteNode(nodetype *node) \ | |
638 | { return wxListBase::DeleteNode(node); } \ | |
f03fc89f | 639 | bool DeleteObject(Tbase *object) \ |
fd3f686c | 640 | { return wxListBase::DeleteObject(object); } \ |
df5168c4 MB |
641 | void Erase(compatibility_iterator it) \ |
642 | { DeleteNode(it); } \ | |
fd3f686c | 643 | \ |
f03fc89f | 644 | nodetype *Find(Tbase *object) const \ |
fd3f686c VZ |
645 | { return (nodetype *)wxListBase::Find(object); } \ |
646 | \ | |
647 | virtual nodetype *Find(const wxListKey& key) const \ | |
648 | { return (nodetype *)wxListBase::Find(key); } \ | |
649 | \ | |
f03fc89f | 650 | int IndexOf(Tbase *object) const \ |
77c5eefb VZ |
651 | { return wxListBase::IndexOf(object); } \ |
652 | \ | |
fd3f686c VZ |
653 | void Sort(wxSortFuncFor_##name func) \ |
654 | { wxListBase::Sort((wxSortCompareFunction)func); } \ | |
655 | \ | |
656 | protected: \ | |
ea48aff3 | 657 | virtual wxNodeBase *CreateNode(wxNodeBase *prev, wxNodeBase *next, \ |
fd3f686c | 658 | void *data, \ |
e146b8c8 | 659 | const wxListKey& key = wxDefaultListKey) \ |
fd3f686c VZ |
660 | { \ |
661 | return new nodetype(this, \ | |
662 | (nodetype *)prev, (nodetype *)next, \ | |
663 | (T *)data, key); \ | |
664 | } \ | |
df5168c4 MB |
665 | /* STL interface */ \ |
666 | public: \ | |
667 | typedef size_t size_type; \ | |
668 | typedef int difference_type; \ | |
669 | typedef T* value_type; \ | |
670 | typedef Tbase* base_value_type; \ | |
671 | typedef value_type& reference; \ | |
672 | typedef const value_type& const_reference; \ | |
673 | typedef base_value_type& base_reference; \ | |
674 | typedef const base_value_type& const_base_reference; \ | |
675 | \ | |
676 | class iterator \ | |
677 | { \ | |
678 | typedef name list; \ | |
679 | public: \ | |
37589419 | 680 | typedef nodetype Node; \ |
df5168c4 | 681 | typedef iterator itor; \ |
37589419 MB |
682 | typedef T* value_type; \ |
683 | typedef value_type* ptr_type; \ | |
684 | typedef value_type& reference; \ | |
df5168c4 MB |
685 | \ |
686 | Node* m_node; \ | |
687 | Node* m_init; \ | |
688 | public: \ | |
37589419 | 689 | typedef reference reference_type; \ |
df5168c4 MB |
690 | typedef ptr_type pointer_type; \ |
691 | \ | |
692 | iterator(Node* node, Node* init) : m_node(node), m_init(init) {}\ | |
693 | iterator() : m_node(NULL), m_init(NULL) { } \ | |
694 | reference_type operator*() const \ | |
695 | { return *(pointer_type)m_node->GetDataPtr(); } \ | |
6de44038 | 696 | ptrop \ |
df5168c4 MB |
697 | itor& operator++() { m_node = m_node->GetNext(); return *this; }\ |
698 | itor operator++(int) \ | |
699 | { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }\ | |
700 | itor& operator--() \ | |
701 | { \ | |
702 | m_node = m_node ? m_node->GetPrevious() : m_init; \ | |
703 | return *this; \ | |
704 | } \ | |
705 | itor operator--(int) \ | |
706 | { \ | |
707 | itor tmp = *this; \ | |
708 | m_node = m_node ? m_node->GetPrevious() : m_init; \ | |
709 | return tmp; \ | |
710 | } \ | |
711 | bool operator!=(const itor& it) const \ | |
712 | { return it.m_node != m_node; } \ | |
713 | bool operator==(const itor& it) const \ | |
714 | { return it.m_node == m_node; } \ | |
715 | }; \ | |
716 | class const_iterator \ | |
717 | { \ | |
718 | typedef name list; \ | |
719 | public: \ | |
37589419 MB |
720 | typedef nodetype Node; \ |
721 | typedef T* value_type; \ | |
722 | typedef const value_type& const_reference; \ | |
df5168c4 | 723 | typedef const_iterator itor; \ |
37589419 | 724 | typedef value_type* ptr_type; \ |
df5168c4 MB |
725 | \ |
726 | Node* m_node; \ | |
727 | Node* m_init; \ | |
728 | public: \ | |
37589419 | 729 | typedef const_reference reference_type; \ |
df5168c4 MB |
730 | typedef const ptr_type pointer_type; \ |
731 | \ | |
732 | const_iterator(Node* node, Node* init) \ | |
733 | : m_node(node), m_init(init) { } \ | |
734 | const_iterator() : m_node(NULL), m_init(NULL) { } \ | |
735 | const_iterator(const iterator& it) \ | |
736 | : m_node(it.m_node), m_init(it.m_init) { } \ | |
737 | reference_type operator*() const \ | |
738 | { return *(pointer_type)m_node->GetDataPtr(); } \ | |
6de44038 | 739 | ptrop \ |
df5168c4 MB |
740 | itor& operator++() { m_node = m_node->GetNext(); return *this; }\ |
741 | itor operator++(int) \ | |
742 | { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }\ | |
743 | itor& operator--() \ | |
744 | { \ | |
745 | m_node = m_node ? m_node->GetPrevious() : m_init; \ | |
746 | return *this; \ | |
747 | } \ | |
748 | itor operator--(int) \ | |
749 | { \ | |
750 | itor tmp = *this; \ | |
751 | m_node = m_node ? m_node->GetPrevious() : m_init; \ | |
752 | return tmp; \ | |
753 | } \ | |
754 | bool operator!=(const itor& it) const \ | |
755 | { return it.m_node != m_node; } \ | |
756 | bool operator==(const itor& it) const \ | |
757 | { return it.m_node == m_node; } \ | |
758 | }; \ | |
759 | class reverse_iterator \ | |
760 | { \ | |
761 | typedef name list; \ | |
762 | public: \ | |
37589419 MB |
763 | typedef nodetype Node; \ |
764 | typedef T* value_type; \ | |
df5168c4 | 765 | typedef reverse_iterator itor; \ |
37589419 MB |
766 | typedef value_type* ptr_type; \ |
767 | typedef value_type& reference; \ | |
df5168c4 MB |
768 | \ |
769 | Node* m_node; \ | |
770 | Node* m_init; \ | |
771 | public: \ | |
37589419 | 772 | typedef reference reference_type; \ |
df5168c4 MB |
773 | typedef ptr_type pointer_type; \ |
774 | \ | |
775 | reverse_iterator(Node* node, Node* init) \ | |
776 | : m_node(node), m_init(init) { } \ | |
777 | reverse_iterator() : m_node(NULL), m_init(NULL) { } \ | |
778 | reference_type operator*() const \ | |
779 | { return *(pointer_type)m_node->GetDataPtr(); } \ | |
6de44038 | 780 | ptrop \ |
df5168c4 MB |
781 | itor& operator++() \ |
782 | { m_node = m_node->GetPrevious(); return *this; } \ | |
783 | itor operator++(int) \ | |
784 | { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }\ | |
785 | itor& operator--() \ | |
786 | { m_node = m_node ? m_node->GetNext() : m_init; return *this; } \ | |
787 | itor operator--(int) \ | |
788 | { \ | |
789 | itor tmp = *this; \ | |
790 | m_node = m_node ? m_node->GetNext() : m_init; \ | |
791 | return tmp; \ | |
792 | } \ | |
793 | bool operator!=(const itor& it) const \ | |
794 | { return it.m_node != m_node; } \ | |
795 | bool operator==(const itor& it) const \ | |
796 | { return it.m_node == m_node; } \ | |
797 | }; \ | |
798 | class const_reverse_iterator \ | |
799 | { \ | |
800 | typedef name list; \ | |
801 | public: \ | |
37589419 MB |
802 | typedef nodetype Node; \ |
803 | typedef T* value_type; \ | |
df5168c4 | 804 | typedef const_reverse_iterator itor; \ |
37589419 MB |
805 | typedef value_type* ptr_type; \ |
806 | typedef const value_type& const_reference; \ | |
df5168c4 MB |
807 | \ |
808 | Node* m_node; \ | |
809 | Node* m_init; \ | |
810 | public: \ | |
37589419 | 811 | typedef const_reference reference_type; \ |
df5168c4 MB |
812 | typedef const ptr_type pointer_type; \ |
813 | \ | |
814 | const_reverse_iterator(Node* node, Node* init) \ | |
815 | : m_node(node), m_init(init) { } \ | |
816 | const_reverse_iterator() : m_node(NULL), m_init(NULL) { } \ | |
817 | const_reverse_iterator(const reverse_iterator& it) \ | |
818 | : m_node(it.m_node), m_init(it.m_init) { } \ | |
819 | reference_type operator*() const \ | |
820 | { return *(pointer_type)m_node->GetDataPtr(); } \ | |
6de44038 | 821 | ptrop \ |
df5168c4 MB |
822 | itor& operator++() \ |
823 | { m_node = m_node->GetPrevious(); return *this; } \ | |
824 | itor operator++(int) \ | |
825 | { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }\ | |
826 | itor& operator--() \ | |
827 | { m_node = m_node ? m_node->GetNext() : m_init; return *this;}\ | |
828 | itor operator--(int) \ | |
829 | { \ | |
830 | itor tmp = *this; \ | |
831 | m_node = m_node ? m_node->GetNext() : m_init; \ | |
832 | return tmp; \ | |
833 | } \ | |
834 | bool operator!=(const itor& it) const \ | |
835 | { return it.m_node != m_node; } \ | |
836 | bool operator==(const itor& it) const \ | |
837 | { return it.m_node == m_node; } \ | |
838 | }; \ | |
839 | \ | |
840 | wxEXPLICIT name(size_type n, const_reference v = value_type()) \ | |
841 | { assign(n, v); } \ | |
842 | name(const_iterator first, const_iterator last) \ | |
843 | { assign(first, last); } \ | |
844 | iterator begin() { return iterator(GetFirst(), GetLast()); } \ | |
845 | const_iterator begin() const \ | |
846 | { return const_iterator(GetFirst(), GetLast()); } \ | |
847 | iterator end() { return iterator(NULL, GetLast()); } \ | |
848 | const_iterator end() const { return const_iterator(NULL, GetLast()); }\ | |
849 | reverse_iterator rbegin() \ | |
850 | { return reverse_iterator(GetLast(), GetFirst()); } \ | |
851 | const_reverse_iterator rbegin() const \ | |
852 | { return const_reverse_iterator(GetLast(), GetFirst()); } \ | |
853 | reverse_iterator rend() { return reverse_iterator(NULL, GetFirst()); }\ | |
854 | const_reverse_iterator rend() const \ | |
855 | { return const_reverse_iterator(NULL, GetFirst()); } \ | |
856 | void resize(size_type n, value_type v = value_type()) \ | |
857 | { \ | |
858 | if(n < size()) \ | |
859 | for(; n < size(); pop_back()); \ | |
860 | else if(n > size()) \ | |
861 | for(; n > size(); push_back(v)); \ | |
862 | } \ | |
863 | size_type size() const { return GetCount(); } \ | |
864 | size_type max_size() const { return INT_MAX; } \ | |
865 | bool empty() const { return IsEmpty(); } \ | |
866 | reference front() { return *begin(); } \ | |
867 | const_reference front() const { return *begin(); } \ | |
868 | reference back() { return *--end(); } \ | |
869 | const_reference back() const { return *--end(); } \ | |
870 | void push_front(const_reference v = value_type()) \ | |
871 | { Insert(GetFirst(), (const_base_reference)v); } \ | |
872 | void pop_front() { DeleteNode(GetFirst()); } \ | |
873 | void push_back(const_reference v = value_type()) \ | |
874 | { Append((const_base_reference)v); } \ | |
875 | void pop_back() { DeleteNode(GetLast()); } \ | |
876 | void assign(const_iterator first, const_iterator last) \ | |
877 | { \ | |
878 | clear(); \ | |
879 | for(; first != last; ++first) \ | |
880 | Append((const_base_reference)*first); \ | |
881 | } \ | |
882 | void assign(size_type n, const_reference v = value_type()) \ | |
883 | { \ | |
884 | clear(); \ | |
885 | for(size_type i = 0; i < n; ++i) \ | |
886 | Append((const_base_reference)v); \ | |
887 | } \ | |
888 | iterator insert(iterator it, const_reference v = value_type()) \ | |
889 | { \ | |
890 | Insert(it.m_node, (const_base_reference)v); \ | |
891 | return iterator(it.m_node->GetPrevious(), GetLast()); \ | |
892 | } \ | |
893 | void insert(iterator it, size_type n, const_reference v = value_type())\ | |
894 | { \ | |
895 | for(size_type i = 0; i < n; ++i) \ | |
896 | Insert(it.m_node, (const_base_reference)v); \ | |
897 | } \ | |
898 | void insert(iterator it, const_iterator first, const_iterator last) \ | |
899 | { \ | |
900 | for(; first != last; ++first) \ | |
901 | Insert(it.m_node, (const_base_reference)*first); \ | |
902 | } \ | |
903 | iterator erase(iterator it) \ | |
904 | { \ | |
905 | iterator next = iterator(it.m_node->GetNext(), GetLast()); \ | |
906 | DeleteNode(it.m_node); return next; \ | |
907 | } \ | |
908 | iterator erase(iterator first, iterator last) \ | |
909 | { \ | |
910 | iterator next = last; ++next; \ | |
911 | DeleteNodes(first.m_node, last.m_node); \ | |
912 | return next; \ | |
913 | } \ | |
914 | void clear() { Clear(); } \ | |
915 | void splice(iterator it, name& l, iterator first, iterator last) \ | |
916 | { insert(it, first, last); l.erase(first, last); } \ | |
917 | void splice(iterator it, name& l) \ | |
918 | { splice(it, l, l.begin(), l.end() ); } \ | |
919 | void splice(iterator it, name& l, iterator first) \ | |
920 | { \ | |
921 | iterator tmp = first; ++tmp; \ | |
922 | if(it == first || it == tmp) return; \ | |
923 | insert(it, *first); \ | |
924 | l.erase(first); \ | |
925 | } \ | |
926 | void remove(const_reference v) \ | |
927 | { DeleteObject((const_base_reference)v); } \ | |
928 | void reverse() \ | |
929 | { Reverse(); } \ | |
930 | /* void swap(name& l) \ | |
931 | { \ | |
932 | { size_t t = m_count; m_count = l.m_count; l.m_count = t; } \ | |
933 | { bool t = m_destroy; m_destroy = l.m_destroy; l.m_destroy = t; }\ | |
934 | { wxNodeBase* t = m_nodeFirst; m_nodeFirst = l.m_nodeFirst; l.m_nodeFirst = t; }\ | |
935 | { wxNodeBase* t = m_nodeLast; m_nodeLast = l.m_nodeLast; l.m_nodeLast = t; }\ | |
936 | { wxKeyType t = m_keyType; m_keyType = l.m_keyType; l.m_keyType = t; }\ | |
937 | } */ \ | |
385bcb35 | 938 | } |
fd3f686c | 939 | |
6de44038 VZ |
940 | #define WX_LIST_PTROP \ |
941 | pointer_type operator->() const \ | |
942 | { return (pointer_type)m_node->GetDataPtr(); } | |
943 | #define WX_LIST_PTROP_NONE | |
944 | ||
945 | #define WX_DECLARE_LIST_3(T, Tbase, name, nodetype, classexp) \ | |
946 | WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, WX_LIST_PTROP_NONE) | |
947 | #define WX_DECLARE_LIST_PTR_3(T, Tbase, name, nodetype, classexp) \ | |
948 | WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, WX_LIST_PTROP) | |
949 | ||
f6bcfd97 BP |
950 | #define WX_DECLARE_LIST_2(elementtype, listname, nodename, classexp) \ |
951 | WX_DECLARE_LIST_3(elementtype, elementtype, listname, nodename, classexp) | |
6de44038 VZ |
952 | #define WX_DECLARE_LIST_PTR_2(elementtype, listname, nodename, classexp) \ |
953 | WX_DECLARE_LIST_PTR_3(elementtype, elementtype, listname, nodename, classexp) | |
f03fc89f | 954 | |
fd3f686c VZ |
955 | #define WX_DECLARE_LIST(elementtype, listname) \ |
956 | typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \ | |
f6bcfd97 | 957 | WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class) |
6de44038 VZ |
958 | #define WX_DECLARE_LIST_PTR(elementtype, listname) \ |
959 | typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \ | |
960 | WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class) | |
f6bcfd97 BP |
961 | |
962 | #define WX_DECLARE_EXPORTED_LIST(elementtype, listname) \ | |
963 | typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \ | |
964 | WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class WXDLLEXPORT) | |
6de44038 VZ |
965 | #define WX_DECLARE_EXPORTED_LIST_PTR(elementtype, listname) \ |
966 | typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \ | |
967 | WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class WXDLLEXPORT) | |
fd3f686c | 968 | |
0b9ab0bd RL |
969 | #define WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) \ |
970 | typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \ | |
971 | WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class usergoo) | |
6de44038 VZ |
972 | #define WX_DECLARE_USER_EXPORTED_LIST_PTR(elementtype, listname, usergoo) \ |
973 | typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \ | |
974 | WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class usergoo) | |
0b9ab0bd | 975 | |
fd3f686c VZ |
976 | // this macro must be inserted in your program after |
977 | // #include <wx/listimpl.cpp> | |
978 | #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!" | |
979 | ||
0b9ab0bd RL |
980 | #define WX_DEFINE_EXPORTED_LIST(name) WX_DEFINE_LIST(name) |
981 | #define WX_DEFINE_USER_EXPORTED_LIST(name) WX_DEFINE_LIST(name) | |
982 | ||
df5168c4 | 983 | #endif // !wxUSE_STL |
0b9ab0bd | 984 | |
69b9f4cc | 985 | // ============================================================================ |
fd3f686c | 986 | // now we can define classes 100% compatible with the old ones |
69b9f4cc | 987 | // ============================================================================ |
fd3f686c | 988 | |
e146b8c8 | 989 | // ---------------------------------------------------------------------------- |
f03fc89f | 990 | // commonly used list classes |
e146b8c8 VZ |
991 | // ---------------------------------------------------------------------------- |
992 | ||
69b9f4cc | 993 | #if defined(wxLIST_COMPATIBILITY) |
fd3f686c | 994 | |
137b7303 VZ |
995 | // inline compatibility functions |
996 | ||
69b9f4cc MB |
997 | #if !wxUSE_STL |
998 | ||
999 | // ---------------------------------------------------------------------------- | |
137b7303 | 1000 | // wxNodeBase deprecated methods |
69b9f4cc | 1001 | // ---------------------------------------------------------------------------- |
137b7303 VZ |
1002 | |
1003 | inline wxNode *wxNodeBase::Next() const { return (wxNode *)GetNext(); } | |
1004 | inline wxNode *wxNodeBase::Previous() const { return (wxNode *)GetPrevious(); } | |
1005 | inline wxObject *wxNodeBase::Data() const { return (wxObject *)GetData(); } | |
1006 | ||
69b9f4cc | 1007 | // ---------------------------------------------------------------------------- |
137b7303 | 1008 | // wxListBase deprecated methods |
69b9f4cc | 1009 | // ---------------------------------------------------------------------------- |
137b7303 VZ |
1010 | |
1011 | inline int wxListBase::Number() const { return (int)GetCount(); } | |
1012 | inline wxNode *wxListBase::First() const { return (wxNode *)GetFirst(); } | |
1013 | inline wxNode *wxListBase::Last() const { return (wxNode *)GetLast(); } | |
1014 | inline wxNode *wxListBase::Nth(size_t n) const { return (wxNode *)Item(n); } | |
1015 | inline wxListBase::operator wxList&() const { return *(wxList*)this; } | |
1016 | ||
69b9f4cc | 1017 | #endif |
137b7303 | 1018 | |
b1d4dd7a RL |
1019 | // define this to make a lot of noise about use of the old wxList classes. |
1020 | //#define wxWARN_COMPAT_LIST_USE | |
1021 | ||
69b9f4cc | 1022 | // ---------------------------------------------------------------------------- |
fd3f686c | 1023 | // wxList compatibility class: in fact, it's a list of wxObjects |
69b9f4cc | 1024 | // ---------------------------------------------------------------------------- |
6de44038 VZ |
1025 | |
1026 | WX_DECLARE_LIST_2(wxObject, wxObjectList, wxObjectListNode, | |
1027 | class WXDLLIMPEXP_BASE); | |
fd3f686c | 1028 | |
bddd7a8d | 1029 | class WXDLLIMPEXP_BASE wxList : public wxObjectList |
c801d85f | 1030 | { |
fd3f686c | 1031 | public: |
df5168c4 | 1032 | #if defined(wxWARN_COMPAT_LIST_USE) && !wxUSE_STL |
b1d4dd7a | 1033 | wxDEPRECATED( wxList(int key_type = wxKEY_NONE) ); |
df5168c4 | 1034 | #elif !wxUSE_STL |
b1d4dd7a RL |
1035 | wxList(int key_type = wxKEY_NONE); |
1036 | #endif | |
1037 | ||
799ea011 | 1038 | // this destructor is required for Darwin |
f11bdd03 | 1039 | ~wxList() { } |
fd3f686c | 1040 | |
df5168c4 | 1041 | #if !wxUSE_STL |
f6bcfd97 | 1042 | wxList& operator=(const wxList& list) |
f2af4afb | 1043 | { (void) wxListBase::operator=(list); return *this; } |
f6bcfd97 | 1044 | |
fd3f686c | 1045 | // compatibility methods |
3bef6c4c | 1046 | void Sort(wxSortCompareFunction compfunc) { wxListBase::Sort(compfunc); } |
df5168c4 | 1047 | #endif |
3bef6c4c | 1048 | |
df5168c4 MB |
1049 | #if wxUSE_STL |
1050 | #else | |
fd3f686c | 1051 | wxNode *Member(wxObject *object) const { return (wxNode *)Find(object); } |
df5168c4 | 1052 | #endif |
f98bd52a VZ |
1053 | |
1054 | private: | |
df5168c4 | 1055 | #if !wxUSE_STL |
f98bd52a | 1056 | DECLARE_DYNAMIC_CLASS(wxList) |
df5168c4 | 1057 | #endif |
c801d85f KB |
1058 | }; |
1059 | ||
df5168c4 MB |
1060 | #if !wxUSE_STL |
1061 | ||
fd3f686c VZ |
1062 | // ----------------------------------------------------------------------------- |
1063 | // wxStringList class for compatibility with the old code | |
1064 | // ----------------------------------------------------------------------------- | |
bddd7a8d | 1065 | WX_DECLARE_LIST_2(wxChar, wxStringListBase, wxStringListNode, class WXDLLIMPEXP_BASE); |
fd3f686c | 1066 | |
bddd7a8d | 1067 | class WXDLLIMPEXP_BASE wxStringList : public wxStringListBase |
c801d85f | 1068 | { |
fd3f686c VZ |
1069 | public: |
1070 | // ctors and such | |
1071 | // default | |
b1d4dd7a RL |
1072 | #ifdef wxWARN_COMPAT_LIST_USE |
1073 | wxDEPRECATED( wxStringList() ); | |
1074 | wxDEPRECATED( wxStringList(const wxChar *first ...) ); | |
1075 | #else | |
1076 | wxStringList(); | |
9d2f3c71 | 1077 | wxStringList(const wxChar *first ...); |
b1d4dd7a | 1078 | #endif |
fd3f686c | 1079 | |
db9504c5 VZ |
1080 | // copying the string list: the strings are copied, too (extremely |
1081 | // inefficient!) | |
6788ecb8 | 1082 | wxStringList(const wxStringList& other) : wxStringListBase() { DeleteContents(TRUE); DoCopy(other); } |
db9504c5 VZ |
1083 | wxStringList& operator=(const wxStringList& other) |
1084 | { Clear(); DoCopy(other); return *this; } | |
1085 | ||
fd3f686c VZ |
1086 | // operations |
1087 | // makes a copy of the string | |
f526f752 | 1088 | wxNode *Add(const wxChar *s); |
890f8a7c RR |
1089 | |
1090 | // Append to beginning of list | |
f526f752 | 1091 | wxNode *Prepend(const wxChar *s); |
fd3f686c | 1092 | |
9d2f3c71 | 1093 | bool Delete(const wxChar *s); |
fd3f686c | 1094 | |
9d2f3c71 OK |
1095 | wxChar **ListToArray(bool new_copies = FALSE) const; |
1096 | bool Member(const wxChar *s) const; | |
fd3f686c VZ |
1097 | |
1098 | // alphabetic sort | |
1099 | void Sort(); | |
1100 | ||
db9504c5 VZ |
1101 | private: |
1102 | void DoCopy(const wxStringList&); // common part of copy ctor and operator= | |
f98bd52a VZ |
1103 | |
1104 | DECLARE_DYNAMIC_CLASS(wxStringList) | |
c801d85f KB |
1105 | }; |
1106 | ||
df5168c4 MB |
1107 | #else // if wxUSE_STL |
1108 | ||
1109 | WX_DECLARE_LIST_XO(wxString, wxStringListBase, class WXDLLEXPORT); | |
1110 | ||
1111 | class WXDLLEXPORT wxStringList : public wxStringListBase | |
1112 | { | |
1113 | public: | |
fd82f4e6 MB |
1114 | compatibility_iterator Append(wxChar* s) |
1115 | { wxString tmp = s; delete[] s; return wxStringListBase::Append(tmp); } | |
1116 | compatibility_iterator Insert(wxChar* s) | |
1117 | { wxString tmp = s; delete[] s; return wxStringListBase::Insert(tmp); } | |
1118 | compatibility_iterator Insert(size_t pos, wxChar* s) | |
1119 | { | |
1120 | wxString tmp = s; | |
1121 | delete[] s; | |
1122 | return wxStringListBase::Insert(pos, tmp); | |
1123 | } | |
1124 | compatibility_iterator Add(const wxChar* s) | |
1125 | { push_back(s); return GetLast(); } | |
1126 | compatibility_iterator Prepend(const wxChar* s) | |
1127 | { push_front(s); return GetFirst(); } | |
df5168c4 MB |
1128 | }; |
1129 | ||
1130 | #endif // wxUSE_STL | |
1131 | ||
fd3f686c VZ |
1132 | #endif // wxLIST_COMPATIBILITY |
1133 | ||
df5168c4 MB |
1134 | // delete all list elements |
1135 | // | |
1136 | // NB: the class declaration of the list elements must be visible from the | |
1137 | // place where you use this macro, otherwise the proper destructor may not | |
1138 | // be called (a decent compiler should give a warning about it, but don't | |
1139 | // count on it)! | |
1140 | #define WX_CLEAR_LIST(type, list) \ | |
1141 | { \ | |
1142 | type::iterator it, en; \ | |
1143 | for( it = (list).begin(), en = (list).end(); it != en; ++it ) \ | |
1144 | delete *it; \ | |
1145 | (list).clear(); \ | |
1146 | } | |
1147 | ||
c801d85f | 1148 | #endif |
34138703 | 1149 | // _WX_LISTH__ |