]> git.saurik.com Git - wxWidgets.git/blob - include/wx/arrstr.h
destroy m_impl even if an exception is thrown from the main loop
[wxWidgets.git] / include / wx / arrstr.h
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
18 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
19 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
20
21 #if wxUSE_STL
22
23 #include "wx/dynarray.h"
24
25 typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
26 typedef wxString _wxArraywxBaseArrayStringBase;
27 _WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
28 wxArray_SortFunction<wxString>,
29 class WXDLLIMPEXP_BASE);
30 WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
31 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
32 _WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
33 wxBaseArrayStringBase, = wxStringSortAscending,
34 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
35
36 class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
37 {
38 public:
39 // type of function used by wxArrayString::Sort()
40 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
41 const wxString& second);
42
43 wxArrayString() { }
44 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
45 wxArrayString(size_t sz, const wxChar** a);
46 wxArrayString(size_t sz, const wxString* a);
47
48 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
49
50 void Sort(bool reverseOrder = false);
51 void Sort(CompareFunction function);
52 void Sort(CMPFUNCwxString function) { wxArrayStringBase::Sort(function); }
53
54 size_t Add(const wxString& string, size_t copies = 1)
55 {
56 wxArrayStringBase::Add(string, copies);
57 return size() - copies;
58 }
59 };
60
61 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
62 {
63 public:
64 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending)
65 { }
66 wxSortedArrayString(const wxSortedArrayString& array)
67 : wxSortedArrayStringBase(array)
68 { }
69 wxSortedArrayString(const wxArrayString& src)
70 : wxSortedArrayStringBase(wxStringSortAscending)
71 {
72 reserve(src.size());
73
74 for ( size_t n = 0; n < src.size(); n++ )
75 Add(src[n]);
76 }
77
78 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
79 };
80
81 #else // if !wxUSE_STL
82
83 class WXDLLIMPEXP_BASE wxArrayString
84 {
85 public:
86 // type of function used by wxArrayString::Sort()
87 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
88 const wxString& second);
89 // type of function used by wxArrayString::Sort(), for compatibility with
90 // wxArray
91 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
92 wxString* second);
93
94 // constructors and destructor
95 // default ctor
96 wxArrayString() { Init(false); }
97 // if autoSort is true, the array is always sorted (in alphabetical order)
98 //
99 // NB: the reason for using int and not bool is that like this we can avoid
100 // using this ctor for implicit conversions from "const char *" (which
101 // we'd like to be implicitly converted to wxString instead!)
102 //
103 // of course, using explicit would be even better - if all compilers
104 // supported it...
105 wxArrayString(int autoSort) { Init(autoSort != 0); }
106 // C string array ctor
107 wxArrayString(size_t sz, const wxChar** a);
108 // wxString string array ctor
109 wxArrayString(size_t sz, const wxString* a);
110 // copy ctor
111 wxArrayString(const wxArrayString& array);
112 // assignment operator
113 wxArrayString& operator=(const wxArrayString& src);
114 // not virtual, this class should not be derived from
115 ~wxArrayString();
116
117 // memory management
118 // empties the list, but doesn't release memory
119 void Empty();
120 // empties the list and releases memory
121 void Clear();
122 // preallocates memory for given number of items
123 void Alloc(size_t nCount);
124 // minimzes the memory usage (by freeing all extra memory)
125 void Shrink();
126
127 // simple accessors
128 // number of elements in the array
129 size_t GetCount() const { return m_nCount; }
130 // is it empty?
131 bool IsEmpty() const { return m_nCount == 0; }
132 // number of elements in the array (GetCount is preferred API)
133 size_t Count() const { return m_nCount; }
134
135 // items access (range checking is done in debug version)
136 // get item at position uiIndex
137 wxString& Item(size_t nIndex) const
138 {
139 wxASSERT_MSG( nIndex < m_nCount,
140 _T("wxArrayString: index out of bounds") );
141
142 return m_pItems[nIndex];
143 }
144
145 // same as Item()
146 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
147 // get last item
148 wxString& Last() const
149 {
150 wxASSERT_MSG( !IsEmpty(),
151 _T("wxArrayString: index out of bounds") );
152 return Item(Count() - 1);
153 }
154
155
156 // item management
157 // Search the element in the array, starting from the beginning if
158 // bFromEnd is false or from end otherwise. If bCase, comparison is case
159 // sensitive (default). Returns index of the first item matched or
160 // wxNOT_FOUND
161 int Index (const wxChar *sz, bool bCase = true, bool bFromEnd = false) const;
162 // add new element at the end (if the array is not sorted), return its
163 // index
164 size_t Add(const wxString& str, size_t nInsert = 1);
165 // add new element at given position
166 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
167 // expand the array to have count elements
168 void SetCount(size_t count);
169 // remove first item matching this value
170 void Remove(const wxChar *sz);
171 // remove item by index
172 void RemoveAt(size_t nIndex, size_t nRemove = 1);
173
174 // sorting
175 // sort array elements in alphabetical order (or reversed alphabetical
176 // order if reverseOrder parameter is true)
177 void Sort(bool reverseOrder = false);
178 // sort array elements using specified comparaison function
179 void Sort(CompareFunction compareFunction);
180 void Sort(CompareFunction2 compareFunction);
181
182 // comparison
183 // compare two arrays case sensitively
184 bool operator==(const wxArrayString& a) const;
185 // compare two arrays case sensitively
186 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
187
188 // STL-like interface
189 typedef wxString value_type;
190 typedef value_type* pointer;
191 typedef const value_type* const_pointer;
192 typedef value_type* iterator;
193 typedef const value_type* const_iterator;
194 typedef value_type& reference;
195 typedef const value_type& const_reference;
196 typedef int difference_type;
197 typedef size_t size_type;
198
199 // TODO: this code duplicates the one in dynarray.h
200 class reverse_iterator
201 {
202 typedef wxString value_type;
203 typedef value_type* pointer;
204 typedef value_type& reference;
205 typedef reverse_iterator itor;
206 friend itor operator+(int o, const itor& it);
207 friend itor operator+(const itor& it, int o);
208 friend itor operator-(const itor& it, int o);
209 friend difference_type operator -(const itor& i1, const itor& i2);
210 public:
211 pointer m_ptr;
212 reverse_iterator() : m_ptr(NULL) { }
213 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
214 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
215 reference operator*() const { return *m_ptr; }
216 pointer operator->() const { return m_ptr; }
217 itor& operator++() { --m_ptr; return *this; }
218 const itor operator++(int)
219 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
220 itor& operator--() { ++m_ptr; return *this; }
221 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
222 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
223 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
224 };
225
226 class const_reverse_iterator
227 {
228 typedef wxString value_type;
229 typedef const value_type* pointer;
230 typedef const value_type& reference;
231 typedef const_reverse_iterator itor;
232 friend itor operator+(int o, const itor& it);
233 friend itor operator+(const itor& it, int o);
234 friend itor operator-(const itor& it, int o);
235 friend difference_type operator -(const itor& i1, const itor& i2);
236 public:
237 pointer m_ptr;
238 const_reverse_iterator() : m_ptr(NULL) { }
239 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
240 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
241 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
242 reference operator*() const { return *m_ptr; }
243 pointer operator->() const { return m_ptr; }
244 itor& operator++() { --m_ptr; return *this; }
245 const itor operator++(int)
246 { itor tmp = *this; --m_ptr; return tmp; }
247 itor& operator--() { ++m_ptr; return *this; }
248 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
249 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
250 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
251 };
252
253 wxArrayString(const_iterator first, const_iterator last)
254 { Init(false); assign(first, last); }
255 wxArrayString(size_type n, const_reference v) { Init(false); assign(n, v); }
256 void assign(const_iterator first, const_iterator last);
257 void assign(size_type n, const_reference v)
258 { clear(); Add(v, n); }
259 reference back() { return *(end() - 1); }
260 const_reference back() const { return *(end() - 1); }
261 iterator begin() { return m_pItems; }
262 const_iterator begin() const { return m_pItems; }
263 size_type capacity() const { return m_nSize; }
264 void clear() { Clear(); }
265 bool empty() const { return IsEmpty(); }
266 iterator end() { return begin() + GetCount(); }
267 const_iterator end() const { return begin() + GetCount(); }
268 iterator erase(iterator first, iterator last)
269 {
270 size_t idx = first - begin();
271 RemoveAt(idx, last - first);
272 return begin() + idx;
273 }
274 iterator erase(iterator it) { return erase(it, it + 1); }
275 reference front() { return *begin(); }
276 const_reference front() const { return *begin(); }
277 void insert(iterator it, size_type n, const_reference v)
278 { Insert(v, it - begin(), n); }
279 iterator insert(iterator it, const_reference v = value_type())
280 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
281 void insert(iterator it, const_iterator first, const_iterator last);
282 size_type max_size() const { return INT_MAX; }
283 void pop_back() { RemoveAt(GetCount() - 1); }
284 void push_back(const_reference v) { Add(v); }
285 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
286 const_reverse_iterator rbegin() const;
287 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
288 const_reverse_iterator rend() const;
289 void reserve(size_type n) /* base::reserve*/;
290 void resize(size_type n, value_type v = value_type());
291 size_type size() const { return GetCount(); }
292
293 protected:
294 void Init(bool autoSort); // common part of all ctors
295 void Copy(const wxArrayString& src); // copies the contents of another array
296
297 private:
298 void Grow(size_t nIncrement = 0); // makes array bigger if needed
299
300 void DoSort(); // common part of all Sort() variants
301
302 size_t m_nSize, // current size of the array
303 m_nCount; // current number of elements
304
305 wxString *m_pItems; // pointer to data
306
307 bool m_autoSort; // if true, keep the array always sorted
308 };
309
310 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
311 {
312 public:
313 wxSortedArrayString() : wxArrayString(true)
314 { }
315 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
316 { Copy(array); }
317 };
318
319 #endif // !wxUSE_STL
320
321 // this class provides a temporary wxString* from a
322 // wxArrayString
323 class WXDLLIMPEXP_BASE wxCArrayString
324 {
325 public:
326 wxCArrayString( const wxArrayString& array )
327 : m_array( array ), m_strings( NULL )
328 { }
329 ~wxCArrayString() { delete[] m_strings; }
330
331 size_t GetCount() const { return m_array.GetCount(); }
332 wxString* GetStrings()
333 {
334 if( m_strings ) return m_strings;
335 size_t count = m_array.GetCount();
336 m_strings = new wxString[count];
337 for( size_t i = 0; i < count; ++i )
338 m_strings[i] = m_array[i];
339 return m_strings;
340 }
341 private:
342 const wxArrayString& m_array;
343 wxString* m_strings;
344 };
345
346
347 // ----------------------------------------------------------------------------
348 // helper functions for working with arrays
349 // ----------------------------------------------------------------------------
350
351 // by default, these functions use the escape character to escape the
352 // separators occuring inside the string to be joined, this can be disabled by
353 // passing '\0' as escape
354
355 WXDLLIMPEXP_BASE wxString wxJoin(const wxArrayString& arr,
356 const wxChar sep,
357 const wxChar escape = wxT('\\'));
358
359 WXDLLIMPEXP_BASE wxArrayString wxSplit(const wxString& str,
360 const wxChar sep,
361 const wxChar escape = wxT('\\'));
362
363 #endif // _WX_ARRSTR_H