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