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