]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dynarray.h
fixed a Really Stupid Bug
[wxWidgets.git] / include / wx / dynarray.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dynarray.h
3 // Purpose: auto-resizable (i.e. dynamic) array support
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 12.09.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _DYNARRAY_H
13 #define _DYNARRAY_H
14
15 #ifdef __GNUG__
16 #pragma interface "dynarray.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 /*
22 This header defines the dynamic arrays and object arrays (i.e. arrays which
23 own their elements). Dynamic means that the arrays grow automatically as
24 needed.
25
26 These macros are ugly (especially if you look in the sources ;-), but they
27 allow us to define "template" classes without actually using templates and so
28 this works with all compilers (and may be also much faster to compile even
29 with a compiler which does support templates). The arrays defined with these
30 macros are type-safe.
31
32 Range checking is performed in debug build for both arrays and objarrays but
33 not in release build - so using an invalid index will just lead to a crash
34 then.
35
36 Note about memory usage: arrays never shrink automatically (although you may
37 use Shrink() function explicitly), they only grow, so loading 10 millions in
38 an array only to delete them 2 lines below might be a bad idea if the array
39 object is not going to be destroyed soon. However, as it does free memory
40 when destroyed, it is ok if the array is a local variable.
41 */
42
43 // ----------------------------------------------------------------------------
44 // constants
45 // ----------------------------------------------------------------------------
46
47 /*
48 The initial size by which an array grows when an element is added default
49 value avoids allocate one or two bytes when the array is created which is
50 rather inefficient
51 */
52 #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
53
54 // ----------------------------------------------------------------------------
55 // types
56 // ----------------------------------------------------------------------------
57
58 /*
59 Callback compare function for quick sort.
60
61 It must return negative value, 0 or positive value if the first item is
62 less than, equal to or greater than the second one.
63 */
64 extern "C"
65 {
66 typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
67 }
68
69 // ----------------------------------------------------------------------------
70 // Base class managing data having size of type 'long' (not used directly)
71 //
72 // NB: for efficiency this often used class has no virtual functions (hence no
73 // virtual table), even dtor is *not* virtual. If used as expected it
74 // won't create any problems because ARRAYs from DEFINE_ARRAY have no dtor
75 // at all, so it's not too important if it's not called (this happens when
76 // you cast "SomeArray *" as "BaseArray *" and then delete it)
77 // ----------------------------------------------------------------------------
78
79 #define _WX_DECLARE_BASEARRAY(T, name, classexp) \
80 classexp name \
81 { \
82 public: \
83 name(); \
84 name(const name& array); \
85 name& operator=(const name& src); \
86 ~name(); \
87 \
88 void Empty() { m_nCount = 0; } \
89 void Clear(); \
90 void Alloc(size_t uiSize); \
91 void Shrink(); \
92 \
93 size_t GetCount() const { return m_nCount; } \
94 bool IsEmpty() const { return m_nCount == 0; } \
95 size_t Count() const { return m_nCount; } \
96 \
97 typedef T base_type; \
98 protected: \
99 T& Item(size_t uiIndex) const \
100 { wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; } \
101 T& operator[](size_t uiIndex) const { return Item(uiIndex); } \
102 \
103 int Index(T lItem, bool bFromEnd = FALSE) const; \
104 int Index(T lItem, CMPFUNC fnCompare) const; \
105 size_t IndexForInsert(T lItem, CMPFUNC fnCompare) const; \
106 void Add(T lItem, size_t nInsert = 1); \
107 void Add(T lItem, CMPFUNC fnCompare); \
108 void Insert(T lItem, size_t uiIndex, size_t nInsert = 1); \
109 void Remove(T lItem); \
110 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
111 \
112 void Sort(CMPFUNC fnCompare); \
113 \
114 private: \
115 \
116 void Grow(size_t nIncrement = 0); \
117 \
118 size_t m_nSize, \
119 m_nCount; \
120 \
121 T *m_pItems; \
122 };
123
124 // ============================================================================
125 // The private helper macros containing the core of the array classes
126 // ============================================================================
127
128 // Implementation notes:
129 //
130 // JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
131 // { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
132 // so using a temporary variable instead.
133 //
134 // The classes need a (even trivial) ~name() to link under Mac X
135 //
136 // _WX_ERROR_REMOVE is needed to resolve the name conflict between the wxT()
137 // macro and T typedef: we can't use wxT() inside WX_DEFINE_ARRAY!
138
139 #define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
140
141 // ----------------------------------------------------------------------------
142 // _WX_DEFINE_TYPEARRAY: array for simple types
143 // ----------------------------------------------------------------------------
144
145 #define _WX_DEFINE_TYPEARRAY(T, name, base, classexp) \
146 wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(base::base_type), \
147 TypeTooBigToBeStoredIn##base, \
148 name); \
149 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
150 classexp name : public base \
151 { \
152 public: \
153 name() { } \
154 ~name() { } \
155 \
156 name& operator=(const name& src) \
157 { base* temp = (base*) this; \
158 (*temp) = ((const base&)src); \
159 return *this; } \
160 \
161 T& operator[](size_t uiIndex) const \
162 { return (T&)(base::Item(uiIndex)); } \
163 T& Item(size_t uiIndex) const \
164 { return (T&)(base::Item(uiIndex)); } \
165 T& Last() const \
166 { return (T&)(base::Item(Count() - 1)); } \
167 \
168 int Index(T Item, bool bFromEnd = FALSE) const \
169 { return base::Index(Item, bFromEnd); } \
170 \
171 void Add(T Item, size_t nInsert = 1) \
172 { base::Add(Item, nInsert); } \
173 void Insert(T Item, size_t uiIndex, size_t nInsert = 1) \
174 { base::Insert(Item, uiIndex, nInsert) ; } \
175 \
176 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
177 { base::RemoveAt(uiIndex, nRemove); } \
178 void Remove(T Item) \
179 { int iIndex = Index(Item); \
180 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
181 _WX_ERROR_REMOVE); \
182 base::RemoveAt((size_t)iIndex); } \
183 \
184 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \
185 }
186
187 // ----------------------------------------------------------------------------
188 // _WX_DEFINE_SORTED_TYPEARRAY: sorted array for simple data types
189 // cannot handle types with size greater than pointer because of sorting
190 // ----------------------------------------------------------------------------
191
192 #define _WX_DEFINE_SORTED_TYPEARRAY(T, name, base, defcomp, classexp) \
193 wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(void *), \
194 TypeTooBigToBeStoredInSorted##base, \
195 name); \
196 typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
197 classexp name : public base \
198 { \
199 public: \
200 name(SCMPFUNC##T fn defcomp) { m_fnCompare = fn; } \
201 \
202 name& operator=(const name& src) \
203 { base* temp = (base*) this; \
204 (*temp) = ((const base&)src); \
205 m_fnCompare = src.m_fnCompare; \
206 return *this; } \
207 \
208 T& operator[](size_t uiIndex) const \
209 { return (T&)(base::Item(uiIndex)); } \
210 T& Item(size_t uiIndex) const \
211 { return (T&)(base::Item(uiIndex)); } \
212 T& Last() const \
213 { return (T&)(base::Item(Count() - 1)); } \
214 \
215 int Index(T Item) const \
216 { return base::Index(Item, (CMPFUNC)m_fnCompare); } \
217 \
218 size_t IndexForInsert(T Item) const \
219 { return base::IndexForInsert(Item, (CMPFUNC)m_fnCompare); } \
220 \
221 void AddAt(T item, size_t index) \
222 { base::Insert(item, index); } \
223 \
224 void Add(T Item) \
225 { base::Add(Item, (CMPFUNC)m_fnCompare); } \
226 \
227 void RemoveAt(size_t uiIndex, size_t nRemove = 1) \
228 { base::RemoveAt(uiIndex, nRemove); } \
229 void Remove(T Item) \
230 { int iIndex = Index(Item); \
231 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
232 _WX_ERROR_REMOVE ); \
233 base::RemoveAt((size_t)iIndex); } \
234 \
235 private: \
236 SCMPFUNC##T m_fnCompare; \
237 }
238
239 // ----------------------------------------------------------------------------
240 // _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics
241 // ----------------------------------------------------------------------------
242
243 #define _WX_DECLARE_OBJARRAY(T, name, base, classexp) \
244 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T **pItem1, T **pItem2); \
245 classexp name : public base \
246 { \
247 typedef int (CMPFUNC_CONV *CMPFUNC##base)(void **pItem1, void **pItem2); \
248 public: \
249 name() { } \
250 name(const name& src); \
251 name& operator=(const name& src); \
252 \
253 ~name(); \
254 \
255 T& operator[](size_t uiIndex) const \
256 { return *(T*)base::Item(uiIndex); } \
257 T& Item(size_t uiIndex) const \
258 { return *(T*)base::Item(uiIndex); } \
259 T& Last() const \
260 { return *(T*)(base::Item(Count() - 1)); } \
261 \
262 int Index(const T& Item, bool bFromEnd = FALSE) const; \
263 \
264 void Add(const T& Item, size_t nInsert = 1); \
265 void Add(const T* pItem) \
266 { base::Add((T*)pItem); } \
267 \
268 void Insert(const T& Item, size_t uiIndex, size_t nInsert = 1); \
269 void Insert(const T* pItem, size_t uiIndex) \
270 { base::Insert((T*)pItem, uiIndex); } \
271 \
272 void Empty() { DoEmpty(); base::Empty(); } \
273 void Clear() { DoEmpty(); base::Clear(); } \
274 \
275 T* Detach(size_t uiIndex) \
276 { T* p = (T*)base::Item(uiIndex); \
277 base::RemoveAt(uiIndex); return p; } \
278 void RemoveAt(size_t uiIndex, size_t nRemove = 1); \
279 \
280 void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC##base)fCmp); } \
281 \
282 private: \
283 void DoEmpty(); \
284 void DoCopy(const name& src); \
285 }
286
287 // ============================================================================
288 // The public macros for declaration and definition of the dynamic arrays
289 // ============================================================================
290
291 // Please note that for each macro WX_FOO_ARRAY we also have
292 // WX_FOO_EXPORTED_ARRAY and WX_FOO_USER_EXPORTED_ARRAY which are exactly the
293 // same except that they use an additional __declspec(dllexport) or equivalent
294 // under Windows if needed.
295 //
296 // The first (just EXPORTED) macros do it if wxWindows was compiled as a DLL
297 // and so must be used used inside the library. The second kind (USER_EXPORTED)
298 // allow the user code to do it when it wants. This is needed if you have a dll
299 // that wants to export a wxArray daubed with your own import/export goo.
300 //
301 // Finally, you can define the macro below as something special to modify the
302 // arrays defined by a simple WX_FOO_ARRAY as well. By default is is empty.
303 #define wxARRAY_DEFAULT_EXPORT
304
305 // ----------------------------------------------------------------------------
306 // WX_DECLARE_BASEARRAY(T, name) declare an array class named "name" containing
307 // the elements of type T
308 // ----------------------------------------------------------------------------
309
310 #define WX_DECLARE_BASEARRAY(T, name) \
311 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
312
313 #define WX_DECLARE_EXPORTED_BASEARRAY(T, name) \
314 WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, WXDLLEXPORT)
315
316 #define WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, expmode) \
317 typedef T _wxArray##name; \
318 _WX_DECLARE_BASEARRAY(_wxArray##name, name, class expmode)
319
320 // ----------------------------------------------------------------------------
321 // WX_DEFINE_TYPEARRAY(T, name, base) define an array class named "name" deriving
322 // from class "base" containing the elements of type T
323 //
324 // Note that the class defined has only inline function and doesn't take any
325 // space at all so there is no size penalty for defining multiple array classes
326 // ----------------------------------------------------------------------------
327
328 #define WX_DEFINE_TYPEARRAY(T, name, base) \
329 WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, wxARRAY_DEFAULT_EXPORT)
330
331 #define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \
332 WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT)
333
334 #define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \
335 typedef T _wxArray##name; \
336 _WX_DEFINE_TYPEARRAY(_wxArray##name, name, base, class expmode)
337
338 // ----------------------------------------------------------------------------
339 // WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it
340 // defines a sorted array.
341 //
342 // Differences:
343 // 1) it must be given a COMPARE function in ctor which takes 2 items of type
344 // T* and should return -1, 0 or +1 if the first one is less/greater
345 // than/equal to the second one.
346 // 2) the Add() method inserts the item in such was that the array is always
347 // sorted (it uses the COMPARE function)
348 // 3) it has no Sort() method because it's always sorted
349 // 4) Index() method is much faster (the sorted arrays use binary search
350 // instead of linear one), but Add() is slower.
351 // 5) there is no Insert() method because you can't insert an item into the
352 // given position in a sorted array but there is IndexForInsert()/AddAt()
353 // pair which may be used to optimize a common operation of "insert only if
354 // not found"
355 //
356 // Note that you have to specify the comparison function when creating the
357 // objects of this array type. If, as in 99% of cases, the comparison function
358 // is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below
359 // is more convenient.
360 //
361 // Summary: use this class when the speed of Index() function is important, use
362 // the normal arrays otherwise.
363 // ----------------------------------------------------------------------------
364
365 #define wxARRAY_EMPTY_CMP
366
367 #define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \
368 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \
369 wxARRAY_DEFAULT_EXPORT)
370
371 #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \
372 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT)
373
374 #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \
375 typedef T _wxArray##name; \
376 _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, \
377 wxARRAY_EMPTY_CMP, class expmode)
378
379 // ----------------------------------------------------------------------------
380 // WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison
381 // function is provided by this macro and the objects of this class have a
382 // default constructor which just uses it.
383 //
384 // The arguments are: the element type, the comparison function and the array
385 // name
386 //
387 // NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked
388 // from the very beginning - unfortunately I didn't think about this earlier
389 // ----------------------------------------------------------------------------
390
391 #define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
392 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
393 wxARRAY_DEFAULT_EXPORT)
394
395 #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \
396 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
397 WXDLLEXPORT)
398
399 #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \
400 expmode) \
401 typedef T _wxArray##name; \
402 _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, = cmpfunc, \
403 class expmode)
404
405 // ----------------------------------------------------------------------------
406 // WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class
407 // named "name" which owns the objects of type T it contains, i.e. it will
408 // delete them when it is destroyed.
409 //
410 // An element is of type T*, but arguments of type T& are taken (see below!)
411 // and T& is returned.
412 //
413 // Don't use this for simple types such as "int" or "long"!
414 //
415 // Note on Add/Insert functions:
416 // 1) function(T*) gives the object to the array, i.e. it will delete the
417 // object when it's removed or in the array's dtor
418 // 2) function(T&) will create a copy of the object and work with it
419 //
420 // Also:
421 // 1) Remove() will delete the object after removing it from the array
422 // 2) Detach() just removes the object from the array (returning pointer to it)
423 //
424 // NB1: Base type T should have an accessible copy ctor if Add(T&) is used
425 // NB2: Never ever cast a array to it's base type: as dtor is not virtual
426 // and so you risk having at least the memory leaks and probably worse
427 //
428 // Some functions of this class are not inline, so it takes some space to
429 // define new class from this template even if you don't use it - which is not
430 // the case for the simple (non-object) array classes
431 //
432 // To use an objarray class you must
433 // #include "dynarray.h"
434 // WX_DECLARE_OBJARRAY(element_type, list_class_name)
435 // #include "arrimpl.cpp"
436 // WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above!
437 //
438 // This is necessary because at the moment of DEFINE_OBJARRAY class parsing the
439 // element_type must be fully defined (i.e. forward declaration is not
440 // enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
441 // two allows to break cicrcular dependencies with classes which have member
442 // variables of objarray type.
443 // ----------------------------------------------------------------------------
444
445 #define WX_DECLARE_OBJARRAY(T, name) \
446 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT)
447
448 #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
449 WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLEXPORT)
450
451 #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \
452 typedef T _wxObjArray##name; \
453 _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, wxArrayPtrVoid, class expmode)
454
455 // WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included,
456 // try to provoke a human-understandable error if it used incorrectly.
457 //
458 // there is no real need for 3 different macros in the DEFINE case but do it
459 // anyhow for consistency
460 #define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp
461 #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
462 #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
463
464 // ----------------------------------------------------------------------------
465 // Some commonly used predefined base arrays
466 // ----------------------------------------------------------------------------
467
468 WX_DECLARE_EXPORTED_BASEARRAY(const void *, wxBaseArrayPtrVoid);
469 WX_DECLARE_EXPORTED_BASEARRAY(short, wxBaseArrayShort);
470 WX_DECLARE_EXPORTED_BASEARRAY(int, wxBaseArrayInt);
471 WX_DECLARE_EXPORTED_BASEARRAY(long, wxBaseArrayLong);
472 WX_DECLARE_EXPORTED_BASEARRAY(double, wxBaseArrayDouble);
473
474 // ----------------------------------------------------------------------------
475 // Convenience macros to define arrays from base arrays
476 // ----------------------------------------------------------------------------
477
478 #define WX_DEFINE_ARRAY(T, name) \
479 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
480 #define WX_DEFINE_EXPORTED_ARRAY(T, name) \
481 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
482 #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \
483 WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode)
484
485 #define WX_DEFINE_ARRAY_SHORT(T, name) \
486 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayShort)
487 #define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \
488 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort)
489 #define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
490 WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode)
491
492 #define WX_DEFINE_ARRAY_INT(T, name) \
493 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayInt)
494 #define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \
495 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt)
496 #define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
497 WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode)
498
499 #define WX_DEFINE_ARRAY_LONG(T, name) \
500 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayLong)
501 #define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \
502 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong)
503 #define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
504 WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode)
505
506 #define WX_DEFINE_ARRAY_DOUBLE(T, name) \
507 WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayDouble)
508 #define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \
509 WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayDouble)
510 #define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \
511 WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayDouble, expmode)
512
513 // ----------------------------------------------------------------------------
514 // Convenience macros to define sorted arrays from base arrays
515 // ----------------------------------------------------------------------------
516
517 #define WX_DEFINE_SORTED_ARRAY(T, name) \
518 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
519 #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
520 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid)
521 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \
522 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode)
523
524 #define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \
525 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort)
526 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \
527 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort)
528 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \
529 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode)
530
531 #define WX_DEFINE_SORTED_ARRAY_INT(T, name) \
532 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt)
533 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \
534 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt)
535 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \
536 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode)
537
538 #define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \
539 WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong)
540 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \
541 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong)
542 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \
543 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode)
544
545 // ----------------------------------------------------------------------------
546 // Convenience macros to define sorted arrays from base arrays
547 // ----------------------------------------------------------------------------
548
549 #define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \
550 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
551 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \
552 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid)
553 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \
554 name, expmode) \
555 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
556 wxBaseArrayPtrVoid, expmode)
557
558 #define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
559 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
560 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \
561 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort)
562 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \
563 name, expmode) \
564 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
565 wxBaseArrayShort, expmode)
566
567 #define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
568 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
569 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \
570 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt)
571 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \
572 name, expmode) \
573 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
574 wxBaseArrayInt, expmode)
575
576 #define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
577 WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
578 #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \
579 WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong)
580 #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \
581 name, expmode) \
582 WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \
583 wxBaseArrayLong, expmode)
584
585 // ----------------------------------------------------------------------------
586 // Some commonly used predefined arrays
587 // ----------------------------------------------------------------------------
588
589 WX_DEFINE_EXPORTED_ARRAY_SHORT (short, wxArrayShort);
590 WX_DEFINE_EXPORTED_ARRAY_INT (int, wxArrayInt);
591 WX_DEFINE_EXPORTED_ARRAY_LONG (long, wxArrayLong);
592 WX_DEFINE_EXPORTED_ARRAY (void *, wxArrayPtrVoid);
593
594 // -----------------------------------------------------------------------------
595 // convenience macros
596 // -----------------------------------------------------------------------------
597
598 // append all element of one array to another one
599 #define WX_APPEND_ARRAY(array, other) \
600 { \
601 size_t count = (other).Count(); \
602 for ( size_t n = 0; n < count; n++ ) \
603 { \
604 (array).Add((other)[n]); \
605 } \
606 }
607
608 // delete all array elements
609 //
610 // NB: the class declaration of the array elements must be visible from the
611 // place where you use this macro, otherwise the proper destructor may not
612 // be called (a decent compiler should give a warning about it, but don't
613 // count on it)!
614 #define WX_CLEAR_ARRAY(array) \
615 { \
616 size_t count = (array).Count(); \
617 for ( size_t n = 0; n < count; n++ ) \
618 { \
619 delete (array)[n]; \
620 } \
621 \
622 (array).Empty(); \
623 }
624
625 #endif // _DYNARRAY_H
626