]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dynarray.h
tree control and dc::drawlines bug
[wxWidgets.git] / include / wx / dynarray.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: dynarray.h
3 // Purpose: auto-resizable (i.e. dynamic) array support
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 12.09.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _DYNARRAY_H
13 #define _DYNARRAY_H
14
15 #ifdef __GNUG__
16 #pragma interface "dynarray.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/utils.h"
21
22 /** @name Dynamic arrays and lists
23 @memo Arrays which grow on demand and do range checking (only in debug)
24 */
25 //@{
26
27 // ----------------------------------------------------------------------------
28 // constants
29 // ----------------------------------------------------------------------------
30
31 /**
32 the initial size by which an array/list grows when an element is added
33 default value avoids allocate one or two bytes when the array is created
34 which is rather inefficient
35 */
36 #define WX_ARRAY_DEFAULT_INITIAL_SIZE (16)
37
38 // ----------------------------------------------------------------------------
39 // types
40 // ----------------------------------------------------------------------------
41
42 /**
43 callback compare function for quick sort
44 must return -1, 0 or +1 if pItem1 <, = or > pItem2
45 */
46
47 #ifdef __VISUALC__
48 #define CMPFUNC_CONV _cdecl
49 #else // !Visual C++
50 #define CMPFUNC_CONV
51 #endif // compiler
52 typedef int (CMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2);
53
54 // ----------------------------------------------------------------------------
55 /**
56 base class managing data having size of type 'long' (not used directly)
57
58 NB: for efficiency this often used class has no virtual functions (hence no
59 VTBL), even dtor is <B>not</B> virtual. If used as expected it won't
60 create any problems because ARRAYs from DEFINE_ARRAY have no dtor at all,
61 so it's not too important if it's not called (this happens when you cast
62 "SomeArray *" as "BaseArray *" and then delete it)
63
64 @memo Base class for template array and list classes
65 */
66 // ----------------------------------------------------------------------------
67 class wxBaseArray
68 {
69 public:
70 /** @name ctors and dtor */
71 //@{
72 /// default ctor
73 wxBaseArray();
74 /// copy ctor
75 wxBaseArray(const wxBaseArray& array);
76 /// assignment operator
77 wxBaseArray& operator=(const wxBaseArray& src);
78 /// not virtual, see above
79 /// EXCEPT for Gnu compiler to reduce warnings...
80 #ifdef __GNUG__
81 virtual
82 #endif
83 ~wxBaseArray();
84 //@}
85
86 /** @name memory management */
87 //@{
88 /// empties the list, but doesn't release memory
89 void Empty() { m_uiCount = 0; }
90 /// empties the list and releases memory
91 void Clear();
92 /// preallocates memory for given number of items
93 void Alloc(uint uiSize);
94 //@}
95
96 /** @name simple accessors */
97 //@{
98 /// number of elements in the array
99 uint Count() const { return m_uiCount; }
100 /// is it empty?
101 bool IsEmpty() const { return m_uiCount == 0; }
102 //@}
103
104 protected:
105 // these methods are protected because if they were public one could
106 // mistakenly call one of them instead of DEFINE_ARRAY's or LIST's
107 // type safe methods
108
109 /** @name items access */
110 //@{
111 /// get item at position uiIndex (range checking is done in debug version)
112 long& Item(uint uiIndex) const
113 { wxASSERT( uiIndex < m_uiCount ); return m_pItems[uiIndex]; }
114 /// same as Item()
115 long& operator[](uint uiIndex) const { return Item(uiIndex); }
116 //@}
117
118 /** @name item management */
119 //@{
120 /**
121 Search the element in the array, starting from the either side
122 @param bFromEnd if TRUE, start from the end
123 @return index of the first item matched or NOT_FOUND
124 @see NOT_FOUND
125 */
126 int Index (long lItem, bool bFromEnd = FALSE) const;
127 /// add new element at the end
128 void Add (long lItem);
129 /// add new element at given position
130 void Insert(long lItem, uint uiIndex);
131 /// remove first item matching this value
132 void Remove(long lItem);
133 /// remove item by index
134 void Remove(uint uiIndex);
135 //@}
136
137 /// sort array elements using given compare function
138 void Sort(CMPFUNC fCmp);
139
140 private:
141 void Grow(); // makes array bigger if needed
142
143 uint m_uiSize, // current size of the array
144 m_uiCount; // current number of elements
145
146 long *m_pItems; // pointer to data
147 };
148
149 // ============================================================================
150 // template classes
151 // ============================================================================
152
153 // ----------------------------------------------------------------------------
154 // This macro generates a new array class. It is intended for storage of simple
155 // types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
156 //
157 // NB: it has only inline functions => takes no space at all
158 // ----------------------------------------------------------------------------
159 #define _WX_DEFINE_ARRAY(T, name) \
160 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T *pItem1, T *pItem2); \
161 class name : public wxBaseArray \
162 { \
163 public: \
164 name() \
165 { wxASSERT( sizeof(T) <= sizeof(long) ); } \
166 \
167 name& operator=(const name& src) \
168 { ((wxBaseArray *)this)->operator=((const wxBaseArray&)src); \
169 return *this; } \
170 \
171 T& operator[](uint uiIndex) const \
172 { return (T&)(wxBaseArray::Item(uiIndex)); } \
173 T& Item(uint uiIndex) const \
174 { return (T&)(wxBaseArray::Item(uiIndex)); } \
175 T& Last() const \
176 { return (T&)(wxBaseArray::Item(Count() - 1)); } \
177 \
178 int Index(T Item, bool bFromEnd = FALSE) const \
179 { return wxBaseArray::Index((long)Item, bFromEnd); } \
180 \
181 void Add(T Item) \
182 { wxBaseArray::Add((long)Item); } \
183 void Insert(T Item, uint uiIndex) \
184 { wxBaseArray::Insert((long)Item, uiIndex) ; } \
185 \
186 void Remove(uint uiIndex) { wxBaseArray::Remove(uiIndex); } \
187 void Remove(T Item) \
188 { int iIndex = Index(Item); \
189 wxCHECK2_MSG( iIndex != NOT_FOUND, return, \
190 "removing inexisting element in wxArray::Remove" ); \
191 wxBaseArray::Remove((uint)iIndex); } \
192 \
193 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
194 }
195
196 // ----------------------------------------------------------------------------
197 // see WX_DECLARE_LIST and WX_DEFINE_LIST
198 // ----------------------------------------------------------------------------
199 #define _WX_DECLARE_LIST(T, name) \
200 typedef int (CMPFUNC_CONV *CMPFUNC##T)(T** pItem1, T** pItem2); \
201 class name : public wxBaseArray \
202 { \
203 public: \
204 name() { } \
205 name(const name& src); \
206 name& operator=(const name& src); \
207 \
208 ~name(); \
209 \
210 T& operator[](uint uiIndex) const \
211 { return *(T*)wxBaseArray::Item(uiIndex); } \
212 T& Item(uint uiIndex) const \
213 { return *(T*)wxBaseArray::Item(uiIndex); } \
214 T& Last() const \
215 { return *(T*)(wxBaseArray::Item(Count() - 1)); } \
216 \
217 int Index(const T& Item, bool bFromEnd = FALSE) const; \
218 \
219 void Add(const T& Item); \
220 void Add(const T* pItem) \
221 { wxBaseArray::Add((long)pItem); } \
222 \
223 void Insert(const T& Item, uint uiIndex); \
224 void Insert(const T* pItem, uint uiIndex) \
225 { wxBaseArray::Insert((long)pItem, uiIndex); } \
226 \
227 void Empty(); \
228 \
229 T* Detach(uint uiIndex) \
230 { T* p = (T*)wxBaseArray::Item(uiIndex); \
231 wxBaseArray::Remove(uiIndex); return p; } \
232 void Remove(uint uiIndex); \
233 \
234 void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
235 \
236 private: \
237 void DoCopy(const name& src); \
238 }
239
240 // ----------------------------------------------------------------------------
241 /** @name Macros for definition of dynamic arrays and lists
242
243 These macros are ugly (especially if you look in the sources ;-), but they
244 allow us to define 'template' classes without actually using templates.
245 <BR>
246 <BR>
247 Range checking is performed in debug build for both arrays and lists. Type
248 checking is done at compile-time. Warning: arrays <I>never</I> shrink, they
249 only grow, so loading 10 millions in an array only to delete them 2 lines
250 below is <I>not</I> recommended. However, it does free memory when it's
251 destroyed, so if you destroy array also, it's ok.
252 */
253 // ----------------------------------------------------------------------------
254
255 //@{
256 /**
257 This macro generates a new array class. It is intended for storage of simple
258 types of sizeof()<=sizeof(long) or pointers if sizeof(pointer)<=sizeof(long)
259 <BR>
260 NB: it has only inline functions => takes no space at all
261 <BR>
262
263 @memo declare and define array class 'name' containing elements of type 'T'
264 */
265 #define WX_DEFINE_ARRAY(T, name) typedef T _A##name; \
266 _WX_DEFINE_ARRAY(_A##name, name)
267 /**
268 This macro generates a new list class which owns the objects it contains,
269 i.e. it will delete them when it is destroyed. An element is of type T*,
270 but arguments of type T& are taken (see below!) and T& is returned.
271 <BR>
272 Don't use this for simple types such as "int" or "long"!
273 You _may_ use it for "double" but it's awfully inefficient.
274 <BR>
275 <BR>
276 Note on Add/Insert functions:
277 <BR>
278 1) function(T*) gives the object to the list, i.e. it will delete the
279 object when it's removed or in the list's dtor
280 <BR>
281 2) function(T&) will create a copy of the object and work with it
282 <BR>
283 <BR>
284 Also:
285 <BR>
286 1) Remove() will delete the object after removing it from the list
287 <BR>
288 2) Detach() just removes the object from the list (returning pointer to it)
289 <BR>
290 <BR>
291 NB1: Base type T should have an accessible copy ctor if Add(T&) is used,
292 <BR>
293 NB2: Never ever cast a list to it's base type: as dtor is <B>not</B> virtual
294 it will provoke memory leaks
295 <BR>
296 <BR>
297 some functions of this class are not inline, so it takes some space to
298 define new class from this template.
299
300 @memo declare list class 'name' containing elements of type 'T'
301 */
302 #define WX_DECLARE_LIST(T, name) typedef T _L##name; \
303 _WX_DECLARE_LIST(_L##name, name)
304 /**
305 To use a list class you must
306 <ll>
307 <li>#include "dynarray.h"
308 <li>DECLARE_LIST(element_type, list_class_name)
309 <li>#include "listimpl.cpp"
310 <li>DEFINE_LIST(list_class_name) // same as above!
311 </ll>
312 <BR><BR>
313 This is necessary because at the moment of DEFINE_LIST class element_type
314 must be fully defined (i.e. forward declaration is not enough), while
315 DECLARE_LIST may be done anywhere. The separation of two allows to break
316 cicrcular dependencies with classes which have member variables of list
317 type.
318
319 @memo define (must include listimpl.cpp!) list class 'name'
320 */
321 #define WX_DEFINE_LIST(name) "don't forget to include listimpl.cpp!"
322 //@}
323
324 // ----------------------------------------------------------------------------
325 /** @name Some commonly used predefined arrays */
326 // # overhead if not used?
327 // ----------------------------------------------------------------------------
328
329 //@{
330 /** @name ArrayInt */
331 WX_DEFINE_ARRAY(int, wxArrayInt);
332 /** @name ArrayLong */
333 WX_DEFINE_ARRAY(long, wxArrayLong);
334 /** @name ArrayPtrVoid */
335 WX_DEFINE_ARRAY(void *, wxArrayPtrVoid);
336 //@}
337
338 //@}
339
340 #endif // _DYNARRAY_H
341