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 #if defined(__GNUG__) && !defined(__APPLE__)
29 #pragma interface "list.h"
32 // -----------------------------------------------------------------------------
34 // -----------------------------------------------------------------------------
37 #include "wx/object.h"
38 #include "wx/string.h"
40 class WXDLLIMPEXP_BASE wxObjectListNode
;
41 typedef wxObjectListNode wxNode
;
43 // undef it to get rid of old, deprecated functions
44 #define wxLIST_COMPATIBILITY
46 // -----------------------------------------------------------------------------
48 // -----------------------------------------------------------------------------
56 // -----------------------------------------------------------------------------
58 // -----------------------------------------------------------------------------
60 // type of compare function for list sort operation (as in 'qsort'): it should
61 // return a negative value, 0 or positive value if the first element is less
62 // than, equal or greater than the second
65 typedef int (* LINKAGEMODE wxSortCompareFunction
)(const void *elem1
, const void *elem2
);
69 typedef int (* LINKAGEMODE wxListIterateFunction
)(void *current
);
71 // -----------------------------------------------------------------------------
72 // key stuff: a list may be optionally keyed on integer or string key
73 // -----------------------------------------------------------------------------
81 // a struct which may contain both types of keys
83 // implementation note: on one hand, this class allows to have only one function
84 // for any keyed operation instead of 2 almost equivalent. OTOH, it's needed to
85 // resolve ambiguity which we would otherwise have with wxStringList::Find() and
86 // wxList::Find(const char *).
87 class WXDLLIMPEXP_BASE wxListKey
91 wxListKey() : m_keyType(wxKEY_NONE
)
93 wxListKey(long i
) : m_keyType(wxKEY_INTEGER
)
94 { m_key
.integer
= i
; }
95 wxListKey(const wxChar
*s
) : m_keyType(wxKEY_STRING
)
96 { m_key
.string
= wxStrdup(s
); }
97 wxListKey(const wxString
& s
) : m_keyType(wxKEY_STRING
)
98 { m_key
.string
= wxStrdup(s
.c_str()); }
101 wxKeyType
GetKeyType() const { return m_keyType
; }
102 const wxChar
*GetString() const
103 { wxASSERT( m_keyType
== wxKEY_STRING
); return m_key
.string
; }
104 long GetNumber() const
105 { wxASSERT( m_keyType
== wxKEY_INTEGER
); return m_key
.integer
; }
108 // Note: implementation moved to list.cpp to prevent BC++ inline
109 // expansion warning.
110 bool operator==(wxListKeyValue value
) const ;
115 if ( m_keyType
== wxKEY_STRING
)
121 wxListKeyValue m_key
;
124 // -----------------------------------------------------------------------------
125 // wxNodeBase class is a (base for) node in a double linked list
126 // -----------------------------------------------------------------------------
128 WXDLLIMPEXP_DATA_BASE(extern wxListKey
) wxDefaultListKey
;
130 class WXDLLIMPEXP_BASE wxListBase
;
132 class WXDLLIMPEXP_BASE wxNodeBase
134 friend class wxListBase
;
137 wxNodeBase(wxListBase
*list
= (wxListBase
*)NULL
,
138 wxNodeBase
*previous
= (wxNodeBase
*)NULL
,
139 wxNodeBase
*next
= (wxNodeBase
*)NULL
,
141 const wxListKey
& key
= wxDefaultListKey
);
143 virtual ~wxNodeBase();
145 // FIXME no check is done that the list is really keyed on strings
146 const wxChar
*GetKeyString() const { return m_key
.string
; }
147 long GetKeyInteger() const { return m_key
.integer
; }
149 // Necessary for some existing code
150 void SetKeyString(wxChar
* s
) { m_key
.string
= s
; }
151 void SetKeyInteger(long i
) { m_key
.integer
= i
; }
153 #ifdef wxLIST_COMPATIBILITY
154 // compatibility methods, use Get* instead.
155 wxDEPRECATED( wxNode
*Next() const );
156 wxDEPRECATED( wxNode
*Previous() const );
157 wxDEPRECATED( wxObject
*Data() const );
158 #endif // wxLIST_COMPATIBILITY
161 // all these are going to be "overloaded" in the derived classes
162 wxNodeBase
*GetNext() const { return m_next
; }
163 wxNodeBase
*GetPrevious() const { return m_previous
; }
165 void *GetData() const { return m_data
; }
166 void SetData(void *data
) { m_data
= data
; }
168 // get 0-based index of this node within the list or wxNOT_FOUND
171 virtual void DeleteData() { }
174 // optional key stuff
175 wxListKeyValue m_key
;
177 void *m_data
; // user data
178 wxNodeBase
*m_next
, // next and previous nodes in the list
181 wxListBase
*m_list
; // list we belong to
183 DECLARE_NO_COPY_CLASS(wxNodeBase
)
186 // -----------------------------------------------------------------------------
187 // a double-linked list class
188 // -----------------------------------------------------------------------------
192 class WXDLLIMPEXP_BASE wxListBase
: public wxObject
194 friend class WXDLLIMPEXP_BASE wxNodeBase
; // should be able to call DetachNode()
195 friend class wxHashTableBase
; // should be able to call untyped Find()
197 // common part of all ctors
198 void Init(wxKeyType keyType
= wxKEY_NONE
); // Must be declared before it's used (for VC++ 1.5)
200 // default ctor & dtor
201 wxListBase(wxKeyType keyType
= wxKEY_NONE
)
203 virtual ~wxListBase();
206 // count of items in the list
207 size_t GetCount() const { return m_count
; }
209 // return TRUE if this list is empty
210 bool IsEmpty() const { return m_count
== 0; }
217 // instruct it to destroy user data when deleting nodes
218 void DeleteContents(bool destroy
) { m_destroy
= destroy
; }
220 // query if to delete
221 bool GetDeleteContents() const
222 { return m_destroy
; }
225 wxKeyType
GetKeyType() const
226 { return m_keyType
; }
228 // set the keytype (required by the serial code)
229 void SetKeyType(wxKeyType keyType
)
230 { wxASSERT( m_count
==0 ); m_keyType
= keyType
; }
232 #ifdef wxLIST_COMPATIBILITY
233 // compatibility methods from old wxList
234 wxDEPRECATED( int Number() const ); // use GetCount instead.
235 wxDEPRECATED( wxNode
*First() const ); // use GetFirst
236 wxDEPRECATED( wxNode
*Last() const ); // use GetLast
237 wxDEPRECATED( wxNode
*Nth(size_t n
) const ); // use Item
239 // kludge for typesafe list migration in core classes.
240 wxDEPRECATED( operator wxList
&() const );
241 #endif // wxLIST_COMPATIBILITY
245 // all methods here are "overloaded" in derived classes to provide compile
246 // time type checking
248 // create a node for the list of this type
249 virtual wxNodeBase
*CreateNode(wxNodeBase
*prev
, wxNodeBase
*next
,
251 const wxListKey
& key
= wxDefaultListKey
) = 0;
253 // Can't access these from derived classes otherwise (bug in Salford C++?)
260 wxListBase(size_t count
, void *elements
[]);
261 // from a sequence of objects
262 wxListBase(void *object
, ... /* terminate with NULL */);
265 // copy ctor and assignment operator
266 wxListBase(const wxListBase
& list
) : wxObject()
267 { Init(); DoCopy(list
); }
268 wxListBase
& operator=(const wxListBase
& list
)
269 { Clear(); DoCopy(list
); return *this; }
271 // get list head/tail
272 wxNodeBase
*GetFirst() const { return m_nodeFirst
; }
273 wxNodeBase
*GetLast() const { return m_nodeLast
; }
275 // by (0-based) index
276 wxNodeBase
*Item(size_t index
) const;
278 // get the list item's data
279 void *operator[](size_t n
) const
281 wxNodeBase
*node
= Item(n
);
283 return node
? node
->GetData() : (wxNodeBase
*)NULL
;
287 // append to end of list
288 wxNodeBase
*Prepend(void *object
)
289 { return (wxNodeBase
*)wxListBase::Insert(object
); }
290 // append to beginning of list
291 wxNodeBase
*Append(void *object
);
292 // insert a new item at the beginning of the list
293 wxNodeBase
*Insert(void *object
) { return Insert( (wxNodeBase
*)NULL
, object
); }
294 // insert a new item at the given position
295 wxNodeBase
*Insert(size_t pos
, void *object
)
296 { return pos
== GetCount() ? Append(object
)
297 : Insert(Item(pos
), object
); }
298 // insert before given node or at front of list if prev == NULL
299 wxNodeBase
*Insert(wxNodeBase
*prev
, void *object
);
302 wxNodeBase
*Append(long key
, void *object
);
303 wxNodeBase
*Append(const wxChar
*key
, void *object
);
305 // removes node from the list but doesn't delete it (returns pointer
306 // to the node or NULL if it wasn't found in the list)
307 wxNodeBase
*DetachNode(wxNodeBase
*node
);
308 // delete element from list, returns FALSE if node not found
309 bool DeleteNode(wxNodeBase
*node
);
310 // finds object pointer and deletes node (and object if DeleteContents
311 // is on), returns FALSE if object not found
312 bool DeleteObject(void *object
);
314 // search (all return NULL if item not found)
316 wxNodeBase
*Find(void *object
) const;
319 wxNodeBase
*Find(const wxListKey
& key
) const;
321 // get 0-based index of object or wxNOT_FOUND
322 int IndexOf( void *object
) const;
324 // this function allows the sorting of arbitrary lists by giving
325 // a function to compare two list elements. The list is sorted in place.
326 void Sort(const wxSortCompareFunction compfunc
);
328 // functions for iterating over the list
329 void *FirstThat(wxListIterateFunction func
);
330 void ForEach(wxListIterateFunction func
);
331 void *LastThat(wxListIterateFunction func
);
335 // common part of copy ctor and assignment operator
336 void DoCopy(const wxListBase
& list
);
337 // common part of all Append()s
338 wxNodeBase
*AppendCommon(wxNodeBase
*node
);
339 // free node's data and node itself
340 void DoDeleteNode(wxNodeBase
*node
);
342 size_t m_count
; // number of elements in the list
343 bool m_destroy
; // destroy user data when deleting list items?
344 wxNodeBase
*m_nodeFirst
, // pointers to the head and tail of the list
347 wxKeyType m_keyType
; // type of our keys (may be wxKEY_NONE)
350 // -----------------------------------------------------------------------------
351 // macros for definition of "template" list type
352 // -----------------------------------------------------------------------------
354 // and now some heavy magic...
356 // declare a list type named 'name' and containing elements of type 'T *'
357 // (as a by product of macro expansion you also get wx##name##Node
358 // wxNode-derived type)
360 // implementation details:
361 // 1. We define _WX_LIST_ITEM_TYPE_##name typedef to save in it the item type
362 // for the list of given type - this allows us to pass only the list name
363 // to WX_DEFINE_LIST() even if it needs both the name and the type
365 // 2. We redefine all non-type-safe wxList functions with type-safe versions
366 // which don't take any space (everything is inline), but bring compile
367 // time error checking.
369 // 3. The macro which is usually used (WX_DECLARE_LIST) is defined in terms of
370 // a more generic WX_DECLARE_LIST_2 macro which, in turn, uses the most
371 // generic WX_DECLARE_LIST_3 one. The last macro adds a sometimes
372 // interesting capability to store polymorphic objects in the list and is
373 // particularly useful with, for example, "wxWindow *" list where the
374 // wxWindowBase pointers are put into the list, but wxWindow pointers are
375 // retrieved from it.
377 #define WX_DECLARE_LIST_3(T, Tbase, name, nodetype, classexp) \
378 typedef int (*wxSortFuncFor_##name)(const T **, const T **); \
380 classexp nodetype : public wxNodeBase \
383 nodetype(wxListBase *list = (wxListBase *)NULL, \
384 nodetype *previous = (nodetype *)NULL, \
385 nodetype *next = (nodetype *)NULL, \
386 T *data = (T *)NULL, \
387 const wxListKey& key = wxDefaultListKey) \
388 : wxNodeBase(list, previous, next, data, key) { } \
390 nodetype *GetNext() const \
391 { return (nodetype *)wxNodeBase::GetNext(); } \
392 nodetype *GetPrevious() const \
393 { return (nodetype *)wxNodeBase::GetPrevious(); } \
396 { return (T *)wxNodeBase::GetData(); } \
397 void SetData(T *data) \
398 { wxNodeBase::SetData(data); } \
400 virtual void DeleteData(); \
403 classexp name : public wxListBase \
406 typedef nodetype Node; \
408 name(wxKeyType keyType = wxKEY_NONE) : wxListBase(keyType) \
410 name(size_t count, T *elements[]) \
411 : wxListBase(count, (void **)elements) { } \
413 name& operator=(const name& list) \
414 { (void) wxListBase::operator=(list); return *this; } \
416 nodetype *GetFirst() const \
417 { return (nodetype *)wxListBase::GetFirst(); } \
418 nodetype *GetLast() const \
419 { return (nodetype *)wxListBase::GetLast(); } \
421 nodetype *Item(size_t index) const \
422 { return (nodetype *)wxListBase::Item(index); } \
424 T *operator[](size_t index) const \
426 nodetype *node = Item(index); \
427 return node ? (T*)(node->GetData()) : (T*)NULL; \
430 nodetype *Append(Tbase *object) \
431 { return (nodetype *)wxListBase::Append(object); } \
432 nodetype *Insert(Tbase *object) \
433 { return (nodetype *)Insert((nodetype*)NULL, object); } \
434 nodetype *Insert(size_t pos, Tbase *object) \
435 { return (nodetype *)wxListBase::Insert(pos, object); } \
436 nodetype *Insert(nodetype *prev, Tbase *object) \
437 { return (nodetype *)wxListBase::Insert(prev, object); } \
439 nodetype *Append(long key, void *object) \
440 { return (nodetype *)wxListBase::Append(key, object); } \
441 nodetype *Append(const wxChar *key, void *object) \
442 { return (nodetype *)wxListBase::Append(key, object); } \
444 nodetype *DetachNode(nodetype *node) \
445 { return (nodetype *)wxListBase::DetachNode(node); } \
446 bool DeleteNode(nodetype *node) \
447 { return wxListBase::DeleteNode(node); } \
448 bool DeleteObject(Tbase *object) \
449 { return wxListBase::DeleteObject(object); } \
451 nodetype *Find(Tbase *object) const \
452 { return (nodetype *)wxListBase::Find(object); } \
454 virtual nodetype *Find(const wxListKey& key) const \
455 { return (nodetype *)wxListBase::Find(key); } \
457 int IndexOf(Tbase *object) const \
458 { return wxListBase::IndexOf(object); } \
460 void Sort(wxSortFuncFor_##name func) \
461 { wxListBase::Sort((wxSortCompareFunction)func); } \
464 virtual wxNodeBase *CreateNode(wxNodeBase *prev, wxNodeBase *next, \
466 const wxListKey& key = wxDefaultListKey) \
468 return new nodetype(this, \
469 (nodetype *)prev, (nodetype *)next, \
474 #define WX_DECLARE_LIST_2(elementtype, listname, nodename, classexp) \
475 WX_DECLARE_LIST_3(elementtype, elementtype, listname, nodename, classexp)
477 #define WX_DECLARE_LIST(elementtype, listname) \
478 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
479 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class)
481 #define WX_DECLARE_EXPORTED_LIST(elementtype, listname) \
482 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
483 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class WXDLLEXPORT)
485 #define WX_DECLARE_USER_EXPORTED_LIST(elementtype, listname, usergoo) \
486 typedef elementtype _WX_LIST_ITEM_TYPE_##listname; \
487 WX_DECLARE_LIST_2(elementtype, listname, wx##listname##Node, class usergoo)
489 // this macro must be inserted in your program after
490 // #include <wx/listimpl.cpp>
491 #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
493 #define WX_DEFINE_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
494 #define WX_DEFINE_USER_EXPORTED_LIST(name) WX_DEFINE_LIST(name)
497 // =============================================================================
498 // now we can define classes 100% compatible with the old ones
499 // =============================================================================
501 // ----------------------------------------------------------------------------
502 // commonly used list classes
503 // ----------------------------------------------------------------------------
505 #ifdef wxLIST_COMPATIBILITY
507 // define this to make a lot of noise about use of the old wxList classes.
508 //#define wxWARN_COMPAT_LIST_USE
510 // -----------------------------------------------------------------------------
511 // wxList compatibility class: in fact, it's a list of wxObjects
512 // -----------------------------------------------------------------------------
514 WX_DECLARE_LIST_2(wxObject
, wxObjectList
, wxObjectListNode
, class WXDLLIMPEXP_BASE
);
516 class WXDLLIMPEXP_BASE wxList
: public wxObjectList
519 #ifdef wxWARN_COMPAT_LIST_USE
520 wxDEPRECATED( wxList(int key_type
= wxKEY_NONE
) );
522 wxList(int key_type
= wxKEY_NONE
);
525 // this destructor is required for Darwin
528 wxList
& operator=(const wxList
& list
)
529 { (void) wxListBase::operator=(list
); return *this; }
531 // compatibility methods
532 void Sort(wxSortCompareFunction compfunc
) { wxListBase::Sort(compfunc
); }
534 wxNode
*Member(wxObject
*object
) const { return (wxNode
*)Find(object
); }
537 DECLARE_DYNAMIC_CLASS(wxList
)
540 // -----------------------------------------------------------------------------
541 // wxStringList class for compatibility with the old code
542 // -----------------------------------------------------------------------------
544 WX_DECLARE_LIST_2(wxChar
, wxStringListBase
, wxStringListNode
, class WXDLLIMPEXP_BASE
);
546 class WXDLLIMPEXP_BASE wxStringList
: public wxStringListBase
551 #ifdef wxWARN_COMPAT_LIST_USE
552 wxDEPRECATED( wxStringList() );
553 wxDEPRECATED( wxStringList(const wxChar
*first
...) );
556 wxStringList(const wxChar
*first
...);
559 // copying the string list: the strings are copied, too (extremely
561 wxStringList(const wxStringList
& other
) : wxStringListBase() { DeleteContents(TRUE
); DoCopy(other
); }
562 wxStringList
& operator=(const wxStringList
& other
)
563 { Clear(); DoCopy(other
); return *this; }
566 // makes a copy of the string
567 wxNode
*Add(const wxChar
*s
);
569 // Append to beginning of list
570 wxNode
*Prepend(const wxChar
*s
);
572 bool Delete(const wxChar
*s
);
574 wxChar
**ListToArray(bool new_copies
= FALSE
) const;
575 bool Member(const wxChar
*s
) const;
581 void DoCopy(const wxStringList
&); // common part of copy ctor and operator=
583 DECLARE_DYNAMIC_CLASS(wxStringList
)
586 #endif // wxLIST_COMPATIBILITY