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