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