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