]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynarray.h
Add functor-taking overload of CallAfter().
[wxWidgets.git] / include / wx / dynarray.h
CommitLineData
c801d85f 1///////////////////////////////////////////////////////////////////////////////
ce7208d4 2// Name: wx/dynarray.h
c801d85f
KB
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>
65571936 9// Licence: wxWindows licence
c801d85f
KB
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _DYNARRAY_H
13#define _DYNARRAY_H
14
c801d85f 15#include "wx/defs.h"
c801d85f 16
01871bf6 17#if wxUSE_STD_CONTAINERS
df5168c4
MB
18 #include "wx/beforestd.h"
19 #include <vector>
20 #include <algorithm>
21 #include "wx/afterstd.h"
df5168c4
MB
22#endif
23
1a931653
VZ
24/*
25 This header defines the dynamic arrays and object arrays (i.e. arrays which
26 own their elements). Dynamic means that the arrays grow automatically as
27 needed.
28
29 These macros are ugly (especially if you look in the sources ;-), but they
30 allow us to define "template" classes without actually using templates and so
31 this works with all compilers (and may be also much faster to compile even
32 with a compiler which does support templates). The arrays defined with these
33 macros are type-safe.
34
35 Range checking is performed in debug build for both arrays and objarrays but
36 not in release build - so using an invalid index will just lead to a crash
37 then.
38
39 Note about memory usage: arrays never shrink automatically (although you may
40 use Shrink() function explicitly), they only grow, so loading 10 millions in
41 an array only to delete them 2 lines below might be a bad idea if the array
42 object is not going to be destroyed soon. However, as it does free memory
43 when destroyed, it is ok if the array is a local variable.
44 */
c801d85f
KB
45
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
1a931653
VZ
50/*
51 The initial size by which an array grows when an element is added default
52 value avoids allocate one or two bytes when the array is created which is
53 rather inefficient
c801d85f 54*/
1a931653 55#define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
c801d85f 56
35d5da67
VZ
57#define _WX_ERROR_REMOVE "removing inexistent element in wxArray::Remove"
58
c801d85f
KB
59// ----------------------------------------------------------------------------
60// types
61// ----------------------------------------------------------------------------
62
1a931653
VZ
63/*
64 Callback compare function for quick sort.
3b0b5f13 65
1a931653
VZ
66 It must return negative value, 0 or positive value if the first item is
67 less than, equal to or greater than the second one.
c801d85f 68 */
90350682
VZ
69extern "C"
70{
0661ec39 71typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
90350682 72}
c801d85f
KB
73
74// ----------------------------------------------------------------------------
1a931653
VZ
75// Base class managing data having size of type 'long' (not used directly)
76//
77// NB: for efficiency this often used class has no virtual functions (hence no
78// virtual table), even dtor is *not* virtual. If used as expected it
79// won't create any problems because ARRAYs from DEFINE_ARRAY have no dtor
80// at all, so it's not too important if it's not called (this happens when
81// you cast "SomeArray *" as "BaseArray *" and then delete it)
c801d85f 82// ----------------------------------------------------------------------------
1a931653 83
01871bf6 84#if wxUSE_STD_CONTAINERS
df5168c4 85
f700f98c 86template<class T>
638205e5 87class wxArray_SortFunction
f700f98c
MB
88{
89public:
90 typedef int (wxCMPFUNC_CONV *CMPFUNC)(T* pItem1, T* pItem2);
91
92 wxArray_SortFunction(CMPFUNC f) : m_f(f) { }
93 bool operator()(const T& i1, const T& i2)
94 { return m_f((T*)&i1, (T*)&i2) < 0; }
95private:
96 CMPFUNC m_f;
97};
98
99template<class T, typename F>
638205e5 100class wxSortedArray_SortFunction
f700f98c
MB
101{
102public:
103 typedef F CMPFUNC;
104
105 wxSortedArray_SortFunction(CMPFUNC f) : m_f(f) { }
106 bool operator()(const T& i1, const T& i2)
107 { return m_f(i1, i2) < 0; }
108private:
109 CMPFUNC m_f;
110};
111
df5168c4 112#define _WX_DECLARE_BASEARRAY(T, name, classexp) \
f700f98c
MB
113 typedef int (wxCMPFUNC_CONV *CMPFUN##name)(T pItem1, T pItem2); \
114 typedef wxSortedArray_SortFunction<T, CMPFUN##name> name##_Predicate; \
115 _WX_DECLARE_BASEARRAY_2(T, name, name##_Predicate, classexp)
116
117#define _WX_DECLARE_BASEARRAY_2(T, name, predicate, classexp) \
df5168c4
MB
118classexp name : public std::vector<T> \
119{ \
f700f98c
MB
120 typedef predicate Predicate; \
121 typedef predicate::CMPFUNC SCMPFUNC; \
7df07b10 122public: \
f700f98c 123 typedef wxArray_SortFunction<T>::CMPFUNC CMPFUNC; \
35d5da67 124 \
df5168c4 125public: \
35d5da67
VZ
126 typedef T base_type; \
127 \
b1f8bee5
VZ
128 name() : std::vector<T>() { } \
129 name(size_type n) : std::vector<T>(n) { } \
130 name(size_type n, const_reference v) : std::vector<T>(n, v) { } \
41da7d97
VZ
131 template <class InputIterator> \
132 name(InputIterator first, InputIterator last) : std::vector<T>(first, last) { } \
b1f8bee5 133 \
df5168c4
MB
134 void Empty() { clear(); } \
135 void Clear() { clear(); } \
136 void Alloc(size_t uiSize) { reserve(uiSize); } \
35d5da67 137 void Shrink() { name tmp(*this); swap(tmp); } \
df5168c4
MB
138 \
139 size_t GetCount() const { return size(); } \
140 void SetCount(size_t n, T v = T()) { resize(n, v); } \
141 bool IsEmpty() const { return empty(); } \
142 size_t Count() const { return size(); } \
143 \
df5168c4
MB
144 T& Item(size_t uiIndex) const \
145 { wxASSERT( uiIndex < size() ); return (T&)operator[](uiIndex); } \
35d5da67 146 T& Last() const { return Item(size() - 1); } \
df5168c4 147 \
35d5da67
VZ
148 int Index(T item, bool bFromEnd = false) const \
149 { \
150 if ( bFromEnd ) \
151 { \
152 const const_reverse_iterator b = rbegin(), \
153 e = rend(); \
154 for ( const_reverse_iterator i = b; i != e; ++i ) \
155 if ( *i == item ) \
12a7fb5f 156 return (int)(e - i - 1); \
35d5da67
VZ
157 } \
158 else \
159 { \
160 const const_iterator b = begin(), \
161 e = end(); \
162 for ( const_iterator i = b; i != e; ++i ) \
163 if ( *i == item ) \
164 return (int)(i - b); \
165 } \
166 \
167 return wxNOT_FOUND; \
168 } \
169 int Index(T lItem, CMPFUNC fnCompare) const \
170 { \
171 Predicate p((SCMPFUNC)fnCompare); \
172 const_iterator i = std::lower_bound(begin(), end(), lItem, p);\
173 return i != end() && !p(lItem, *i) ? (int)(i - begin()) \
174 : wxNOT_FOUND; \
175 } \
176 size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const \
177 { \
178 Predicate p((SCMPFUNC)fnCompare); \
179 const_iterator i = std::lower_bound(begin(), end(), lItem, p);\
180 return i - begin(); \
181 } \
df5168c4
MB
182 void Add(T lItem, size_t nInsert = 1) \
183 { insert(end(), nInsert, lItem); } \
35d5da67
VZ
184 size_t Add(T lItem, CMPFUNC fnCompare) \
185 { \
186 size_t n = IndexForInsert(lItem, fnCompare); \
187 Insert(lItem, n); \
188 return n; \
189 } \
df5168c4
MB
190 void Insert(T lItem, size_t uiIndex, size_t nInsert = 1) \
191 { insert(begin() + uiIndex, nInsert, lItem); } \
35d5da67
VZ
192 void Remove(T lItem) \
193 { \
194 int n = Index(lItem); \
195 wxCHECK_RET( n != wxNOT_FOUND, _WX_ERROR_REMOVE ); \
196 RemoveAt((size_t)n); \
197 } \
df5168c4
MB
198 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
199 { erase(begin() + uiIndex, begin() + uiIndex + nRemove); } \
200 \
201 void Sort(CMPFUNC fCmp) \
202 { \
f700f98c 203 wxArray_SortFunction<T> p(fCmp); \
df5168c4
MB
204 std::sort(begin(), end(), p); \
205 } \
df5168c4
MB
206}
207
01871bf6 208#else // if !wxUSE_STD_CONTAINERS
df5168c4 209
5a1cad6e
GD
210#define _WX_DECLARE_BASEARRAY(T, name, classexp) \
211classexp name \
212{ \
01871bf6 213 typedef CMPFUNC SCMPFUNC; /* for compatibility wuth wxUSE_STD_CONTAINERS */ \
5a1cad6e
GD
214public: \
215 name(); \
216 name(const name& array); \
217 name& operator=(const name& src); \
218 ~name(); \
219 \
220 void Empty() { m_nCount = 0; } \
221 void Clear(); \
7788fc40 222 void Alloc(size_t n) { if ( n > m_nSize ) Realloc(n); } \
5a1cad6e
GD
223 void Shrink(); \
224 \
2abb9d2f 225 size_t GetCount() const { return m_nCount; } \
9cde322b 226 void SetCount(size_t n, T defval = T()); \
2abb9d2f
VZ
227 bool IsEmpty() const { return m_nCount == 0; } \
228 size_t Count() const { return m_nCount; } \
5a1cad6e 229 \
75d7ef36 230 typedef T base_type; \
2abb9d2f 231 \
5a1cad6e
GD
232protected: \
233 T& Item(size_t uiIndex) const \
234 { wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; } \
235 T& operator[](size_t uiIndex) const { return Item(uiIndex); } \
236 \
68379eaf 237 int Index(T lItem, bool bFromEnd = false) const; \
5a1cad6e
GD
238 int Index(T lItem, CMPFUNC fnCompare) const; \
239 size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const; \
3b0b5f13 240 void Add(T lItem, size_t nInsert = 1); \
6992d326 241 size_t Add(T lItem, CMPFUNC fnCompare); \
3b0b5f13 242 void Insert(T lItem, size_t uiIndex, size_t nInsert = 1); \
5a1cad6e 243 void Remove(T lItem); \
3b0b5f13 244 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
5a1cad6e
GD
245 \
246 void Sort(CMPFUNC fnCompare); \
247 \
df5168c4
MB
248 /* *minimal* STL-ish interface, for derived classes */ \
249 typedef T value_type; \
250 typedef value_type* iterator; \
251 typedef const value_type* const_iterator; \
252 typedef value_type& reference; \
253 typedef const value_type& const_reference; \
17473a77 254 typedef ptrdiff_t difference_type; \
df5168c4
MB
255 typedef size_t size_type; \
256 \
257 void assign(const_iterator first, const_iterator last); \
258 void assign(size_type n, const_reference v); \
259 size_type capacity() const { return m_nSize; } \
df5168c4
MB
260 iterator erase(iterator first, iterator last) \
261 { \
262 size_type idx = first - begin(); \
263 RemoveAt(idx, last - first); \
264 return begin() + idx; \
265 } \
266 iterator erase(iterator it) { return erase(it, it + 1); } \
267 void insert(iterator it, size_type n, const value_type& v) \
268 { Insert(v, it - begin(), n); } \
269 iterator insert(iterator it, const value_type& v = value_type()) \
270 { \
271 size_type idx = it - begin(); \
272 Insert(v, idx); \
273 return begin() + idx; \
274 } \
275 void insert(iterator it, const_iterator first, const_iterator last);\
df5168c4
MB
276 void pop_back() { RemoveAt(size() - 1); } \
277 void push_back(const value_type& v) { Add(v); } \
7788fc40 278 void reserve(size_type n) { Alloc(n); } \
bed49287
VZ
279 void resize(size_type count, value_type defval = value_type()) \
280 { \
281 if ( count < m_nCount ) \
282 m_nCount = count; \
283 else \
284 SetCount(count, defval); \
285 } \
df5168c4
MB
286 \
287 iterator begin() { return m_pItems; } \
288 iterator end() { return m_pItems + m_nCount; } \
289 const_iterator begin() const { return m_pItems; } \
290 const_iterator end() const { return m_pItems + m_nCount; } \
5da0803c 291 \
b2794028
VZ
292 void swap(name& other) \
293 { \
1372f8cc
VZ
294 wxSwap(m_nSize, other.m_nSize); \
295 wxSwap(m_nCount, other.m_nCount); \
296 wxSwap(m_pItems, other.m_pItems); \
b2794028
VZ
297 } \
298 \
5da0803c
VZ
299 /* the following functions may be made directly public because */ \
300 /* they don't use the type of the elements at all */ \
301public: \
302 void clear() { Clear(); } \
303 bool empty() const { return IsEmpty(); } \
304 size_type max_size() const { return INT_MAX; } \
305 size_type size() const { return GetCount(); } \
306 \
5a1cad6e 307private: \
2abb9d2f
VZ
308 void Grow(size_t nIncrement = 0); \
309 bool Realloc(size_t nSize); \
5a1cad6e
GD
310 \
311 size_t m_nSize, \
312 m_nCount; \
313 \
314 T *m_pItems; \
e9bb94cf 315}
c801d85f 316
01871bf6 317#endif // !wxUSE_STD_CONTAINERS
df5168c4 318
c801d85f 319// ============================================================================
1a931653 320// The private helper macros containing the core of the array classes
c801d85f
KB
321// ============================================================================
322
1a931653
VZ
323// Implementation notes:
324//
325// JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
326// { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
327// so using a temporary variable instead.
328//
329// The classes need a (even trivial) ~name() to link under Mac X
e90c1d2a 330
c801d85f 331// ----------------------------------------------------------------------------
5a1cad6e 332// _WX_DEFINE_TYPEARRAY: array for simple types
c801d85f 333// ----------------------------------------------------------------------------
1a931653 334
01871bf6 335#if wxUSE_STD_CONTAINERS
df5168c4 336
35d5da67
VZ
337// in STL case we don't need the entire base arrays hack as standard container
338// don't suffer from alignment/storage problems as our home-grown do
df5168c4 339#define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \
35d5da67 340 _WX_DECLARE_BASEARRAY(T, name, classexp)
df5168c4 341
d5d29b8a 342#define _WX_DEFINE_TYPEARRAY_PTR(T, name, base, classexp) \
68379eaf 343 _WX_DEFINE_TYPEARRAY(T, name, base, classexp)
6108e3fd 344
01871bf6 345#else // if !wxUSE_STD_CONTAINERS
df5168c4 346
6108e3fd 347// common declaration used by both _WX_DEFINE_TYPEARRAY and
d5d29b8a 348// _WX_DEFINE_TYPEARRAY_PTR
6108e3fd 349#define _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, ptrop) \
1d6560d7
VZ
350wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \
351 TypeTooBigToBeStoredIn##base, \
352 name); \
5a1cad6e
GD
353typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
354classexp name : public base \
355{ \
356public: \
357 name() { } \
358 ~name() { } \
359 \
5a1cad6e 360 T& operator[](size_t uiIndex) const \
df5168c4 361 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 362 T& Item(size_t uiIndex) const \
df5168c4 363 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 364 T& Last() const \
b4a980f4 365 { return (T&)(base::operator[](GetCount() - 1)); } \
5a1cad6e 366 \
222702b1
WS
367 int Index(T lItem, bool bFromEnd = false) const \
368 { return base::Index((base_type)lItem, bFromEnd); } \
5a1cad6e 369 \
222702b1
WS
370 void Add(T lItem, size_t nInsert = 1) \
371 { base::Add((base_type)lItem, nInsert); } \
372 void Insert(T lItem, size_t uiIndex, size_t nInsert = 1) \
373 { base::Insert((base_type)lItem, uiIndex, nInsert) ; } \
5a1cad6e 374 \
3b0b5f13
VZ
375 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
376 { base::RemoveAt(uiIndex, nRemove); } \
222702b1
WS
377 void Remove(T lItem) \
378 { int iIndex = Index(lItem); \
35d5da67 379 wxCHECK_RET( iIndex != wxNOT_FOUND, _WX_ERROR_REMOVE); \
5a1cad6e
GD
380 base::RemoveAt((size_t)iIndex); } \
381 \
382 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \
df5168c4
MB
383 \
384 /* STL-like interface */ \
385private: \
386 typedef base::iterator biterator; \
387 typedef base::const_iterator bconst_iterator; \
388 typedef base::value_type bvalue_type; \
389 typedef base::const_reference bconst_reference; \
390public: \
391 typedef T value_type; \
392 typedef value_type* pointer; \
393 typedef const value_type* const_pointer; \
394 typedef value_type* iterator; \
395 typedef const value_type* const_iterator; \
396 typedef value_type& reference; \
397 typedef const value_type& const_reference; \
398 typedef base::difference_type difference_type; \
399 typedef base::size_type size_type; \
400 \
401 class reverse_iterator \
402 { \
c514cd53
MB
403 typedef T value_type; \
404 typedef value_type& reference; \
405 typedef value_type* pointer; \
df5168c4 406 typedef reverse_iterator itor; \
31222b7c
VZ
407 friend inline itor operator+(int o, const itor& it) \
408 { return it.m_ptr - o; } \
409 friend inline itor operator+(const itor& it, int o) \
410 { return it.m_ptr - o; } \
411 friend inline itor operator-(const itor& it, int o) \
412 { return it.m_ptr + o; } \
413 friend inline difference_type operator-(const itor& i1, \
414 const itor& i2) \
415 { return i1.m_ptr - i2.m_ptr; } \
416 \
df5168c4
MB
417 public: \
418 pointer m_ptr; \
419 reverse_iterator() : m_ptr(NULL) { } \
420 reverse_iterator(pointer ptr) : m_ptr(ptr) { } \
421 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { } \
422 reference operator*() const { return *m_ptr; } \
6108e3fd 423 ptrop \
60d8e886
VZ
424 itor& operator++() { --m_ptr; return *this; } \
425 const itor operator++(int) \
df5168c4 426 { reverse_iterator tmp = *this; --m_ptr; return tmp; } \
60d8e886
VZ
427 itor& operator--() { ++m_ptr; return *this; } \
428 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }\
fbfb8bcc
VZ
429 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }\
430 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }\
df5168c4
MB
431 }; \
432 \
433 class const_reverse_iterator \
434 { \
c514cd53
MB
435 typedef T value_type; \
436 typedef const value_type& reference; \
437 typedef const value_type* pointer; \
df5168c4 438 typedef const_reverse_iterator itor; \
31222b7c
VZ
439 friend inline itor operator+(int o, const itor& it) \
440 { return it.m_ptr - o; } \
441 friend inline itor operator+(const itor& it, int o) \
442 { return it.m_ptr - o; } \
443 friend inline itor operator-(const itor& it, int o) \
444 { return it.m_ptr + o; } \
445 friend inline difference_type operator-(const itor& i1, \
446 const itor& i2) \
447 { return i1.m_ptr - i2.m_ptr; } \
448 \
df5168c4
MB
449 public: \
450 pointer m_ptr; \
451 const_reverse_iterator() : m_ptr(NULL) { } \
452 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { } \
453 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { } \
454 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }\
455 reference operator*() const { return *m_ptr; } \
6108e3fd 456 ptrop \
60d8e886
VZ
457 itor& operator++() { --m_ptr; return *this; } \
458 const itor operator++(int) \
df5168c4 459 { itor tmp = *this; --m_ptr; return tmp; } \
60d8e886
VZ
460 itor& operator--() { ++m_ptr; return *this; } \
461 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }\
fbfb8bcc
VZ
462 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }\
463 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }\
df5168c4
MB
464 }; \
465 \
5041b46f 466 name(size_type n) { assign(n, value_type()); } \
584ad2a3
MB
467 name(size_type n, const_reference v) { assign(n, v); } \
468 name(const_iterator first, const_iterator last) \
469 { assign(first, last); } \
df5168c4
MB
470 void assign(const_iterator first, const_iterator last) \
471 { base::assign((bconst_iterator)first, (bconst_iterator)last); } \
472 void assign(size_type n, const_reference v) \
473 { base::assign(n, (bconst_reference)v); } \
474 reference back() { return *(end() - 1); } \
475 const_reference back() const { return *(end() - 1); } \
476 iterator begin() { return (iterator)base::begin(); } \
477 const_iterator begin() const { return (const_iterator)base::begin(); }\
478 size_type capacity() const { return base::capacity(); } \
df5168c4
MB
479 iterator end() { return (iterator)base::end(); } \
480 const_iterator end() const { return (const_iterator)base::end(); } \
481 iterator erase(iterator first, iterator last) \
482 { return (iterator)base::erase((biterator)first, (biterator)last); }\
483 iterator erase(iterator it) \
484 { return (iterator)base::erase((biterator)it); } \
485 reference front() { return *begin(); } \
486 const_reference front() const { return *begin(); } \
487 void insert(iterator it, size_type n, const_reference v) \
488 { base::insert((biterator)it, n, (bconst_reference)v); } \
489 iterator insert(iterator it, const_reference v = value_type()) \
490 { return (iterator)base::insert((biterator)it, (bconst_reference)v); }\
491 void insert(iterator it, const_iterator first, const_iterator last) \
492 { base::insert((biterator)it, (bconst_iterator)first, \
493 (bconst_iterator)last); } \
df5168c4
MB
494 void pop_back() { base::pop_back(); } \
495 void push_back(const_reference v) \
496 { base::push_back((bconst_reference)v); } \
497 reverse_iterator rbegin() { return reverse_iterator(end() - 1); } \
498 const_reverse_iterator rbegin() const; \
499 reverse_iterator rend() { return reverse_iterator(begin() - 1); } \
500 const_reverse_iterator rend() const; \
47b378bd 501 void reserve(size_type n) { base::reserve(n); } \
bf004951
VZ
502 void resize(size_type n, value_type v = value_type()) \
503 { base::resize(n, v); } \
b2794028 504 void swap(name& other) { base::swap(other); } \
31222b7c
VZ
505}
506
6108e3fd
VZ
507#define _WX_PTROP pointer operator->() const { return m_ptr; }
508#define _WX_PTROP_NONE
509
510#define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \
511 _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, _WX_PTROP)
d5d29b8a 512#define _WX_DEFINE_TYPEARRAY_PTR(T, name, base, classexp) \
6108e3fd 513 _WX_DEFINE_TYPEARRAY_HELPER(T, name, base, classexp, _WX_PTROP_NONE)
df5168c4 514
01871bf6 515#endif // !wxUSE_STD_CONTAINERS
c801d85f 516
3bfa4402 517// ----------------------------------------------------------------------------
5a1cad6e
GD
518// _WX_DEFINE_SORTED_TYPEARRAY: sorted array for simple data types
519// cannot handle types with size greater than pointer because of sorting
3bfa4402 520// ----------------------------------------------------------------------------
1a931653 521
df5168c4 522#define _WX_DEFINE_SORTED_TYPEARRAY_2(T, name, base, defcomp, classexp, comptype)\
1d6560d7
VZ
523wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \
524 TypeTooBigToBeStoredInSorted##base, \
525 name); \
5a1cad6e
GD
526classexp name : public base \
527{ \
f700f98c 528 typedef comptype SCMPFUNC; \
5a1cad6e 529public: \
df5168c4 530 name(comptype fn defcomp) { m_fnCompare = fn; } \
5a1cad6e
GD
531 \
532 name& operator=(const name& src) \
533 { base* temp = (base*) this; \
534 (*temp) = ((const base&)src); \
535 m_fnCompare = src.m_fnCompare; \
536 return *this; } \
537 \
538 T& operator[](size_t uiIndex) const \
df5168c4 539 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 540 T& Item(size_t uiIndex) const \
df5168c4 541 { return (T&)(base::operator[](uiIndex)); } \
5a1cad6e 542 T& Last() const \
df5168c4 543 { return (T&)(base::operator[](size() - 1)); } \
5a1cad6e 544 \
222702b1
WS
545 int Index(T lItem) const \
546 { return base::Index(lItem, (CMPFUNC)m_fnCompare); } \
5a1cad6e 547 \
222702b1
WS
548 size_t IndexForInsert(T lItem) const \
549 { return base::IndexForInsert(lItem, (CMPFUNC)m_fnCompare); } \
5a1cad6e
GD
550 \
551 void AddAt(T item, size_t index) \
df5168c4 552 { base::insert(begin() + index, item); } \
5a1cad6e 553 \
222702b1
WS
554 size_t Add(T lItem) \
555 { return base::Add(lItem, (CMPFUNC)m_fnCompare); } \
0287ae5c
VZ
556 void push_back(T lItem) \
557 { Add(lItem); } \
5a1cad6e 558 \
3b0b5f13 559 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
df5168c4 560 { base::erase(begin() + uiIndex, begin() + uiIndex + nRemove); } \
222702b1
WS
561 void Remove(T lItem) \
562 { int iIndex = Index(lItem); \
35d5da67 563 wxCHECK_RET( iIndex != wxNOT_FOUND, _WX_ERROR_REMOVE ); \
df5168c4 564 base::erase(begin() + iIndex); } \
5a1cad6e
GD
565 \
566private: \
df5168c4 567 comptype m_fnCompare; \
3bfa4402
VZ
568}
569
df5168c4 570
c801d85f 571// ----------------------------------------------------------------------------
1a931653 572// _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics
c801d85f 573// ----------------------------------------------------------------------------
1a931653 574
5a1cad6e
GD
575#define _WX_DECLARE_OBJARRAY(T, name, base, classexp) \
576typedef int (CMPFUNC_CONV *CMPFUNC##T)(T **pItem1, T **pItem2); \
df5168c4 577classexp name : protected base \
5a1cad6e
GD
578{ \
579typedef int (CMPFUNC_CONV *CMPFUNC##base)(void **pItem1, void **pItem2); \
df5168c4 580typedef base base_array; \
5a1cad6e
GD
581public: \
582 name() { } \
583 name(const name& src); \
584 name& operator=(const name& src); \
585 \
586 ~name(); \
587 \
43de4157
VS
588 void Alloc(size_t count) { base::reserve(count); } \
589 void reserve(size_t count) { base::reserve(count); } \
df5168c4
MB
590 size_t GetCount() const { return base_array::size(); } \
591 size_t size() const { return base_array::size(); } \
592 bool IsEmpty() const { return base_array::empty(); } \
009c4392 593 bool empty() const { return base_array::empty(); } \
df5168c4
MB
594 size_t Count() const { return base_array::size(); } \
595 void Shrink() { base::Shrink(); } \
596 \
5a1cad6e 597 T& operator[](size_t uiIndex) const \
df5168c4 598 { return *(T*)base::operator[](uiIndex); } \
5a1cad6e 599 T& Item(size_t uiIndex) const \
df5168c4 600 { return *(T*)base::operator[](uiIndex); } \
5a1cad6e 601 T& Last() const \
df5168c4 602 { return *(T*)(base::operator[](size() - 1)); } \
5a1cad6e 603 \
222702b1 604 int Index(const T& lItem, bool bFromEnd = false) const; \
5a1cad6e 605 \
222702b1 606 void Add(const T& lItem, size_t nInsert = 1); \
5a1cad6e 607 void Add(const T* pItem) \
df5168c4
MB
608 { base::push_back((T*)pItem); } \
609 void push_back(const T* pItem) \
610 { base::push_back((T*)pItem); } \
222702b1
WS
611 void push_back(const T& lItem) \
612 { Add(lItem); } \
5a1cad6e 613 \
222702b1 614 void Insert(const T& lItem, size_t uiIndex, size_t nInsert = 1); \
5a1cad6e 615 void Insert(const T* pItem, size_t uiIndex) \
df5168c4 616 { base::insert(begin() + uiIndex, (T*)pItem); } \
5a1cad6e 617 \
df5168c4
MB
618 void Empty() { DoEmpty(); base::clear(); } \
619 void Clear() { DoEmpty(); base::clear(); } \
5a1cad6e
GD
620 \
621 T* Detach(size_t uiIndex) \
df5168c4
MB
622 { T* p = (T*)base::operator[](uiIndex); \
623 base::erase(begin() + uiIndex); return p; } \
3b0b5f13 624 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
5a1cad6e
GD
625 \
626 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC##base)fCmp); } \
627 \
628private: \
629 void DoEmpty(); \
630 void DoCopy(const name& src); \
c801d85f
KB
631}
632
1a931653
VZ
633// ============================================================================
634// The public macros for declaration and definition of the dynamic arrays
635// ============================================================================
636
637// Please note that for each macro WX_FOO_ARRAY we also have
638// WX_FOO_EXPORTED_ARRAY and WX_FOO_USER_EXPORTED_ARRAY which are exactly the
639// same except that they use an additional __declspec(dllexport) or equivalent
640// under Windows if needed.
641//
77ffb593 642// The first (just EXPORTED) macros do it if wxWidgets was compiled as a DLL
1a931653
VZ
643// and so must be used used inside the library. The second kind (USER_EXPORTED)
644// allow the user code to do it when it wants. This is needed if you have a dll
645// that wants to export a wxArray daubed with your own import/export goo.
646//
647// Finally, you can define the macro below as something special to modify the
d13b34d3 648// arrays defined by a simple WX_FOO_ARRAY as well. By default is empty.
1a931653
VZ
649#define wxARRAY_DEFAULT_EXPORT
650
651// ----------------------------------------------------------------------------
5a1cad6e
GD
652// WX_DECLARE_BASEARRAY(T, name) declare an array class named "name" containing
653// the elements of type T
654// ----------------------------------------------------------------------------
655
656#define WX_DECLARE_BASEARRAY(T, name) \
657 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
658
659#define WX_DECLARE_EXPORTED_BASEARRAY(T, name) \
53a2db12 660 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, WXDLLIMPEXP_CORE)
5a1cad6e
GD
661
662#define WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, expmode) \
663 typedef T _wxArray##name; \
664 _WX_DECLARE_BASEARRAY(_wxArray##name, name, class expmode)
665
666// ----------------------------------------------------------------------------
667// WX_DEFINE_TYPEARRAY(T, name, base) define an array class named "name" deriving
668// from class "base" containing the elements of type T
1a931653
VZ
669//
670// Note that the class defined has only inline function and doesn't take any
671// space at all so there is no size penalty for defining multiple array classes
c801d85f 672// ----------------------------------------------------------------------------
c801d85f 673
5a1cad6e 674#define WX_DEFINE_TYPEARRAY(T, name, base) \
68559fd5 675 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class wxARRAY_DEFAULT_EXPORT)
1a931653 676
d5d29b8a
VZ
677#define WX_DEFINE_TYPEARRAY_PTR(T, name, base) \
678 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class wxARRAY_DEFAULT_EXPORT)
6108e3fd 679
5a1cad6e 680#define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \
53a2db12 681 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class WXDLLIMPEXP_CORE)
1a931653 682
d5d29b8a 683#define WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, base) \
53a2db12 684 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class WXDLLIMPEXP_CORE)
6108e3fd 685
68559fd5
VZ
686#define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expdecl) \
687 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class expdecl)
688
d5d29b8a
VZ
689#define WX_DEFINE_USER_EXPORTED_TYPEARRAY_PTR(T, name, base, expdecl) \
690 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class expdecl)
6108e3fd 691
68559fd5 692#define WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, classdecl) \
df5168c4 693 typedef T _wxArray##name; \
68559fd5 694 _WX_DEFINE_TYPEARRAY(_wxArray##name, name, base, classdecl)
1a931653 695
d5d29b8a 696#define WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, classdecl) \
6108e3fd 697 typedef T _wxArray##name; \
d5d29b8a 698 _WX_DEFINE_TYPEARRAY_PTR(_wxArray##name, name, base, classdecl)
6108e3fd 699
1a931653 700// ----------------------------------------------------------------------------
5a1cad6e 701// WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it
1a931653
VZ
702// defines a sorted array.
703//
704// Differences:
705// 1) it must be given a COMPARE function in ctor which takes 2 items of type
706// T* and should return -1, 0 or +1 if the first one is less/greater
707// than/equal to the second one.
708// 2) the Add() method inserts the item in such was that the array is always
709// sorted (it uses the COMPARE function)
710// 3) it has no Sort() method because it's always sorted
711// 4) Index() method is much faster (the sorted arrays use binary search
712// instead of linear one), but Add() is slower.
713// 5) there is no Insert() method because you can't insert an item into the
714// given position in a sorted array but there is IndexForInsert()/AddAt()
715// pair which may be used to optimize a common operation of "insert only if
716// not found"
717//
718// Note that you have to specify the comparison function when creating the
719// objects of this array type. If, as in 99% of cases, the comparison function
5a1cad6e
GD
720// is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below
721// is more convenient.
1a931653
VZ
722//
723// Summary: use this class when the speed of Index() function is important, use
724// the normal arrays otherwise.
c801d85f
KB
725// ----------------------------------------------------------------------------
726
c75cc2e8
VZ
727// we need a macro which expands to nothing to pass correct number of
728// parameters to a nested macro invocation even when we don't have anything to
729// pass it
730#define wxARRAY_EMPTY
17e656b0 731
5a1cad6e
GD
732#define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \
733 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \
734 wxARRAY_DEFAULT_EXPORT)
a497618a 735
5a1cad6e 736#define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \
53a2db12 737 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLIMPEXP_CORE)
a497618a 738
5a1cad6e
GD
739#define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \
740 typedef T _wxArray##name; \
3e49e577
MB
741 typedef int (CMPFUNC_CONV *SCMPFUNC##name)(T pItem1, T pItem2); \
742 _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, \
c75cc2e8 743 wxARRAY_EMPTY, class expmode, SCMPFUNC##name)
1a931653
VZ
744
745// ----------------------------------------------------------------------------
5a1cad6e 746// WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison
1a931653
VZ
747// function is provided by this macro and the objects of this class have a
748// default constructor which just uses it.
749//
750// The arguments are: the element type, the comparison function and the array
751// name
752//
5a1cad6e
GD
753// NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked
754// from the very beginning - unfortunately I didn't think about this earlier
1a931653 755// ----------------------------------------------------------------------------
76314141 756
5a1cad6e
GD
757#define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
758 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
759 wxARRAY_DEFAULT_EXPORT)
76314141 760
5a1cad6e
GD
761#define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
762 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
53a2db12 763 WXDLLIMPEXP_CORE)
1a931653 764
5a1cad6e
GD
765#define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
766 expmode) \
1a931653 767 typedef T _wxArray##name; \
3e49e577
MB
768 typedef int (CMPFUNC_CONV *SCMPFUNC##name)(T pItem1, T pItem2); \
769 _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, = cmpfunc, \
770 class expmode, SCMPFUNC##name)
1a931653
VZ
771
772// ----------------------------------------------------------------------------
773// WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class
774// named "name" which owns the objects of type T it contains, i.e. it will
775// delete them when it is destroyed.
776//
777// An element is of type T*, but arguments of type T& are taken (see below!)
778// and T& is returned.
779//
780// Don't use this for simple types such as "int" or "long"!
1a931653
VZ
781//
782// Note on Add/Insert functions:
783// 1) function(T*) gives the object to the array, i.e. it will delete the
784// object when it's removed or in the array's dtor
785// 2) function(T&) will create a copy of the object and work with it
786//
787// Also:
788// 1) Remove() will delete the object after removing it from the array
789// 2) Detach() just removes the object from the array (returning pointer to it)
790//
791// NB1: Base type T should have an accessible copy ctor if Add(T&) is used
792// NB2: Never ever cast a array to it's base type: as dtor is not virtual
793// and so you risk having at least the memory leaks and probably worse
794//
795// Some functions of this class are not inline, so it takes some space to
796// define new class from this template even if you don't use it - which is not
797// the case for the simple (non-object) array classes
798//
1a931653
VZ
799// To use an objarray class you must
800// #include "dynarray.h"
801// WX_DECLARE_OBJARRAY(element_type, list_class_name)
802// #include "arrimpl.cpp"
803// WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above!
804//
805// This is necessary because at the moment of DEFINE_OBJARRAY class parsing the
806// element_type must be fully defined (i.e. forward declaration is not
807// enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
808// two allows to break cicrcular dependencies with classes which have member
809// variables of objarray type.
810// ----------------------------------------------------------------------------
811
812#define WX_DECLARE_OBJARRAY(T, name) \
813 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
814
815#define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
53a2db12 816 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLIMPEXP_CORE)
1a931653 817
fd68cfdb 818#define WX_DECLARE_OBJARRAY_WITH_DECL(T, name, decl) \
1a931653 819 typedef T _wxObjArray##name; \
fd68cfdb 820 _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, wxArrayPtrVoid, decl)
68379eaf 821
fd68cfdb
VS
822#define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \
823 WX_DECLARE_OBJARRAY_WITH_DECL(T, name, class expmode)
1a931653
VZ
824
825// WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included,
826// try to provoke a human-understandable error if it used incorrectly.
827//
828// there is no real need for 3 different macros in the DEFINE case but do it
829// anyhow for consistency
830#define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp
831#define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
76314141 832#define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
76314141 833
5a1cad6e
GD
834// ----------------------------------------------------------------------------
835// Some commonly used predefined base arrays
836// ----------------------------------------------------------------------------
837
eee614d3
VS
838WX_DECLARE_USER_EXPORTED_BASEARRAY(const void *, wxBaseArrayPtrVoid,
839 WXDLLIMPEXP_BASE);
1ffc8d7a 840WX_DECLARE_USER_EXPORTED_BASEARRAY(char, wxBaseArrayChar, WXDLLIMPEXP_BASE);
6108e3fd
VZ
841WX_DECLARE_USER_EXPORTED_BASEARRAY(short, wxBaseArrayShort, WXDLLIMPEXP_BASE);
842WX_DECLARE_USER_EXPORTED_BASEARRAY(int, wxBaseArrayInt, WXDLLIMPEXP_BASE);
843WX_DECLARE_USER_EXPORTED_BASEARRAY(long, wxBaseArrayLong, WXDLLIMPEXP_BASE);
d2213b1f 844WX_DECLARE_USER_EXPORTED_BASEARRAY(size_t, wxBaseArraySizeT, WXDLLIMPEXP_BASE);
6108e3fd 845WX_DECLARE_USER_EXPORTED_BASEARRAY(double, wxBaseArrayDouble, WXDLLIMPEXP_BASE);
5a1cad6e
GD
846
847// ----------------------------------------------------------------------------
848// Convenience macros to define arrays from base arrays
849// ----------------------------------------------------------------------------
850
851#define WX_DEFINE_ARRAY(T, name) \
852 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
d5d29b8a
VZ
853#define WX_DEFINE_ARRAY_PTR(T, name) \
854 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid)
5a1cad6e
GD
855#define WX_DEFINE_EXPORTED_ARRAY(T, name) \
856 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
d5d29b8a
VZ
857#define WX_DEFINE_EXPORTED_ARRAY_PTR(T, name) \
858 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid)
859#define WX_DEFINE_ARRAY_WITH_DECL_PTR(T, name, decl) \
860 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, decl)
5a1cad6e 861#define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \
c75cc2e8 862 WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayPtrVoid, wxARRAY_EMPTY expmode)
d5d29b8a 863#define WX_DEFINE_USER_EXPORTED_ARRAY_PTR(T, name, expmode) \
c75cc2e8 864 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, wxARRAY_EMPTY expmode)
5a1cad6e 865
1ffc8d7a
VZ
866#define WX_DEFINE_ARRAY_CHAR(T, name) \
867 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayChar)
868#define WX_DEFINE_EXPORTED_ARRAY_CHAR(T, name) \
869 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayChar)
870#define WX_DEFINE_USER_EXPORTED_ARRAY_CHAR(T, name, expmode) \
c75cc2e8 871 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayChar, wxARRAY_EMPTY expmode)
1ffc8d7a 872
5a1cad6e 873#define WX_DEFINE_ARRAY_SHORT(T, name) \
d5d29b8a 874 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayShort)
5a1cad6e 875#define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \
d5d29b8a 876 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayShort)
5a1cad6e 877#define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
c75cc2e8 878 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayShort, wxARRAY_EMPTY expmode)
5a1cad6e
GD
879
880#define WX_DEFINE_ARRAY_INT(T, name) \
d5d29b8a 881 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayInt)
5a1cad6e 882#define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \
d5d29b8a 883 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayInt)
5a1cad6e 884#define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
c75cc2e8 885 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayInt, wxARRAY_EMPTY expmode)
5a1cad6e
GD
886
887#define WX_DEFINE_ARRAY_LONG(T, name) \
d5d29b8a 888 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayLong)
5a1cad6e 889#define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \
d5d29b8a 890 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayLong)
5a1cad6e 891#define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
c75cc2e8 892 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayLong, wxARRAY_EMPTY expmode)
5a1cad6e 893
d2213b1f
VZ
894#define WX_DEFINE_ARRAY_SIZE_T(T, name) \
895 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArraySizeT)
896#define WX_DEFINE_EXPORTED_ARRAY_SIZE_T(T, name) \
897 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArraySizeT)
898#define WX_DEFINE_USER_EXPORTED_ARRAY_SIZE_T(T, name, expmode) \
c75cc2e8 899 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArraySizeT, wxARRAY_EMPTY expmode)
d2213b1f 900
5a1cad6e 901#define WX_DEFINE_ARRAY_DOUBLE(T, name) \
d5d29b8a 902 WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayDouble)
5a1cad6e 903#define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \
d5d29b8a 904 WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayDouble)
5a1cad6e 905#define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \
c75cc2e8 906 WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayDouble, wxARRAY_EMPTY expmode)
5a1cad6e
GD
907
908// ----------------------------------------------------------------------------
909// Convenience macros to define sorted arrays from base arrays
910// ----------------------------------------------------------------------------
911
912#define WX_DEFINE_SORTED_ARRAY(T, name) \
913 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
914#define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
915 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
916#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \
c75cc2e8 917 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, wxARRAY_EMPTY expmode)
5a1cad6e 918
1ffc8d7a
VZ
919#define WX_DEFINE_SORTED_ARRAY_CHAR(T, name) \
920 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayChar)
921#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CHAR(T, name) \
922 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayChar)
923#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CHAR(T, name, expmode) \
c75cc2e8 924 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayChar, wxARRAY_EMPTY expmode)
1ffc8d7a 925
5a1cad6e
GD
926#define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \
927 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort)
928#define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \
929 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort)
930#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
c75cc2e8 931 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, wxARRAY_EMPTY expmode)
5a1cad6e
GD
932
933#define WX_DEFINE_SORTED_ARRAY_INT(T, name) \
934 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt)
935#define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \
936 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt)
937#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
938 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode)
939
940#define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \
941 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong)
942#define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \
943 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong)
944#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
945 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode)
946
d2213b1f
VZ
947#define WX_DEFINE_SORTED_ARRAY_SIZE_T(T, name) \
948 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArraySizeT)
949#define WX_DEFINE_SORTED_EXPORTED_ARRAY_SIZE_T(T, name) \
950 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArraySizeT)
951#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(T, name, expmode) \
c75cc2e8 952 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArraySizeT, wxARRAY_EMPTY expmode)
d2213b1f 953
5a1cad6e
GD
954// ----------------------------------------------------------------------------
955// Convenience macros to define sorted arrays from base arrays
956// ----------------------------------------------------------------------------
957
958#define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \
959 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
960#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \
961 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
962#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \
963 name, expmode) \
964 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
c75cc2e8
VZ
965 wxBaseArrayPtrVoid, \
966 wxARRAY_EMPTY expmode)
5a1cad6e 967
1ffc8d7a
VZ
968#define WX_DEFINE_SORTED_ARRAY_CMP_CHAR(T, cmpfunc, name) \
969 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayChar)
970#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_CHAR(T, cmpfunc, name) \
971 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayChar)
c75cc2e8 972#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_CHAR(T, cmpfunc, \
1ffc8d7a
VZ
973 name, expmode) \
974 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
c75cc2e8
VZ
975 wxBaseArrayChar, \
976 wxARRAY_EMPTY expmode)
1ffc8d7a 977
5a1cad6e
GD
978#define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
979 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
980#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
981 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
982#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \
983 name, expmode) \
984 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
c75cc2e8
VZ
985 wxBaseArrayShort, \
986 wxARRAY_EMPTY expmode)
5a1cad6e
GD
987
988#define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
989 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
990#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
991 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
992#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \
993 name, expmode) \
994 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
c75cc2e8
VZ
995 wxBaseArrayInt, \
996 wxARRAY_EMPTY expmode)
5a1cad6e
GD
997
998#define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
999 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
1000#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
1001 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
1002#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \
1003 name, expmode) \
1004 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
c75cc2e8
VZ
1005 wxBaseArrayLong, \
1006 wxARRAY_EMPTY expmode)
5a1cad6e 1007
d2213b1f
VZ
1008#define WX_DEFINE_SORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, name) \
1009 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArraySizeT)
1010#define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, name) \
1011 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArraySizeT)
1012#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, \
1013 name, expmode) \
1014 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
c75cc2e8
VZ
1015 wxBaseArraySizeT, \
1016 wxARRAY_EMPTY expmode)
d2213b1f 1017
c801d85f 1018// ----------------------------------------------------------------------------
1a931653 1019// Some commonly used predefined arrays
c801d85f
KB
1020// ----------------------------------------------------------------------------
1021
6108e3fd
VZ
1022WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(short, wxArrayShort, class WXDLLIMPEXP_BASE);
1023WX_DEFINE_USER_EXPORTED_ARRAY_INT(int, wxArrayInt, class WXDLLIMPEXP_BASE);
87cc1cc7 1024WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(double, wxArrayDouble, class WXDLLIMPEXP_BASE);
6108e3fd 1025WX_DEFINE_USER_EXPORTED_ARRAY_LONG(long, wxArrayLong, class WXDLLIMPEXP_BASE);
d5d29b8a 1026WX_DEFINE_USER_EXPORTED_ARRAY_PTR(void *, wxArrayPtrVoid, class WXDLLIMPEXP_BASE);
c801d85f 1027
2b9bd418 1028// -----------------------------------------------------------------------------
0cbff120 1029// convenience macros
2b9bd418
VZ
1030// -----------------------------------------------------------------------------
1031
bf7f7793
RR
1032// prepend all element of one array to another one; e.g. if first array contains
1033// elements X,Y,Z and the second contains A,B,C (in those orders), then the
1034// first array will be result as A,B,C,X,Y,Z
1035#define WX_PREPEND_ARRAY(array, other) \
1036 { \
1037 size_t wxAAcnt = (other).size(); \
cd643444 1038 (array).reserve(wxAAcnt); \
bf7f7793
RR
1039 for ( size_t wxAAn = 0; wxAAn < wxAAcnt; wxAAn++ ) \
1040 { \
1041 (array).Insert((other)[wxAAn], wxAAn); \
1042 } \
1043 }
1044
4f6aed9c
VZ
1045// append all element of one array to another one
1046#define WX_APPEND_ARRAY(array, other) \
1047 { \
17a1ebd1 1048 size_t wxAAcnt = (other).size(); \
cd643444 1049 (array).reserve(wxAAcnt); \
17a1ebd1 1050 for ( size_t wxAAn = 0; wxAAn < wxAAcnt; wxAAn++ ) \
4f6aed9c 1051 { \
17a1ebd1 1052 (array).push_back((other)[wxAAn]); \
4f6aed9c
VZ
1053 } \
1054 }
1055
2b9bd418
VZ
1056// delete all array elements
1057//
1058// NB: the class declaration of the array elements must be visible from the
1059// place where you use this macro, otherwise the proper destructor may not
1060// be called (a decent compiler should give a warning about it, but don't
1061// count on it)!
1062#define WX_CLEAR_ARRAY(array) \
1063 { \
17a1ebd1
VZ
1064 size_t wxAAcnt = (array).size(); \
1065 for ( size_t wxAAn = 0; wxAAn < wxAAcnt; wxAAn++ ) \
2b9bd418 1066 { \
17a1ebd1 1067 delete (array)[wxAAn]; \
2b9bd418 1068 } \
2c356747 1069 \
df5168c4 1070 (array).clear(); \
2b9bd418 1071 }
a497618a 1072
c801d85f 1073#endif // _DYNARRAY_H