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