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