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