]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynarray.h
added wxStaticCast, moved wxConstCast from wx/object.h to wx/defs.h for consistency
[wxWidgets.git] / include / wx / dynarray.h
CommitLineData
c801d85f
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: dynarray.h
3// Purpose: auto-resizable (i.e. dynamic) array support
4// Author: Vadim Zeitlin
3bfa4402 5// Modified by:
c801d85f
KB
6// Created: 12.09.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
371a5b4e 9// Licence: wxWindows licence
c801d85f
KB
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _DYNARRAY_H
13#define _DYNARRAY_H
14
12028905 15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && \
df5168c4 16 !(defined(__MINGW32__) && __GNUC__ == 3 && __GNUC_MINOR__ == 2)
c801d85f
KB
17#pragma interface "dynarray.h"
18#endif
19
20#include "wx/defs.h"
c801d85f 21
df5168c4
MB
22#if wxUSE_STL
23 #include "wx/beforestd.h"
24 #include <vector>
25 #include <algorithm>
26 #include "wx/afterstd.h"
df5168c4
MB
27#endif
28
1a931653
VZ
29/*
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
32 needed.
33
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
38 macros are type-safe.
39
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
42 then.
43
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.
49 */
c801d85f
KB
50
51// ----------------------------------------------------------------------------
52// constants
53// ----------------------------------------------------------------------------
54
1a931653
VZ
55/*
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
58 rather inefficient
c801d85f 59*/
1a931653 60#define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
c801d85f
KB
61
62// ----------------------------------------------------------------------------
63// types
64// ----------------------------------------------------------------------------
65
1a931653
VZ
66/*
67 Callback compare function for quick sort.
3b0b5f13 68
1a931653
VZ
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.
c801d85f 71 */
90350682
VZ
72extern "C"
73{
0661ec39 74typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
90350682 75}
c801d85f
KB
76
77// ----------------------------------------------------------------------------
1a931653
VZ
78// Base class managing data having size of type 'long' (not used directly)
79//
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)
c801d85f 85// ----------------------------------------------------------------------------
1a931653 86
df5168c4
MB
87#if wxUSE_STL
88
f700f98c
MB
89template<class T>
90class WXDLLIMPEXP_BASE wxArray_SortFunction
91{
92public:
93 typedef int (wxCMPFUNC_CONV *CMPFUNC)(T* pItem1, T* pItem2);
94
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; }
98private:
99 CMPFUNC m_f;
100};
101
102template<class T, typename F>
103class WXDLLIMPEXP_BASE wxSortedArray_SortFunction
104{
105public:
106 typedef F CMPFUNC;
107
108 wxSortedArray_SortFunction(CMPFUNC f) : m_f(f) { }
109 bool operator()(const T& i1, const T& i2)
110 { return m_f(i1, i2) < 0; }
111private:
112 CMPFUNC m_f;
113};
114
df5168c4 115#define _WX_DECLARE_BASEARRAY(T, name, classexp) \
f700f98c
MB
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)
119
120#define _WX_DECLARE_BASEARRAY_2(T, name, predicate, classexp) \
df5168c4
MB
121classexp name : public std::vector<T> \
122{ \
f700f98c
MB
123 typedef predicate Predicate; \
124 typedef predicate::CMPFUNC SCMPFUNC; \
7df07b10 125public: \
f700f98c 126 typedef wxArray_SortFunction<T>::CMPFUNC CMPFUNC; \
df5168c4
MB
127public: \
128 void Empty() { clear(); } \
129 void Clear() { clear(); } \
130 void Alloc(size_t uiSize) { reserve(uiSize); } \
131 void Shrink(); \
132 \
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(); } \
137 \
138 typedef T base_type; \
139 \
140protected: \
141 T& Item(size_t uiIndex) const \
142 { wxASSERT( uiIndex < size() ); return (T&)operator[](uiIndex); } \
143 \
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); } \
6992d326 149 size_t Add(T lItem, CMPFUNC fnCompare); \
df5168c4
MB
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); } \
155 \
156 void Sort(CMPFUNC fCmp) \
157 { \
f700f98c 158 wxArray_SortFunction<T> p(fCmp); \
df5168c4
MB
159 std::sort(begin(), end(), p); \
160 } \
df5168c4
MB
161}
162
163#else // if !wxUSE_STL
164
5a1cad6e
GD
165#define _WX_DECLARE_BASEARRAY(T, name, classexp) \
166classexp name \
167{ \
f700f98c 168 typedef CMPFUNC SCMPFUNC; /* for compatibility wuth wxUSE_STL */ \
5a1cad6e
GD
169public: \
170 name(); \
171 name(const name& array); \
172 name& operator=(const name& src); \
173 ~name(); \
174 \
175 void Empty() { m_nCount = 0; } \
176 void Clear(); \
177 void Alloc(size_t uiSize); \
178 void Shrink(); \
179 \
2abb9d2f
VZ
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; } \
5a1cad6e 184 \
75d7ef36 185 typedef T base_type; \
2abb9d2f 186 \
5a1cad6e
GD
187protected: \
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); } \
191 \
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; \
3b0b5f13 195 void Add(T lItem, size_t nInsert = 1); \
6992d326 196 size_t Add(T lItem, CMPFUNC fnCompare); \
3b0b5f13 197 void Insert(T lItem, size_t uiIndex, size_t nInsert = 1); \
5a1cad6e 198 void Remove(T lItem); \
3b0b5f13 199 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
5a1cad6e
GD
200 \
201 void Sort(CMPFUNC fnCompare); \
202 \
df5168c4
MB
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; \
211 \
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; } \
df5168c4
MB
215 iterator erase(iterator first, iterator last) \
216 { \
217 size_type idx = first - begin(); \
218 RemoveAt(idx, last - first); \
219 return begin() + idx; \
220 } \
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()) \
225 { \
226 size_type idx = it - begin(); \
227 Insert(v, idx); \
228 return begin() + idx; \
229 } \
230 void insert(iterator it, const_iterator first, const_iterator last);\
df5168c4
MB
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()); \
df5168c4
MB
235 \
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; } \
5da0803c
VZ
240 \
241 /* the following functions may be made directly public because */ \
242 /* they don't use the type of the elements at all */ \
243public: \
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(); } \
248 \
5a1cad6e 249private: \
2abb9d2f
VZ
250 void Grow(size_t nIncrement = 0); \
251 bool Realloc(size_t nSize); \
5a1cad6e
GD
252 \
253 size_t m_nSize, \
254 m_nCount; \
255 \
256 T *m_pItems; \
e9bb94cf 257}
c801d85f 258
df5168c4
MB
259#endif // !wxUSE_STL
260
c801d85f 261// ============================================================================
1a931653 262// The private helper macros containing the core of the array classes
c801d85f
KB
263// ============================================================================
264
1a931653
VZ
265// Implementation notes:
266//
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.
270//
271// The classes need a (even trivial) ~name() to link under Mac X
272//
273// _WX_ERROR_REMOVE is needed to resolve the name conflict between the wxT()
5a1cad6e 274// macro and T typedef: we can't use wxT() inside WX_DEFINE_ARRAY!
1a931653
VZ
275
276#define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
e90c1d2a 277
c801d85f 278// ----------------------------------------------------------------------------
5a1cad6e 279// _WX_DEFINE_TYPEARRAY: array for simple types
c801d85f 280// ----------------------------------------------------------------------------
1a931653 281
df5168c4
MB
282#if wxUSE_STL
283
284#define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \
285typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
286classexp name : public base \
287{ \
288public: \
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); } \
293 T& Last() const \
294 { return Item(Count() - 1); } \
295 \
296 int Index(T e, bool bFromEnd = FALSE) const \
297 { return base::Index(e, bFromEnd); } \
298 \
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); } \
303 \
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, \
309 _WX_ERROR_REMOVE); \
310 RemoveAt((size_t)iIndex); } \
311 \
312 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \
313}
314
d5d29b8a 315#define _WX_DEFINE_TYPEARRAY_PTR(T, name, base, classexp) \
6108e3fd
VZ
316 _WX_DEFINE_TYPEARRAY(T, name, base, classexp)
317
df5168c4
MB
318#else // if !wxUSE_STL
319
6108e3fd 320// common declaration used by both _WX_DEFINE_TYPEARRAY and
d5d29b8a 321// _WX_DEFINE_TYPEARRAY_PTR
6108e3fd 322#define _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, ptrop) \
5a1cad6e
GD
323wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \
324 TypeTooBigToBeStoredIn##base, \
325 name); \
326typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
327classexp name : public base \
328{ \
329public: \
330 name() { } \
331 ~name() { } \
332 \
333 name& operator=(const name& src) \
334 { base* temp = (base*) this; \
335 (*temp) = ((const base&)src); \
336 return *this; } \
337 \
338 T& operator[](size_t uiIndex) const \
df5168c4 339 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 340 T& Item(size_t uiIndex) const \
df5168c4 341 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 342 T& Last() const \
df5168c4 343 { return (T&)(base::operator[](Count() - 1)); } \
5a1cad6e
GD
344 \
345 int Index(T Item, bool bFromEnd = FALSE) const \
e38ed919 346 { return base::Index((base_type)Item, bFromEnd); } \
5a1cad6e 347 \
3b0b5f13 348 void Add(T Item, size_t nInsert = 1) \
e38ed919 349 { base::Add((base_type)Item, nInsert); } \
3b0b5f13 350 void Insert(T Item, size_t uiIndex, size_t nInsert = 1) \
e38ed919 351 { base::Insert((base_type)Item, uiIndex, nInsert) ; } \
5a1cad6e 352 \
3b0b5f13
VZ
353 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
354 { base::RemoveAt(uiIndex, nRemove); } \
5a1cad6e
GD
355 void Remove(T Item) \
356 { int iIndex = Index(Item); \
357 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
358 _WX_ERROR_REMOVE); \
359 base::RemoveAt((size_t)iIndex); } \
360 \
361 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \
df5168c4
MB
362 \
363 /* STL-like interface */ \
364private: \
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; \
369public: \
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; \
379 \
380 class reverse_iterator \
381 { \
c514cd53
MB
382 typedef T value_type; \
383 typedef value_type& reference; \
384 typedef value_type* pointer; \
df5168c4 385 typedef reverse_iterator itor; \
31222b7c
VZ
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, \
393 const itor& i2) \
394 { return i1.m_ptr - i2.m_ptr; } \
395 \
df5168c4
MB
396 public: \
397 pointer 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; } \
6108e3fd 402 ptrop \
df5168c4
MB
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; } \
410 }; \
411 \
412 class const_reverse_iterator \
413 { \
c514cd53
MB
414 typedef T value_type; \
415 typedef const value_type& reference; \
416 typedef const value_type* pointer; \
df5168c4 417 typedef const_reverse_iterator itor; \
31222b7c
VZ
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, \
425 const itor& i2) \
426 { return i1.m_ptr - i2.m_ptr; } \
427 \
df5168c4
MB
428 public: \
429 pointer 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; } \
6108e3fd 435 ptrop \
df5168c4
MB
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; } \
443 }; \
444 \
584ad2a3
MB
445 name(size_type n, const_reference v) { assign(n, v); } \
446 name(const_iterator first, const_iterator last) \
447 { assign(first, last); } \
df5168c4
MB
448 void assign(const_iterator first, const_iterator last) \
449 { base::assign((bconst_iterator)first, (bconst_iterator)last); } \
450 void assign(size_type n, const_reference v) \
451 { base::assign(n, (bconst_reference)v); } \
452 reference back() { return *(end() - 1); } \
453 const_reference back() const { return *(end() - 1); } \
454 iterator begin() { return (iterator)base::begin(); } \
455 const_iterator begin() const { return (const_iterator)base::begin(); }\
456 size_type capacity() const { return base::capacity(); } \
df5168c4
MB
457 iterator end() { return (iterator)base::end(); } \
458 const_iterator end() const { return (const_iterator)base::end(); } \
459 iterator erase(iterator first, iterator last) \
460 { return (iterator)base::erase((biterator)first, (biterator)last); }\
461 iterator erase(iterator it) \
462 { return (iterator)base::erase((biterator)it); } \
463 reference front() { return *begin(); } \
464 const_reference front() const { return *begin(); } \
465 void insert(iterator it, size_type n, const_reference v) \
466 { base::insert((biterator)it, n, (bconst_reference)v); } \
467 iterator insert(iterator it, const_reference v = value_type()) \
468 { return (iterator)base::insert((biterator)it, (bconst_reference)v); }\
469 void insert(iterator it, const_iterator first, const_iterator last) \
470 { base::insert((biterator)it, (bconst_iterator)first, \
471 (bconst_iterator)last); } \
df5168c4
MB
472 void pop_back() { base::pop_back(); } \
473 void push_back(const_reference v) \
474 { base::push_back((bconst_reference)v); } \
475 reverse_iterator rbegin() { return reverse_iterator(end() - 1); } \
476 const_reverse_iterator rbegin() const; \
477 reverse_iterator rend() { return reverse_iterator(begin() - 1); } \
478 const_reverse_iterator rend() const; \
479 void reserve(size_type n) { base::reserve(n); }; \
480 void resize(size_type n, value_type v = value_type()); \
31222b7c
VZ
481}
482
6108e3fd
VZ
483#define _WX_PTROP pointer operator->() const { return m_ptr; }
484#define _WX_PTROP_NONE
485
486#define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \
487 _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, _WX_PTROP)
d5d29b8a 488#define _WX_DEFINE_TYPEARRAY_PTR(T, name, base, classexp) \
6108e3fd 489 _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, _WX_PTROP_NONE)
df5168c4
MB
490
491#endif // !wxUSE_STL
c801d85f 492
3bfa4402 493// ----------------------------------------------------------------------------
5a1cad6e
GD
494// _WX_DEFINE_SORTED_TYPEARRAY: sorted array for simple data types
495// cannot handle types with size greater than pointer because of sorting
3bfa4402 496// ----------------------------------------------------------------------------
1a931653 497
df5168c4 498#define _WX_DEFINE_SORTED_TYPEARRAY_2(T, name, base, defcomp, classexp, comptype)\
335991af 499wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \
5a1cad6e
GD
500 TypeTooBigToBeStoredInSorted##base, \
501 name); \
5a1cad6e
GD
502classexp name : public base \
503{ \
f700f98c 504 typedef comptype SCMPFUNC; \
5a1cad6e 505public: \
df5168c4 506 name(comptype fn defcomp) { m_fnCompare = fn; } \
5a1cad6e
GD
507 \
508 name& operator=(const name& src) \
509 { base* temp = (base*) this; \
510 (*temp) = ((const base&)src); \
511 m_fnCompare = src.m_fnCompare; \
512 return *this; } \
513 \
514 T& operator[](size_t uiIndex) const \
df5168c4 515 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 516 T& Item(size_t uiIndex) const \
df5168c4 517 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 518 T& Last() const \
df5168c4 519 { return (T&)(base::operator[](size() - 1)); } \
5a1cad6e
GD
520 \
521 int Index(T Item) const \
522 { return base::Index(Item, (CMPFUNC)m_fnCompare); } \
523 \
524 size_t IndexForInsert(T Item) const \
525 { return base::IndexForInsert(Item, (CMPFUNC)m_fnCompare); } \
526 \
527 void AddAt(T item, size_t index) \
df5168c4 528 { base::insert(begin() + index, item); } \
5a1cad6e 529 \
6992d326
MB
530 size_t Add(T Item) \
531 { return base::Add(Item, (CMPFUNC)m_fnCompare); } \
5a1cad6e 532 \
3b0b5f13 533 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
df5168c4 534 { base::erase(begin() + uiIndex, begin() + uiIndex + nRemove); } \
5a1cad6e
GD
535 void Remove(T Item) \
536 { int iIndex = Index(Item); \
537 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
538 _WX_ERROR_REMOVE ); \
df5168c4 539 base::erase(begin() + iIndex); } \
5a1cad6e
GD
540 \
541private: \
df5168c4 542 comptype m_fnCompare; \
3bfa4402
VZ
543}
544
df5168c4 545
c801d85f 546// ----------------------------------------------------------------------------
1a931653 547// _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics
c801d85f 548// ----------------------------------------------------------------------------
1a931653 549
5a1cad6e
GD
550#define _WX_DECLARE_OBJARRAY(T, name, base, classexp) \
551typedef int (CMPFUNC_CONV *CMPFUNC##T)(T **pItem1, T **pItem2); \
df5168c4 552classexp name : protected base \
5a1cad6e
GD
553{ \
554typedef int (CMPFUNC_CONV *CMPFUNC##base)(void **pItem1, void **pItem2); \
df5168c4 555typedef base base_array; \
5a1cad6e
GD
556public: \
557 name() { } \
558 name(const name& src); \
559 name& operator=(const name& src); \
560 \
561 ~name(); \
562 \
df5168c4
MB
563 void Alloc(size_t count) { reserve(count); } \
564 size_t GetCount() const { return base_array::size(); } \
565 size_t size() const { return base_array::size(); } \
566 bool IsEmpty() const { return base_array::empty(); } \
567 size_t Count() const { return base_array::size(); } \
568 void Shrink() { base::Shrink(); } \
569 \
5a1cad6e 570 T& operator[](size_t uiIndex) const \
df5168c4 571 { return *(T*)base::operator[](uiIndex); } \
5a1cad6e 572 T& Item(size_t uiIndex) const \
df5168c4 573 { return *(T*)base::operator[](uiIndex); } \
5a1cad6e 574 T& Last() const \
df5168c4 575 { return *(T*)(base::operator[](size() - 1)); } \
5a1cad6e
GD
576 \
577 int Index(const T& Item, bool bFromEnd = FALSE) const; \
578 \
3b0b5f13 579 void Add(const T& Item, size_t nInsert = 1); \
5a1cad6e 580 void Add(const T* pItem) \
df5168c4
MB
581 { base::push_back((T*)pItem); } \
582 void push_back(const T* pItem) \
583 { base::push_back((T*)pItem); } \
584 void push_back(const T& Item) \
585 { Add(Item); } \
5a1cad6e 586 \
3b0b5f13 587 void Insert(const T& Item, size_t uiIndex, size_t nInsert = 1); \
5a1cad6e 588 void Insert(const T* pItem, size_t uiIndex) \
df5168c4 589 { base::insert(begin() + uiIndex, (T*)pItem); } \
5a1cad6e 590 \
df5168c4
MB
591 void Empty() { DoEmpty(); base::clear(); } \
592 void Clear() { DoEmpty(); base::clear(); } \
5a1cad6e
GD
593 \
594 T* Detach(size_t uiIndex) \
df5168c4
MB
595 { T* p = (T*)base::operator[](uiIndex); \
596 base::erase(begin() + uiIndex); return p; } \
3b0b5f13 597 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
5a1cad6e
GD
598 \
599 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC##base)fCmp); } \
600 \
601private: \
602 void DoEmpty(); \
603 void DoCopy(const name& src); \
c801d85f
KB
604}
605
1a931653
VZ
606// ============================================================================
607// The public macros for declaration and definition of the dynamic arrays
608// ============================================================================
609
610// Please note that for each macro WX_FOO_ARRAY we also have
611// WX_FOO_EXPORTED_ARRAY and WX_FOO_USER_EXPORTED_ARRAY which are exactly the
612// same except that they use an additional __declspec(dllexport) or equivalent
613// under Windows if needed.
614//
615// The first (just EXPORTED) macros do it if wxWindows was compiled as a DLL
616// and so must be used used inside the library. The second kind (USER_EXPORTED)
617// allow the user code to do it when it wants. This is needed if you have a dll
618// that wants to export a wxArray daubed with your own import/export goo.
619//
620// Finally, you can define the macro below as something special to modify the
621// arrays defined by a simple WX_FOO_ARRAY as well. By default is is empty.
622#define wxARRAY_DEFAULT_EXPORT
623
624// ----------------------------------------------------------------------------
5a1cad6e
GD
625// WX_DECLARE_BASEARRAY(T, name) declare an array class named "name" containing
626// the elements of type T
627// ----------------------------------------------------------------------------
628
629#define WX_DECLARE_BASEARRAY(T, name) \
630 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
631
632#define WX_DECLARE_EXPORTED_BASEARRAY(T, name) \
633 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, WXDLLEXPORT)
634
635#define WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, expmode) \
636 typedef T _wxArray##name; \
637 _WX_DECLARE_BASEARRAY(_wxArray##name, name, class expmode)
638
639// ----------------------------------------------------------------------------
640// WX_DEFINE_TYPEARRAY(T, name, base) define an array class named "name" deriving
641// from class "base" containing the elements of type T
1a931653
VZ
642//
643// Note that the class defined has only inline function and doesn't take any
644// space at all so there is no size penalty for defining multiple array classes
c801d85f 645// ----------------------------------------------------------------------------
c801d85f 646
5a1cad6e 647#define WX_DEFINE_TYPEARRAY(T, name, base) \
68559fd5 648 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class wxARRAY_DEFAULT_EXPORT)
1a931653 649
d5d29b8a
VZ
650#define WX_DEFINE_TYPEARRAY_PTR(T, name, base) \
651 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class wxARRAY_DEFAULT_EXPORT)
6108e3fd 652
5a1cad6e 653#define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \
68559fd5 654 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class WXDLLEXPORT)
1a931653 655
d5d29b8a
VZ
656#define WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, base) \
657 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class WXDLLEXPORT)
6108e3fd 658
68559fd5
VZ
659#define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expdecl) \
660 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class expdecl)
661
d5d29b8a
VZ
662#define WX_DEFINE_USER_EXPORTED_TYPEARRAY_PTR(T, name, base, expdecl) \
663 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class expdecl)
6108e3fd 664
68559fd5 665#define WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, classdecl) \
df5168c4 666 typedef T _wxArray##name; \
68559fd5 667 _WX_DEFINE_TYPEARRAY(_wxArray##name, name, base, classdecl)
1a931653 668
d5d29b8a 669#define WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, classdecl) \
6108e3fd 670 typedef T _wxArray##name; \
d5d29b8a 671 _WX_DEFINE_TYPEARRAY_PTR(_wxArray##name, name, base, classdecl)
6108e3fd 672
1a931653 673// ----------------------------------------------------------------------------
5a1cad6e 674// WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it
1a931653
VZ
675// defines a sorted array.
676//
677// Differences:
678// 1) it must be given a COMPARE function in ctor which takes 2 items of type
679// T* and should return -1, 0 or +1 if the first one is less/greater
680// than/equal to the second one.
681// 2) the Add() method inserts the item in such was that the array is always
682// sorted (it uses the COMPARE function)
683// 3) it has no Sort() method because it's always sorted
684// 4) Index() method is much faster (the sorted arrays use binary search
685// instead of linear one), but Add() is slower.
686// 5) there is no Insert() method because you can't insert an item into the
687// given position in a sorted array but there is IndexForInsert()/AddAt()
688// pair which may be used to optimize a common operation of "insert only if
689// not found"
690//
691// Note that you have to specify the comparison function when creating the
692// objects of this array type. If, as in 99% of cases, the comparison function
5a1cad6e
GD
693// is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below
694// is more convenient.
1a931653
VZ
695//
696// Summary: use this class when the speed of Index() function is important, use
697// the normal arrays otherwise.
c801d85f
KB
698// ----------------------------------------------------------------------------
699
17e656b0
VZ
700#define wxARRAY_EMPTY_CMP
701
5a1cad6e
GD
702#define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \
703 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \
704 wxARRAY_DEFAULT_EXPORT)
a497618a 705
5a1cad6e
GD
706#define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \
707 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT)
a497618a 708
5a1cad6e
GD
709#define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \
710 typedef T _wxArray##name; \
3e49e577
MB
711 typedef int (CMPFUNC_CONV *SCMPFUNC##name)(T pItem1, T pItem2); \
712 _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, \
713 wxARRAY_EMPTY_CMP, class expmode, SCMPFUNC##name)
1a931653
VZ
714
715// ----------------------------------------------------------------------------
5a1cad6e 716// WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison
1a931653
VZ
717// function is provided by this macro and the objects of this class have a
718// default constructor which just uses it.
719//
720// The arguments are: the element type, the comparison function and the array
721// name
722//
5a1cad6e
GD
723// NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked
724// from the very beginning - unfortunately I didn't think about this earlier
1a931653 725// ----------------------------------------------------------------------------
76314141 726
5a1cad6e
GD
727#define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
728 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
729 wxARRAY_DEFAULT_EXPORT)
76314141 730
5a1cad6e
GD
731#define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
732 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
733 WXDLLEXPORT)
1a931653 734
5a1cad6e
GD
735#define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
736 expmode) \
1a931653 737 typedef T _wxArray##name; \
3e49e577
MB
738 typedef int (CMPFUNC_CONV *SCMPFUNC##name)(T pItem1, T pItem2); \
739 _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, = cmpfunc, \
740 class expmode, SCMPFUNC##name)
1a931653
VZ
741
742// ----------------------------------------------------------------------------
743// WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class
744// named "name" which owns the objects of type T it contains, i.e. it will
745// delete them when it is destroyed.
746//
747// An element is of type T*, but arguments of type T& are taken (see below!)
748// and T& is returned.
749//
750// Don't use this for simple types such as "int" or "long"!
1a931653
VZ
751//
752// Note on Add/Insert functions:
753// 1) function(T*) gives the object to the array, i.e. it will delete the
754// object when it's removed or in the array's dtor
755// 2) function(T&) will create a copy of the object and work with it
756//
757// Also:
758// 1) Remove() will delete the object after removing it from the array
759// 2) Detach() just removes the object from the array (returning pointer to it)
760//
761// NB1: Base type T should have an accessible copy ctor if Add(T&) is used
762// NB2: Never ever cast a array to it's base type: as dtor is not virtual
763// and so you risk having at least the memory leaks and probably worse
764//
765// Some functions of this class are not inline, so it takes some space to
766// define new class from this template even if you don't use it - which is not
767// the case for the simple (non-object) array classes
768//
1a931653
VZ
769// To use an objarray class you must
770// #include "dynarray.h"
771// WX_DECLARE_OBJARRAY(element_type, list_class_name)
772// #include "arrimpl.cpp"
773// WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above!
774//
775// This is necessary because at the moment of DEFINE_OBJARRAY class parsing the
776// element_type must be fully defined (i.e. forward declaration is not
777// enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
778// two allows to break cicrcular dependencies with classes which have member
779// variables of objarray type.
780// ----------------------------------------------------------------------------
781
782#define WX_DECLARE_OBJARRAY(T, name) \
783 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
784
785#define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
786 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLEXPORT)
787
fd68cfdb 788#define WX_DECLARE_OBJARRAY_WITH_DECL(T, name, decl) \
1a931653 789 typedef T _wxObjArray##name; \
fd68cfdb
VS
790 _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, wxArrayPtrVoid, decl)
791
792#define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \
793 WX_DECLARE_OBJARRAY_WITH_DECL(T, name, class expmode)
1a931653
VZ
794
795// WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included,
796// try to provoke a human-understandable error if it used incorrectly.
797//
798// there is no real need for 3 different macros in the DEFINE case but do it
799// anyhow for consistency
800#define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp
801#define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
76314141 802#define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
76314141 803
5a1cad6e
GD
804// ----------------------------------------------------------------------------
805// Some commonly used predefined base arrays
806// ----------------------------------------------------------------------------
807
eee614d3
VS
808WX_DECLARE_USER_EXPORTED_BASEARRAY(const void *, wxBaseArrayPtrVoid,
809 WXDLLIMPEXP_BASE);
6108e3fd
VZ
810WX_DECLARE_USER_EXPORTED_BASEARRAY(short, wxBaseArrayShort, WXDLLIMPEXP_BASE);
811WX_DECLARE_USER_EXPORTED_BASEARRAY(int, wxBaseArrayInt, WXDLLIMPEXP_BASE);
812WX_DECLARE_USER_EXPORTED_BASEARRAY(long, wxBaseArrayLong, WXDLLIMPEXP_BASE);
d2213b1f 813WX_DECLARE_USER_EXPORTED_BASEARRAY(size_t, wxBaseArraySizeT, WXDLLIMPEXP_BASE);
6108e3fd 814WX_DECLARE_USER_EXPORTED_BASEARRAY(double, wxBaseArrayDouble, WXDLLIMPEXP_BASE);
5a1cad6e
GD
815
816// ----------------------------------------------------------------------------
817// Convenience macros to define arrays from base arrays
818// ----------------------------------------------------------------------------
819
820#define WX_DEFINE_ARRAY(T, name) \
821 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
d5d29b8a
VZ
822#define WX_DEFINE_ARRAY_PTR(T, name) \
823 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid)
5a1cad6e
GD
824#define WX_DEFINE_EXPORTED_ARRAY(T, name) \
825 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
d5d29b8a
VZ
826#define WX_DEFINE_EXPORTED_ARRAY_PTR(T, name) \
827 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid)
828#define WX_DEFINE_ARRAY_WITH_DECL_PTR(T, name, decl) \
829 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, decl)
5a1cad6e 830#define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \
991023b4 831 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayPtrVoid, expmode)
d5d29b8a
VZ
832#define WX_DEFINE_USER_EXPORTED_ARRAY_PTR(T, name, expmode) \
833 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, expmode)
5a1cad6e
GD
834
835#define WX_DEFINE_ARRAY_SHORT(T, name) \
d5d29b8a 836 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayShort)
5a1cad6e 837#define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \
d5d29b8a 838 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayShort)
5a1cad6e 839#define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
d5d29b8a 840 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayShort, expmode)
5a1cad6e
GD
841
842#define WX_DEFINE_ARRAY_INT(T, name) \
d5d29b8a 843 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayInt)
5a1cad6e 844#define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \
d5d29b8a 845 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayInt)
5a1cad6e 846#define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
d5d29b8a 847 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayInt, expmode)
5a1cad6e
GD
848
849#define WX_DEFINE_ARRAY_LONG(T, name) \
d5d29b8a 850 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayLong)
5a1cad6e 851#define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \
d5d29b8a 852 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayLong)
5a1cad6e 853#define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
d5d29b8a 854 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayLong, expmode)
5a1cad6e 855
d2213b1f
VZ
856#define WX_DEFINE_ARRAY_SIZE_T(T, name) \
857 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArraySizeT)
858#define WX_DEFINE_EXPORTED_ARRAY_SIZE_T(T, name) \
859 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArraySizeT)
860#define WX_DEFINE_USER_EXPORTED_ARRAY_SIZE_T(T, name, expmode) \
861 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArraySizeT, expmode)
862
5a1cad6e 863#define WX_DEFINE_ARRAY_DOUBLE(T, name) \
d5d29b8a 864 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayDouble)
5a1cad6e 865#define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \
d5d29b8a 866 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayDouble)
5a1cad6e 867#define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \
d5d29b8a 868 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayDouble, expmode)
5a1cad6e
GD
869
870// ----------------------------------------------------------------------------
871// Convenience macros to define sorted arrays from base arrays
872// ----------------------------------------------------------------------------
873
874#define WX_DEFINE_SORTED_ARRAY(T, name) \
875 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
876#define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
877 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
878#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \
879 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode)
880
881#define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \
882 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort)
883#define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \
884 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort)
885#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
886 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode)
887
888#define WX_DEFINE_SORTED_ARRAY_INT(T, name) \
889 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt)
890#define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \
891 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt)
892#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
893 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode)
894
895#define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \
896 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong)
897#define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \
898 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong)
899#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
900 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode)
901
d2213b1f
VZ
902#define WX_DEFINE_SORTED_ARRAY_SIZE_T(T, name) \
903 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArraySizeT)
904#define WX_DEFINE_SORTED_EXPORTED_ARRAY_SIZE_T(T, name) \
905 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArraySizeT)
906#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(T, name, expmode) \
907 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArraySizeT, expmode)
908
5a1cad6e
GD
909// ----------------------------------------------------------------------------
910// Convenience macros to define sorted arrays from base arrays
911// ----------------------------------------------------------------------------
912
913#define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \
914 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
915#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \
916 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
917#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \
918 name, expmode) \
919 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
920 wxBaseArrayPtrVoid, expmode)
921
922#define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
923 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
924#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
925 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
926#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \
927 name, expmode) \
928 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
929 wxBaseArrayShort, expmode)
930
931#define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
932 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
933#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
934 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
935#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \
936 name, expmode) \
937 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
938 wxBaseArrayInt, expmode)
939
940#define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
941 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
942#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
943 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
944#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \
945 name, expmode) \
946 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
947 wxBaseArrayLong, expmode)
948
d2213b1f
VZ
949#define WX_DEFINE_SORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, name) \
950 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArraySizeT)
951#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, name) \
952 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArraySizeT)
953#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, \
954 name, expmode) \
955 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
956 wxBaseArraySizeT, expmode)
957
c801d85f 958// ----------------------------------------------------------------------------
1a931653 959// Some commonly used predefined arrays
c801d85f
KB
960// ----------------------------------------------------------------------------
961
6108e3fd
VZ
962WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(short, wxArrayShort, class WXDLLIMPEXP_BASE);
963WX_DEFINE_USER_EXPORTED_ARRAY_INT(int, wxArrayInt, class WXDLLIMPEXP_BASE);
964WX_DEFINE_USER_EXPORTED_ARRAY_LONG(long, wxArrayLong, class WXDLLIMPEXP_BASE);
d5d29b8a 965WX_DEFINE_USER_EXPORTED_ARRAY_PTR(void *, wxArrayPtrVoid, class WXDLLIMPEXP_BASE);
c801d85f 966
2b9bd418 967// -----------------------------------------------------------------------------
0cbff120 968// convenience macros
2b9bd418
VZ
969// -----------------------------------------------------------------------------
970
4f6aed9c
VZ
971// append all element of one array to another one
972#define WX_APPEND_ARRAY(array, other) \
973 { \
df5168c4 974 size_t count = (other).size(); \
4f6aed9c
VZ
975 for ( size_t n = 0; n < count; n++ ) \
976 { \
df5168c4 977 (array).push_back((other)[n]); \
4f6aed9c
VZ
978 } \
979 }
980
2b9bd418
VZ
981// delete all array elements
982//
983// NB: the class declaration of the array elements must be visible from the
984// place where you use this macro, otherwise the proper destructor may not
985// be called (a decent compiler should give a warning about it, but don't
986// count on it)!
987#define WX_CLEAR_ARRAY(array) \
988 { \
df5168c4 989 size_t count = (array).size(); \
2b9bd418
VZ
990 for ( size_t n = 0; n < count; n++ ) \
991 { \
5d5b1c0c 992 delete (array)[n]; \
2b9bd418 993 } \
2c356747 994 \
df5168c4 995 (array).clear(); \
2b9bd418 996 }
a497618a 997
c801d85f
KB
998#endif // _DYNARRAY_H
999