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 // type of compare function for list sort operation (as in 'qsort'): it should
49 // return a negative value, 0 or positive value if the first element is less
50 // than, equal or greater than the second
54 typedef int (* LINKAGEMODE wxSortCompareFunction
)(const void *elem1
, const void *elem2
);
57 class WXDLLIMPEXP_BASE wxObjectListNode
;
58 typedef wxObjectListNode wxNode
;
61 typedef int (* LINKAGEMODE wxListIterateFunction
)(void *current
);
65 #define wxLIST_COMPATIBILITY
67 #define WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl) \
68 WX_DECLARE_LIST_WITH_DECL(elT, liT, decl)
69 #define WX_DECLARE_LIST_PTR_3(elT, dummy1, liT, dummy2, decl) \
70 WX_DECLARE_LIST_3(elT, dummy1, liT, dummy2, decl)
72 #define WX_DECLARE_LIST_2(elT, liT, dummy, decl) \
73 WX_DECLARE_LIST_WITH_DECL(elT, liT, decl)
74 #define WX_DECLARE_LIST_PTR_2(elT, liT, dummy, decl) \
75 WX_DECLARE_LIST_2(elT, liT, dummy, decl) \
77 #define WX_DECLARE_LIST_WITH_DECL(elT, liT, decl) \
78 WX_DECLARE_LIST_XO(elT*, liT, decl)
80 #if !defined(__VISUALC__) || __VISUALC__ >= 1300 // == !VC6
83 class WXDLLIMPEXP_BASE wxList_SortFunction
86 wxList_SortFunction(wxSortCompareFunction f
) : m_f(f
) { }
87 bool operator()(const T
& i1
, const T
& i2
)
88 { return m_f((T
*)&i1
, (T
*)&i2
) < 0; }
90 wxSortCompareFunction m_f
;
93 #define WX_LIST_SORTFUNCTION( elT, f ) wxList_SortFunction<elT>(f)
94 #define VC6_WORKAROUND(elT, liT, decl)
96 #else // if defined( __VISUALC__ ) && __VISUALC__ < 1300 // == VC6
98 #define WX_LIST_SORTFUNCTION( elT, f ) std::greater<elT>( f )
99 #define VC6_WORKAROUND(elT, liT, decl) \
102 /* Workaround for broken VC6 STL incorrectly requires a std::greater<> */ \
103 /* to be passed into std::list::sort() */ \
105 struct std::greater<elT> \
108 wxSortCompareFunction m_CompFunc; \
110 greater( wxSortCompareFunction compfunc = NULL ) \
111 : m_CompFunc( compfunc ) {} \
112 bool operator()(const elT X, const elT Y) const \
114 return m_CompFunc ? \
115 ( m_CompFunc( wxListCastElementToVoidPtr(X), \
116 wxListCastElementToVoidPtr(Y) ) < 0 ) : \
121 // helper for std::greater<elT> above:
123 inline const void *wxListCastElementToVoidPtr(const T
* ptr
) { return ptr
; }
124 inline const void *wxListCastElementToVoidPtr(const wxString
& str
)
125 { return (const char*)str
; }
130 Note 1: the outer helper class _WX_LIST_HELPER_##liT below is a workaround
131 for mingw 3.2.3 compiler bug that prevents a static function of liT class
132 from being exported into dll. A minimal code snippet reproducing the bug:
134 struct WXDLLEXPORT Foo
137 struct SomeInnerClass
139 friend class Foo; // comment this out to make it link
147 The program does not link under mingw_gcc 3.2.3 producing undefined
148 reference to Foo::Bar() function
151 Note 2: the EmptyList is needed to allow having a NULL pointer-like
152 invalid iterator. We used to use just an uninitialized iterator object
153 instead but this fails with some debug/checked versions of STL, notably the
154 glibc version activated with _GLIBCXX_DEBUG, so we need to have a separate
158 // the real wxList-class declaration
159 #define WX_DECLARE_LIST_XO(elT, liT, decl) \
160 decl _WX_LIST_HELPER_##liT \
162 typedef elT _WX_LIST_ITEM_TYPE_##liT; \
164 static void DeleteFunction( _WX_LIST_ITEM_TYPE_##liT X ); \
167 VC6_WORKAROUND(elT, liT, decl) \
168 decl liT : public std::list<elT> \
171 typedef std::list<elT> BaseListType; \
172 static BaseListType EmptyList; \
177 decl compatibility_iterator \
180 /* Workaround for broken VC6 nested class name resolution */ \
181 typedef std::list<elT>::iterator iterator; \
188 compatibility_iterator() \
189 : m_iter(EmptyList.end()), m_list( NULL ) {} \
190 compatibility_iterator( liT* li, iterator i ) \
191 : m_iter( i ), m_list( li ) {} \
192 compatibility_iterator( const liT* li, iterator i ) \
193 : m_iter( i ), m_list( const_cast< liT* >( li ) ) {} \
195 compatibility_iterator* operator->() { return this; } \
196 const compatibility_iterator* operator->() const { return this; } \
198 bool operator==(const compatibility_iterator& i) const \
200 wxASSERT_MSG( m_list && i.m_list, \
201 _T("comparing invalid iterators is illegal") ); \
202 return (m_list == i.m_list) && (m_iter == i.m_iter); \
204 bool operator!=(const compatibility_iterator& i) const \
205 { return !( operator==( i ) ); } \
206 operator bool() const \
207 { return m_list ? m_iter != m_list->end() : false; } \
208 bool operator !() const \
209 { return !( operator bool() ); } \
211 elT GetData() const \
212 { return *m_iter; } \
213 void SetData( elT e ) \
216 compatibility_iterator GetNext() const \
218 iterator i = m_iter; \
219 return compatibility_iterator( m_list, ++i ); \
221 compatibility_iterator GetPrevious() const \
223 if ( m_iter == m_list->begin() ) \
224 return compatibility_iterator(); \
226 iterator i = m_iter; \
227 return compatibility_iterator( m_list, --i ); \
229 int IndexOf() const \
231 return *this ? std::distance( m_list->begin(), m_iter ) \
236 liT() : m_destroy( false ) {} \
238 compatibility_iterator Find( const elT e ) const \
240 liT* _this = const_cast< liT* >( this ); \
241 return compatibility_iterator( _this, \
242 std::find( _this->begin(), _this->end(), e ) ); \
245 bool IsEmpty() const \
246 { return empty(); } \
247 size_t GetCount() const \
250 { return static_cast< int >( GetCount() ); } \
252 compatibility_iterator Item( size_t idx ) const \
254 iterator i = const_cast< liT* >(this)->begin(); \
255 std::advance( i, idx ); \
256 return compatibility_iterator( this, i ); \
258 elT operator[](size_t idx) const \
260 return Item(idx).GetData(); \
263 compatibility_iterator GetFirst() const \
265 return compatibility_iterator( this, \
266 const_cast< liT* >(this)->begin() ); \
268 compatibility_iterator GetLast() const \
270 iterator i = const_cast< liT* >(this)->end(); \
271 return compatibility_iterator( this, !empty() ? --i : i ); \
273 compatibility_iterator Member( elT e ) const \
274 { return Find( e ); } \
275 compatibility_iterator Nth( int n ) const \
276 { return Item( n ); } \
277 int IndexOf( elT e ) const \
278 { return Find( e ).IndexOf(); } \
280 compatibility_iterator Append( elT e ) \
285 compatibility_iterator Insert( elT e ) \
288 return compatibility_iterator( this, begin() ); \
290 compatibility_iterator Insert( compatibility_iterator & i, elT e ) \
292 return compatibility_iterator( this, insert( i.m_iter, e ) ); \
294 compatibility_iterator Insert( size_t idx, elT e ) \
296 return compatibility_iterator( this, \
297 insert( Item( idx ).m_iter, e ) ); \
300 void DeleteContents( bool destroy ) \
301 { m_destroy = destroy; } \
302 bool GetDeleteContents() const \
303 { return m_destroy; } \
304 void Erase( const compatibility_iterator& i ) \
307 _WX_LIST_HELPER_##liT::DeleteFunction( i->GetData() ); \
310 bool DeleteNode( const compatibility_iterator& i ) \
319 bool DeleteObject( elT e ) \
321 return DeleteNode( Find( e ) ); \
326 std::for_each( begin(), end(), \
327 _WX_LIST_HELPER_##liT::DeleteFunction ); \
330 /* Workaround for broken VC6 std::list::sort() see above */ \
331 void Sort( wxSortCompareFunction compfunc ) \
332 { sort( WX_LIST_SORTFUNCTION( elT, compfunc ) ); } \
333 ~liT() { Clear(); } \
335 /* It needs access to our EmptyList */ \
336 friend decl compatibility_iterator; \
339 #define WX_DECLARE_LIST(elementtype, listname) \
340 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class)
341 #define WX_DECLARE_LIST_PTR(elementtype, listname) \
342 WX_DECLARE_LIST(elementtype, listname)
344 #define WX_DECLARE_EXPORTED_LIST(elementtype, listname) \
345 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class WXDLLEXPORT)
346 #define WX_DECLARE_EXPORTED_LIST_PTR(elementtype, listname) \
347 WX_DECLARE_EXPORTED_LIST(elementtype, listname)
349 #define WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) \
350 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class usergoo)
351 #define WX_DECLARE_USER_EXPORTED_LIST_PTR(elementtype, listname, usergoo) \
352 WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo)
354 // this macro must be inserted in your program after
355 // #include "wx/listimpl.cpp"
356 #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
358 #define WX_DEFINE_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
359 #define WX_DEFINE_USER_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
361 #else // if !wxUSE_STL
364 // undef it to get rid of old, deprecated functions
365 #define wxLIST_COMPATIBILITY
367 // -----------------------------------------------------------------------------
368 // key stuff: a list may be optionally keyed on integer or string key
369 // -----------------------------------------------------------------------------
377 // a struct which may contain both types of keys
379 // implementation note: on one hand, this class allows to have only one function
380 // for any keyed operation instead of 2 almost equivalent. OTOH, it's needed to
381 // resolve ambiguity which we would otherwise have with wxStringList::Find() and
382 // wxList::Find(const char *).
383 class WXDLLIMPEXP_BASE wxListKey
387 wxListKey() : m_keyType(wxKEY_NONE
)
389 wxListKey(long i
) : m_keyType(wxKEY_INTEGER
)
390 { m_key
.integer
= i
; }
391 wxListKey(const wxString
& s
) : m_keyType(wxKEY_STRING
)
392 { m_key
.string
= new wxString(s
); }
395 wxKeyType
GetKeyType() const { return m_keyType
; }
396 const wxString
GetString() const
397 { wxASSERT( m_keyType
== wxKEY_STRING
); return *m_key
.string
; }
398 long GetNumber() const
399 { wxASSERT( m_keyType
== wxKEY_INTEGER
); return m_key
.integer
; }
402 // Note: implementation moved to list.cpp to prevent BC++ inline
403 // expansion warning.
404 bool operator==(wxListKeyValue value
) const ;
409 if ( m_keyType
== wxKEY_STRING
)
415 wxListKeyValue m_key
;
418 // -----------------------------------------------------------------------------
419 // wxNodeBase class is a (base for) node in a double linked list
420 // -----------------------------------------------------------------------------
422 extern WXDLLIMPEXP_DATA_BASE(wxListKey
) wxDefaultListKey
;
424 class WXDLLIMPEXP_BASE wxListBase
;
426 class WXDLLIMPEXP_BASE wxNodeBase
428 friend class wxListBase
;
431 wxNodeBase(wxListBase
*list
= (wxListBase
*)NULL
,
432 wxNodeBase
*previous
= (wxNodeBase
*)NULL
,
433 wxNodeBase
*next
= (wxNodeBase
*)NULL
,
435 const wxListKey
& key
= wxDefaultListKey
);
437 virtual ~wxNodeBase();
439 // FIXME no check is done that the list is really keyed on strings
440 wxString
GetKeyString() const { return *m_key
.string
; }
441 long GetKeyInteger() const { return m_key
.integer
; }
443 // Necessary for some existing code
444 void SetKeyString(const wxString
& s
) { m_key
.string
= new wxString(s
); }
445 void SetKeyInteger(long i
) { m_key
.integer
= i
; }
447 #ifdef wxLIST_COMPATIBILITY
448 // compatibility methods, use Get* instead.
449 wxDEPRECATED( wxNode
*Next() const );
450 wxDEPRECATED( wxNode
*Previous() const );
451 wxDEPRECATED( wxObject
*Data() const );
452 #endif // wxLIST_COMPATIBILITY
455 // all these are going to be "overloaded" in the derived classes
456 wxNodeBase
*GetNext() const { return m_next
; }
457 wxNodeBase
*GetPrevious() const { return m_previous
; }
459 void *GetData() const { return m_data
; }
460 void SetData(void *data
) { m_data
= data
; }
462 // get 0-based index of this node within the list or wxNOT_FOUND
465 virtual void DeleteData() { }
467 // for wxList::iterator
468 void** GetDataPtr() const { return &(((wxNodeBase
*)this)->m_data
); }
470 // optional key stuff
471 wxListKeyValue m_key
;
473 void *m_data
; // user data
474 wxNodeBase
*m_next
, // next and previous nodes in the list
477 wxListBase
*m_list
; // list we belong to
479 DECLARE_NO_COPY_CLASS(wxNodeBase
)
482 // -----------------------------------------------------------------------------
483 // a double-linked list class
484 // -----------------------------------------------------------------------------
486 class WXDLLIMPEXP_BASE wxList
;
488 class WXDLLIMPEXP_BASE wxListBase
: public wxObject
490 friend class WXDLLIMPEXP_BASE wxNodeBase
; // should be able to call DetachNode()
491 friend class wxHashTableBase
; // should be able to call untyped Find()
494 // default ctor & dtor
495 wxListBase(wxKeyType keyType
= wxKEY_NONE
)
497 virtual ~wxListBase();
500 // count of items in the list
501 size_t GetCount() const { return m_count
; }
503 // return true if this list is empty
504 bool IsEmpty() const { return m_count
== 0; }
511 // instruct it to destroy user data when deleting nodes
512 void DeleteContents(bool destroy
) { m_destroy
= destroy
; }
514 // query if to delete
515 bool GetDeleteContents() const
516 { return m_destroy
; }
519 wxKeyType
GetKeyType() const
520 { return m_keyType
; }
522 // set the keytype (required by the serial code)
523 void SetKeyType(wxKeyType keyType
)
524 { wxASSERT( m_count
==0 ); m_keyType
= keyType
; }
526 #ifdef wxLIST_COMPATIBILITY
527 // compatibility methods from old wxList
528 wxDEPRECATED( int Number() const ); // use GetCount instead.
529 wxDEPRECATED( wxNode
*First() const ); // use GetFirst
530 wxDEPRECATED( wxNode
*Last() const ); // use GetLast
531 wxDEPRECATED( wxNode
*Nth(size_t n
) const ); // use Item
533 // kludge for typesafe list migration in core classes.
534 wxDEPRECATED( operator wxList
&() const );
535 #endif // wxLIST_COMPATIBILITY
539 // all methods here are "overloaded" in derived classes to provide compile
540 // time type checking
542 // create a node for the list of this type
543 virtual wxNodeBase
*CreateNode(wxNodeBase
*prev
, wxNodeBase
*next
,
545 const wxListKey
& key
= wxDefaultListKey
) = 0;
547 // Can't access these from derived classes otherwise (bug in Salford C++?)
554 wxListBase(size_t count
, void *elements
[]);
555 // from a sequence of objects
556 wxListBase(void *object
, ... /* terminate with NULL */);
559 void Assign(const wxListBase
& list
)
560 { Clear(); DoCopy(list
); }
562 // get list head/tail
563 wxNodeBase
*GetFirst() const { return m_nodeFirst
; }
564 wxNodeBase
*GetLast() const { return m_nodeLast
; }
566 // by (0-based) index
567 wxNodeBase
*Item(size_t index
) const;
569 // get the list item's data
570 void *operator[](size_t n
) const
572 wxNodeBase
*node
= Item(n
);
574 return node
? node
->GetData() : (wxNodeBase
*)NULL
;
578 // append to end of list
579 wxNodeBase
*Prepend(void *object
)
580 { return (wxNodeBase
*)wxListBase::Insert(object
); }
581 // append to beginning of list
582 wxNodeBase
*Append(void *object
);
583 // insert a new item at the beginning of the list
584 wxNodeBase
*Insert(void *object
) { return Insert( (wxNodeBase
*)NULL
, object
); }
585 // insert a new item at the given position
586 wxNodeBase
*Insert(size_t pos
, void *object
)
587 { return pos
== GetCount() ? Append(object
)
588 : Insert(Item(pos
), object
); }
589 // insert before given node or at front of list if prev == NULL
590 wxNodeBase
*Insert(wxNodeBase
*prev
, void *object
);
593 wxNodeBase
*Append(long key
, void *object
);
594 wxNodeBase
*Append(const wxString
& key
, void *object
);
596 // removes node from the list but doesn't delete it (returns pointer
597 // to the node or NULL if it wasn't found in the list)
598 wxNodeBase
*DetachNode(wxNodeBase
*node
);
599 // delete element from list, returns false if node not found
600 bool DeleteNode(wxNodeBase
*node
);
601 // finds object pointer and deletes node (and object if DeleteContents
602 // is on), returns false if object not found
603 bool DeleteObject(void *object
);
605 // search (all return NULL if item not found)
607 wxNodeBase
*Find(const void *object
) const;
610 wxNodeBase
*Find(const wxListKey
& key
) const;
612 // get 0-based index of object or wxNOT_FOUND
613 int IndexOf( void *object
) const;
615 // this function allows the sorting of arbitrary lists by giving
616 // a function to compare two list elements. The list is sorted in place.
617 void Sort(const wxSortCompareFunction compfunc
);
619 // functions for iterating over the list
620 void *FirstThat(wxListIterateFunction func
);
621 void ForEach(wxListIterateFunction func
);
622 void *LastThat(wxListIterateFunction func
);
624 // for STL interface, "last" points to one after the last node
625 // of the controlled sequence (NULL for the end of the list)
627 void DeleteNodes(wxNodeBase
* first
, wxNodeBase
* last
);
630 // common part of all ctors
631 void Init(wxKeyType keyType
= wxKEY_NONE
);
634 // common part of copy ctor and assignment operator
635 void DoCopy(const wxListBase
& list
);
636 // common part of all Append()s
637 wxNodeBase
*AppendCommon(wxNodeBase
*node
);
638 // free node's data and node itself
639 void DoDeleteNode(wxNodeBase
*node
);
641 size_t m_count
; // number of elements in the list
642 bool m_destroy
; // destroy user data when deleting list items?
643 wxNodeBase
*m_nodeFirst
, // pointers to the head and tail of the list
646 wxKeyType m_keyType
; // type of our keys (may be wxKEY_NONE)
649 // -----------------------------------------------------------------------------
650 // macros for definition of "template" list type
651 // -----------------------------------------------------------------------------
653 // and now some heavy magic...
655 // declare a list type named 'name' and containing elements of type 'T *'
656 // (as a by product of macro expansion you also get wx##name##Node
657 // wxNode-derived type)
659 // implementation details:
660 // 1. We define _WX_LIST_ITEM_TYPE_##name typedef to save in it the item type
661 // for the list of given type - this allows us to pass only the list name
662 // to WX_DEFINE_LIST() even if it needs both the name and the type
664 // 2. We redefine all non-type-safe wxList functions with type-safe versions
665 // which don't take any space (everything is inline), but bring compile
666 // time error checking.
668 // 3. The macro which is usually used (WX_DECLARE_LIST) is defined in terms of
669 // a more generic WX_DECLARE_LIST_2 macro which, in turn, uses the most
670 // generic WX_DECLARE_LIST_3 one. The last macro adds a sometimes
671 // interesting capability to store polymorphic objects in the list and is
672 // particularly useful with, for example, "wxWindow *" list where the
673 // wxWindowBase pointers are put into the list, but wxWindow pointers are
674 // retrieved from it.
676 // 4. final hack is that WX_DECLARE_LIST_3 is defined in terms of
677 // WX_DECLARE_LIST_4 to allow defining classes without operator->() as
678 // it results in compiler warnings when this operator doesn't make sense
679 // (i.e. stored elements are not pointers)
681 // common part of WX_DECLARE_LIST_3 and WX_DECLARE_LIST_PTR_3
682 #define WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, ptrop) \
683 typedef int (*wxSortFuncFor_##name)(const T **, const T **); \
685 classexp nodetype : public wxNodeBase \
688 nodetype(wxListBase *list = (wxListBase *)NULL, \
689 nodetype *previous = (nodetype *)NULL, \
690 nodetype *next = (nodetype *)NULL, \
691 T *data = (T *)NULL, \
692 const wxListKey& key = wxDefaultListKey) \
693 : wxNodeBase(list, previous, next, data, key) { } \
695 nodetype *GetNext() const \
696 { return (nodetype *)wxNodeBase::GetNext(); } \
697 nodetype *GetPrevious() const \
698 { return (nodetype *)wxNodeBase::GetPrevious(); } \
701 { return (T *)wxNodeBase::GetData(); } \
702 void SetData(T *data) \
703 { wxNodeBase::SetData(data); } \
706 virtual void DeleteData(); \
708 DECLARE_NO_COPY_CLASS(nodetype) \
711 classexp name : public wxListBase \
714 typedef nodetype Node; \
715 classexp compatibility_iterator \
718 compatibility_iterator(Node *ptr = NULL) : m_ptr(ptr) { } \
720 Node *operator->() const { return m_ptr; } \
721 operator Node *() const { return m_ptr; } \
727 name(wxKeyType keyType = wxKEY_NONE) : wxListBase(keyType) \
729 name(const name& list) : wxListBase(list.GetKeyType()) \
731 name(size_t count, T *elements[]) \
732 : wxListBase(count, (void **)elements) { } \
734 name& operator=(const name& list) \
735 { Assign(list); return *this; } \
737 nodetype *GetFirst() const \
738 { return (nodetype *)wxListBase::GetFirst(); } \
739 nodetype *GetLast() const \
740 { return (nodetype *)wxListBase::GetLast(); } \
742 nodetype *Item(size_t index) const \
743 { return (nodetype *)wxListBase::Item(index); } \
745 T *operator[](size_t index) const \
747 nodetype *node = Item(index); \
748 return node ? (T*)(node->GetData()) : (T*)NULL; \
751 nodetype *Append(Tbase *object) \
752 { return (nodetype *)wxListBase::Append(object); } \
753 nodetype *Insert(Tbase *object) \
754 { return (nodetype *)Insert((nodetype*)NULL, object); } \
755 nodetype *Insert(size_t pos, Tbase *object) \
756 { return (nodetype *)wxListBase::Insert(pos, object); } \
757 nodetype *Insert(nodetype *prev, Tbase *object) \
758 { return (nodetype *)wxListBase::Insert(prev, object); } \
760 nodetype *Append(long key, void *object) \
761 { return (nodetype *)wxListBase::Append(key, object); } \
762 nodetype *Append(const wxChar *key, void *object) \
763 { return (nodetype *)wxListBase::Append(key, object); } \
765 nodetype *DetachNode(nodetype *node) \
766 { return (nodetype *)wxListBase::DetachNode(node); } \
767 bool DeleteNode(nodetype *node) \
768 { return wxListBase::DeleteNode(node); } \
769 bool DeleteObject(Tbase *object) \
770 { return wxListBase::DeleteObject(object); } \
771 void Erase(nodetype *it) \
772 { DeleteNode(it); } \
774 nodetype *Find(const Tbase *object) const \
775 { return (nodetype *)wxListBase::Find(object); } \
777 virtual nodetype *Find(const wxListKey& key) const \
778 { return (nodetype *)wxListBase::Find(key); } \
780 int IndexOf(Tbase *object) const \
781 { return wxListBase::IndexOf(object); } \
783 void Sort(wxSortCompareFunction func) \
784 { wxListBase::Sort(func); } \
785 void Sort(wxSortFuncFor_##name func) \
786 { Sort((wxSortCompareFunction)func); } \
789 virtual wxNodeBase *CreateNode(wxNodeBase *prev, wxNodeBase *next, \
791 const wxListKey& key = wxDefaultListKey) \
793 return new nodetype(this, \
794 (nodetype *)prev, (nodetype *)next, \
797 /* STL interface */ \
799 typedef size_t size_type; \
800 typedef int difference_type; \
801 typedef T* value_type; \
802 typedef Tbase* base_value_type; \
803 typedef value_type& reference; \
804 typedef const value_type& const_reference; \
805 typedef base_value_type& base_reference; \
806 typedef const base_value_type& const_base_reference; \
812 typedef nodetype Node; \
813 typedef iterator itor; \
814 typedef T* value_type; \
815 typedef value_type* ptr_type; \
816 typedef value_type& reference; \
821 typedef reference reference_type; \
822 typedef ptr_type pointer_type; \
824 iterator(Node* node, Node* init) : m_node(node), m_init(init) {}\
825 iterator() : m_node(NULL), m_init(NULL) { } \
826 reference_type operator*() const \
827 { return *(pointer_type)m_node->GetDataPtr(); } \
829 itor& operator++() { m_node = m_node->GetNext(); return *this; }\
830 const itor operator++(int) \
831 { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }\
834 m_node = m_node ? m_node->GetPrevious() : m_init; \
837 const itor operator--(int) \
840 m_node = m_node ? m_node->GetPrevious() : m_init; \
843 bool operator!=(const itor& it) const \
844 { return it.m_node != m_node; } \
845 bool operator==(const itor& it) const \
846 { return it.m_node == m_node; } \
848 classexp const_iterator \
852 typedef nodetype Node; \
853 typedef T* value_type; \
854 typedef const value_type& const_reference; \
855 typedef const_iterator itor; \
856 typedef value_type* ptr_type; \
861 typedef const_reference reference_type; \
862 typedef const ptr_type pointer_type; \
864 const_iterator(Node* node, Node* init) \
865 : m_node(node), m_init(init) { } \
866 const_iterator() : m_node(NULL), m_init(NULL) { } \
867 const_iterator(const iterator& it) \
868 : m_node(it.m_node), m_init(it.m_init) { } \
869 reference_type operator*() const \
870 { return *(pointer_type)m_node->GetDataPtr(); } \
872 itor& operator++() { m_node = m_node->GetNext(); return *this; }\
873 const itor operator++(int) \
874 { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }\
877 m_node = m_node ? m_node->GetPrevious() : m_init; \
880 const itor operator--(int) \
883 m_node = m_node ? m_node->GetPrevious() : m_init; \
886 bool operator!=(const itor& it) const \
887 { return it.m_node != m_node; } \
888 bool operator==(const itor& it) const \
889 { return it.m_node == m_node; } \
891 classexp reverse_iterator \
895 typedef nodetype Node; \
896 typedef T* value_type; \
897 typedef reverse_iterator itor; \
898 typedef value_type* ptr_type; \
899 typedef value_type& reference; \
904 typedef reference reference_type; \
905 typedef ptr_type pointer_type; \
907 reverse_iterator(Node* node, Node* init) \
908 : m_node(node), m_init(init) { } \
909 reverse_iterator() : m_node(NULL), m_init(NULL) { } \
910 reference_type operator*() const \
911 { return *(pointer_type)m_node->GetDataPtr(); } \
914 { m_node = m_node->GetPrevious(); return *this; } \
915 const itor operator++(int) \
916 { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }\
918 { m_node = m_node ? m_node->GetNext() : m_init; return *this; } \
919 const itor operator--(int) \
922 m_node = m_node ? m_node->GetNext() : m_init; \
925 bool operator!=(const itor& it) const \
926 { return it.m_node != m_node; } \
927 bool operator==(const itor& it) const \
928 { return it.m_node == m_node; } \
930 classexp const_reverse_iterator \
934 typedef nodetype Node; \
935 typedef T* value_type; \
936 typedef const_reverse_iterator itor; \
937 typedef value_type* ptr_type; \
938 typedef const value_type& const_reference; \
943 typedef const_reference reference_type; \
944 typedef const ptr_type pointer_type; \
946 const_reverse_iterator(Node* node, Node* init) \
947 : m_node(node), m_init(init) { } \
948 const_reverse_iterator() : m_node(NULL), m_init(NULL) { } \
949 const_reverse_iterator(const reverse_iterator& it) \
950 : m_node(it.m_node), m_init(it.m_init) { } \
951 reference_type operator*() const \
952 { return *(pointer_type)m_node->GetDataPtr(); } \
955 { m_node = m_node->GetPrevious(); return *this; } \
956 const itor operator++(int) \
957 { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }\
959 { m_node = m_node ? m_node->GetNext() : m_init; return *this;}\
960 const itor operator--(int) \
963 m_node = m_node ? m_node->GetNext() : m_init; \
966 bool operator!=(const itor& it) const \
967 { return it.m_node != m_node; } \
968 bool operator==(const itor& it) const \
969 { return it.m_node == m_node; } \
972 wxEXPLICIT name(size_type n, const_reference v = value_type()) \
974 name(const const_iterator& first, const const_iterator& last) \
975 { assign(first, last); } \
976 iterator begin() { return iterator(GetFirst(), GetLast()); } \
977 const_iterator begin() const \
978 { return const_iterator(GetFirst(), GetLast()); } \
979 iterator end() { return iterator(NULL, GetLast()); } \
980 const_iterator end() const { return const_iterator(NULL, GetLast()); }\
981 reverse_iterator rbegin() \
982 { return reverse_iterator(GetLast(), GetFirst()); } \
983 const_reverse_iterator rbegin() const \
984 { return const_reverse_iterator(GetLast(), GetFirst()); } \
985 reverse_iterator rend() { return reverse_iterator(NULL, GetFirst()); }\
986 const_reverse_iterator rend() const \
987 { return const_reverse_iterator(NULL, GetFirst()); } \
988 void resize(size_type n, value_type v = value_type()) \
995 size_type size() const { return GetCount(); } \
996 size_type max_size() const { return INT_MAX; } \
997 bool empty() const { return IsEmpty(); } \
998 reference front() { return *begin(); } \
999 const_reference front() const { return *begin(); } \
1000 reference back() { iterator tmp = end(); return *--tmp; } \
1001 const_reference back() const { const_iterator tmp = end(); return *--tmp; }\
1002 void push_front(const_reference v = value_type()) \
1003 { Insert(GetFirst(), (const_base_reference)v); } \
1004 void pop_front() { DeleteNode(GetFirst()); } \
1005 void push_back(const_reference v = value_type()) \
1006 { Append((const_base_reference)v); } \
1007 void pop_back() { DeleteNode(GetLast()); } \
1008 void assign(const_iterator first, const const_iterator& last) \
1011 for(; first != last; ++first) \
1012 Append((const_base_reference)*first); \
1014 void assign(size_type n, const_reference v = value_type()) \
1017 for(size_type i = 0; i < n; ++i) \
1018 Append((const_base_reference)v); \
1020 iterator insert(const iterator& it, const_reference v = value_type())\
1022 Insert(it.m_node, (const_base_reference)v); \
1023 return iterator(it.m_node->GetPrevious(), GetLast()); \
1025 void insert(const iterator& it, size_type n, const_reference v = value_type())\
1027 for(size_type i = 0; i < n; ++i) \
1028 Insert(it.m_node, (const_base_reference)v); \
1030 void insert(const iterator& it, const_iterator first, const const_iterator& last)\
1032 for(; first != last; ++first) \
1033 Insert(it.m_node, (const_base_reference)*first); \
1035 iterator erase(const iterator& it) \
1037 iterator next = iterator(it.m_node->GetNext(), GetLast()); \
1038 DeleteNode(it.m_node); return next; \
1040 iterator erase(const iterator& first, const iterator& last) \
1042 iterator next = last; ++next; \
1043 DeleteNodes(first.m_node, last.m_node); \
1046 void clear() { Clear(); } \
1047 void splice(const iterator& it, name& l, const iterator& first, const iterator& last)\
1048 { insert(it, first, last); l.erase(first, last); } \
1049 void splice(const iterator& it, name& l) \
1050 { splice(it, l, l.begin(), l.end() ); } \
1051 void splice(const iterator& it, name& l, const iterator& first) \
1053 iterator tmp = first; ++tmp; \
1054 if(it == first || it == tmp) return; \
1055 insert(it, *first); \
1058 void remove(const_reference v) \
1059 { DeleteObject((const_base_reference)v); } \
1062 /* void swap(name& l) \
1064 { size_t t = m_count; m_count = l.m_count; l.m_count = t; } \
1065 { bool t = m_destroy; m_destroy = l.m_destroy; l.m_destroy = t; }\
1066 { wxNodeBase* t = m_nodeFirst; m_nodeFirst = l.m_nodeFirst; l.m_nodeFirst = t; }\
1067 { wxNodeBase* t = m_nodeLast; m_nodeLast = l.m_nodeLast; l.m_nodeLast = t; }\
1068 { wxKeyType t = m_keyType; m_keyType = l.m_keyType; l.m_keyType = t; }\
1072 #define WX_LIST_PTROP \
1073 pointer_type operator->() const \
1074 { return (pointer_type)m_node->GetDataPtr(); }
1075 #define WX_LIST_PTROP_NONE
1077 #define WX_DECLARE_LIST_3(T, Tbase, name, nodetype, classexp) \
1078 WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, WX_LIST_PTROP_NONE)
1079 #define WX_DECLARE_LIST_PTR_3(T, Tbase, name, nodetype, classexp) \
1080 WX_DECLARE_LIST_4(T, Tbase, name, nodetype, classexp, WX_LIST_PTROP)
1082 #define WX_DECLARE_LIST_2(elementtype, listname, nodename, classexp) \
1083 WX_DECLARE_LIST_3(elementtype, elementtype, listname, nodename, classexp)
1084 #define WX_DECLARE_LIST_PTR_2(elementtype, listname, nodename, classexp) \
1085 WX_DECLARE_LIST_PTR_3(elementtype, elementtype, listname, nodename, classexp)
1087 #define WX_DECLARE_LIST(elementtype, listname) \
1088 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1089 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class)
1090 #define WX_DECLARE_LIST_PTR(elementtype, listname) \
1091 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1092 WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class)
1094 #define WX_DECLARE_LIST_WITH_DECL(elementtype, listname, decl) \
1095 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1096 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, decl)
1098 #define WX_DECLARE_EXPORTED_LIST(elementtype, listname) \
1099 WX_DECLARE_LIST_WITH_DECL(elementtype, listname, class WXDLLEXPORT)
1101 #define WX_DECLARE_EXPORTED_LIST_PTR(elementtype, listname) \
1102 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1103 WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class WXDLLEXPORT)
1105 #define WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) \
1106 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1107 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class usergoo)
1108 #define WX_DECLARE_USER_EXPORTED_LIST_PTR(elementtype, listname, usergoo) \
1109 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
1110 WX_DECLARE_LIST_PTR_2(elementtype, listname, wx##listname##Node, class usergoo)
1112 // this macro must be inserted in your program after
1113 // #include "wx/listimpl.cpp"
1114 #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
1116 #define WX_DEFINE_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
1117 #define WX_DEFINE_USER_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
1119 #endif // !wxUSE_STL
1121 // ============================================================================
1122 // now we can define classes 100% compatible with the old ones
1123 // ============================================================================
1125 // ----------------------------------------------------------------------------
1126 // commonly used list classes
1127 // ----------------------------------------------------------------------------
1129 #if defined(wxLIST_COMPATIBILITY)
1131 // inline compatibility functions
1135 // ----------------------------------------------------------------------------
1136 // wxNodeBase deprecated methods
1137 // ----------------------------------------------------------------------------
1139 inline wxNode
*wxNodeBase::Next() const { return (wxNode
*)GetNext(); }
1140 inline wxNode
*wxNodeBase::Previous() const { return (wxNode
*)GetPrevious(); }
1141 inline wxObject
*wxNodeBase::Data() const { return (wxObject
*)GetData(); }
1143 // ----------------------------------------------------------------------------
1144 // wxListBase deprecated methods
1145 // ----------------------------------------------------------------------------
1147 inline int wxListBase::Number() const { return (int)GetCount(); }
1148 inline wxNode
*wxListBase::First() const { return (wxNode
*)GetFirst(); }
1149 inline wxNode
*wxListBase::Last() const { return (wxNode
*)GetLast(); }
1150 inline wxNode
*wxListBase::Nth(size_t n
) const { return (wxNode
*)Item(n
); }
1151 inline wxListBase::operator wxList
&() const { return *(wxList
*)this; }
1155 // define this to make a lot of noise about use of the old wxList classes.
1156 //#define wxWARN_COMPAT_LIST_USE
1158 // ----------------------------------------------------------------------------
1159 // wxList compatibility class: in fact, it's a list of wxObjects
1160 // ----------------------------------------------------------------------------
1162 WX_DECLARE_LIST_2(wxObject
, wxObjectList
, wxObjectListNode
,
1163 class WXDLLIMPEXP_BASE
);
1165 class WXDLLIMPEXP_BASE wxList
: public wxObjectList
1168 #if defined(wxWARN_COMPAT_LIST_USE) && !wxUSE_STL
1170 wxDEPRECATED( wxList(int key_type
) );
1172 wxList(int key_type
= wxKEY_NONE
);
1175 // this destructor is required for Darwin
1179 wxList
& operator=(const wxList
& list
)
1180 { (void) wxListBase::operator=(list
); return *this; }
1182 // compatibility methods
1183 void Sort(wxSortCompareFunction compfunc
) { wxListBase::Sort(compfunc
); }
1188 wxNode
*Member(wxObject
*object
) const { return (wxNode
*)Find(object
); }
1193 DECLARE_DYNAMIC_CLASS(wxList
)
1199 // -----------------------------------------------------------------------------
1200 // wxStringList class for compatibility with the old code
1201 // -----------------------------------------------------------------------------
1202 WX_DECLARE_LIST_2(wxChar
, wxStringListBase
, wxStringListNode
, class WXDLLIMPEXP_BASE
);
1204 class WXDLLIMPEXP_BASE wxStringList
: public wxStringListBase
1209 #ifdef wxWARN_COMPAT_LIST_USE
1211 wxDEPRECATED( wxStringList(const wxChar
*first
...) ); // FIXME-UTF8
1214 wxStringList(const wxChar
*first
...); // FIXME-UTF8
1217 // copying the string list: the strings are copied, too (extremely
1219 wxStringList(const wxStringList
& other
) : wxStringListBase() { DeleteContents(true); DoCopy(other
); }
1220 wxStringList
& operator=(const wxStringList
& other
)
1221 { Clear(); DoCopy(other
); return *this; }
1224 // makes a copy of the string
1225 wxNode
*Add(const wxChar
*s
);
1227 // Append to beginning of list
1228 wxNode
*Prepend(const wxChar
*s
);
1230 bool Delete(const wxChar
*s
);
1232 wxChar
**ListToArray(bool new_copies
= false) const;
1233 bool Member(const wxChar
*s
) const;
1239 void DoCopy(const wxStringList
&); // common part of copy ctor and operator=
1241 DECLARE_DYNAMIC_CLASS(wxStringList
)
1244 #else // if wxUSE_STL
1246 WX_DECLARE_LIST_XO(wxString
, wxStringListBase
, class WXDLLIMPEXP_BASE
);
1248 class WXDLLIMPEXP_BASE wxStringList
: public wxStringListBase
1251 compatibility_iterator
Append(wxChar
* s
)
1252 { wxString tmp
= s
; delete[] s
; return wxStringListBase::Append(tmp
); }
1253 compatibility_iterator
Insert(wxChar
* s
)
1254 { wxString tmp
= s
; delete[] s
; return wxStringListBase::Insert(tmp
); }
1255 compatibility_iterator
Insert(size_t pos
, wxChar
* s
)
1259 return wxStringListBase::Insert(pos
, tmp
);
1261 compatibility_iterator
Add(const wxChar
* s
)
1262 { push_back(s
); return GetLast(); }
1263 compatibility_iterator
Prepend(const wxChar
* s
)
1264 { push_front(s
); return GetFirst(); }
1269 #endif // wxLIST_COMPATIBILITY
1271 // delete all list elements
1273 // NB: the class declaration of the list elements must be visible from the
1274 // place where you use this macro, otherwise the proper destructor may not
1275 // be called (a decent compiler should give a warning about it, but don't
1277 #define WX_CLEAR_LIST(type, list) \
1279 type::iterator it, en; \
1280 for( it = (list).begin(), en = (list).end(); it != en; ++it ) \
1285 #endif // _WX_LISTH__