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