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