]> git.saurik.com Git - wxWidgets.git/blame - include/wx/dynarray.h
wxToolBar support in wxUniv/wxFrame
[wxWidgets.git] / include / wx / dynarray.h
CommitLineData
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
2600d8ee 21/** @name Dynamic arrays and object arrays (array which own their elements)
c801d85f
KB
22 @memo Arrays which grow on demand and do range checking (only in debug)
23 */
24//@{
25
26// ----------------------------------------------------------------------------
27// constants
28// ----------------------------------------------------------------------------
29
30/**
2600d8ee 31 the initial size by which an array grows when an element is added
3bfa4402 32 default value avoids allocate one or two bytes when the array is created
c801d85f
KB
33 which is rather inefficient
34*/
35#define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
36
37// ----------------------------------------------------------------------------
38// types
39// ----------------------------------------------------------------------------
40
41/**
42 callback compare function for quick sort
3bfa4402 43 must return negative value, 0 or positive value if pItem1 <, = or > pItem2
c801d85f 44 */
0661ec39 45typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
c801d85f
KB
46
47// ----------------------------------------------------------------------------
48/**
49 base class managing data having size of type 'long' (not used directly)
50
51 NB: for efficiency this often used class has no virtual functions (hence no
3bfa4402
VZ
52 VTBL), even dtor is <B>not</B> virtual. If used as expected it won't
53 create any problems because ARRAYs from DEFINE_ARRAY have no dtor at all,
54 so it's not too important if it's not called (this happens when you cast
c801d85f
KB
55 "SomeArray *" as "BaseArray *" and then delete it)
56
2600d8ee 57 @memo Base class for template array classes
c801d85f
KB
58*/
59// ----------------------------------------------------------------------------
fbcb4166 60class WXDLLEXPORT wxBaseArray
c801d85f
KB
61{
62public:
63 /** @name ctors and dtor */
64 //@{
65 /// default ctor
66 wxBaseArray();
67 /// copy ctor
68 wxBaseArray(const wxBaseArray& array);
69 /// assignment operator
70 wxBaseArray& operator=(const wxBaseArray& src);
71 /// not virtual, see above
c801d85f
KB
72 ~wxBaseArray();
73 //@}
74
75 /** @name memory management */
76 //@{
2600d8ee 77 /// empties the array, but doesn't release memory
3093cef8 78 void Empty() { m_nCount = 0; }
2600d8ee 79 /// empties the array and releases memory
c801d85f
KB
80 void Clear();
81 /// preallocates memory for given number of items
c86f1403 82 void Alloc(size_t uiSize);
3093cef8
VZ
83 /// minimizes the memory used by the array (frees unused memory)
84 void Shrink();
c801d85f
KB
85 //@}
86
87 /** @name simple accessors */
88 //@{
89 /// number of elements in the array
3093cef8
VZ
90 size_t Count() const { return m_nCount; }
91 size_t GetCount() const { return m_nCount; }
c801d85f 92 /// is it empty?
3093cef8 93 bool IsEmpty() const { return m_nCount == 0; }
c801d85f
KB
94 //@}
95
96protected:
3bfa4402 97 // these methods are protected because if they were public one could
2600d8ee 98 // mistakenly call one of them instead of DEFINE_ARRAY's or OBJARRAY's
c801d85f
KB
99 // type safe methods
100
101 /** @name items access */
102 //@{
103 /// get item at position uiIndex (range checking is done in debug version)
c86f1403 104 long& Item(size_t uiIndex) const
3093cef8 105 { wxASSERT( uiIndex < m_nCount ); return m_pItems[uiIndex]; }
c801d85f 106 /// same as Item()
c86f1403 107 long& operator[](size_t uiIndex) const { return Item(uiIndex); }
c801d85f
KB
108 //@}
109
110 /** @name item management */
111 //@{
112 /**
113 Search the element in the array, starting from the either side
114 @param bFromEnd if TRUE, start from the end
3c67202d
VZ
115 @return index of the first item matched or wxNOT_FOUND
116 @see wxNOT_FOUND
c801d85f 117 */
3bfa4402
VZ
118 int Index(long lItem, bool bFromEnd = FALSE) const;
119 /// search for an item using binary search in a sorted array
e99c3048 120 int Index(long lItem, CMPFUNC fnCompare) const;
b54e41c5
VZ
121 /// search for a place to insert the element into a sorted array
122 size_t IndexForInsert(long lItem, CMPFUNC fnCompare) const;
c801d85f 123 /// add new element at the end
3bfa4402
VZ
124 void Add(long lItem);
125 /// add item assuming the array is sorted with fnCompare function
126 void Add(long lItem, CMPFUNC fnCompare);
127 /// add new element at given position (it becomes Item[uiIndex])
c86f1403 128 void Insert(long lItem, size_t uiIndex);
c801d85f
KB
129 /// remove first item matching this value
130 void Remove(long lItem);
131 /// remove item by index
8a729bb8 132 void RemoveAt(size_t uiIndex);
c801d85f
KB
133 //@}
134
135 /// sort array elements using given compare function
3bfa4402 136 void Sort(CMPFUNC fnCompare);
c801d85f
KB
137
138private:
139 void Grow(); // makes array bigger if needed
140
3093cef8
VZ
141 size_t m_nSize, // current size of the array
142 m_nCount; // current number of elements
c801d85f
KB
143
144 long *m_pItems; // pointer to data
145};
146
147// ============================================================================
148// template classes
149// ============================================================================
150
223d09f6
KB
151// resolves the name conflict between the wxT() macor and T typedef: we can't
152// use wxT() inside WX_DEFINE_ARRAY!
153#define _WX_ERROR_SIZEOF wxT("illegal use of DEFINE_ARRAY")
154#define _WX_ERROR_REMOVE wxT("removing inexisting element in wxArray::Remove")
e90c1d2a 155
c801d85f
KB
156// ----------------------------------------------------------------------------
157// This macro generates a new array class. It is intended for storage of simple
158// types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
3bfa4402 159//
c801d85f 160// NB: it has only inline functions => takes no space at all
a3ef5bf5
JS
161// Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
162// { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
163// so using a temporary variable instead.
c801d85f 164// ----------------------------------------------------------------------------
03e11df5 165// __MAC_X__ added min ~name() below for compiling Mac X
a497618a 166#define _WX_DEFINE_ARRAY(T, name, classexp) \
c801d85f 167typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
a497618a 168classexp name : public wxBaseArray \
c801d85f
KB
169{ \
170public: \
171 name() \
e70f5e13
RR
172 { \
173 size_t type = sizeof(T); \
54da4255 174 size_t sizelong = sizeof(long); \
d422d01e 175 if ( type > sizelong ) \
e90c1d2a 176 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
54da4255 177 } \
03e11df5 178 ~name() {} \
c801d85f
KB
179 \
180 name& operator=(const name& src) \
a3ef5bf5
JS
181 { wxBaseArray* temp = (wxBaseArray*) this; \
182 (*temp) = ((const wxBaseArray&)src); \
c801d85f
KB
183 return *this; } \
184 \
3093cef8 185 T& operator[](size_t uiIndex) const \
c801d85f 186 { return (T&)(wxBaseArray::Item(uiIndex)); } \
3093cef8 187 T& Item(size_t uiIndex) const \
c801d85f 188 { return (T&)(wxBaseArray::Item(uiIndex)); } \
3bfaaefe
VZ
189 T& Last() const \
190 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
c801d85f 191 \
bc7665d4 192 int Index(T Item, bool bFromEnd = FALSE) const \
c801d85f
KB
193 { return wxBaseArray::Index((long)Item, bFromEnd); } \
194 \
195 void Add(T Item) \
196 { wxBaseArray::Add((long)Item); } \
3093cef8 197 void Insert(T Item, size_t uiIndex) \
c801d85f
KB
198 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
199 \
8a729bb8 200 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
c801d85f
KB
201 void Remove(T Item) \
202 { int iIndex = Index(Item); \
9d2f3c71 203 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
e90c1d2a 204 _WX_ERROR_REMOVE); \
f373f197 205 wxBaseArray::RemoveAt((size_t)iIndex); } \
c801d85f
KB
206 \
207 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
208}
209
3bfa4402
VZ
210// ----------------------------------------------------------------------------
211// This is the same as the previous macro, but it defines a sorted array.
212// Differences:
213// 1) it must be given a COMPARE function in ctor which takes 2 items of type
214// T* and should return -1, 0 or +1 if the first one is less/greater
215// than/equal to the second one.
216// 2) the Add() method inserts the item in such was that the array is always
217// sorted (it uses the COMPARE function)
218// 3) it has no Sort() method because it's always sorted
219// 4) Index() method is much faster (the sorted arrays use binary search
220// instead of linear one), but Add() is slower.
b54e41c5
VZ
221// 5) there is no Insert() method because you can't insert an item into the
222// given position in a sorted array but there is IndexForInsert()/AddAt()
223// pair which may be used to optimize a common operation of "insert only if
224// not found"
3bfa4402
VZ
225//
226// Summary: use this class when the speed of Index() function is important, use
227// the normal arrays otherwise.
228//
229// NB: it has only inline functions => takes no space at all
a3ef5bf5
JS
230// Mod by JACS: Salford C++ doesn't like 'var->operator=' syntax, as in:
231// { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src);
232// so using a temporary variable instead.
3bfa4402 233// ----------------------------------------------------------------------------
a497618a 234#define _WX_DEFINE_SORTED_ARRAY(T, name, classexp) \
3bfa4402 235typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
a497618a 236classexp name : public wxBaseArray \
3bfa4402
VZ
237{ \
238public: \
239 name(SCMPFUNC##T fn) \
54da4255
DW
240 { size_t type = sizeof(T); \
241 size_t sizelong = sizeof(long); \
d422d01e 242 if ( type > sizelong ) \
e90c1d2a 243 { wxFAIL_MSG( _WX_ERROR_SIZEOF ); } \
54da4255
DW
244 m_fnCompare = fn; \
245 } \
3bfa4402
VZ
246 \
247 name& operator=(const name& src) \
a3ef5bf5
JS
248 { wxBaseArray* temp = (wxBaseArray*) this; \
249 (*temp) = ((const wxBaseArray&)src); \
3bfa4402
VZ
250 m_fnCompare = src.m_fnCompare; \
251 return *this; } \
252 \
3093cef8 253 T& operator[](size_t uiIndex) const \
3bfa4402 254 { return (T&)(wxBaseArray::Item(uiIndex)); } \
3093cef8 255 T& Item(size_t uiIndex) const \
3bfa4402
VZ
256 { return (T&)(wxBaseArray::Item(uiIndex)); } \
257 T& Last() const \
258 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
259 \
260 int Index(T Item) const \
261 { return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
262 \
b54e41c5
VZ
263 size_t IndexForInsert(T Item) const \
264 { return wxBaseArray::IndexForInsert((long)Item, \
265 (CMPFUNC)m_fnCompare); } \
266 \
267 void AddAt(T item, size_t index) \
268 { wxBaseArray::Insert((long)item, index); } \
269 \
3bfa4402
VZ
270 void Add(T Item) \
271 { wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
272 \
8a729bb8 273 void RemoveAt(size_t uiIndex) { wxBaseArray::RemoveAt(uiIndex); } \
3bfa4402
VZ
274 void Remove(T Item) \
275 { int iIndex = Index(Item); \
9d2f3c71 276 wxCHECK2_MSG( iIndex != wxNOT_FOUND, return, \
e90c1d2a 277 _WX_ERROR_REMOVE ); \
f373f197 278 wxBaseArray::RemoveAt((size_t)iIndex); } \
3bfa4402
VZ
279 \
280private: \
281 SCMPFUNC##T m_fnCompare; \
282}
283
c801d85f 284// ----------------------------------------------------------------------------
2600d8ee 285// see WX_DECLARE_OBJARRAY and WX_DEFINE_OBJARRAY
c801d85f 286// ----------------------------------------------------------------------------
a497618a 287#define _WX_DECLARE_OBJARRAY(T, name, classexp) \
c801d85f 288typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
a497618a 289classexp name : public wxBaseArray \
c801d85f
KB
290{ \
291public: \
292 name() { } \
293 name(const name& src); \
294 name& operator=(const name& src); \
295 \
296 ~name(); \
297 \
3093cef8 298 T& operator[](size_t uiIndex) const \
c801d85f 299 { return *(T*)wxBaseArray::Item(uiIndex); } \
3093cef8 300 T& Item(size_t uiIndex) const \
c801d85f 301 { return *(T*)wxBaseArray::Item(uiIndex); } \
3bfaaefe
VZ
302 T& Last() const \
303 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
c801d85f 304 \
bc7665d4 305 int Index(const T& Item, bool bFromEnd = FALSE) const; \
c801d85f
KB
306 \
307 void Add(const T& Item); \
308 void Add(const T* pItem) \
309 { wxBaseArray::Add((long)pItem); } \
310 \
3093cef8
VZ
311 void Insert(const T& Item, size_t uiIndex); \
312 void Insert(const T* pItem, size_t uiIndex) \
c801d85f
KB
313 { wxBaseArray::Insert((long)pItem, uiIndex); } \
314 \
f6bcfd97
BP
315 void Empty() { DoEmpty(); wxBaseArray::Empty(); } \
316 void Clear() { DoEmpty(); wxBaseArray::Clear(); } \
c801d85f 317 \
3093cef8 318 T* Detach(size_t uiIndex) \
c801d85f 319 { T* p = (T*)wxBaseArray::Item(uiIndex); \
32193feb 320 wxBaseArray::RemoveAt(uiIndex); return p; } \
8a729bb8 321 void RemoveAt(size_t uiIndex); \
c801d85f
KB
322 \
323 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
324 \
325private: \
f6bcfd97 326 void DoEmpty(); \
c801d85f
KB
327 void DoCopy(const name& src); \
328}
329
330// ----------------------------------------------------------------------------
2600d8ee 331/** @name Macros for definition of dynamic arrays and objarrays
c801d85f
KB
332
333 These macros are ugly (especially if you look in the sources ;-), but they
334 allow us to define 'template' classes without actually using templates.
335 <BR>
336 <BR>
2600d8ee
VZ
337 Range checking is performed in debug build for both arrays and objarrays.
338 Type checking is done at compile-time. Warning: arrays <I>never</I> shrink,
339 they only grow, so loading 10 millions in an array only to delete them 2
340 lines below is <I>not</I> recommended. However, it does free memory when
341 it's destroyed, so if you destroy array also, it's ok.
c801d85f
KB
342 */
343// ----------------------------------------------------------------------------
344
345//@{
346 /**
347 This macro generates a new array class. It is intended for storage of simple
348 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
349 <BR>
350 NB: it has only inline functions => takes no space at all
351 <BR>
352
353 @memo declare and define array class 'name' containing elements of type 'T'
354 */
a497618a
VZ
355#define WX_DEFINE_ARRAY(T, name) \
356 typedef T _A##name; \
357 _WX_DEFINE_ARRAY(_A##name, name, class)
3bfa4402
VZ
358
359 /**
360 This macro does the same as WX_DEFINE_ARRAY except that the array will be
361 sorted with the specified compare function.
362 */
a497618a
VZ
363#define WX_DEFINE_SORTED_ARRAY(T, name) \
364 typedef T _A##name; \
365 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class)
3bfa4402 366
c801d85f 367 /**
2600d8ee
VZ
368 This macro generates a new objarrays class which owns the objects it
369 contains, i.e. it will delete them when it is destroyed. An element is of
370 type T*, but arguments of type T& are taken (see below!) and T& is
371 returned. <BR>
c801d85f
KB
372 Don't use this for simple types such as "int" or "long"!
373 You _may_ use it for "double" but it's awfully inefficient.
374 <BR>
375 <BR>
376 Note on Add/Insert functions:
377 <BR>
2600d8ee
VZ
378 1) function(T*) gives the object to the array, i.e. it will delete the
379 object when it's removed or in the array's dtor
c801d85f
KB
380 <BR>
381 2) function(T&) will create a copy of the object and work with it
382 <BR>
383 <BR>
384 Also:
385 <BR>
2600d8ee 386 1) Remove() will delete the object after removing it from the array
c801d85f 387 <BR>
2600d8ee 388 2) Detach() just removes the object from the array (returning pointer to it)
c801d85f
KB
389 <BR>
390 <BR>
391 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
392 <BR>
2600d8ee 393 NB2: Never ever cast a array to it's base type: as dtor is <B>not</B> virtual
c801d85f
KB
394 it will provoke memory leaks
395 <BR>
396 <BR>
3bfa4402 397 some functions of this class are not inline, so it takes some space to
c801d85f
KB
398 define new class from this template.
399
2600d8ee 400 @memo declare objarray class 'name' containing elements of type 'T'
c801d85f 401 */
a497618a
VZ
402#define WX_DECLARE_OBJARRAY(T, name) \
403 typedef T _L##name; \
404 _WX_DECLARE_OBJARRAY(_L##name, name, class)
405
c801d85f 406 /**
2600d8ee 407 To use an objarray class you must
c801d85f
KB
408 <ll>
409 <li>#include "dynarray.h"
2600d8ee
VZ
410 <li>WX_DECLARE_OBJARRAY(element_type, list_class_name)
411 <li>#include "arrimpl.cpp"
412 <li>WX_DEFINE_OBJARRAY(list_class_name) // same as above!
c801d85f
KB
413 </ll>
414 <BR><BR>
2600d8ee
VZ
415 This is necessary because at the moment of DEFINE_OBJARRAY class
416 element_type must be fully defined (i.e. forward declaration is not
417 enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of
418 two allows to break cicrcular dependencies with classes which have member
419 variables of objarray type.
c801d85f 420
2600d8ee 421 @memo define (must include arrimpl.cpp!) objarray class 'name'
c801d85f 422 */
2600d8ee 423#define WX_DEFINE_OBJARRAY(name) "don't forget to include arrimpl.cpp!"
c801d85f
KB
424//@}
425
a497618a
VZ
426// these macros do the same thing as the WX_XXX ones above, but should be used
427// inside the library for user visible classes because otherwise they wouldn't
428// be visible from outside (when using wxWindows as DLL under Windows)
429#define WX_DEFINE_EXPORTED_ARRAY(T, name) \
430 typedef T _A##name; \
431 _WX_DEFINE_ARRAY(_A##name, name, class WXDLLEXPORT)
432
433#define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \
434 typedef T _A##name; \
435 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class WXDLLEXPORT)
436
76314141 437#define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
a497618a
VZ
438#define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \
439 typedef T _L##name; \
440 _WX_DECLARE_OBJARRAY(_L##name, name, class WXDLLEXPORT)
441
76314141
RL
442// ..and likewise these macros do very same thing as the ones above them too,
443// but allow the user to specify the export spec. Needed if you have a dll
444// that wants to export a wxArray daubed with your own import/export goo.
445#define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, usergoo) \
446 typedef T _A##name; \
447 _WX_DEFINE_ARRAY(_A##name, name, class usergoo)
448
449#define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, usergoo) \
450 typedef T _A##name; \
451 _WX_DEFINE_SORTED_ARRAY(_A##name, name, class usergoo)
452
453#define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name)
454#define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, usergoo) \
455 typedef T _L##name; \
456 _WX_DECLARE_OBJARRAY(_L##name, name, class usergoo)
457
c801d85f
KB
458// ----------------------------------------------------------------------------
459/** @name Some commonly used predefined arrays */
c801d85f
KB
460// ----------------------------------------------------------------------------
461
462//@{
463 /** @name ArrayInt */
a497618a 464WX_DEFINE_EXPORTED_ARRAY(int, wxArrayInt);
c801d85f 465 /** @name ArrayLong */
a497618a 466WX_DEFINE_EXPORTED_ARRAY(long, wxArrayLong);
c801d85f 467 /** @name ArrayPtrVoid */
a497618a 468WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid);
c801d85f
KB
469//@}
470
471//@}
472
2b9bd418 473// -----------------------------------------------------------------------------
0cbff120 474// convenience macros
2b9bd418
VZ
475// -----------------------------------------------------------------------------
476
4f6aed9c
VZ
477// append all element of one array to another one
478#define WX_APPEND_ARRAY(array, other) \
479 { \
5d5b1c0c 480 size_t count = (other).Count(); \
4f6aed9c
VZ
481 for ( size_t n = 0; n < count; n++ ) \
482 { \
5d5b1c0c 483 (array).Add((other)[n]); \
4f6aed9c
VZ
484 } \
485 }
486
2b9bd418
VZ
487// delete all array elements
488//
489// NB: the class declaration of the array elements must be visible from the
490// place where you use this macro, otherwise the proper destructor may not
491// be called (a decent compiler should give a warning about it, but don't
492// count on it)!
493#define WX_CLEAR_ARRAY(array) \
494 { \
5d5b1c0c 495 size_t count = (array).Count(); \
2b9bd418
VZ
496 for ( size_t n = 0; n < count; n++ ) \
497 { \
5d5b1c0c 498 delete (array)[n]; \
2b9bd418 499 } \
2c356747 500 \
5d5b1c0c 501 (array).Empty(); \
2b9bd418 502 }
a497618a 503
c801d85f
KB
504#endif // _DYNARRAY_H
505