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