]>
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> | |
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" | |
c801d85f | 20 | |
1a931653 VZ |
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 | */ | |
c801d85f KB |
42 | |
43 | // ---------------------------------------------------------------------------- | |
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
1a931653 VZ |
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 | |
c801d85f | 51 | */ |
1a931653 | 52 | #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16) |
c801d85f KB |
53 | |
54 | // ---------------------------------------------------------------------------- | |
55 | // types | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
1a931653 VZ |
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. | |
c801d85f | 63 | */ |
90350682 VZ |
64 | extern "C" |
65 | { | |
0661ec39 | 66 | typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2); |
90350682 | 67 | } |
c801d85f KB |
68 | |
69 | // ---------------------------------------------------------------------------- | |
1a931653 VZ |
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) | |
c801d85f | 77 | // ---------------------------------------------------------------------------- |
1a931653 | 78 | |
5a1cad6e GD |
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 | \ | |
75d7ef36 | 97 | typedef T base_type; \ |
5a1cad6e GD |
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); \ | |
107 | void Add(T lItem, CMPFUNC fnCompare); \ | |
108 | void Insert(T lItem, size_t uiIndex); \ | |
109 | void Remove(T lItem); \ | |
110 | void RemoveAt(size_t uiIndex); \ | |
111 | \ | |
112 | void Sort(CMPFUNC fnCompare); \ | |
113 | \ | |
114 | private: \ | |
5a1cad6e GD |
115 | \ |
116 | void Grow(); \ | |
117 | \ | |
118 | size_t m_nSize, \ | |
119 | m_nCount; \ | |
120 | \ | |
121 | T *m_pItems; \ | |
c801d85f KB |
122 | }; |
123 | ||
124 | // ============================================================================ | |
1a931653 | 125 | // The private helper macros containing the core of the array classes |
c801d85f KB |
126 | // ============================================================================ |
127 | ||
1a931653 VZ |
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() | |
5a1cad6e | 137 | // macro and T typedef: we can't use wxT() inside WX_DEFINE_ARRAY! |
1a931653 VZ |
138 | |
139 | #define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove") | |
e90c1d2a | 140 | |
c801d85f | 141 | // ---------------------------------------------------------------------------- |
5a1cad6e | 142 | // _WX_DEFINE_TYPEARRAY: array for simple types |
c801d85f | 143 | // ---------------------------------------------------------------------------- |
1a931653 | 144 | |
5a1cad6e GD |
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) \ | |
172 | { base::Add(Item); } \ | |
173 | void Insert(T Item, size_t uiIndex) \ | |
174 | { base::Insert(Item, uiIndex) ; } \ | |
175 | \ | |
176 | void RemoveAt(size_t uiIndex) { base::RemoveAt(uiIndex); } \ | |
177 | void Remove(T Item) \ | |
178 | { int iIndex = Index(Item); \ | |
179 | wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ | |
180 | _WX_ERROR_REMOVE); \ | |
181 | base::RemoveAt((size_t)iIndex); } \ | |
182 | \ | |
183 | void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC)fCmp); } \ | |
c801d85f KB |
184 | } |
185 | ||
3bfa4402 | 186 | // ---------------------------------------------------------------------------- |
5a1cad6e GD |
187 | // _WX_DEFINE_SORTED_TYPEARRAY: sorted array for simple data types |
188 | // cannot handle types with size greater than pointer because of sorting | |
3bfa4402 | 189 | // ---------------------------------------------------------------------------- |
1a931653 | 190 | |
5a1cad6e GD |
191 | #define _WX_DEFINE_SORTED_TYPEARRAY(T, name, base, defcomp, classexp) \ |
192 | wxCOMPILE_TIME_ASSERT2(sizeof(T) <= sizeof(void *), \ | |
193 | TypeTooBigToBeStoredInSorted##base, \ | |
194 | name); \ | |
195 | typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \ | |
196 | classexp name : public base \ | |
197 | { \ | |
198 | public: \ | |
199 | name(SCMPFUNC##T fn defcomp) { m_fnCompare = fn; } \ | |
200 | \ | |
201 | name& operator=(const name& src) \ | |
202 | { base* temp = (base*) this; \ | |
203 | (*temp) = ((const base&)src); \ | |
204 | m_fnCompare = src.m_fnCompare; \ | |
205 | return *this; } \ | |
206 | \ | |
207 | T& operator[](size_t uiIndex) const \ | |
208 | { return (T&)(base::Item(uiIndex)); } \ | |
209 | T& Item(size_t uiIndex) const \ | |
210 | { return (T&)(base::Item(uiIndex)); } \ | |
211 | T& Last() const \ | |
212 | { return (T&)(base::Item(Count() - 1)); } \ | |
213 | \ | |
214 | int Index(T Item) const \ | |
215 | { return base::Index(Item, (CMPFUNC)m_fnCompare); } \ | |
216 | \ | |
217 | size_t IndexForInsert(T Item) const \ | |
218 | { return base::IndexForInsert(Item, (CMPFUNC)m_fnCompare); } \ | |
219 | \ | |
220 | void AddAt(T item, size_t index) \ | |
221 | { base::Insert(item, index); } \ | |
222 | \ | |
223 | void Add(T Item) \ | |
224 | { base::Add(Item, (CMPFUNC)m_fnCompare); } \ | |
225 | \ | |
226 | void RemoveAt(size_t uiIndex) { base::RemoveAt(uiIndex); } \ | |
227 | void Remove(T Item) \ | |
228 | { int iIndex = Index(Item); \ | |
229 | wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \ | |
230 | _WX_ERROR_REMOVE ); \ | |
231 | base::RemoveAt((size_t)iIndex); } \ | |
232 | \ | |
233 | private: \ | |
234 | SCMPFUNC##T m_fnCompare; \ | |
3bfa4402 VZ |
235 | } |
236 | ||
c801d85f | 237 | // ---------------------------------------------------------------------------- |
1a931653 | 238 | // _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics |
c801d85f | 239 | // ---------------------------------------------------------------------------- |
1a931653 | 240 | |
5a1cad6e GD |
241 | #define _WX_DECLARE_OBJARRAY(T, name, base, classexp) \ |
242 | typedef int (CMPFUNC_CONV *CMPFUNC##T)(T **pItem1, T **pItem2); \ | |
243 | classexp name : public base \ | |
244 | { \ | |
245 | typedef int (CMPFUNC_CONV *CMPFUNC##base)(void **pItem1, void **pItem2); \ | |
246 | public: \ | |
247 | name() { } \ | |
248 | name(const name& src); \ | |
249 | name& operator=(const name& src); \ | |
250 | \ | |
251 | ~name(); \ | |
252 | \ | |
253 | T& operator[](size_t uiIndex) const \ | |
254 | { return *(T*)base::Item(uiIndex); } \ | |
255 | T& Item(size_t uiIndex) const \ | |
256 | { return *(T*)base::Item(uiIndex); } \ | |
257 | T& Last() const \ | |
258 | { return *(T*)(base::Item(Count() - 1)); } \ | |
259 | \ | |
260 | int Index(const T& Item, bool bFromEnd = FALSE) const; \ | |
261 | \ | |
262 | void Add(const T& Item); \ | |
263 | void Add(const T* pItem) \ | |
264 | { base::Add((T*)pItem); } \ | |
265 | \ | |
266 | void Insert(const T& Item, size_t uiIndex); \ | |
267 | void Insert(const T* pItem, size_t uiIndex) \ | |
268 | { base::Insert((T*)pItem, uiIndex); } \ | |
269 | \ | |
270 | void Empty() { DoEmpty(); base::Empty(); } \ | |
271 | void Clear() { DoEmpty(); base::Clear(); } \ | |
272 | \ | |
273 | T* Detach(size_t uiIndex) \ | |
274 | { T* p = (T*)base::Item(uiIndex); \ | |
275 | base::RemoveAt(uiIndex); return p; } \ | |
276 | void RemoveAt(size_t uiIndex); \ | |
277 | \ | |
278 | void Sort(CMPFUNC##T fCmp) { base::Sort((CMPFUNC##base)fCmp); } \ | |
279 | \ | |
280 | private: \ | |
281 | void DoEmpty(); \ | |
282 | void DoCopy(const name& src); \ | |
c801d85f KB |
283 | } |
284 | ||
1a931653 VZ |
285 | // ============================================================================ |
286 | // The public macros for declaration and definition of the dynamic arrays | |
287 | // ============================================================================ | |
288 | ||
289 | // Please note that for each macro WX_FOO_ARRAY we also have | |
290 | // WX_FOO_EXPORTED_ARRAY and WX_FOO_USER_EXPORTED_ARRAY which are exactly the | |
291 | // same except that they use an additional __declspec(dllexport) or equivalent | |
292 | // under Windows if needed. | |
293 | // | |
294 | // The first (just EXPORTED) macros do it if wxWindows was compiled as a DLL | |
295 | // and so must be used used inside the library. The second kind (USER_EXPORTED) | |
296 | // allow the user code to do it when it wants. This is needed if you have a dll | |
297 | // that wants to export a wxArray daubed with your own import/export goo. | |
298 | // | |
299 | // Finally, you can define the macro below as something special to modify the | |
300 | // arrays defined by a simple WX_FOO_ARRAY as well. By default is is empty. | |
301 | #define wxARRAY_DEFAULT_EXPORT | |
302 | ||
303 | // ---------------------------------------------------------------------------- | |
5a1cad6e GD |
304 | // WX_DECLARE_BASEARRAY(T, name) declare an array class named "name" containing |
305 | // the elements of type T | |
306 | // ---------------------------------------------------------------------------- | |
307 | ||
308 | #define WX_DECLARE_BASEARRAY(T, name) \ | |
309 | WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, wxARRAY_DEFAULT_EXPORT) | |
310 | ||
311 | #define WX_DECLARE_EXPORTED_BASEARRAY(T, name) \ | |
312 | WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, WXDLLEXPORT) | |
313 | ||
314 | #define WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, expmode) \ | |
315 | typedef T _wxArray##name; \ | |
316 | _WX_DECLARE_BASEARRAY(_wxArray##name, name, class expmode) | |
317 | ||
318 | // ---------------------------------------------------------------------------- | |
319 | // WX_DEFINE_TYPEARRAY(T, name, base) define an array class named "name" deriving | |
320 | // from class "base" containing the elements of type T | |
1a931653 VZ |
321 | // |
322 | // Note that the class defined has only inline function and doesn't take any | |
323 | // space at all so there is no size penalty for defining multiple array classes | |
c801d85f | 324 | // ---------------------------------------------------------------------------- |
c801d85f | 325 | |
5a1cad6e GD |
326 | #define WX_DEFINE_TYPEARRAY(T, name, base) \ |
327 | WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, wxARRAY_DEFAULT_EXPORT) | |
1a931653 | 328 | |
5a1cad6e GD |
329 | #define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \ |
330 | WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT) | |
1a931653 | 331 | |
5a1cad6e GD |
332 | #define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \ |
333 | typedef T _wxArray##name; \ | |
334 | _WX_DEFINE_TYPEARRAY(_wxArray##name, name, base, class expmode) | |
1a931653 VZ |
335 | |
336 | // ---------------------------------------------------------------------------- | |
5a1cad6e | 337 | // WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it |
1a931653 VZ |
338 | // defines a sorted array. |
339 | // | |
340 | // Differences: | |
341 | // 1) it must be given a COMPARE function in ctor which takes 2 items of type | |
342 | // T* and should return -1, 0 or +1 if the first one is less/greater | |
343 | // than/equal to the second one. | |
344 | // 2) the Add() method inserts the item in such was that the array is always | |
345 | // sorted (it uses the COMPARE function) | |
346 | // 3) it has no Sort() method because it's always sorted | |
347 | // 4) Index() method is much faster (the sorted arrays use binary search | |
348 | // instead of linear one), but Add() is slower. | |
349 | // 5) there is no Insert() method because you can't insert an item into the | |
350 | // given position in a sorted array but there is IndexForInsert()/AddAt() | |
351 | // pair which may be used to optimize a common operation of "insert only if | |
352 | // not found" | |
353 | // | |
354 | // Note that you have to specify the comparison function when creating the | |
355 | // objects of this array type. If, as in 99% of cases, the comparison function | |
5a1cad6e GD |
356 | // is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below |
357 | // is more convenient. | |
1a931653 VZ |
358 | // |
359 | // Summary: use this class when the speed of Index() function is important, use | |
360 | // the normal arrays otherwise. | |
c801d85f KB |
361 | // ---------------------------------------------------------------------------- |
362 | ||
17e656b0 VZ |
363 | #define wxARRAY_EMPTY_CMP |
364 | ||
5a1cad6e GD |
365 | #define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \ |
366 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \ | |
367 | wxARRAY_DEFAULT_EXPORT) | |
a497618a | 368 | |
5a1cad6e GD |
369 | #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \ |
370 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT) | |
a497618a | 371 | |
5a1cad6e GD |
372 | #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \ |
373 | typedef T _wxArray##name; \ | |
374 | _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, \ | |
375 | wxARRAY_EMPTY_CMP, class expmode) | |
1a931653 VZ |
376 | |
377 | // ---------------------------------------------------------------------------- | |
5a1cad6e | 378 | // WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison |
1a931653 VZ |
379 | // function is provided by this macro and the objects of this class have a |
380 | // default constructor which just uses it. | |
381 | // | |
382 | // The arguments are: the element type, the comparison function and the array | |
383 | // name | |
384 | // | |
5a1cad6e GD |
385 | // NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked |
386 | // from the very beginning - unfortunately I didn't think about this earlier | |
1a931653 | 387 | // ---------------------------------------------------------------------------- |
76314141 | 388 | |
5a1cad6e GD |
389 | #define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ |
390 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ | |
391 | wxARRAY_DEFAULT_EXPORT) | |
76314141 | 392 | |
5a1cad6e GD |
393 | #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ |
394 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ | |
395 | WXDLLEXPORT) | |
1a931653 | 396 | |
5a1cad6e GD |
397 | #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ |
398 | expmode) \ | |
1a931653 | 399 | typedef T _wxArray##name; \ |
5a1cad6e GD |
400 | _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, = cmpfunc, \ |
401 | class expmode) | |
1a931653 VZ |
402 | |
403 | // ---------------------------------------------------------------------------- | |
404 | // WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class | |
405 | // named "name" which owns the objects of type T it contains, i.e. it will | |
406 | // delete them when it is destroyed. | |
407 | // | |
408 | // An element is of type T*, but arguments of type T& are taken (see below!) | |
409 | // and T& is returned. | |
410 | // | |
411 | // Don't use this for simple types such as "int" or "long"! | |
1a931653 VZ |
412 | // |
413 | // Note on Add/Insert functions: | |
414 | // 1) function(T*) gives the object to the array, i.e. it will delete the | |
415 | // object when it's removed or in the array's dtor | |
416 | // 2) function(T&) will create a copy of the object and work with it | |
417 | // | |
418 | // Also: | |
419 | // 1) Remove() will delete the object after removing it from the array | |
420 | // 2) Detach() just removes the object from the array (returning pointer to it) | |
421 | // | |
422 | // NB1: Base type T should have an accessible copy ctor if Add(T&) is used | |
423 | // NB2: Never ever cast a array to it's base type: as dtor is not virtual | |
424 | // and so you risk having at least the memory leaks and probably worse | |
425 | // | |
426 | // Some functions of this class are not inline, so it takes some space to | |
427 | // define new class from this template even if you don't use it - which is not | |
428 | // the case for the simple (non-object) array classes | |
429 | // | |
1a931653 VZ |
430 | // To use an objarray class you must |
431 | // #include "dynarray.h" | |
432 | // WX_DECLARE_OBJARRAY(element_type, list_class_name) | |
433 | // #include "arrimpl.cpp" | |
434 | // WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above! | |
435 | // | |
436 | // This is necessary because at the moment of DEFINE_OBJARRAY class parsing the | |
437 | // element_type must be fully defined (i.e. forward declaration is not | |
438 | // enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of | |
439 | // two allows to break cicrcular dependencies with classes which have member | |
440 | // variables of objarray type. | |
441 | // ---------------------------------------------------------------------------- | |
442 | ||
443 | #define WX_DECLARE_OBJARRAY(T, name) \ | |
444 | WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT) | |
445 | ||
446 | #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \ | |
447 | WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLEXPORT) | |
448 | ||
449 | #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \ | |
450 | typedef T _wxObjArray##name; \ | |
5a1cad6e | 451 | _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, wxArrayPtrVoid, class expmode) |
1a931653 VZ |
452 | |
453 | // WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included, | |
454 | // try to provoke a human-understandable error if it used incorrectly. | |
455 | // | |
456 | // there is no real need for 3 different macros in the DEFINE case but do it | |
457 | // anyhow for consistency | |
458 | #define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp | |
459 | #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) | |
76314141 | 460 | #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) |
76314141 | 461 | |
5a1cad6e GD |
462 | // ---------------------------------------------------------------------------- |
463 | // Some commonly used predefined base arrays | |
464 | // ---------------------------------------------------------------------------- | |
465 | ||
f1322419 VZ |
466 | WX_DECLARE_EXPORTED_BASEARRAY(const void *, wxBaseArrayPtrVoid); |
467 | WX_DECLARE_EXPORTED_BASEARRAY(short, wxBaseArrayShort); | |
468 | WX_DECLARE_EXPORTED_BASEARRAY(int, wxBaseArrayInt); | |
469 | WX_DECLARE_EXPORTED_BASEARRAY(long, wxBaseArrayLong); | |
470 | WX_DECLARE_EXPORTED_BASEARRAY(double, wxBaseArrayDouble); | |
5a1cad6e GD |
471 | |
472 | // ---------------------------------------------------------------------------- | |
473 | // Convenience macros to define arrays from base arrays | |
474 | // ---------------------------------------------------------------------------- | |
475 | ||
476 | #define WX_DEFINE_ARRAY(T, name) \ | |
477 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
478 | #define WX_DEFINE_EXPORTED_ARRAY(T, name) \ | |
479 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
480 | #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \ | |
481 | WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode) | |
482 | ||
483 | #define WX_DEFINE_ARRAY_SHORT(T, name) \ | |
484 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayShort) | |
485 | #define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \ | |
486 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort) | |
487 | #define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ | |
488 | WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode) | |
489 | ||
490 | #define WX_DEFINE_ARRAY_INT(T, name) \ | |
491 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayInt) | |
492 | #define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \ | |
493 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt) | |
494 | #define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ | |
495 | WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode) | |
496 | ||
497 | #define WX_DEFINE_ARRAY_LONG(T, name) \ | |
498 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayLong) | |
499 | #define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \ | |
500 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong) | |
501 | #define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ | |
502 | WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode) | |
503 | ||
504 | #define WX_DEFINE_ARRAY_DOUBLE(T, name) \ | |
505 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayDouble) | |
506 | #define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \ | |
507 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayDouble) | |
508 | #define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \ | |
509 | WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayDouble, expmode) | |
510 | ||
511 | // ---------------------------------------------------------------------------- | |
512 | // Convenience macros to define sorted arrays from base arrays | |
513 | // ---------------------------------------------------------------------------- | |
514 | ||
515 | #define WX_DEFINE_SORTED_ARRAY(T, name) \ | |
516 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
517 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \ | |
518 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
519 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \ | |
520 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode) | |
521 | ||
522 | #define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \ | |
523 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort) | |
524 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \ | |
525 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort) | |
526 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ | |
527 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode) | |
528 | ||
529 | #define WX_DEFINE_SORTED_ARRAY_INT(T, name) \ | |
530 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt) | |
531 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \ | |
532 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt) | |
533 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ | |
534 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode) | |
535 | ||
536 | #define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \ | |
537 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong) | |
538 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \ | |
539 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong) | |
540 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ | |
541 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode) | |
542 | ||
543 | // ---------------------------------------------------------------------------- | |
544 | // Convenience macros to define sorted arrays from base arrays | |
545 | // ---------------------------------------------------------------------------- | |
546 | ||
547 | #define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \ | |
548 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) | |
549 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \ | |
550 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) | |
551 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \ | |
552 | name, expmode) \ | |
553 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
554 | wxBaseArrayPtrVoid, expmode) | |
555 | ||
556 | #define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ | |
557 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) | |
558 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ | |
559 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) | |
560 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \ | |
561 | name, expmode) \ | |
562 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
563 | wxBaseArrayShort, expmode) | |
564 | ||
565 | #define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ | |
566 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) | |
567 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ | |
568 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) | |
569 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \ | |
570 | name, expmode) \ | |
571 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
572 | wxBaseArrayInt, expmode) | |
573 | ||
574 | #define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ | |
575 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) | |
576 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ | |
577 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) | |
578 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \ | |
579 | name, expmode) \ | |
580 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
581 | wxBaseArrayLong, expmode) | |
582 | ||
c801d85f | 583 | // ---------------------------------------------------------------------------- |
1a931653 | 584 | // Some commonly used predefined arrays |
c801d85f KB |
585 | // ---------------------------------------------------------------------------- |
586 | ||
5a1cad6e GD |
587 | WX_DEFINE_EXPORTED_ARRAY_SHORT (short, wxArrayShort); |
588 | WX_DEFINE_EXPORTED_ARRAY_INT (int, wxArrayInt); | |
589 | WX_DEFINE_EXPORTED_ARRAY_LONG (long, wxArrayLong); | |
590 | WX_DEFINE_EXPORTED_ARRAY (void *, wxArrayPtrVoid); | |
c801d85f | 591 | |
2b9bd418 | 592 | // ----------------------------------------------------------------------------- |
0cbff120 | 593 | // convenience macros |
2b9bd418 VZ |
594 | // ----------------------------------------------------------------------------- |
595 | ||
4f6aed9c VZ |
596 | // append all element of one array to another one |
597 | #define WX_APPEND_ARRAY(array, other) \ | |
598 | { \ | |
5d5b1c0c | 599 | size_t count = (other).Count(); \ |
4f6aed9c VZ |
600 | for ( size_t n = 0; n < count; n++ ) \ |
601 | { \ | |
5d5b1c0c | 602 | (array).Add((other)[n]); \ |
4f6aed9c VZ |
603 | } \ |
604 | } | |
605 | ||
2b9bd418 VZ |
606 | // delete all array elements |
607 | // | |
608 | // NB: the class declaration of the array elements must be visible from the | |
609 | // place where you use this macro, otherwise the proper destructor may not | |
610 | // be called (a decent compiler should give a warning about it, but don't | |
611 | // count on it)! | |
612 | #define WX_CLEAR_ARRAY(array) \ | |
613 | { \ | |
5d5b1c0c | 614 | size_t count = (array).Count(); \ |
2b9bd418 VZ |
615 | for ( size_t n = 0; n < count; n++ ) \ |
616 | { \ | |
5d5b1c0c | 617 | delete (array)[n]; \ |
2b9bd418 | 618 | } \ |
2c356747 | 619 | \ |
5d5b1c0c | 620 | (array).Empty(); \ |
2b9bd418 | 621 | } |
a497618a | 622 | |
c801d85f KB |
623 | #endif // _DYNARRAY_H |
624 |