1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: auto-resizable (i.e. dynamic) array support
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && \
16 !(defined(__MINGW32__) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
17 #pragma interface "dynarray.h"
23 #include "wx/beforestd.h"
26 #include "wx/afterstd.h"
30 This header defines the dynamic arrays and object arrays (i.e. arrays which
31 own their elements). Dynamic means that the arrays grow automatically as
34 These macros are ugly (especially if you look in the sources ;-), but they
35 allow us to define "template" classes without actually using templates and so
36 this works with all compilers (and may be also much faster to compile even
37 with a compiler which does support templates). The arrays defined with these
40 Range checking is performed in debug build for both arrays and objarrays but
41 not in release build - so using an invalid index will just lead to a crash
44 Note about memory usage: arrays never shrink automatically (although you may
45 use Shrink() function explicitly), they only grow, so loading 10 millions in
46 an array only to delete them 2 lines below might be a bad idea if the array
47 object is not going to be destroyed soon. However, as it does free memory
48 when destroyed, it is ok if the array is a local variable.
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
56 The initial size by which an array grows when an element is added default
57 value avoids allocate one or two bytes when the array is created which is
60 #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
67 Callback compare function for quick sort.
69 It must return negative value, 0 or positive value if the first item is
70 less than, equal to or greater than the second one.
74 typedef int (wxCMPFUNC_CONV
*CMPFUNC
)(const void* pItem1
, const void* pItem2
);
77 // ----------------------------------------------------------------------------
78 // Base class managing data having size of type 'long' (not used directly)
80 // NB: for efficiency this often used class has no virtual functions (hence no
81 // virtual table), even dtor is *not* virtual. If used as expected it
82 // won't create any problems because ARRAYs from DEFINE_ARRAY have no dtor
83 // at all, so it's not too important if it's not called (this happens when
84 // you cast "SomeArray *" as "BaseArray *" and then delete it)
85 // ----------------------------------------------------------------------------
90 class WXDLLIMPEXP_BASE wxArray_SortFunction
93 typedef int (wxCMPFUNC_CONV
*CMPFUNC
)(T
* pItem1
, T
* pItem2
);
95 wxArray_SortFunction(CMPFUNC f
) : m_f(f
) { }
96 bool operator()(const T
& i1
, const T
& i2
)
97 { return m_f((T
*)&i1
, (T
*)&i2
) < 0; }
102 template<class T
, typename F
>
103 class WXDLLIMPEXP_BASE wxSortedArray_SortFunction
108 wxSortedArray_SortFunction(CMPFUNC f
) : m_f(f
) { }
109 bool operator()(const T
& i1
, const T
& i2
)
110 { return m_f(i1
, i2
) < 0; }
115 #define _WX_DECLARE_BASEARRAY(T, name, classexp) \
116 typedef int (wxCMPFUNC_CONV *CMPFUN##name)(T pItem1, T pItem2); \
117 typedef wxSortedArray_SortFunction<T, CMPFUN##name> name##_Predicate; \
118 _WX_DECLARE_BASEARRAY_2(T, name, name##_Predicate, classexp)
120 #define _WX_DECLARE_BASEARRAY_2(T, name, predicate, classexp) \
121 classexp name : public std::vector<T> \
123 typedef predicate Predicate; \
124 typedef predicate::CMPFUNC SCMPFUNC; \
126 typedef wxArray_SortFunction<T>::CMPFUNC CMPFUNC; \
128 void Empty() { clear(); } \
129 void Clear() { clear(); } \
130 void Alloc(size_t uiSize) { reserve(uiSize); } \
133 size_t GetCount() const { return size(); } \
134 void SetCount(size_t n, T v = T()) { resize(n, v); } \
135 bool IsEmpty() const { return empty(); } \
136 size_t Count() const { return size(); } \
138 typedef T base_type; \
141 T& Item(size_t uiIndex) const \
142 { wxASSERT( uiIndex < size() ); return (T&)operator[](uiIndex); } \
144 int Index(T e, bool bFromEnd = FALSE) const; \
145 int Index(T lItem, CMPFUNC fnCompare) const; \
146 size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const; \
147 void Add(T lItem, size_t nInsert = 1) \
148 { insert(end(), nInsert, lItem); } \
149 size_t Add(T lItem, CMPFUNC fnCompare); \
150 void Insert(T lItem, size_t uiIndex, size_t nInsert = 1) \
151 { insert(begin() + uiIndex, nInsert, lItem); } \
152 void Remove(T lItem); \
153 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
154 { erase(begin() + uiIndex, begin() + uiIndex + nRemove); } \
156 void Sort(CMPFUNC fCmp) \
158 wxArray_SortFunction<T> p(fCmp); \
159 std::sort(begin(), end(), p); \
163 #else // if !wxUSE_STL
165 #define _WX_DECLARE_BASEARRAY(T, name, classexp) \
168 typedef CMPFUNC SCMPFUNC; /* for compatibility wuth wxUSE_STL */ \
171 name(const name& array); \
172 name& operator=(const name& src); \
175 void Empty() { m_nCount = 0; } \
177 void Alloc(size_t uiSize); \
180 size_t GetCount() const { return m_nCount; } \
181 void SetCount(size_t n, T defval = T(0)); \
182 bool IsEmpty() const { return m_nCount == 0; } \
183 size_t Count() const { return m_nCount; } \
185 typedef T base_type; \
188 T& Item(size_t uiIndex) const \
189 { wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; } \
190 T& operator[](size_t uiIndex) const { return Item(uiIndex); } \
192 int Index(T lItem, bool bFromEnd = FALSE) const; \
193 int Index(T lItem, CMPFUNC fnCompare) const; \
194 size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const; \
195 void Add(T lItem, size_t nInsert = 1); \
196 size_t Add(T lItem, CMPFUNC fnCompare); \
197 void Insert(T lItem, size_t uiIndex, size_t nInsert = 1); \
198 void Remove(T lItem); \
199 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
201 void Sort(CMPFUNC fnCompare); \
203 /* *minimal* STL-ish interface, for derived classes */ \
204 typedef T value_type; \
205 typedef value_type* iterator; \
206 typedef const value_type* const_iterator; \
207 typedef value_type& reference; \
208 typedef const value_type& const_reference; \
209 typedef int difference_type; \
210 typedef size_t size_type; \
212 void assign(const_iterator first, const_iterator last); \
213 void assign(size_type n, const_reference v); \
214 size_type capacity() const { return m_nSize; } \
215 iterator erase(iterator first, iterator last) \
217 size_type idx = first - begin(); \
218 RemoveAt(idx, last - first); \
219 return begin() + idx; \
221 iterator erase(iterator it) { return erase(it, it + 1); } \
222 void insert(iterator it, size_type n, const value_type& v) \
223 { Insert(v, it - begin(), n); } \
224 iterator insert(iterator it, const value_type& v = value_type()) \
226 size_type idx = it - begin(); \
228 return begin() + idx; \
230 void insert(iterator it, const_iterator first, const_iterator last);\
231 void pop_back() { RemoveAt(size() - 1); } \
232 void push_back(const value_type& v) { Add(v); } \
233 void reserve(size_type n) { if(n > m_nSize) Realloc(n); } \
234 void resize(size_type n, value_type v = value_type()); \
236 iterator begin() { return m_pItems; } \
237 iterator end() { return m_pItems + m_nCount; } \
238 const_iterator begin() const { return m_pItems; } \
239 const_iterator end() const { return m_pItems + m_nCount; } \
241 /* the following functions may be made directly public because */ \
242 /* they don't use the type of the elements at all */ \
244 void clear() { Clear(); } \
245 bool empty() const { return IsEmpty(); } \
246 size_type max_size() const { return INT_MAX; } \
247 size_type size() const { return GetCount(); } \
250 void Grow(size_t nIncrement = 0); \
251 bool Realloc(size_t nSize); \
261 // ============================================================================
262 // The private helper macros containing the core of the array classes
263 // ============================================================================
265 // Implementation notes:
267 // JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
268 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
269 // so using a temporary variable instead.
271 // The classes need a (even trivial) ~name() to link under Mac X
273 // _WX_ERROR_REMOVE is needed to resolve the name conflict between the wxT()
274 // macro and T typedef: we can't use wxT() inside WX_DEFINE_ARRAY!
276 #define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
278 // ----------------------------------------------------------------------------
279 // _WX_DEFINE_TYPEARRAY: array for simple types
280 // ----------------------------------------------------------------------------
284 #define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \
285 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
286 classexp name : public base \
289 T& operator[](size_t uiIndex) const \
290 { return (T&)(base::operator[](uiIndex)); } \
291 T& Item(size_t uiIndex) const \
292 { return (T&)/*const cast*/base::operator[](uiIndex); } \
294 { return Item(Count() - 1); } \
296 int Index(T e, bool bFromEnd = FALSE) const \
297 { return base::Index(e, bFromEnd); } \
299 void Add(T Item, size_t nInsert = 1) \
300 { insert(end(), nInsert, Item); } \
301 void Insert(T Item, size_t uiIndex, size_t nInsert = 1) \
302 { insert(begin() + uiIndex, nInsert, Item); } \
304 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
305 { base::RemoveAt(uiIndex, nRemove); } \
306 void Remove(T Item) \
307 { int iIndex = Index(Item); \
308 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
310 RemoveAt((size_t)iIndex); } \
312 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \
315 #define _WX_DEFINE_TYPEARRAY_PTR(T, name, base, classexp) \
316 _WX_DEFINE_TYPEARRAY(T, name, base, classexp)
318 #else // if !wxUSE_STL
320 // common declaration used by both _WX_DEFINE_TYPEARRAY and
321 // _WX_DEFINE_TYPEARRAY_PTR
322 #define _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, ptrop) \
323 wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \
324 TypeTooBigToBeStoredIn##base, \
326 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
327 classexp name : public base \
333 name& operator=(const name& src) \
334 { base* temp = (base*) this; \
335 (*temp) = ((const base&)src); \
338 T& operator[](size_t uiIndex) const \
339 { return (T&)(base::operator[](uiIndex)); } \
340 T& Item(size_t uiIndex) const \
341 { return (T&)(base::operator[](uiIndex)); } \
343 { return (T&)(base::operator[](Count() - 1)); } \
345 int Index(T Item, bool bFromEnd = FALSE) const \
346 { return base::Index((base_type)Item, bFromEnd); } \
348 void Add(T Item, size_t nInsert = 1) \
349 { base::Add((base_type)Item, nInsert); } \
350 void Insert(T Item, size_t uiIndex, size_t nInsert = 1) \
351 { base::Insert((base_type)Item, uiIndex, nInsert) ; } \
353 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
354 { base::RemoveAt(uiIndex, nRemove); } \
355 void Remove(T Item) \
356 { int iIndex = Index(Item); \
357 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
359 base::RemoveAt((size_t)iIndex); } \
361 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \
363 /* STL-like interface */ \
365 typedef base::iterator biterator; \
366 typedef base::const_iterator bconst_iterator; \
367 typedef base::value_type bvalue_type; \
368 typedef base::const_reference bconst_reference; \
370 typedef T value_type; \
371 typedef value_type* pointer; \
372 typedef const value_type* const_pointer; \
373 typedef value_type* iterator; \
374 typedef const value_type* const_iterator; \
375 typedef value_type& reference; \
376 typedef const value_type& const_reference; \
377 typedef base::difference_type difference_type; \
378 typedef base::size_type size_type; \
380 class reverse_iterator \
382 typedef T value_type; \
383 typedef value_type& reference; \
384 typedef value_type* pointer; \
385 typedef reverse_iterator itor; \
386 friend inline itor operator+(int o, const itor& it) \
387 { return it.m_ptr - o; } \
388 friend inline itor operator+(const itor& it, int o) \
389 { return it.m_ptr - o; } \
390 friend inline itor operator-(const itor& it, int o) \
391 { return it.m_ptr + o; } \
392 friend inline difference_type operator-(const itor& i1, \
394 { return i1.m_ptr - i2.m_ptr; } \
398 reverse_iterator() : m_ptr(NULL) { } \
399 reverse_iterator(pointer ptr) : m_ptr(ptr) { } \
400 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { } \
401 reference operator*() const { return *m_ptr; } \
403 itor operator++() { --m_ptr; return *this; } \
404 itor operator++(int) \
405 { reverse_iterator tmp = *this; --m_ptr; return tmp; } \
406 itor operator--() { ++m_ptr; return *this; } \
407 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; } \
408 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; } \
409 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; } \
412 class const_reverse_iterator \
414 typedef T value_type; \
415 typedef const value_type& reference; \
416 typedef const value_type* pointer; \
417 typedef const_reverse_iterator itor; \
418 friend inline itor operator+(int o, const itor& it) \
419 { return it.m_ptr - o; } \
420 friend inline itor operator+(const itor& it, int o) \
421 { return it.m_ptr - o; } \
422 friend inline itor operator-(const itor& it, int o) \
423 { return it.m_ptr + o; } \
424 friend inline difference_type operator-(const itor& i1, \
426 { return i1.m_ptr - i2.m_ptr; } \
430 const_reverse_iterator() : m_ptr(NULL) { } \
431 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { } \
432 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { } \
433 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }\
434 reference operator*() const { return *m_ptr; } \
436 itor operator++() { --m_ptr; return *this; } \
437 itor operator++(int) \
438 { itor tmp = *this; --m_ptr; return tmp; } \
439 itor operator--() { ++m_ptr; return *this; } \
440 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; } \
441 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; } \
442 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; } \
445 void assign(const_iterator first, const_iterator last) \
446 { base::assign((bconst_iterator)first, (bconst_iterator)last); } \
447 void assign(size_type n, const_reference v) \
448 { base::assign(n, (bconst_reference)v); } \
449 reference back() { return *(end() - 1); } \
450 const_reference back() const { return *(end() - 1); } \
451 iterator begin() { return (iterator)base::begin(); } \
452 const_iterator begin() const { return (const_iterator)base::begin(); }\
453 size_type capacity() const { return base::capacity(); } \
454 iterator end() { return (iterator)base::end(); } \
455 const_iterator end() const { return (const_iterator)base::end(); } \
456 iterator erase(iterator first, iterator last) \
457 { return (iterator)base::erase((biterator)first, (biterator)last); }\
458 iterator erase(iterator it) \
459 { return (iterator)base::erase((biterator)it); } \
460 reference front() { return *begin(); } \
461 const_reference front() const { return *begin(); } \
462 void insert(iterator it, size_type n, const_reference v) \
463 { base::insert((biterator)it, n, (bconst_reference)v); } \
464 iterator insert(iterator it, const_reference v = value_type()) \
465 { return (iterator)base::insert((biterator)it, (bconst_reference)v); }\
466 void insert(iterator it, const_iterator first, const_iterator last) \
467 { base::insert((biterator)it, (bconst_iterator)first, \
468 (bconst_iterator)last); } \
469 void pop_back() { base::pop_back(); } \
470 void push_back(const_reference v) \
471 { base::push_back((bconst_reference)v); } \
472 reverse_iterator rbegin() { return reverse_iterator(end() - 1); } \
473 const_reverse_iterator rbegin() const; \
474 reverse_iterator rend() { return reverse_iterator(begin() - 1); } \
475 const_reverse_iterator rend() const; \
476 void reserve(size_type n) { base::reserve(n); }; \
477 void resize(size_type n, value_type v = value_type()); \
480 #define _WX_PTROP pointer operator->() const { return m_ptr; }
481 #define _WX_PTROP_NONE
483 #define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \
484 _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, _WX_PTROP)
485 #define _WX_DEFINE_TYPEARRAY_PTR(T, name, base, classexp) \
486 _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, _WX_PTROP_NONE)
490 // ----------------------------------------------------------------------------
491 // _WX_DEFINE_SORTED_TYPEARRAY: sorted array for simple data types
492 // cannot handle types with size greater than pointer because of sorting
493 // ----------------------------------------------------------------------------
495 #define _WX_DEFINE_SORTED_TYPEARRAY_2(T, name, base, defcomp, classexp, comptype)\
496 wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \
497 TypeTooBigToBeStoredInSorted##base, \
499 classexp name : public base \
501 typedef comptype SCMPFUNC; \
503 name(comptype fn defcomp) { m_fnCompare = fn; } \
505 name& operator=(const name& src) \
506 { base* temp = (base*) this; \
507 (*temp) = ((const base&)src); \
508 m_fnCompare = src.m_fnCompare; \
511 T& operator[](size_t uiIndex) const \
512 { return (T&)(base::operator[](uiIndex)); } \
513 T& Item(size_t uiIndex) const \
514 { return (T&)(base::operator[](uiIndex)); } \
516 { return (T&)(base::operator[](size() - 1)); } \
518 int Index(T Item) const \
519 { return base::Index(Item, (CMPFUNC)m_fnCompare); } \
521 size_t IndexForInsert(T Item) const \
522 { return base::IndexForInsert(Item, (CMPFUNC)m_fnCompare); } \
524 void AddAt(T item, size_t index) \
525 { base::insert(begin() + index, item); } \
528 { return base::Add(Item, (CMPFUNC)m_fnCompare); } \
530 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
531 { base::erase(begin() + uiIndex, begin() + uiIndex + nRemove); } \
532 void Remove(T Item) \
533 { int iIndex = Index(Item); \
534 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
535 _WX_ERROR_REMOVE ); \
536 base::erase(begin() + iIndex); } \
539 comptype m_fnCompare; \
543 // ----------------------------------------------------------------------------
544 // _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics
545 // ----------------------------------------------------------------------------
547 #define _WX_DECLARE_OBJARRAY(T, name, base, classexp) \
548 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T **pItem1, T **pItem2); \
549 classexp name : protected base \
551 typedef int (CMPFUNC_CONV *CMPFUNC##base)(void **pItem1, void **pItem2); \
552 typedef base base_array; \
555 name(const name& src); \
556 name& operator=(const name& src); \
560 void Alloc(size_t count) { reserve(count); } \
561 size_t GetCount() const { return base_array::size(); } \
562 size_t size() const { return base_array::size(); } \
563 bool IsEmpty() const { return base_array::empty(); } \
564 size_t Count() const { return base_array::size(); } \
565 void Shrink() { base::Shrink(); } \
567 T& operator[](size_t uiIndex) const \
568 { return *(T*)base::operator[](uiIndex); } \
569 T& Item(size_t uiIndex) const \
570 { return *(T*)base::operator[](uiIndex); } \
572 { return *(T*)(base::operator[](size() - 1)); } \
574 int Index(const T& Item, bool bFromEnd = FALSE) const; \
576 void Add(const T& Item, size_t nInsert = 1); \
577 void Add(const T* pItem) \
578 { base::push_back((T*)pItem); } \
579 void push_back(const T* pItem) \
580 { base::push_back((T*)pItem); } \
581 void push_back(const T& Item) \
584 void Insert(const T& Item, size_t uiIndex, size_t nInsert = 1); \
585 void Insert(const T* pItem, size_t uiIndex) \
586 { base::insert(begin() + uiIndex, (T*)pItem); } \
588 void Empty() { DoEmpty(); base::clear(); } \
589 void Clear() { DoEmpty(); base::clear(); } \
591 T* Detach(size_t uiIndex) \
592 { T* p = (T*)base::operator[](uiIndex); \
593 base::erase(begin() + uiIndex); return p; } \
594 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
596 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC##base)fCmp); } \
600 void DoCopy(const name& src); \
603 // ============================================================================
604 // The public macros for declaration and definition of the dynamic arrays
605 // ============================================================================
607 // Please note that for each macro WX_FOO_ARRAY we also have
608 // WX_FOO_EXPORTED_ARRAY and WX_FOO_USER_EXPORTED_ARRAY which are exactly the
609 // same except that they use an additional __declspec(dllexport) or equivalent
610 // under Windows if needed.
612 // The first (just EXPORTED) macros do it if wxWindows was compiled as a DLL
613 // and so must be used used inside the library. The second kind (USER_EXPORTED)
614 // allow the user code to do it when it wants. This is needed if you have a dll
615 // that wants to export a wxArray daubed with your own import/export goo.
617 // Finally, you can define the macro below as something special to modify the
618 // arrays defined by a simple WX_FOO_ARRAY as well. By default is is empty.
619 #define wxARRAY_DEFAULT_EXPORT
621 // ----------------------------------------------------------------------------
622 // WX_DECLARE_BASEARRAY(T, name) declare an array class named "name" containing
623 // the elements of type T
624 // ----------------------------------------------------------------------------
626 #define WX_DECLARE_BASEARRAY(T, name) \
627 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
629 #define WX_DECLARE_EXPORTED_BASEARRAY(T, name) \
630 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, WXDLLEXPORT)
632 #define WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, expmode) \
633 typedef T _wxArray##name; \
634 _WX_DECLARE_BASEARRAY(_wxArray##name, name, class expmode)
636 // ----------------------------------------------------------------------------
637 // WX_DEFINE_TYPEARRAY(T, name, base) define an array class named "name" deriving
638 // from class "base" containing the elements of type T
640 // Note that the class defined has only inline function and doesn't take any
641 // space at all so there is no size penalty for defining multiple array classes
642 // ----------------------------------------------------------------------------
644 #define WX_DEFINE_TYPEARRAY(T, name, base) \
645 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class wxARRAY_DEFAULT_EXPORT)
647 #define WX_DEFINE_TYPEARRAY_PTR(T, name, base) \
648 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class wxARRAY_DEFAULT_EXPORT)
650 #define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \
651 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class WXDLLEXPORT)
653 #define WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, base) \
654 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class WXDLLEXPORT)
656 #define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expdecl) \
657 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class expdecl)
659 #define WX_DEFINE_USER_EXPORTED_TYPEARRAY_PTR(T, name, base, expdecl) \
660 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class expdecl)
662 #define WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, classdecl) \
663 typedef T _wxArray##name; \
664 _WX_DEFINE_TYPEARRAY(_wxArray##name, name, base, classdecl)
666 #define WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, classdecl) \
667 typedef T _wxArray##name; \
668 _WX_DEFINE_TYPEARRAY_PTR(_wxArray##name, name, base, classdecl)
670 // ----------------------------------------------------------------------------
671 // WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it
672 // defines a sorted array.
675 // 1) it must be given a COMPARE function in ctor which takes 2 items of type
676 // T* and should return -1, 0 or +1 if the first one is less/greater
677 // than/equal to the second one.
678 // 2) the Add() method inserts the item in such was that the array is always
679 // sorted (it uses the COMPARE function)
680 // 3) it has no Sort() method because it's always sorted
681 // 4) Index() method is much faster (the sorted arrays use binary search
682 // instead of linear one), but Add() is slower.
683 // 5) there is no Insert() method because you can't insert an item into the
684 // given position in a sorted array but there is IndexForInsert()/AddAt()
685 // pair which may be used to optimize a common operation of "insert only if
688 // Note that you have to specify the comparison function when creating the
689 // objects of this array type. If, as in 99% of cases, the comparison function
690 // is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below
691 // is more convenient.
693 // Summary: use this class when the speed of Index() function is important, use
694 // the normal arrays otherwise.
695 // ----------------------------------------------------------------------------
697 #define wxARRAY_EMPTY_CMP
699 #define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \
700 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \
701 wxARRAY_DEFAULT_EXPORT)
703 #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \
704 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT)
706 #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \
707 typedef T _wxArray##name; \
708 typedef int (CMPFUNC_CONV *SCMPFUNC##name)(T pItem1, T pItem2); \
709 _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, \
710 wxARRAY_EMPTY_CMP, class expmode, SCMPFUNC##name)
712 // ----------------------------------------------------------------------------
713 // WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison
714 // function is provided by this macro and the objects of this class have a
715 // default constructor which just uses it.
717 // The arguments are: the element type, the comparison function and the array
720 // NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked
721 // from the very beginning - unfortunately I didn't think about this earlier
722 // ----------------------------------------------------------------------------
724 #define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
725 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
726 wxARRAY_DEFAULT_EXPORT)
728 #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
729 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
732 #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
734 typedef T _wxArray##name; \
735 typedef int (CMPFUNC_CONV *SCMPFUNC##name)(T pItem1, T pItem2); \
736 _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, = cmpfunc, \
737 class expmode, SCMPFUNC##name)
739 // ----------------------------------------------------------------------------
740 // WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class
741 // named "name" which owns the objects of type T it contains, i.e. it will
742 // delete them when it is destroyed.
744 // An element is of type T*, but arguments of type T& are taken (see below!)
745 // and T& is returned.
747 // Don't use this for simple types such as "int" or "long"!
749 // Note on Add/Insert functions:
750 // 1) function(T*) gives the object to the array, i.e. it will delete the
751 // object when it's removed or in the array's dtor
752 // 2) function(T&) will create a copy of the object and work with it
755 // 1) Remove() will delete the object after removing it from the array
756 // 2) Detach() just removes the object from the array (returning pointer to it)
758 // NB1: Base type T should have an accessible copy ctor if Add(T&) is used
759 // NB2: Never ever cast a array to it's base type: as dtor is not virtual
760 // and so you risk having at least the memory leaks and probably worse
762 // Some functions of this class are not inline, so it takes some space to
763 // define new class from this template even if you don't use it - which is not
764 // the case for the simple (non-object) array classes
766 // To use an objarray class you must
767 // #include "dynarray.h"
768 // WX_DECLARE_OBJARRAY(element_type, list_class_name)
769 // #include "arrimpl.cpp"
770 // WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above!
772 // This is necessary because at the moment of DEFINE_OBJARRAY class parsing the
773 // element_type must be fully defined (i.e. forward declaration is not
774 // enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
775 // two allows to break cicrcular dependencies with classes which have member
776 // variables of objarray type.
777 // ----------------------------------------------------------------------------
779 #define WX_DECLARE_OBJARRAY(T, name) \
780 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
782 #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
783 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLEXPORT)
785 #define WX_DECLARE_OBJARRAY_WITH_DECL(T, name, decl) \
786 typedef T _wxObjArray##name; \
787 _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, wxArrayPtrVoid, decl)
789 #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \
790 WX_DECLARE_OBJARRAY_WITH_DECL(T, name, class expmode)
792 // WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included,
793 // try to provoke a human-understandable error if it used incorrectly.
795 // there is no real need for 3 different macros in the DEFINE case but do it
796 // anyhow for consistency
797 #define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp
798 #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
799 #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
801 // ----------------------------------------------------------------------------
802 // Some commonly used predefined base arrays
803 // ----------------------------------------------------------------------------
805 WX_DECLARE_USER_EXPORTED_BASEARRAY(const void *, wxBaseArrayPtrVoid
,
807 WX_DECLARE_USER_EXPORTED_BASEARRAY(short, wxBaseArrayShort
, WXDLLIMPEXP_BASE
);
808 WX_DECLARE_USER_EXPORTED_BASEARRAY(int, wxBaseArrayInt
, WXDLLIMPEXP_BASE
);
809 WX_DECLARE_USER_EXPORTED_BASEARRAY(long, wxBaseArrayLong
, WXDLLIMPEXP_BASE
);
810 WX_DECLARE_USER_EXPORTED_BASEARRAY(double, wxBaseArrayDouble
, WXDLLIMPEXP_BASE
);
812 // ----------------------------------------------------------------------------
813 // Convenience macros to define arrays from base arrays
814 // ----------------------------------------------------------------------------
816 #define WX_DEFINE_ARRAY(T, name) \
817 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
818 #define WX_DEFINE_ARRAY_PTR(T, name) \
819 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid)
820 #define WX_DEFINE_EXPORTED_ARRAY(T, name) \
821 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
822 #define WX_DEFINE_EXPORTED_ARRAY_PTR(T, name) \
823 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid)
824 #define WX_DEFINE_ARRAY_WITH_DECL_PTR(T, name, decl) \
825 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, decl)
826 #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \
827 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayPtrVoid, expmode)
828 #define WX_DEFINE_USER_EXPORTED_ARRAY_PTR(T, name, expmode) \
829 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, expmode)
831 #define WX_DEFINE_ARRAY_SHORT(T, name) \
832 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayShort)
833 #define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \
834 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayShort)
835 #define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
836 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayShort, expmode)
838 #define WX_DEFINE_ARRAY_INT(T, name) \
839 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayInt)
840 #define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \
841 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayInt)
842 #define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
843 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayInt, expmode)
845 #define WX_DEFINE_ARRAY_LONG(T, name) \
846 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayLong)
847 #define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \
848 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayLong)
849 #define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
850 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayLong, expmode)
852 #define WX_DEFINE_ARRAY_DOUBLE(T, name) \
853 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayDouble)
854 #define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \
855 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayDouble)
856 #define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \
857 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayDouble, expmode)
859 // ----------------------------------------------------------------------------
860 // Convenience macros to define sorted arrays from base arrays
861 // ----------------------------------------------------------------------------
863 #define WX_DEFINE_SORTED_ARRAY(T, name) \
864 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
865 #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
866 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
867 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \
868 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode)
870 #define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \
871 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort)
872 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \
873 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort)
874 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
875 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode)
877 #define WX_DEFINE_SORTED_ARRAY_INT(T, name) \
878 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt)
879 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \
880 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt)
881 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
882 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode)
884 #define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \
885 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong)
886 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \
887 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong)
888 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
889 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode)
891 // ----------------------------------------------------------------------------
892 // Convenience macros to define sorted arrays from base arrays
893 // ----------------------------------------------------------------------------
895 #define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \
896 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
897 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \
898 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
899 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \
901 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
902 wxBaseArrayPtrVoid, expmode)
904 #define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
905 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
906 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
907 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
908 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \
910 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
911 wxBaseArrayShort, expmode)
913 #define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
914 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
915 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
916 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
917 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \
919 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
920 wxBaseArrayInt, expmode)
922 #define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
923 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
924 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
925 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
926 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \
928 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
929 wxBaseArrayLong, expmode)
931 // ----------------------------------------------------------------------------
932 // Some commonly used predefined arrays
933 // ----------------------------------------------------------------------------
935 WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(short, wxArrayShort
, class WXDLLIMPEXP_BASE
);
936 WX_DEFINE_USER_EXPORTED_ARRAY_INT(int, wxArrayInt
, class WXDLLIMPEXP_BASE
);
937 WX_DEFINE_USER_EXPORTED_ARRAY_LONG(long, wxArrayLong
, class WXDLLIMPEXP_BASE
);
938 WX_DEFINE_USER_EXPORTED_ARRAY_PTR(void *, wxArrayPtrVoid
, class WXDLLIMPEXP_BASE
);
940 // -----------------------------------------------------------------------------
941 // convenience macros
942 // -----------------------------------------------------------------------------
944 // append all element of one array to another one
945 #define WX_APPEND_ARRAY(array, other) \
947 size_t count = (other).size(); \
948 for ( size_t n = 0; n < count; n++ ) \
950 (array).push_back((other)[n]); \
954 // delete all array elements
956 // NB: the class declaration of the array elements must be visible from the
957 // place where you use this macro, otherwise the proper destructor may not
958 // be called (a decent compiler should give a warning about it, but don't
960 #define WX_CLEAR_ARRAY(array) \
962 size_t count = (array).size(); \
963 for ( size_t n = 0; n < count; n++ ) \
971 #endif // _DYNARRAY_H