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