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