]> git.saurik.com Git - wxWidgets.git/blame - include/wx/arrstr.h
document wxDELETE() and wxDELETEA() (now committing the right file, please disregard...
[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>
65571936 9// Licence: wxWindows licence
df5168c4
MB
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_ARRSTR_H
13#define _WX_ARRSTR_H
14
15#include "wx/defs.h"
16#include "wx/string.h"
17
eea2be6c
VZ
18// these functions are only used in STL build now but we define them in any
19// case for compatibility with the existing code outside of the library which
20// could be using them
9c7c6fa2
VZ
21inline int wxCMPFUNC_CONV wxStringSortAscending(wxString* s1, wxString* s2)
22{
23 return s1->Cmp(*s2);
24}
25
26inline int wxCMPFUNC_CONV wxStringSortDescending(wxString* s1, wxString* s2)
27{
28 return wxStringSortAscending(s2, s1);
29}
30
eea2be6c
VZ
31#if wxUSE_STL
32
33#include "wx/dynarray.h"
34
39318636 35typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
f700f98c
MB
36typedef wxString _wxArraywxBaseArrayStringBase;
37_WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
38 wxArray_SortFunction<wxString>,
39 class WXDLLIMPEXP_BASE);
4629016d 40WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
116270c6 41 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
4629016d 42_WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
df5168c4
MB
43 wxBaseArrayStringBase, = wxStringSortAscending,
44 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
45
46class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
47{
48public:
4e75b65f
MB
49 // type of function used by wxArrayString::Sort()
50 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
51 const wxString& second);
52
df5168c4
MB
53 wxArrayString() { }
54 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
87f351de
VS
55 wxArrayString(size_t sz, const char** a);
56 wxArrayString(size_t sz, const wchar_t** a);
d44d0cbd 57 wxArrayString(size_t sz, const wxString* a);
2da2f941 58
86501081 59 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
4e75b65f
MB
60
61 void Sort(bool reverseOrder = false);
62 void Sort(CompareFunction function);
14baddc9 63 void Sort(CMPFUNCwxString function) { wxArrayStringBase::Sort(function); }
555d0d7e
MB
64
65 size_t Add(const wxString& string, size_t copies = 1)
66 {
67 wxArrayStringBase::Add(string, copies);
68 return size() - copies;
69 }
6580df1b 70};
df5168c4
MB
71
72class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
73{
74public:
75 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending)
76 { }
77 wxSortedArrayString(const wxSortedArrayString& array)
78 : wxSortedArrayStringBase(array)
79 { }
80 wxSortedArrayString(const wxArrayString& src)
81 : wxSortedArrayStringBase(wxStringSortAscending)
82 {
83 reserve(src.size());
84
85 for ( size_t n = 0; n < src.size(); n++ )
86 Add(src[n]);
87 }
2da2f941 88
86501081 89 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
90};
91
92#else // if !wxUSE_STL
93
df5168c4
MB
94class WXDLLIMPEXP_BASE wxArrayString
95{
96public:
97 // type of function used by wxArrayString::Sort()
39318636 98 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
df5168c4
MB
99 const wxString& second);
100 // type of function used by wxArrayString::Sort(), for compatibility with
101 // wxArray
39318636 102 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
df5168c4
MB
103 wxString* second);
104
105 // constructors and destructor
106 // default ctor
584ad2a3
MB
107 wxArrayString() { Init(false); }
108 // if autoSort is true, the array is always sorted (in alphabetical order)
df5168c4
MB
109 //
110 // NB: the reason for using int and not bool is that like this we can avoid
111 // using this ctor for implicit conversions from "const char *" (which
112 // we'd like to be implicitly converted to wxString instead!)
113 //
114 // of course, using explicit would be even better - if all compilers
115 // supported it...
584ad2a3 116 wxArrayString(int autoSort) { Init(autoSort != 0); }
d44d0cbd 117 // C string array ctor
cfa3d7ba
VS
118 wxArrayString(size_t sz, const char** a);
119 wxArrayString(size_t sz, const wchar_t** a);
d44d0cbd
JS
120 // wxString string array ctor
121 wxArrayString(size_t sz, const wxString* a);
df5168c4
MB
122 // copy ctor
123 wxArrayString(const wxArrayString& array);
124 // assignment operator
125 wxArrayString& operator=(const wxArrayString& src);
126 // not virtual, this class should not be derived from
127 ~wxArrayString();
128
129 // memory management
130 // empties the list, but doesn't release memory
131 void Empty();
132 // empties the list and releases memory
133 void Clear();
134 // preallocates memory for given number of items
135 void Alloc(size_t nCount);
136 // minimzes the memory usage (by freeing all extra memory)
137 void Shrink();
138
139 // simple accessors
140 // number of elements in the array
141 size_t GetCount() const { return m_nCount; }
142 // is it empty?
143 bool IsEmpty() const { return m_nCount == 0; }
144 // number of elements in the array (GetCount is preferred API)
145 size_t Count() const { return m_nCount; }
146
147 // items access (range checking is done in debug version)
148 // get item at position uiIndex
149 wxString& Item(size_t nIndex) const
150 {
151 wxASSERT_MSG( nIndex < m_nCount,
152 _T("wxArrayString: index out of bounds") );
153
b7452b3a 154 return m_pItems[nIndex];
df5168c4
MB
155 }
156
157 // same as Item()
158 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
159 // get last item
160 wxString& Last() const
161 {
162 wxASSERT_MSG( !IsEmpty(),
163 _T("wxArrayString: index out of bounds") );
b4a980f4 164 return Item(GetCount() - 1);
df5168c4
MB
165 }
166
df5168c4
MB
167
168 // item management
169 // Search the element in the array, starting from the beginning if
584ad2a3 170 // bFromEnd is false or from end otherwise. If bCase, comparison is case
df5168c4
MB
171 // sensitive (default). Returns index of the first item matched or
172 // wxNOT_FOUND
86501081 173 int Index (const wxString& str, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
174 // add new element at the end (if the array is not sorted), return its
175 // index
176 size_t Add(const wxString& str, size_t nInsert = 1);
177 // add new element at given position
178 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
179 // expand the array to have count elements
180 void SetCount(size_t count);
181 // remove first item matching this value
cfa3d7ba 182 void Remove(const wxString& sz);
df5168c4 183 // remove item by index
df5168c4
MB
184 void RemoveAt(size_t nIndex, size_t nRemove = 1);
185
186 // sorting
187 // sort array elements in alphabetical order (or reversed alphabetical
584ad2a3
MB
188 // order if reverseOrder parameter is true)
189 void Sort(bool reverseOrder = false);
eaf6da07 190 // sort array elements using specified comparison function
df5168c4
MB
191 void Sort(CompareFunction compareFunction);
192 void Sort(CompareFunction2 compareFunction);
193
194 // comparison
195 // compare two arrays case sensitively
196 bool operator==(const wxArrayString& a) const;
197 // compare two arrays case sensitively
198 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
199
200 // STL-like interface
201 typedef wxString value_type;
202 typedef value_type* pointer;
203 typedef const value_type* const_pointer;
204 typedef value_type* iterator;
205 typedef const value_type* const_iterator;
206 typedef value_type& reference;
207 typedef const value_type& const_reference;
208 typedef int difference_type;
209 typedef size_t size_type;
210
3c7896fd 211 // TODO: this code duplicates the one in dynarray.h
df5168c4
MB
212 class reverse_iterator
213 {
37589419
MB
214 typedef wxString value_type;
215 typedef value_type* pointer;
216 typedef value_type& reference;
df5168c4
MB
217 typedef reverse_iterator itor;
218 friend itor operator+(int o, const itor& it);
219 friend itor operator+(const itor& it, int o);
220 friend itor operator-(const itor& it, int o);
221 friend difference_type operator -(const itor& i1, const itor& i2);
222 public:
223 pointer m_ptr;
224 reverse_iterator() : m_ptr(NULL) { }
225 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
226 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
227 reference operator*() const { return *m_ptr; }
228 pointer operator->() const { return m_ptr; }
60d8e886
VZ
229 itor& operator++() { --m_ptr; return *this; }
230 const itor operator++(int)
df5168c4 231 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
60d8e886
VZ
232 itor& operator--() { ++m_ptr; return *this; }
233 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
234 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
235 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
df5168c4
MB
236 };
237
238 class const_reverse_iterator
239 {
37589419
MB
240 typedef wxString value_type;
241 typedef const value_type* pointer;
242 typedef const value_type& reference;
df5168c4
MB
243 typedef const_reverse_iterator itor;
244 friend itor operator+(int o, const itor& it);
245 friend itor operator+(const itor& it, int o);
246 friend itor operator-(const itor& it, int o);
247 friend difference_type operator -(const itor& i1, const itor& i2);
248 public:
249 pointer m_ptr;
250 const_reverse_iterator() : m_ptr(NULL) { }
251 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
252 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
253 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
254 reference operator*() const { return *m_ptr; }
255 pointer operator->() const { return m_ptr; }
60d8e886
VZ
256 itor& operator++() { --m_ptr; return *this; }
257 const itor operator++(int)
df5168c4 258 { itor tmp = *this; --m_ptr; return tmp; }
60d8e886
VZ
259 itor& operator--() { ++m_ptr; return *this; }
260 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
261 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
262 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
df5168c4
MB
263 };
264
584ad2a3
MB
265 wxArrayString(const_iterator first, const_iterator last)
266 { Init(false); assign(first, last); }
267 wxArrayString(size_type n, const_reference v) { Init(false); assign(n, v); }
df5168c4
MB
268 void assign(const_iterator first, const_iterator last);
269 void assign(size_type n, const_reference v)
270 { clear(); Add(v, n); }
271 reference back() { return *(end() - 1); }
272 const_reference back() const { return *(end() - 1); }
b7452b3a
VS
273 iterator begin() { return m_pItems; }
274 const_iterator begin() const { return m_pItems; }
df5168c4
MB
275 size_type capacity() const { return m_nSize; }
276 void clear() { Clear(); }
277 bool empty() const { return IsEmpty(); }
278 iterator end() { return begin() + GetCount(); }
279 const_iterator end() const { return begin() + GetCount(); }
280 iterator erase(iterator first, iterator last)
281 {
282 size_t idx = first - begin();
283 RemoveAt(idx, last - first);
284 return begin() + idx;
285 }
286 iterator erase(iterator it) { return erase(it, it + 1); }
287 reference front() { return *begin(); }
288 const_reference front() const { return *begin(); }
289 void insert(iterator it, size_type n, const_reference v)
290 { Insert(v, it - begin(), n); }
291 iterator insert(iterator it, const_reference v = value_type())
292 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
293 void insert(iterator it, const_iterator first, const_iterator last);
294 size_type max_size() const { return INT_MAX; }
295 void pop_back() { RemoveAt(GetCount() - 1); }
296 void push_back(const_reference v) { Add(v); }
297 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
298 const_reverse_iterator rbegin() const;
299 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
300 const_reverse_iterator rend() const;
301 void reserve(size_type n) /* base::reserve*/;
302 void resize(size_type n, value_type v = value_type());
303 size_type size() const { return GetCount(); }
91276868
VZ
304 void swap(wxArrayString& other)
305 {
306 // not sure if we can rely on having std::swap() everywhere so do it
307 // manually
308 const size_t savedSize = m_nSize;
309 const size_t savedCount = m_nCount;
310 wxString * const savedItems = m_pItems;
311 const bool savedAutoSort = m_autoSort;
312
313 m_nSize = other.m_nSize;
314 m_nCount = other.m_nCount;
315 m_pItems = other.m_pItems;
316 m_autoSort = other.m_autoSort;
317
318 other.m_nSize = savedSize;
319 other.m_nCount = savedCount;
320 other.m_pItems = savedItems;
321 other.m_autoSort = savedAutoSort;
322 }
df5168c4
MB
323
324protected:
325 void Init(bool autoSort); // common part of all ctors
326 void Copy(const wxArrayString& src); // copies the contents of another array
327
328private:
329 void Grow(size_t nIncrement = 0); // makes array bigger if needed
df5168c4 330
df5168c4
MB
331 size_t m_nSize, // current size of the array
332 m_nCount; // current number of elements
333
b7452b3a 334 wxString *m_pItems; // pointer to data
df5168c4 335
584ad2a3 336 bool m_autoSort; // if true, keep the array always sorted
df5168c4
MB
337};
338
339class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
340{
341public:
584ad2a3 342 wxSortedArrayString() : wxArrayString(true)
df5168c4 343 { }
584ad2a3 344 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
df5168c4
MB
345 { Copy(array); }
346};
347
348#endif // !wxUSE_STL
349
584ad2a3
MB
350// this class provides a temporary wxString* from a
351// wxArrayString
352class WXDLLIMPEXP_BASE wxCArrayString
353{
354public:
355 wxCArrayString( const wxArrayString& array )
356 : m_array( array ), m_strings( NULL )
357 { }
358 ~wxCArrayString() { delete[] m_strings; }
359
360 size_t GetCount() const { return m_array.GetCount(); }
361 wxString* GetStrings()
362 {
363 if( m_strings ) return m_strings;
364 size_t count = m_array.GetCount();
365 m_strings = new wxString[count];
366 for( size_t i = 0; i < count; ++i )
367 m_strings[i] = m_array[i];
368 return m_strings;
369 }
370private:
371 const wxArrayString& m_array;
372 wxString* m_strings;
373};
374
abbb59e8
VZ
375
376// ----------------------------------------------------------------------------
377// helper functions for working with arrays
378// ----------------------------------------------------------------------------
379
380// by default, these functions use the escape character to escape the
381// separators occuring inside the string to be joined, this can be disabled by
382// passing '\0' as escape
383
384WXDLLIMPEXP_BASE wxString wxJoin(const wxArrayString& arr,
385 const wxChar sep,
386 const wxChar escape = wxT('\\'));
387
388WXDLLIMPEXP_BASE wxArrayString wxSplit(const wxString& str,
389 const wxChar sep,
390 const wxChar escape = wxT('\\'));
391
a236aa20
VZ
392
393// ----------------------------------------------------------------------------
394// This helper class allows to pass both C array of wxStrings or wxArrayString
395// using the same interface.
396//
397// Use it when you have two methods taking wxArrayString or (int, wxString[]),
398// that do the same thing. This class lets you iterate over input data in the
399// same way whether it is a raw array of strings or wxArrayString.
400//
401// The object does not take ownership of the data -- internally it keeps
402// pointers to the data, therefore the data must be disposed of by user
403// and only after this object is destroyed. Usually it is not a problem as
404// only temporary objects of this class are used.
405// ----------------------------------------------------------------------------
406
407class wxArrayStringsAdapter
408{
409public:
410 // construct an adapter from a wxArrayString
411 wxArrayStringsAdapter(const wxArrayString& strings)
412 : m_type(wxSTRING_ARRAY), m_size(strings.size())
413 {
414 m_data.array = &strings;
415 }
416
417 // construct an adapter from a wxString[]
418 wxArrayStringsAdapter(unsigned int n, const wxString *strings)
419 : m_type(wxSTRING_POINTER), m_size(n)
420 {
421 m_data.ptr = strings;
422 }
423
424 // construct an adapter from a single wxString
425 wxArrayStringsAdapter(const wxString& s)
426 : m_type(wxSTRING_POINTER), m_size(1)
427 {
428 m_data.ptr = &s;
429 }
430
431 // default copy constructor is ok
432
433 // iteration interface
1b6d87d0 434 size_t GetCount() const { return m_size; }
a236aa20
VZ
435 bool IsEmpty() const { return GetCount() == 0; }
436 const wxString& operator[] (unsigned int i) const
437 {
438 wxASSERT_MSG( i < GetCount(), wxT("index out of bounds") );
439 if(m_type == wxSTRING_POINTER)
440 return m_data.ptr[i];
441 return m_data.array->Item(i);
442 }
443 wxArrayString AsArrayString() const
444 {
445 if(m_type == wxSTRING_ARRAY)
446 return *m_data.array;
447 return wxArrayString(GetCount(), m_data.ptr);
448 }
449
450private:
451 // type of the data being held
452 enum wxStringContainerType
453 {
454 wxSTRING_ARRAY, // wxArrayString
455 wxSTRING_POINTER // wxString[]
456 };
457
458 wxStringContainerType m_type;
1b6d87d0 459 size_t m_size;
a236aa20
VZ
460 union
461 {
462 const wxString * ptr;
463 const wxArrayString * array;
464 } m_data;
465
466 DECLARE_NO_ASSIGN_CLASS(wxArrayStringsAdapter)
467};
468
abbb59e8 469#endif // _WX_ARRSTR_H