1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxList, wxStringList classes
4 // Author: Julian Smart
5 // Modified by: VZ at 16/11/98: WX_DECLARE_LIST() and typesafe lists added
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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
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
28 // -----------------------------------------------------------------------------
30 // -----------------------------------------------------------------------------
33 #include "wx/object.h"
34 #include "wx/string.h"
37 #include "wx/beforestd.h"
41 #include "wx/afterstd.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class WXDLLIMPEXP_FWD_BASE wxObjectListNode
;
49 typedef wxObjectListNode wxNode
;
53 #define wxLIST_COMPATIBILITY
55 #define WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl) \
56 WX_DECLARE_LIST_WITH_DECL(elT, liT, decl)
57 #define WX_DECLARE_LIST_PTR_3(elT, dummy1, liT, dummy2, decl) \
58 WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl)
60 #define WX_DECLARE_LIST_2(elT, liT, dummy, decl) \
61 WX_DECLARE_LIST_WITH_DECL(elT, liT, decl)
62 #define WX_DECLARE_LIST_PTR_2(elT, liT, dummy, decl) \
63 WX_DECLARE_LIST_2(elT, liT, dummy, decl) \
65 #define WX_DECLARE_LIST_WITH_DECL(elT, liT, decl) \
66 WX_DECLARE_LIST_XO(elT*, liT, decl)
68 #if !defined(__VISUALC__) || __VISUALC__ >= 1300 // == !VC6
71 class wxList_SortFunction
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; }
78 wxSortCompareFunction m_f
;
81 #define WX_LIST_SORTFUNCTION( elT, f ) wxList_SortFunction<elT>(f)
82 #define WX_LIST_VC6_WORKAROUND(elT, liT, decl)
84 #else // if defined( __VISUALC__ ) && __VISUALC__ < 1300 // == VC6
86 #define WX_LIST_SORTFUNCTION( elT, f ) std::greater<elT>( f )
87 #define WX_LIST_VC6_WORKAROUND(elT, liT, decl) \
90 /* Workaround for broken VC6 STL incorrectly requires a std::greater<> */ \
91 /* to be passed into std::list::sort() */ \
93 struct std::greater<elT> \
96 wxSortCompareFunction m_CompFunc; \
98 greater( wxSortCompareFunction compfunc = NULL ) \
99 : m_CompFunc( compfunc ) {} \
100 bool operator()(const elT X, const elT Y) const \
102 return m_CompFunc ? \
103 ( m_CompFunc( wxListCastElementToVoidPtr(X), \
104 wxListCastElementToVoidPtr(Y) ) < 0 ) : \
109 // helper for std::greater<elT> above:
111 inline const void *wxListCastElementToVoidPtr(const T
* ptr
) { return ptr
; }
112 inline const void *wxListCastElementToVoidPtr(const wxString
& str
)
113 { return (const char*)str
; }
118 Note 1: the outer helper class _WX_LIST_HELPER_##liT below is a workaround
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:
122 struct WXDLLIMPEXP_CORE Foo
125 struct SomeInnerClass
127 friend class Foo; // comment this out to make it link
135 The program does not link under mingw_gcc 3.2.3 producing undefined
136 reference to Foo::Bar() function
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
146 // the real wxList-class declaration
147 #define WX_DECLARE_LIST_XO(elT, liT, decl) \
148 decl _WX_LIST_HELPER_##liT \
150 typedef elT _WX_LIST_ITEM_TYPE_##liT; \
152 static void DeleteFunction( _WX_LIST_ITEM_TYPE_##liT X ); \
155 WX_LIST_VC6_WORKAROUND(elT, liT, decl) \
156 decl liT : public std::list<elT> \
159 typedef std::list<elT> BaseListType; \
160 static BaseListType EmptyList; \
165 decl compatibility_iterator \
168 /* Workaround for broken VC6 nested class name resolution */ \
169 typedef std::list<elT>::iterator iterator; \
176 compatibility_iterator() \
177 : m_iter(EmptyList.end()), m_list( NULL ) {} \
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 ) ) {} \
183 compatibility_iterator* operator->() { return this; } \
184 const compatibility_iterator* operator->() const { return this; } \
186 bool operator==(const compatibility_iterator& i) const \
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); \
192 bool operator!=(const compatibility_iterator& i) const \
193 { return !( operator==( i ) ); } \
194 operator bool() const \
195 { return m_list ? m_iter != m_list->end() : false; } \
196 bool operator !() const \
197 { return !( operator bool() ); } \
199 elT GetData() const \
200 { return *m_iter; } \
201 void SetData( elT e ) \
204 compatibility_iterator GetNext() const \
206 iterator i = m_iter; \
207 return compatibility_iterator( m_list, ++i ); \
209 compatibility_iterator GetPrevious() const \
211 if ( m_iter == m_list->begin() ) \
212 return compatibility_iterator(); \
214 iterator i = m_iter; \
215 return compatibility_iterator( m_list, --i ); \
217 int IndexOf() const \
219 return *this ? std::distance( m_list->begin(), m_iter ) \
224 liT() : m_destroy( false ) {} \
226 compatibility_iterator Find( const elT e ) const \
228 liT* _this = const_cast< liT* >( this ); \
229 return compatibility_iterator( _this, \
230 std::find( _this->begin(), _this->end(), e ) ); \
233 bool IsEmpty() const \
234 { return empty(); } \
235 size_t GetCount() const \
238 { return static_cast< int >( GetCount() ); } \
240 compatibility_iterator Item( size_t idx ) const \
242 iterator i = const_cast< liT* >(this)->begin(); \
243 std::advance( i, idx ); \
244 return compatibility_iterator( this, i ); \
246 elT operator[](size_t idx) const \
248 return Item(idx).GetData(); \
251 compatibility_iterator GetFirst() const \
253 return compatibility_iterator( this, \
254 const_cast< liT* >(this)->begin() ); \
256 compatibility_iterator GetLast() const \
258 iterator i = const_cast< liT* >(this)->end(); \
259 return compatibility_iterator( this, !empty() ? --i : i ); \
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(); } \
268 compatibility_iterator Append( elT e ) \
273 compatibility_iterator Insert( elT e ) \
276 return compatibility_iterator( this, begin() ); \
278 compatibility_iterator Insert( compatibility_iterator & i, elT e ) \
280 return compatibility_iterator( this, insert( i.m_iter, e ) ); \
282 compatibility_iterator Insert( size_t idx, elT e ) \
284 return compatibility_iterator( this, \
285 insert( Item( idx ).m_iter, e ) ); \
288 void DeleteContents( bool destroy ) \
289 { m_destroy = destroy; } \
290 bool GetDeleteContents() const \
291 { return m_destroy; } \
292 void Erase( const compatibility_iterator& i ) \
295 _WX_LIST_HELPER_##liT::DeleteFunction( i->GetData() ); \
298 bool DeleteNode( const compatibility_iterator& i ) \
307 bool DeleteObject( elT e ) \
309 return DeleteNode( Find( e ) ); \
314 std::for_each( begin(), end(), \
315 _WX_LIST_HELPER_##liT::DeleteFunction ); \
318 /* Workaround for broken VC6 std::list::sort() see above */ \
319 void Sort( wxSortCompareFunction compfunc ) \
320 { sort( WX_LIST_SORTFUNCTION( elT, compfunc ) ); } \
321 ~liT() { Clear(); } \
323 /* It needs access to our EmptyList */ \
324 friend class compatibility_iterator; \
327 #define WX_DECLARE_LIST(elementtype, listname) \
328 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class)
329 #define WX_DECLARE_LIST_PTR(elementtype, listname) \
330 WX_DECLARE_LIST(elementtype, listname)
332 #define WX_DECLARE_EXPORTED_LIST(elementtype, listname) \
333 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class WXDLLIMPEXP_CORE)
334 #define WX_DECLARE_EXPORTED_LIST_PTR(elementtype, listname) \
335 WX_DECLARE_EXPORTED_LIST(elementtype, listname)
337 #define WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) \
338 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class usergoo)
339 #define WX_DECLARE_USER_EXPORTED_LIST_PTR(elementtype, listname, usergoo) \
340 WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo)
342 // this macro must be inserted in your program after
343 // #include "wx/listimpl.cpp"
344 #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
346 #define WX_DEFINE_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
347 #define WX_DEFINE_USER_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
349 #else // if !wxUSE_STL
352 // undef it to get rid of old, deprecated functions
353 #define wxLIST_COMPATIBILITY
355 // -----------------------------------------------------------------------------
356 // key stuff: a list may be optionally keyed on integer or string key
357 // -----------------------------------------------------------------------------
365 // a struct which may contain both types of keys
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 *).
371 class WXDLLIMPEXP_BASE wxListKey
375 wxListKey() : m_keyType(wxKEY_NONE
)
377 wxListKey(long i
) : m_keyType(wxKEY_INTEGER
)
378 { m_key
.integer
= i
; }
379 wxListKey(const wxString
& s
) : m_keyType(wxKEY_STRING
)
380 { m_key
.string
= new wxString(s
); }
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
); }
387 wxKeyType
GetKeyType() const { return m_keyType
; }
388 const wxString
GetString() const
389 { wxASSERT( m_keyType
== wxKEY_STRING
); return *m_key
.string
; }
390 long GetNumber() const
391 { wxASSERT( m_keyType
== wxKEY_INTEGER
); return m_key
.integer
; }
394 // Note: implementation moved to list.cpp to prevent BC++ inline
395 // expansion warning.
396 bool operator==(wxListKeyValue value
) const ;
401 if ( m_keyType
== wxKEY_STRING
)
407 wxListKeyValue m_key
;
410 // -----------------------------------------------------------------------------
411 // wxNodeBase class is a (base for) node in a double linked list
412 // -----------------------------------------------------------------------------
414 extern WXDLLIMPEXP_DATA_BASE(wxListKey
) wxDefaultListKey
;
416 class WXDLLIMPEXP_FWD_BASE wxListBase
;
418 class WXDLLIMPEXP_BASE wxNodeBase
420 friend class wxListBase
;
423 wxNodeBase(wxListBase
*list
= (wxListBase
*)NULL
,
424 wxNodeBase
*previous
= (wxNodeBase
*)NULL
,
425 wxNodeBase
*next
= (wxNodeBase
*)NULL
,
427 const wxListKey
& key
= wxDefaultListKey
);
429 virtual ~wxNodeBase();
431 // FIXME no check is done that the list is really keyed on strings
432 wxString
GetKeyString() const { return *m_key
.string
; }
433 long GetKeyInteger() const { return m_key
.integer
; }
435 // Necessary for some existing code
436 void SetKeyString(const wxString
& s
) { m_key
.string
= new wxString(s
); }
437 void SetKeyInteger(long i
) { m_key
.integer
= i
; }
439 #ifdef wxLIST_COMPATIBILITY
440 // compatibility methods, use Get* instead.
441 wxDEPRECATED( wxNode
*Next() const );
442 wxDEPRECATED( wxNode
*Previous() const );
443 wxDEPRECATED( wxObject
*Data() const );
444 #endif // wxLIST_COMPATIBILITY
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
; }
451 void *GetData() const { return m_data
; }
452 void SetData(void *data
) { m_data
= data
; }
454 // get 0-based index of this node within the list or wxNOT_FOUND
457 virtual void DeleteData() { }
459 // for wxList::iterator
460 void** GetDataPtr() const { return &(wx_const_cast(wxNodeBase
*, this)->m_data
); }
462 // optional key stuff
463 wxListKeyValue m_key
;
465 void *m_data
; // user data
466 wxNodeBase
*m_next
, // next and previous nodes in the list
469 wxListBase
*m_list
; // list we belong to
471 DECLARE_NO_COPY_CLASS(wxNodeBase
)
474 // -----------------------------------------------------------------------------
475 // a double-linked list class
476 // -----------------------------------------------------------------------------
478 class WXDLLIMPEXP_FWD_BASE wxList
;
480 class WXDLLIMPEXP_BASE wxListBase
482 friend class wxNodeBase
; // should be able to call DetachNode()
483 friend class wxHashTableBase
; // should be able to call untyped Find()
486 // default ctor & dtor
487 wxListBase(wxKeyType keyType
= wxKEY_NONE
)
489 virtual ~wxListBase();
492 // count of items in the list
493 size_t GetCount() const { return m_count
; }
495 // return true if this list is empty
496 bool IsEmpty() const { return m_count
== 0; }
503 // instruct it to destroy user data when deleting nodes
504 void DeleteContents(bool destroy
) { m_destroy
= destroy
; }
506 // query if to delete
507 bool GetDeleteContents() const
508 { return m_destroy
; }
511 wxKeyType
GetKeyType() const
512 { return m_keyType
; }
514 // set the keytype (required by the serial code)
515 void SetKeyType(wxKeyType keyType
)
516 { wxASSERT( m_count
==0 ); m_keyType
= keyType
; }
518 #ifdef wxLIST_COMPATIBILITY
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
525 // kludge for typesafe list migration in core classes.
526 wxDEPRECATED( operator wxList
&() const );
527 #endif // wxLIST_COMPATIBILITY
531 // all methods here are "overloaded" in derived classes to provide compile
532 // time type checking
534 // create a node for the list of this type
535 virtual wxNodeBase
*CreateNode(wxNodeBase
*prev
, wxNodeBase
*next
,
537 const wxListKey
& key
= wxDefaultListKey
) = 0;
542 wxListBase(size_t count
, void *elements
[]);
543 // from a sequence of objects
544 wxListBase(void *object
, ... /* terminate with NULL */);
547 void Assign(const wxListBase
& list
)
548 { Clear(); DoCopy(list
); }
550 // get list head/tail
551 wxNodeBase
*GetFirst() const { return m_nodeFirst
; }
552 wxNodeBase
*GetLast() const { return m_nodeLast
; }
554 // by (0-based) index
555 wxNodeBase
*Item(size_t index
) const;
557 // get the list item's data
558 void *operator[](size_t n
) const
560 wxNodeBase
*node
= Item(n
);
562 return node
? node
->GetData() : (wxNodeBase
*)NULL
;
566 // append to end of list
567 wxNodeBase
*Prepend(void *object
)
568 { return (wxNodeBase
*)wxListBase::Insert(object
); }
569 // append to beginning of list
570 wxNodeBase
*Append(void *object
);
571 // insert a new item at the beginning of the list
572 wxNodeBase
*Insert(void *object
) { return Insert( (wxNodeBase
*)NULL
, object
); }
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
); }
577 // insert before given node or at front of list if prev == NULL
578 wxNodeBase
*Insert(wxNodeBase
*prev
, void *object
);
581 wxNodeBase
*Append(long key
, void *object
);
582 wxNodeBase
*Append(const wxString
& key
, void *object
);
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
);
587 // delete element from list, returns false if node not found
588 bool DeleteNode(wxNodeBase
*node
);
589 // finds object pointer and deletes node (and object if DeleteContents
590 // is on), returns false if object not found
591 bool DeleteObject(void *object
);
593 // search (all return NULL if item not found)
595 wxNodeBase
*Find(const void *object
) const;
598 wxNodeBase
*Find(const wxListKey
& key
) const;
600 // get 0-based index of object or wxNOT_FOUND
601 int IndexOf( void *object
) const;
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.
605 void Sort(const wxSortCompareFunction compfunc
);
607 // functions for iterating over the list
608 void *FirstThat(wxListIterateFunction func
);
609 void ForEach(wxListIterateFunction func
);
610 void *LastThat(wxListIterateFunction func
);
612 // for STL interface, "last" points to one after the last node
613 // of the controlled sequence (NULL for the end of the list)
615 void DeleteNodes(wxNodeBase
* first
, wxNodeBase
* last
);
618 // common part of all ctors
619 void Init(wxKeyType keyType
= wxKEY_NONE
);
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
);
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
634 wxKeyType m_keyType
; // type of our keys (may be wxKEY_NONE)
637 // -----------------------------------------------------------------------------
638 // macros for definition of "template" list type
639 // -----------------------------------------------------------------------------
641 // and now some heavy magic...
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
645 // wxNode-derived type)
647 // implementation details:
648 // 1. We define _WX_LIST_ITEM_TYPE_##name typedef to save in it the item type
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
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
654 // time error checking.
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.
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)
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) \
671 typedef int (*wxSortFuncFor_##name)(const T **, const T **); \
673 classexp nodetype : public wxNodeBase \
676 nodetype(wxListBase *list = (wxListBase *)NULL, \
677 nodetype *previous = (nodetype *)NULL, \
678 nodetype *next = (nodetype *)NULL, \
679 T *data = (T *)NULL, \
680 const wxListKey& key = wxDefaultListKey) \
681 : wxNodeBase(list, previous, next, data, key) { } \
683 nodetype *GetNext() const \
684 { return (nodetype *)wxNodeBase::GetNext(); } \
685 nodetype *GetPrevious() const \
686 { return (nodetype *)wxNodeBase::GetPrevious(); } \
689 { return (T *)wxNodeBase::GetData(); } \
690 void SetData(T *data) \
691 { wxNodeBase::SetData(data); } \
694 virtual void DeleteData(); \
696 DECLARE_NO_COPY_CLASS(nodetype) \
699 classexp name : public wxListBase \
702 typedef nodetype Node; \
703 classexp compatibility_iterator \
706 compatibility_iterator(Node *ptr = NULL) : m_ptr(ptr) { } \
708 Node *operator->() const { return m_ptr; } \
709 operator Node *() const { return m_ptr; } \
715 name(wxKeyType keyType = wxKEY_NONE) : wxListBase(keyType) \
717 name(const name& list) : wxListBase(list.GetKeyType()) \
719 name(size_t count, T *elements[]) \
720 : wxListBase(count, (void **)elements) { } \
722 name& operator=(const name& list) \
723 { if (&list != this) Assign(list); return *this; } \
725 nodetype *GetFirst() const \
726 { return (nodetype *)wxListBase::GetFirst(); } \
727 nodetype *GetLast() const \
728 { return (nodetype *)wxListBase::GetLast(); } \
730 nodetype *Item(size_t index) const \
731 { return (nodetype *)wxListBase::Item(index); } \
733 T *operator[](size_t index) const \
735 nodetype *node = Item(index); \
736 return node ? (T*)(node->GetData()) : (T*)NULL; \
739 nodetype *Append(Tbase *object) \
740 { return (nodetype *)wxListBase::Append(object); } \
741 nodetype *Insert(Tbase *object) \
742 { return (nodetype *)Insert((nodetype*)NULL, object); } \
743 nodetype *Insert(size_t pos, Tbase *object) \
744 { return (nodetype *)wxListBase::Insert(pos, object); } \
745 nodetype *Insert(nodetype *prev, Tbase *object) \
746 { return (nodetype *)wxListBase::Insert(prev, object); } \
748 nodetype *Append(long key, void *object) \
749 { return (nodetype *)wxListBase::Append(key, object); } \
750 nodetype *Append(const wxChar *key, void *object) \
751 { return (nodetype *)wxListBase::Append(key, object); } \
753 nodetype *DetachNode(nodetype *node) \
754 { return (nodetype *)wxListBase::DetachNode(node); } \
755 bool DeleteNode(nodetype *node) \
756 { return wxListBase::DeleteNode(node); } \
757 bool DeleteObject(Tbase *object) \
758 { return wxListBase::DeleteObject(object); } \
759 void Erase(nodetype *it) \
760 { DeleteNode(it); } \
762 nodetype *Find(const Tbase *object) const \
763 { return (nodetype *)wxListBase::Find(object); } \
765 virtual nodetype *Find(const wxListKey& key) const \
766 { return (nodetype *)wxListBase::Find(key); } \
768 int IndexOf(Tbase *object) const \
769 { return wxListBase::IndexOf(object); } \
771 void Sort(wxSortCompareFunction func) \
772 { wxListBase::Sort(func); } \
773 void Sort(wxSortFuncFor_##name func) \
774 { Sort((wxSortCompareFunction)func); } \
777 virtual wxNodeBase *CreateNode(wxNodeBase *prev, wxNodeBase *next, \
779 const wxListKey& key = wxDefaultListKey) \
781 return new nodetype(this, \
782 (nodetype *)prev, (nodetype *)next, \
785 /* STL interface */ \
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; \
800 typedef nodetype Node; \
801 typedef iterator itor; \
802 typedef T* value_type; \
803 typedef value_type* ptr_type; \
804 typedef value_type& reference; \
809 typedef reference reference_type; \
810 typedef ptr_type pointer_type; \
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(); } \
817 itor& operator++() { m_node = m_node->GetNext(); return *this; }\
818 const itor operator++(int) \
819 { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }\
822 m_node = m_node ? m_node->GetPrevious() : m_init; \
825 const itor operator--(int) \
828 m_node = m_node ? m_node->GetPrevious() : m_init; \
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; } \
836 classexp const_iterator \
840 typedef nodetype Node; \
841 typedef T* value_type; \
842 typedef const value_type& const_reference; \
843 typedef const_iterator itor; \
844 typedef value_type* ptr_type; \
849 typedef const_reference reference_type; \
850 typedef const ptr_type pointer_type; \
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(); } \
860 itor& operator++() { m_node = m_node->GetNext(); return *this; }\
861 const itor operator++(int) \
862 { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }\
865 m_node = m_node ? m_node->GetPrevious() : m_init; \
868 const itor operator--(int) \
871 m_node = m_node ? m_node->GetPrevious() : m_init; \
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; } \
879 classexp reverse_iterator \
883 typedef nodetype Node; \
884 typedef T* value_type; \
885 typedef reverse_iterator itor; \
886 typedef value_type* ptr_type; \
887 typedef value_type& reference; \
892 typedef reference reference_type; \
893 typedef ptr_type pointer_type; \
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(); } \
902 { m_node = m_node->GetPrevious(); return *this; } \
903 const itor operator++(int) \
904 { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }\
906 { m_node = m_node ? m_node->GetNext() : m_init; return *this; } \
907 const itor operator--(int) \
910 m_node = m_node ? m_node->GetNext() : m_init; \
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; } \
918 classexp const_reverse_iterator \
922 typedef nodetype Node; \
923 typedef T* value_type; \
924 typedef const_reverse_iterator itor; \
925 typedef value_type* ptr_type; \
926 typedef const value_type& const_reference; \
931 typedef const_reference reference_type; \
932 typedef const ptr_type pointer_type; \
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(); } \
943 { m_node = m_node->GetPrevious(); return *this; } \
944 const itor operator++(int) \
945 { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }\
947 { m_node = m_node ? m_node->GetNext() : m_init; return *this;}\
948 const itor operator--(int) \
951 m_node = m_node ? m_node->GetNext() : m_init; \
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; } \
960 wxEXPLICIT name(size_type n, const_reference v = value_type()) \
962 name(const const_iterator& first, const const_iterator& last) \
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()) \
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(); } \
988 reference back() { iterator tmp = end(); return *--tmp; } \
989 const_reference back() const { const_iterator tmp = end(); return *--tmp; }\
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()); } \
996 void assign(const_iterator first, const const_iterator& last) \
999 for(; first != last; ++first) \
1000 Append((const_base_reference)*first); \
1002 void assign(size_type n, const_reference v = value_type()) \
1005 for(size_type i = 0; i < n; ++i) \
1006 Append((const_base_reference)v); \
1008 iterator insert(const iterator& it, const_reference v = value_type())\
1010 Insert(it.m_node, (const_base_reference)v); \
1011 iterator itprev(it); \
1014 void insert(const iterator& it, size_type n, const_reference v = value_type())\
1016 for(size_type i = 0; i < n; ++i) \
1017 Insert(it.m_node, (const_base_reference)v); \
1019 void insert(const iterator& it, const_iterator first, const const_iterator& last)\
1021 for(; first != last; ++first) \
1022 Insert(it.m_node, (const_base_reference)*first); \
1024 iterator erase(const iterator& it) \
1026 iterator next = iterator(it.m_node->GetNext(), GetLast()); \
1027 DeleteNode(it.m_node); return next; \
1029 iterator erase(const iterator& first, const iterator& last) \
1031 iterator next = last; ++next; \
1032 DeleteNodes(first.m_node, last.m_node); \
1035 void clear() { Clear(); } \
1036 void splice(const iterator& it, name& l, const iterator& first, const iterator& last)\
1037 { insert(it, first, last); l.erase(first, last); } \
1038 void splice(const iterator& it, name& l) \
1039 { splice(it, l, l.begin(), l.end() ); } \
1040 void splice(const iterator& it, name& l, const iterator& first) \
1042 iterator tmp = first; ++tmp; \
1043 if(it == first || it == tmp) return; \
1044 insert(it, *first); \
1047 void remove(const_reference v) \
1048 { DeleteObject((const_base_reference)v); } \
1051 /* void swap(name& l) \
1053 { size_t t = m_count; m_count = l.m_count; l.m_count = t; } \
1054 { bool t = m_destroy; m_destroy = l.m_destroy; l.m_destroy = t; }\
1055 { wxNodeBase* t = m_nodeFirst; m_nodeFirst = l.m_nodeFirst; l.m_nodeFirst = t; }\
1056 { wxNodeBase* t = m_nodeLast; m_nodeLast = l.m_nodeLast; l.m_nodeLast = t; }\
1057 { wxKeyType t = m_keyType; m_keyType = l.m_keyType; l.m_keyType = t; }\
1061 #define WX_LIST_PTROP \
1062 pointer_type operator->() const \
1063 { return (pointer_type)m_node->GetDataPtr(); }
1064 #define WX_LIST_PTROP_NONE
1066 #define WX_DECLARE_LIST_3(T, Tbase, name, nodetype, classexp) \
1067 WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, WX_LIST_PTROP_NONE)
1068 #define WX_DECLARE_LIST_PTR_3(T, Tbase, name, nodetype, classexp) \
1069 WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, WX_LIST_PTROP)
1071 #define WX_DECLARE_LIST_2(elementtype, listname, nodename, classexp) \
1072 WX_DECLARE_LIST_3(elementtype, elementtype, listname, nodename, classexp)
1073 #define WX_DECLARE_LIST_PTR_2(elementtype, listname, nodename, classexp) \
1074 WX_DECLARE_LIST_PTR_3(elementtype, elementtype, listname, nodename, classexp)
1076 #define WX_DECLARE_LIST(elementtype, listname) \
1077 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1078 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class)
1079 #define WX_DECLARE_LIST_PTR(elementtype, listname) \
1080 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1081 WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class)
1083 #define WX_DECLARE_LIST_WITH_DECL(elementtype, listname, decl) \
1084 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1085 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, decl)
1087 #define WX_DECLARE_EXPORTED_LIST(elementtype, listname) \
1088 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class WXDLLIMPEXP_CORE)
1090 #define WX_DECLARE_EXPORTED_LIST_PTR(elementtype, listname) \
1091 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1092 WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class WXDLLIMPEXP_CORE)
1094 #define WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) \
1095 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1096 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class usergoo)
1097 #define WX_DECLARE_USER_EXPORTED_LIST_PTR(elementtype, listname, usergoo) \
1098 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1099 WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class usergoo)
1101 // this macro must be inserted in your program after
1102 // #include "wx/listimpl.cpp"
1103 #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
1105 #define WX_DEFINE_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
1106 #define WX_DEFINE_USER_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
1108 #endif // !wxUSE_STL
1110 // ============================================================================
1111 // now we can define classes 100% compatible with the old ones
1112 // ============================================================================
1114 // ----------------------------------------------------------------------------
1115 // commonly used list classes
1116 // ----------------------------------------------------------------------------
1118 #if defined(wxLIST_COMPATIBILITY)
1120 // inline compatibility functions
1124 // ----------------------------------------------------------------------------
1125 // wxNodeBase deprecated methods
1126 // ----------------------------------------------------------------------------
1128 inline wxNode
*wxNodeBase::Next() const { return (wxNode
*)GetNext(); }
1129 inline wxNode
*wxNodeBase::Previous() const { return (wxNode
*)GetPrevious(); }
1130 inline wxObject
*wxNodeBase::Data() const { return (wxObject
*)GetData(); }
1132 // ----------------------------------------------------------------------------
1133 // wxListBase deprecated methods
1134 // ----------------------------------------------------------------------------
1136 inline int wxListBase::Number() const { return (int)GetCount(); }
1137 inline wxNode
*wxListBase::First() const { return (wxNode
*)GetFirst(); }
1138 inline wxNode
*wxListBase::Last() const { return (wxNode
*)GetLast(); }
1139 inline wxNode
*wxListBase::Nth(size_t n
) const { return (wxNode
*)Item(n
); }
1140 inline wxListBase::operator wxList
&() const { return *(wxList
*)this; }
1144 // define this to make a lot of noise about use of the old wxList classes.
1145 //#define wxWARN_COMPAT_LIST_USE
1147 // ----------------------------------------------------------------------------
1148 // wxList compatibility class: in fact, it's a list of wxObjects
1149 // ----------------------------------------------------------------------------
1151 WX_DECLARE_LIST_2(wxObject
, wxObjectList
, wxObjectListNode
,
1152 class WXDLLIMPEXP_BASE
);
1154 class WXDLLIMPEXP_BASE wxList
: public wxObjectList
1157 #if defined(wxWARN_COMPAT_LIST_USE) && !wxUSE_STL
1159 wxDEPRECATED( wxList(int key_type
) );
1161 wxList(int key_type
= wxKEY_NONE
);
1164 // this destructor is required for Darwin
1168 wxList
& operator=(const wxList
& list
)
1169 { if (&list
!= this) Assign(list
); return *this; }
1171 // compatibility methods
1172 void Sort(wxSortCompareFunction compfunc
) { wxListBase::Sort(compfunc
); }
1177 wxNode
*Member(wxObject
*object
) const { return (wxNode
*)Find(object
); }
1183 // -----------------------------------------------------------------------------
1184 // wxStringList class for compatibility with the old code
1185 // -----------------------------------------------------------------------------
1186 WX_DECLARE_LIST_2(wxChar
, wxStringListBase
, wxStringListNode
, class WXDLLIMPEXP_BASE
);
1188 class WXDLLIMPEXP_BASE wxStringList
: public wxStringListBase
1193 #ifdef wxWARN_COMPAT_LIST_USE
1195 wxDEPRECATED( wxStringList(const wxChar
*first
...) ); // FIXME-UTF8
1198 wxStringList(const wxChar
*first
...); // FIXME-UTF8
1201 // copying the string list: the strings are copied, too (extremely
1203 wxStringList(const wxStringList
& other
) : wxStringListBase() { DeleteContents(true); DoCopy(other
); }
1204 wxStringList
& operator=(const wxStringList
& other
)
1215 // makes a copy of the string
1216 wxNode
*Add(const wxChar
*s
);
1218 // Append to beginning of list
1219 wxNode
*Prepend(const wxChar
*s
);
1221 bool Delete(const wxChar
*s
);
1223 wxChar
**ListToArray(bool new_copies
= false) const;
1224 bool Member(const wxChar
*s
) const;
1230 void DoCopy(const wxStringList
&); // common part of copy ctor and operator=
1233 #else // if wxUSE_STL
1235 WX_DECLARE_LIST_XO(wxString
, wxStringListBase
, class WXDLLIMPEXP_BASE
);
1237 class WXDLLIMPEXP_BASE wxStringList
: public wxStringListBase
1240 compatibility_iterator
Append(wxChar
* s
)
1241 { wxString tmp
= s
; delete[] s
; return wxStringListBase::Append(tmp
); }
1242 compatibility_iterator
Insert(wxChar
* s
)
1243 { wxString tmp
= s
; delete[] s
; return wxStringListBase::Insert(tmp
); }
1244 compatibility_iterator
Insert(size_t pos
, wxChar
* s
)
1248 return wxStringListBase::Insert(pos
, tmp
);
1250 compatibility_iterator
Add(const wxChar
* s
)
1251 { push_back(s
); return GetLast(); }
1252 compatibility_iterator
Prepend(const wxChar
* s
)
1253 { push_front(s
); return GetFirst(); }
1258 #endif // wxLIST_COMPATIBILITY
1260 // delete all list elements
1262 // NB: the class declaration of the list elements must be visible from the
1263 // place where you use this macro, otherwise the proper destructor may not
1264 // be called (a decent compiler should give a warning about it, but don't
1266 #define WX_CLEAR_LIST(type, list) \
1268 type::iterator it, en; \
1269 for( it = (list).begin(), en = (list).end(); it != en; ++it ) \
1274 #endif // _WX_LISTH__