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