Use correct export directive for multilib.
[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 int WXDLLIMPEXP_BASE wxStringSortAscending(wxString*, wxString*);
19 int WXDLLIMPEXP_BASE wxStringSortDescending(wxString*, wxString*);
20
21 #if wxUSE_STL
22
23 #include "wx/dynarray.h"
24
25 typedef int (*CMPFUNCwxString)(wxString*, wxString*);
26 WX_DECLARE_USER_EXPORTED_BASEARRAY(wxString, wxBaseArrayStringBase,
27 WXDLLIMPEXP_BASE);
28 WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
29 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
30 _WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
31 wxBaseArrayStringBase, = wxStringSortAscending,
32 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
33
34 class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
35 {
36 public:
37 wxArrayString() { }
38 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
39 };
40
41 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
42 {
43 public:
44 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending)
45 { }
46 wxSortedArrayString(const wxSortedArrayString& array)
47 : wxSortedArrayStringBase(array)
48 { }
49 wxSortedArrayString(const wxArrayString& src)
50 : wxSortedArrayStringBase(wxStringSortAscending)
51 {
52 reserve(src.size());
53
54 for ( size_t n = 0; n < src.size(); n++ )
55 Add(src[n]);
56 }
57 };
58
59 #else // if !wxUSE_STL
60
61 // ----------------------------------------------------------------------------
62 // The string array uses it's knowledge of internal structure of the wxString
63 // class to optimize string storage. Normally, we would store pointers to
64 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
65 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
66 // really all we need to turn such pointer into a string!
67 //
68 // Of course, it can be called a dirty hack, but we use twice less memory and
69 // this approach is also more speed efficient, so it's probably worth it.
70 //
71 // Usage notes: when a string is added/inserted, a new copy of it is created,
72 // so the original string may be safely deleted. When a string is retrieved
73 // from the array (operator[] or Item() method), a reference is returned.
74 // ----------------------------------------------------------------------------
75
76 class WXDLLIMPEXP_BASE wxArrayString
77 {
78 public:
79 // type of function used by wxArrayString::Sort()
80 typedef int (*CompareFunction)(const wxString& first,
81 const wxString& second);
82 // type of function used by wxArrayString::Sort(), for compatibility with
83 // wxArray
84 typedef int (*CompareFunction2)(wxString* first,
85 wxString* second);
86
87 // constructors and destructor
88 // default ctor
89 wxArrayString()
90 : m_nSize(0), m_nCount(0), m_pItems(NULL), m_autoSort(FALSE)
91 { Init(FALSE); }
92 // if autoSort is TRUE, the array is always sorted (in alphabetical order)
93 //
94 // NB: the reason for using int and not bool is that like this we can avoid
95 // using this ctor for implicit conversions from "const char *" (which
96 // we'd like to be implicitly converted to wxString instead!)
97 //
98 // of course, using explicit would be even better - if all compilers
99 // supported it...
100 wxArrayString(int autoSort)
101 : m_nSize(0), m_nCount(0), m_pItems(NULL), m_autoSort(FALSE)
102 { Init(autoSort != 0); }
103 // copy ctor
104 wxArrayString(const wxArrayString& array);
105 // assignment operator
106 wxArrayString& operator=(const wxArrayString& src);
107 // not virtual, this class should not be derived from
108 ~wxArrayString();
109
110 // memory management
111 // empties the list, but doesn't release memory
112 void Empty();
113 // empties the list and releases memory
114 void Clear();
115 // preallocates memory for given number of items
116 void Alloc(size_t nCount);
117 // minimzes the memory usage (by freeing all extra memory)
118 void Shrink();
119
120 // simple accessors
121 // number of elements in the array
122 size_t GetCount() const { return m_nCount; }
123 // is it empty?
124 bool IsEmpty() const { return m_nCount == 0; }
125 // number of elements in the array (GetCount is preferred API)
126 size_t Count() const { return m_nCount; }
127
128 // items access (range checking is done in debug version)
129 // get item at position uiIndex
130 wxString& Item(size_t nIndex) const
131 {
132 wxASSERT_MSG( nIndex < m_nCount,
133 _T("wxArrayString: index out of bounds") );
134
135 return *(wxString *)&(m_pItems[nIndex]);
136 }
137
138 // same as Item()
139 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
140 // get last item
141 wxString& Last() const
142 {
143 wxASSERT_MSG( !IsEmpty(),
144 _T("wxArrayString: index out of bounds") );
145 return Item(Count() - 1);
146 }
147
148 #if WXWIN_COMPATIBILITY_2_4
149 // return a wxString[], useful for the controls which
150 // take one in their ctor. You must delete[] it yourself
151 // once you are done with it. Will return NULL if the
152 // ArrayString was empty.
153 wxString* GetStringArray() const;
154 #endif
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 #if WXWIN_COMPATIBILITY_2_4
173 void Remove(size_t nIndex, size_t nRemove = 1) { RemoveAt(nIndex, nRemove); }
174 #endif
175 void RemoveAt(size_t nIndex, size_t nRemove = 1);
176
177 // sorting
178 // sort array elements in alphabetical order (or reversed alphabetical
179 // order if reverseOrder parameter is TRUE)
180 void Sort(bool reverseOrder = FALSE);
181 // sort array elements using specified comparaison function
182 void Sort(CompareFunction compareFunction);
183 void Sort(CompareFunction2 compareFunction);
184
185 // comparison
186 // compare two arrays case sensitively
187 bool operator==(const wxArrayString& a) const;
188 // compare two arrays case sensitively
189 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
190
191 // STL-like interface
192 typedef wxString value_type;
193 typedef value_type* pointer;
194 typedef const value_type* const_pointer;
195 typedef value_type* iterator;
196 typedef const value_type* const_iterator;
197 typedef value_type& reference;
198 typedef const value_type& const_reference;
199 typedef int difference_type;
200 typedef size_t size_type;
201
202 // FIXME: same in dynarray.h
203 class reverse_iterator
204 {
205 typedef wxString value_type;
206 typedef value_type* pointer;
207 typedef value_type& reference;
208 typedef reverse_iterator itor;
209 friend itor operator+(int o, const itor& it);
210 friend itor operator+(const itor& it, int o);
211 friend itor operator-(const itor& it, int o);
212 friend difference_type operator -(const itor& i1, const itor& i2);
213 public:
214 pointer m_ptr;
215 reverse_iterator() : m_ptr(NULL) { }
216 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
217 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
218 reference operator*() const { return *m_ptr; }
219 pointer operator->() const { return m_ptr; }
220 itor operator++() { --m_ptr; return *this; }
221 itor operator++(int)
222 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
223 itor operator--() { ++m_ptr; return *this; }
224 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
225 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
226 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
227 };
228
229 class const_reverse_iterator
230 {
231 typedef wxString value_type;
232 typedef const value_type* pointer;
233 typedef const value_type& reference;
234 typedef const_reverse_iterator itor;
235 friend itor operator+(int o, const itor& it);
236 friend itor operator+(const itor& it, int o);
237 friend itor operator-(const itor& it, int o);
238 friend difference_type operator -(const itor& i1, const itor& i2);
239 public:
240 pointer m_ptr;
241 const_reverse_iterator() : m_ptr(NULL) { }
242 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
243 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
244 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
245 reference operator*() const { return *m_ptr; }
246 pointer operator->() const { return m_ptr; }
247 itor operator++() { --m_ptr; return *this; }
248 itor operator++(int)
249 { itor tmp = *this; --m_ptr; return tmp; }
250 itor operator--() { ++m_ptr; return *this; }
251 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
252 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
253 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
254 };
255
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 (wxString *)&(m_pItems[0]); }
262 const_iterator begin() const { return (wxString *)&(m_pItems[0]); }
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 void Free(); // free all the strings stored
300
301 void DoSort(); // common part of all Sort() variants
302
303 size_t m_nSize, // current size of the array
304 m_nCount; // current number of elements
305
306 wxChar **m_pItems; // pointer to data
307
308 bool m_autoSort; // if TRUE, keep the array always sorted
309 };
310
311 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
312 {
313 public:
314 wxSortedArrayString() : wxArrayString(TRUE)
315 { }
316 wxSortedArrayString(const wxArrayString& array) : wxArrayString(TRUE)
317 { Copy(array); }
318 };
319
320 #endif // !wxUSE_STL
321
322 #endif