]> git.saurik.com Git - wxWidgets.git/blame - include/wx/arrstr.h
Add wxBitmap::GetNSImage() which creates an autoreleased NSImage either
[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
95 wxArrayString()
96 : m_nSize(0), m_nCount(0), m_pItems(NULL), m_autoSort(FALSE)
97 { Init(FALSE); }
98 // if autoSort is TRUE, the array is always sorted (in alphabetical order)
99 //
100 // NB: the reason for using int and not bool is that like this we can avoid
101 // using this ctor for implicit conversions from "const char *" (which
102 // we'd like to be implicitly converted to wxString instead!)
103 //
104 // of course, using explicit would be even better - if all compilers
105 // supported it...
106 wxArrayString(int autoSort)
107 : m_nSize(0), m_nCount(0), m_pItems(NULL), m_autoSort(FALSE)
108 { Init(autoSort != 0); }
109 // copy ctor
110 wxArrayString(const wxArrayString& array);
111 // assignment operator
112 wxArrayString& operator=(const wxArrayString& src);
113 // not virtual, this class should not be derived from
114 ~wxArrayString();
115
116 // memory management
117 // empties the list, but doesn't release memory
118 void Empty();
119 // empties the list and releases memory
120 void Clear();
121 // preallocates memory for given number of items
122 void Alloc(size_t nCount);
123 // minimzes the memory usage (by freeing all extra memory)
124 void Shrink();
125
126 // simple accessors
127 // number of elements in the array
128 size_t GetCount() const { return m_nCount; }
129 // is it empty?
130 bool IsEmpty() const { return m_nCount == 0; }
131 // number of elements in the array (GetCount is preferred API)
132 size_t Count() const { return m_nCount; }
133
134 // items access (range checking is done in debug version)
135 // get item at position uiIndex
136 wxString& Item(size_t nIndex) const
137 {
138 wxASSERT_MSG( nIndex < m_nCount,
139 _T("wxArrayString: index out of bounds") );
140
141 return *(wxString *)&(m_pItems[nIndex]);
142 }
143
144 // same as Item()
145 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
146 // get last item
147 wxString& Last() const
148 {
149 wxASSERT_MSG( !IsEmpty(),
150 _T("wxArrayString: index out of bounds") );
151 return Item(Count() - 1);
152 }
153
df5168c4
MB
154 // return a wxString[], useful for the controls which
155 // take one in their ctor. You must delete[] it yourself
156 // once you are done with it. Will return NULL if the
157 // ArrayString was empty.
158 wxString* GetStringArray() const;
df5168c4
MB
159
160 // item management
161 // Search the element in the array, starting from the beginning if
162 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
163 // sensitive (default). Returns index of the first item matched or
164 // wxNOT_FOUND
165 int Index (const wxChar *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const;
166 // add new element at the end (if the array is not sorted), return its
167 // index
168 size_t Add(const wxString& str, size_t nInsert = 1);
169 // add new element at given position
170 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
171 // expand the array to have count elements
172 void SetCount(size_t count);
173 // remove first item matching this value
174 void Remove(const wxChar *sz);
175 // remove item by index
176#if WXWIN_COMPATIBILITY_2_4
177 void Remove(size_t nIndex, size_t nRemove = 1) { RemoveAt(nIndex, nRemove); }
178#endif
179 void RemoveAt(size_t nIndex, size_t nRemove = 1);
180
181 // sorting
182 // sort array elements in alphabetical order (or reversed alphabetical
183 // order if reverseOrder parameter is TRUE)
184 void Sort(bool reverseOrder = FALSE);
185 // sort array elements using specified comparaison function
186 void Sort(CompareFunction compareFunction);
187 void Sort(CompareFunction2 compareFunction);
188
189 // comparison
190 // compare two arrays case sensitively
191 bool operator==(const wxArrayString& a) const;
192 // compare two arrays case sensitively
193 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
194
195 // STL-like interface
196 typedef wxString value_type;
197 typedef value_type* pointer;
198 typedef const value_type* const_pointer;
199 typedef value_type* iterator;
200 typedef const value_type* const_iterator;
201 typedef value_type& reference;
202 typedef const value_type& const_reference;
203 typedef int difference_type;
204 typedef size_t size_type;
205
206 // FIXME: same in dynarray.h
207 class reverse_iterator
208 {
37589419
MB
209 typedef wxString value_type;
210 typedef value_type* pointer;
211 typedef value_type& reference;
df5168c4
MB
212 typedef reverse_iterator itor;
213 friend itor operator+(int o, const itor& it);
214 friend itor operator+(const itor& it, int o);
215 friend itor operator-(const itor& it, int o);
216 friend difference_type operator -(const itor& i1, const itor& i2);
217 public:
218 pointer m_ptr;
219 reverse_iterator() : m_ptr(NULL) { }
220 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
221 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
222 reference operator*() const { return *m_ptr; }
223 pointer operator->() const { return m_ptr; }
224 itor operator++() { --m_ptr; return *this; }
225 itor operator++(int)
226 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
227 itor operator--() { ++m_ptr; return *this; }
228 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
229 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
230 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
231 };
232
233 class const_reverse_iterator
234 {
37589419
MB
235 typedef wxString value_type;
236 typedef const value_type* pointer;
237 typedef const value_type& reference;
df5168c4
MB
238 typedef const_reverse_iterator itor;
239 friend itor operator+(int o, const itor& it);
240 friend itor operator+(const itor& it, int o);
241 friend itor operator-(const itor& it, int o);
242 friend difference_type operator -(const itor& i1, const itor& i2);
243 public:
244 pointer m_ptr;
245 const_reverse_iterator() : m_ptr(NULL) { }
246 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
247 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
248 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
249 reference operator*() const { return *m_ptr; }
250 pointer operator->() const { return m_ptr; }
251 itor operator++() { --m_ptr; return *this; }
252 itor operator++(int)
253 { itor tmp = *this; --m_ptr; return tmp; }
254 itor operator--() { ++m_ptr; return *this; }
255 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
256 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
257 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
258 };
259
260 void assign(const_iterator first, const_iterator last);
261 void assign(size_type n, const_reference v)
262 { clear(); Add(v, n); }
263 reference back() { return *(end() - 1); }
264 const_reference back() const { return *(end() - 1); }
265 iterator begin() { return (wxString *)&(m_pItems[0]); }
266 const_iterator begin() const { return (wxString *)&(m_pItems[0]); }
267 size_type capacity() const { return m_nSize; }
268 void clear() { Clear(); }
269 bool empty() const { return IsEmpty(); }
270 iterator end() { return begin() + GetCount(); }
271 const_iterator end() const { return begin() + GetCount(); }
272 iterator erase(iterator first, iterator last)
273 {
274 size_t idx = first - begin();
275 RemoveAt(idx, last - first);
276 return begin() + idx;
277 }
278 iterator erase(iterator it) { return erase(it, it + 1); }
279 reference front() { return *begin(); }
280 const_reference front() const { return *begin(); }
281 void insert(iterator it, size_type n, const_reference v)
282 { Insert(v, it - begin(), n); }
283 iterator insert(iterator it, const_reference v = value_type())
284 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
285 void insert(iterator it, const_iterator first, const_iterator last);
286 size_type max_size() const { return INT_MAX; }
287 void pop_back() { RemoveAt(GetCount() - 1); }
288 void push_back(const_reference v) { Add(v); }
289 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
290 const_reverse_iterator rbegin() const;
291 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
292 const_reverse_iterator rend() const;
293 void reserve(size_type n) /* base::reserve*/;
294 void resize(size_type n, value_type v = value_type());
295 size_type size() const { return GetCount(); }
296
297protected:
298 void Init(bool autoSort); // common part of all ctors
299 void Copy(const wxArrayString& src); // copies the contents of another array
300
301private:
302 void Grow(size_t nIncrement = 0); // makes array bigger if needed
303 void Free(); // free all the strings stored
304
305 void DoSort(); // common part of all Sort() variants
306
307 size_t m_nSize, // current size of the array
308 m_nCount; // current number of elements
309
310 wxChar **m_pItems; // pointer to data
311
312 bool m_autoSort; // if TRUE, keep the array always sorted
313};
314
315class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
316{
317public:
318 wxSortedArrayString() : wxArrayString(TRUE)
319 { }
320 wxSortedArrayString(const wxArrayString& array) : wxArrayString(TRUE)
321 { Copy(array); }
322};
323
324#endif // !wxUSE_STL
325
326#endif