]>
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 | 330 | #define WX_DEFINE_TYPEARRAY(T, name, base) \ |
68559fd5 | 331 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class wxARRAY_DEFAULT_EXPORT) |
1a931653 | 332 | |
5a1cad6e | 333 | #define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \ |
68559fd5 | 334 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class WXDLLEXPORT) |
1a931653 | 335 | |
68559fd5 VZ |
336 | #define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expdecl) \ |
337 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class expdecl) | |
338 | ||
339 | #define WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, classdecl) \ | |
5a1cad6e | 340 | typedef T _wxArray##name; \ |
68559fd5 | 341 | _WX_DEFINE_TYPEARRAY(_wxArray##name, name, base, classdecl) |
1a931653 VZ |
342 | |
343 | // ---------------------------------------------------------------------------- | |
5a1cad6e | 344 | // WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it |
1a931653 VZ |
345 | // defines a sorted array. |
346 | // | |
347 | // Differences: | |
348 | // 1) it must be given a COMPARE function in ctor which takes 2 items of type | |
349 | // T* and should return -1, 0 or +1 if the first one is less/greater | |
350 | // than/equal to the second one. | |
351 | // 2) the Add() method inserts the item in such was that the array is always | |
352 | // sorted (it uses the COMPARE function) | |
353 | // 3) it has no Sort() method because it's always sorted | |
354 | // 4) Index() method is much faster (the sorted arrays use binary search | |
355 | // instead of linear one), but Add() is slower. | |
356 | // 5) there is no Insert() method because you can't insert an item into the | |
357 | // given position in a sorted array but there is IndexForInsert()/AddAt() | |
358 | // pair which may be used to optimize a common operation of "insert only if | |
359 | // not found" | |
360 | // | |
361 | // Note that you have to specify the comparison function when creating the | |
362 | // objects of this array type. If, as in 99% of cases, the comparison function | |
5a1cad6e GD |
363 | // is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below |
364 | // is more convenient. | |
1a931653 VZ |
365 | // |
366 | // Summary: use this class when the speed of Index() function is important, use | |
367 | // the normal arrays otherwise. | |
c801d85f KB |
368 | // ---------------------------------------------------------------------------- |
369 | ||
17e656b0 VZ |
370 | #define wxARRAY_EMPTY_CMP |
371 | ||
5a1cad6e GD |
372 | #define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \ |
373 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \ | |
374 | wxARRAY_DEFAULT_EXPORT) | |
a497618a | 375 | |
5a1cad6e GD |
376 | #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \ |
377 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLEXPORT) | |
a497618a | 378 | |
5a1cad6e GD |
379 | #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \ |
380 | typedef T _wxArray##name; \ | |
381 | _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, \ | |
382 | wxARRAY_EMPTY_CMP, class expmode) | |
1a931653 VZ |
383 | |
384 | // ---------------------------------------------------------------------------- | |
5a1cad6e | 385 | // WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison |
1a931653 VZ |
386 | // function is provided by this macro and the objects of this class have a |
387 | // default constructor which just uses it. | |
388 | // | |
389 | // The arguments are: the element type, the comparison function and the array | |
390 | // name | |
391 | // | |
5a1cad6e GD |
392 | // NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked |
393 | // from the very beginning - unfortunately I didn't think about this earlier | |
1a931653 | 394 | // ---------------------------------------------------------------------------- |
76314141 | 395 | |
5a1cad6e GD |
396 | #define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ |
397 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ | |
398 | wxARRAY_DEFAULT_EXPORT) | |
76314141 | 399 | |
5a1cad6e GD |
400 | #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ |
401 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ | |
402 | WXDLLEXPORT) | |
1a931653 | 403 | |
5a1cad6e GD |
404 | #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ |
405 | expmode) \ | |
1a931653 | 406 | typedef T _wxArray##name; \ |
5a1cad6e GD |
407 | _WX_DEFINE_SORTED_TYPEARRAY(_wxArray##name, name, base, = cmpfunc, \ |
408 | class expmode) | |
1a931653 VZ |
409 | |
410 | // ---------------------------------------------------------------------------- | |
411 | // WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class | |
412 | // named "name" which owns the objects of type T it contains, i.e. it will | |
413 | // delete them when it is destroyed. | |
414 | // | |
415 | // An element is of type T*, but arguments of type T& are taken (see below!) | |
416 | // and T& is returned. | |
417 | // | |
418 | // Don't use this for simple types such as "int" or "long"! | |
1a931653 VZ |
419 | // |
420 | // Note on Add/Insert functions: | |
421 | // 1) function(T*) gives the object to the array, i.e. it will delete the | |
422 | // object when it's removed or in the array's dtor | |
423 | // 2) function(T&) will create a copy of the object and work with it | |
424 | // | |
425 | // Also: | |
426 | // 1) Remove() will delete the object after removing it from the array | |
427 | // 2) Detach() just removes the object from the array (returning pointer to it) | |
428 | // | |
429 | // NB1: Base type T should have an accessible copy ctor if Add(T&) is used | |
430 | // NB2: Never ever cast a array to it's base type: as dtor is not virtual | |
431 | // and so you risk having at least the memory leaks and probably worse | |
432 | // | |
433 | // Some functions of this class are not inline, so it takes some space to | |
434 | // define new class from this template even if you don't use it - which is not | |
435 | // the case for the simple (non-object) array classes | |
436 | // | |
1a931653 VZ |
437 | // To use an objarray class you must |
438 | // #include "dynarray.h" | |
439 | // WX_DECLARE_OBJARRAY(element_type, list_class_name) | |
440 | // #include "arrimpl.cpp" | |
441 | // WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above! | |
442 | // | |
443 | // This is necessary because at the moment of DEFINE_OBJARRAY class parsing the | |
444 | // element_type must be fully defined (i.e. forward declaration is not | |
445 | // enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of | |
446 | // two allows to break cicrcular dependencies with classes which have member | |
447 | // variables of objarray type. | |
448 | // ---------------------------------------------------------------------------- | |
449 | ||
450 | #define WX_DECLARE_OBJARRAY(T, name) \ | |
451 | WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT) | |
452 | ||
453 | #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \ | |
454 | WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLEXPORT) | |
455 | ||
456 | #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \ | |
457 | typedef T _wxObjArray##name; \ | |
5a1cad6e | 458 | _WX_DECLARE_OBJARRAY(_wxObjArray##name, name, wxArrayPtrVoid, class expmode) |
1a931653 VZ |
459 | |
460 | // WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included, | |
461 | // try to provoke a human-understandable error if it used incorrectly. | |
462 | // | |
463 | // there is no real need for 3 different macros in the DEFINE case but do it | |
464 | // anyhow for consistency | |
465 | #define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp | |
466 | #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) | |
76314141 | 467 | #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) |
76314141 | 468 | |
5a1cad6e GD |
469 | // ---------------------------------------------------------------------------- |
470 | // Some commonly used predefined base arrays | |
471 | // ---------------------------------------------------------------------------- | |
472 | ||
eee614d3 VS |
473 | WX_DECLARE_USER_EXPORTED_BASEARRAY(const void *, wxBaseArrayPtrVoid, |
474 | WXDLLIMPEXP_BASE); | |
475 | WX_DECLARE_USER_EXPORTED_BASEARRAY(short, wxBaseArrayShort, | |
476 | WXDLLIMPEXP_BASE); | |
477 | WX_DECLARE_USER_EXPORTED_BASEARRAY(int, wxBaseArrayInt, | |
478 | WXDLLIMPEXP_BASE); | |
479 | WX_DECLARE_USER_EXPORTED_BASEARRAY(long, wxBaseArrayLong, | |
480 | WXDLLIMPEXP_BASE); | |
481 | WX_DECLARE_USER_EXPORTED_BASEARRAY(double, wxBaseArrayDouble, | |
482 | WXDLLIMPEXP_BASE); | |
5a1cad6e GD |
483 | |
484 | // ---------------------------------------------------------------------------- | |
485 | // Convenience macros to define arrays from base arrays | |
486 | // ---------------------------------------------------------------------------- | |
487 | ||
488 | #define WX_DEFINE_ARRAY(T, name) \ | |
489 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
490 | #define WX_DEFINE_EXPORTED_ARRAY(T, name) \ | |
491 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
492 | #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \ | |
991023b4 | 493 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayPtrVoid, expmode) |
5a1cad6e GD |
494 | |
495 | #define WX_DEFINE_ARRAY_SHORT(T, name) \ | |
496 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayShort) | |
497 | #define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \ | |
498 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort) | |
499 | #define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ | |
991023b4 | 500 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayShort, expmode) |
5a1cad6e GD |
501 | |
502 | #define WX_DEFINE_ARRAY_INT(T, name) \ | |
503 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayInt) | |
504 | #define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \ | |
505 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt) | |
506 | #define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ | |
991023b4 | 507 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayInt, expmode) |
5a1cad6e GD |
508 | |
509 | #define WX_DEFINE_ARRAY_LONG(T, name) \ | |
510 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayLong) | |
511 | #define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \ | |
512 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong) | |
513 | #define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ | |
991023b4 | 514 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayLong, expmode) |
5a1cad6e GD |
515 | |
516 | #define WX_DEFINE_ARRAY_DOUBLE(T, name) \ | |
517 | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayDouble) | |
518 | #define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \ | |
519 | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayDouble) | |
520 | #define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \ | |
991023b4 | 521 | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayDouble, expmode) |
5a1cad6e GD |
522 | |
523 | // ---------------------------------------------------------------------------- | |
524 | // Convenience macros to define sorted arrays from base arrays | |
525 | // ---------------------------------------------------------------------------- | |
526 | ||
527 | #define WX_DEFINE_SORTED_ARRAY(T, name) \ | |
528 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
529 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \ | |
530 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) | |
531 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \ | |
532 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, expmode) | |
533 | ||
534 | #define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \ | |
535 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort) | |
536 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \ | |
537 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort) | |
538 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ | |
539 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, expmode) | |
540 | ||
541 | #define WX_DEFINE_SORTED_ARRAY_INT(T, name) \ | |
542 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt) | |
543 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \ | |
544 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt) | |
545 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ | |
546 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode) | |
547 | ||
548 | #define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \ | |
549 | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong) | |
550 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \ | |
551 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong) | |
552 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ | |
553 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode) | |
554 | ||
555 | // ---------------------------------------------------------------------------- | |
556 | // Convenience macros to define sorted arrays from base arrays | |
557 | // ---------------------------------------------------------------------------- | |
558 | ||
559 | #define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \ | |
560 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) | |
561 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \ | |
562 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) | |
563 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \ | |
564 | name, expmode) \ | |
565 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
566 | wxBaseArrayPtrVoid, expmode) | |
567 | ||
568 | #define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ | |
569 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) | |
570 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ | |
571 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) | |
572 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \ | |
573 | name, expmode) \ | |
574 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
575 | wxBaseArrayShort, expmode) | |
576 | ||
577 | #define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ | |
578 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) | |
579 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ | |
580 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) | |
581 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \ | |
582 | name, expmode) \ | |
583 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
584 | wxBaseArrayInt, expmode) | |
585 | ||
586 | #define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ | |
587 | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) | |
588 | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ | |
589 | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) | |
590 | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \ | |
591 | name, expmode) \ | |
592 | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ | |
593 | wxBaseArrayLong, expmode) | |
594 | ||
c801d85f | 595 | // ---------------------------------------------------------------------------- |
1a931653 | 596 | // Some commonly used predefined arrays |
c801d85f KB |
597 | // ---------------------------------------------------------------------------- |
598 | ||
991023b4 VS |
599 | WX_DEFINE_USER_EXPORTED_ARRAY_SHORT (short, wxArrayShort, class WXDLLIMPEXP_BASE); |
600 | WX_DEFINE_USER_EXPORTED_ARRAY_INT (int, wxArrayInt, class WXDLLIMPEXP_BASE); | |
601 | WX_DEFINE_USER_EXPORTED_ARRAY_LONG (long, wxArrayLong, class WXDLLIMPEXP_BASE); | |
602 | WX_DEFINE_USER_EXPORTED_ARRAY (void *, wxArrayPtrVoid, class WXDLLIMPEXP_BASE); | |
c801d85f | 603 | |
2b9bd418 | 604 | // ----------------------------------------------------------------------------- |
0cbff120 | 605 | // convenience macros |
2b9bd418 VZ |
606 | // ----------------------------------------------------------------------------- |
607 | ||
4f6aed9c VZ |
608 | // append all element of one array to another one |
609 | #define WX_APPEND_ARRAY(array, other) \ | |
610 | { \ | |
5d5b1c0c | 611 | size_t count = (other).Count(); \ |
4f6aed9c VZ |
612 | for ( size_t n = 0; n < count; n++ ) \ |
613 | { \ | |
5d5b1c0c | 614 | (array).Add((other)[n]); \ |
4f6aed9c VZ |
615 | } \ |
616 | } | |
617 | ||
2b9bd418 VZ |
618 | // delete all array elements |
619 | // | |
620 | // NB: the class declaration of the array elements must be visible from the | |
621 | // place where you use this macro, otherwise the proper destructor may not | |
622 | // be called (a decent compiler should give a warning about it, but don't | |
623 | // count on it)! | |
624 | #define WX_CLEAR_ARRAY(array) \ | |
625 | { \ | |
5d5b1c0c | 626 | size_t count = (array).Count(); \ |
2b9bd418 VZ |
627 | for ( size_t n = 0; n < count; n++ ) \ |
628 | { \ | |
5d5b1c0c | 629 | delete (array)[n]; \ |
2b9bd418 | 630 | } \ |
2c356747 | 631 | \ |
5d5b1c0c | 632 | (array).Empty(); \ |
2b9bd418 | 633 | } |
a497618a | 634 | |
c801d85f KB |
635 | #endif // _DYNARRAY_H |
636 |