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