]> git.saurik.com Git - wxWidgets.git/blame - include/wx/arrstr.h
WinCE compilation fix
[wxWidgets.git] / include / wx / arrstr.h
CommitLineData
df5168c4
MB
1///////////////////////////////////////////////////////////////////////////////
2// Name: include/wx/arrstr.h
3// Purpose: wxArrayString class
4// Author: Mattia Barbon and Vadim Zeitlin
5// Modified by:
6// Created: 07/07/03
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_ARRSTR_H
13#define _WX_ARRSTR_H
14
15#include "wx/defs.h"
16#include "wx/string.h"
17
eae4425d
JS
18WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
19WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
df5168c4
MB
20
21#if wxUSE_STL
22
23#include "wx/dynarray.h"
24
39318636 25typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
f700f98c
MB
26typedef wxString _wxArraywxBaseArrayStringBase;
27_WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
28 wxArray_SortFunction<wxString>,
29 class WXDLLIMPEXP_BASE);
116270c6
MB
30WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
31 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
df5168c4
MB
32_WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
33 wxBaseArrayStringBase, = wxStringSortAscending,
34 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
35
36class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
37{
38public:
39 wxArrayString() { }
40 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
2da2f941
MB
41
42 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
43};
44
45class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
46{
47public:
48 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending)
49 { }
50 wxSortedArrayString(const wxSortedArrayString& array)
51 : wxSortedArrayStringBase(array)
52 { }
53 wxSortedArrayString(const wxArrayString& src)
54 : wxSortedArrayStringBase(wxStringSortAscending)
55 {
56 reserve(src.size());
57
58 for ( size_t n = 0; n < src.size(); n++ )
59 Add(src[n]);
60 }
2da2f941
MB
61
62 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
63};
64
65#else // if !wxUSE_STL
66
67// ----------------------------------------------------------------------------
68// The string array uses it's knowledge of internal structure of the wxString
69// class to optimize string storage. Normally, we would store pointers to
70// string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
71// sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
72// really all we need to turn such pointer into a string!
73//
74// Of course, it can be called a dirty hack, but we use twice less memory and
75// this approach is also more speed efficient, so it's probably worth it.
76//
77// Usage notes: when a string is added/inserted, a new copy of it is created,
78// so the original string may be safely deleted. When a string is retrieved
79// from the array (operator[] or Item() method), a reference is returned.
80// ----------------------------------------------------------------------------
81
82class WXDLLIMPEXP_BASE wxArrayString
83{
84public:
85 // type of function used by wxArrayString::Sort()
39318636 86 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
df5168c4
MB
87 const wxString& second);
88 // type of function used by wxArrayString::Sort(), for compatibility with
89 // wxArray
39318636 90 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
df5168c4
MB
91 wxString* second);
92
93 // constructors and destructor
94 // default ctor
584ad2a3
MB
95 wxArrayString() { Init(false); }
96 // if autoSort is true, the array is always sorted (in alphabetical order)
df5168c4
MB
97 //
98 // NB: the reason for using int and not bool is that like this we can avoid
99 // using this ctor for implicit conversions from "const char *" (which
100 // we'd like to be implicitly converted to wxString instead!)
101 //
102 // of course, using explicit would be even better - if all compilers
103 // supported it...
584ad2a3 104 wxArrayString(int autoSort) { Init(autoSort != 0); }
df5168c4
MB
105 // copy ctor
106 wxArrayString(const wxArrayString& array);
107 // assignment operator
108 wxArrayString& operator=(const wxArrayString& src);
109 // not virtual, this class should not be derived from
110 ~wxArrayString();
111
112 // memory management
113 // empties the list, but doesn't release memory
114 void Empty();
115 // empties the list and releases memory
116 void Clear();
117 // preallocates memory for given number of items
118 void Alloc(size_t nCount);
119 // minimzes the memory usage (by freeing all extra memory)
120 void Shrink();
121
122 // simple accessors
123 // number of elements in the array
124 size_t GetCount() const { return m_nCount; }
125 // is it empty?
126 bool IsEmpty() const { return m_nCount == 0; }
127 // number of elements in the array (GetCount is preferred API)
128 size_t Count() const { return m_nCount; }
129
130 // items access (range checking is done in debug version)
131 // get item at position uiIndex
132 wxString& Item(size_t nIndex) const
133 {
134 wxASSERT_MSG( nIndex < m_nCount,
135 _T("wxArrayString: index out of bounds") );
136
137 return *(wxString *)&(m_pItems[nIndex]);
138 }
139
140 // same as Item()
141 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
142 // get last item
143 wxString& Last() const
144 {
145 wxASSERT_MSG( !IsEmpty(),
146 _T("wxArrayString: index out of bounds") );
147 return Item(Count() - 1);
148 }
149
df5168c4
MB
150 // return a wxString[], useful for the controls which
151 // take one in their ctor. You must delete[] it yourself
152 // once you are done with it. Will return NULL if the
153 // ArrayString was empty.
584ad2a3 154#if WXWIN_COMPATIBILITY_2_4
df5168c4 155 wxString* GetStringArray() const;
584ad2a3 156#endif
df5168c4
MB
157
158 // item management
159 // Search the element in the array, starting from the beginning if
584ad2a3 160 // bFromEnd is false or from end otherwise. If bCase, comparison is case
df5168c4
MB
161 // sensitive (default). Returns index of the first item matched or
162 // wxNOT_FOUND
584ad2a3 163 int Index (const wxChar *sz, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
164 // add new element at the end (if the array is not sorted), return its
165 // index
166 size_t Add(const wxString& str, size_t nInsert = 1);
167 // add new element at given position
168 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
169 // expand the array to have count elements
170 void SetCount(size_t count);
171 // remove first item matching this value
172 void Remove(const wxChar *sz);
173 // remove item by index
174#if WXWIN_COMPATIBILITY_2_4
175 void Remove(size_t nIndex, size_t nRemove = 1) { RemoveAt(nIndex, nRemove); }
176#endif
177 void RemoveAt(size_t nIndex, size_t nRemove = 1);
178
179 // sorting
180 // sort array elements in alphabetical order (or reversed alphabetical
584ad2a3
MB
181 // order if reverseOrder parameter is true)
182 void Sort(bool reverseOrder = false);
df5168c4
MB
183 // sort array elements using specified comparaison function
184 void Sort(CompareFunction compareFunction);
185 void Sort(CompareFunction2 compareFunction);
186
187 // comparison
188 // compare two arrays case sensitively
189 bool operator==(const wxArrayString& a) const;
190 // compare two arrays case sensitively
191 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
192
193 // STL-like interface
194 typedef wxString value_type;
195 typedef value_type* pointer;
196 typedef const value_type* const_pointer;
197 typedef value_type* iterator;
198 typedef const value_type* const_iterator;
199 typedef value_type& reference;
200 typedef const value_type& const_reference;
201 typedef int difference_type;
202 typedef size_t size_type;
203
204 // FIXME: same in dynarray.h
205 class reverse_iterator
206 {
37589419
MB
207 typedef wxString value_type;
208 typedef value_type* pointer;
209 typedef value_type& reference;
df5168c4
MB
210 typedef reverse_iterator itor;
211 friend itor operator+(int o, const itor& it);
212 friend itor operator+(const itor& it, int o);
213 friend itor operator-(const itor& it, int o);
214 friend difference_type operator -(const itor& i1, const itor& i2);
215 public:
216 pointer m_ptr;
217 reverse_iterator() : m_ptr(NULL) { }
218 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
219 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
220 reference operator*() const { return *m_ptr; }
221 pointer operator->() const { return m_ptr; }
222 itor operator++() { --m_ptr; return *this; }
223 itor operator++(int)
224 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
225 itor operator--() { ++m_ptr; return *this; }
226 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
227 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
228 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
229 };
230
231 class const_reverse_iterator
232 {
37589419
MB
233 typedef wxString value_type;
234 typedef const value_type* pointer;
235 typedef const value_type& reference;
df5168c4
MB
236 typedef const_reverse_iterator itor;
237 friend itor operator+(int o, const itor& it);
238 friend itor operator+(const itor& it, int o);
239 friend itor operator-(const itor& it, int o);
240 friend difference_type operator -(const itor& i1, const itor& i2);
241 public:
242 pointer m_ptr;
243 const_reverse_iterator() : m_ptr(NULL) { }
244 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
245 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
246 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
247 reference operator*() const { return *m_ptr; }
248 pointer operator->() const { return m_ptr; }
249 itor operator++() { --m_ptr; return *this; }
250 itor operator++(int)
251 { itor tmp = *this; --m_ptr; return tmp; }
252 itor operator--() { ++m_ptr; return *this; }
253 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
254 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
255 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
256 };
257
584ad2a3
MB
258 wxArrayString(const_iterator first, const_iterator last)
259 { Init(false); assign(first, last); }
260 wxArrayString(size_type n, const_reference v) { Init(false); assign(n, v); }
df5168c4
MB
261 void assign(const_iterator first, const_iterator last);
262 void assign(size_type n, const_reference v)
263 { clear(); Add(v, n); }
264 reference back() { return *(end() - 1); }
265 const_reference back() const { return *(end() - 1); }
266 iterator begin() { return (wxString *)&(m_pItems[0]); }
267 const_iterator begin() const { return (wxString *)&(m_pItems[0]); }
268 size_type capacity() const { return m_nSize; }
269 void clear() { Clear(); }
270 bool empty() const { return IsEmpty(); }
271 iterator end() { return begin() + GetCount(); }
272 const_iterator end() const { return begin() + GetCount(); }
273 iterator erase(iterator first, iterator last)
274 {
275 size_t idx = first - begin();
276 RemoveAt(idx, last - first);
277 return begin() + idx;
278 }
279 iterator erase(iterator it) { return erase(it, it + 1); }
280 reference front() { return *begin(); }
281 const_reference front() const { return *begin(); }
282 void insert(iterator it, size_type n, const_reference v)
283 { Insert(v, it - begin(), n); }
284 iterator insert(iterator it, const_reference v = value_type())
285 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
286 void insert(iterator it, const_iterator first, const_iterator last);
287 size_type max_size() const { return INT_MAX; }
288 void pop_back() { RemoveAt(GetCount() - 1); }
289 void push_back(const_reference v) { Add(v); }
290 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
291 const_reverse_iterator rbegin() const;
292 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
293 const_reverse_iterator rend() const;
294 void reserve(size_type n) /* base::reserve*/;
295 void resize(size_type n, value_type v = value_type());
296 size_type size() const { return GetCount(); }
297
298protected:
299 void Init(bool autoSort); // common part of all ctors
300 void Copy(const wxArrayString& src); // copies the contents of another array
301
302private:
303 void Grow(size_t nIncrement = 0); // makes array bigger if needed
304 void Free(); // free all the strings stored
305
306 void DoSort(); // common part of all Sort() variants
307
308 size_t m_nSize, // current size of the array
309 m_nCount; // current number of elements
310
311 wxChar **m_pItems; // pointer to data
312
584ad2a3 313 bool m_autoSort; // if true, keep the array always sorted
df5168c4
MB
314};
315
316class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
317{
318public:
584ad2a3 319 wxSortedArrayString() : wxArrayString(true)
df5168c4 320 { }
584ad2a3 321 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
df5168c4
MB
322 { Copy(array); }
323};
324
325#endif // !wxUSE_STL
326
584ad2a3
MB
327// this class provides a temporary wxString* from a
328// wxArrayString
329class WXDLLIMPEXP_BASE wxCArrayString
330{
331public:
332 wxCArrayString( const wxArrayString& array )
333 : m_array( array ), m_strings( NULL )
334 { }
335 ~wxCArrayString() { delete[] m_strings; }
336
337 size_t GetCount() const { return m_array.GetCount(); }
338 wxString* GetStrings()
339 {
340 if( m_strings ) return m_strings;
341 size_t count = m_array.GetCount();
342 m_strings = new wxString[count];
343 for( size_t i = 0; i < count; ++i )
344 m_strings[i] = m_array[i];
345 return m_strings;
346 }
347private:
348 const wxArrayString& m_array;
349 wxString* m_strings;
350};
351
df5168c4 352#endif