]> git.saurik.com Git - wxWidgets.git/blob - interface/wx/arrstr.h
Restore wxFile docs
[wxWidgets.git] / interface / wx / arrstr.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: arrstr.h
3 // Purpose: interface of wxArrayString
4 // Author: wxWidgets team
5 // RCS-ID: $Id$
6 // Licence: wxWindows license
7 /////////////////////////////////////////////////////////////////////////////
8
9 /**
10 @todo
11 the following functions are not documented; do they need to be?
12 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
13 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
14 */
15
16 /**
17 @class wxArrayString
18
19 wxArrayString is an efficient container for storing wxString objects.
20
21 It has the same features as all wxArray classes, i.e. it dynamically expands
22 when new items are added to it (so it is as easy to use as a linked list),
23 but the access time to the elements is constant, instead of being linear in
24 number of elements as in the case of linked lists. It is also very size
25 efficient and doesn't take more space than a C array @e wxString[] type
26 (wxArrayString uses its knowledge of internals of wxString class to achieve this).
27
28 This class is used in the same way as other dynamic arrays(), except that no
29 ::WX_DEFINE_ARRAY declaration is needed for it.
30 When a string is added or inserted in the array, a copy of the string is created,
31 so the original string may be safely deleted (e.g. if it was a @e wxChar *
32 pointer the memory it was using can be freed immediately after this).
33 In general, there is no need to worry about string memory deallocation when using
34 this class - it will always free the memory it uses itself.
35
36 The references returned by wxArrayString::Item, wxArrayString::Last or
37 wxArrayString::operator[] are not constant, so the array elements may
38 be modified in place like this:
39
40 @code
41 array.Last().MakeUpper();
42 @endcode
43
44 @note none of the methods of wxArrayString is virtual including its
45 destructor, so this class should not be used as a base class.
46
47 Although this is not true strictly speaking, this class may be considered as
48 a specialization of wxArray class for the wxString member data: it is not
49 implemented like this, but it does have all of the wxArray functions.
50
51 @todo what about stl? how does it integrate?
52
53 @library{wxbase}
54 @category{containers}
55
56 @see wxArray, wxString, @ref overview_string
57 */
58 class wxArrayString : public wxArray
59 {
60 public:
61 /**
62 Default constructor.
63 */
64 wxArrayString();
65
66 /**
67 Copy constructor.
68 */
69 wxArrayString(const wxArrayString& array);
70
71 //@{
72 /**
73 Constructor from a C string array. Pass a size @a sz and an array @a arr.
74 **/
75 wxArrayString(size_t sz, const char** arr);
76 wxArrayString(size_t sz, const wchar_t** arr);
77 //@}
78
79 /**
80 Constructor from a wxString array. Pass a size @a sz and array @a arr.
81 */
82 wxArrayString(size_t sz, const wxString* arr);
83
84 /**
85 Destructor frees memory occupied by the array strings. For performance
86 reasons it is not virtual, so this class should not be derived from.
87 */
88 ~wxArrayString();
89
90 /**
91 Appends the given number of @a copies of the new item @a str to the
92 array and returns the index of the first new item in the array.
93
94 @see Insert()
95 */
96 size_t Add(const wxString& str, size_t copies = 1);
97
98 /**
99 Preallocates enough memory to store @a nCount items. This function may be
100 used to improve array class performance before adding a known number of items
101 consecutively.
102
103 @todo FIX THIS LINK
104
105 @see @ref wxArray::memorymanagement "Dynamic array memory management"
106 */
107 void Alloc(size_t nCount);
108
109 /**
110 Clears the array contents and frees memory.
111
112 @see Empty()
113 */
114 void Clear();
115
116 /**
117 Empties the array: after a call to this function GetCount() will return 0.
118 However, this function does not free the memory used by the array and so
119 should be used when the array is going to be reused for storing other strings.
120 Otherwise, you should use Clear() to empty the array and free memory.
121 */
122 void Empty();
123
124 /**
125 Returns the number of items in the array.
126 */
127 size_t GetCount() const;
128
129 /**
130 Search the element in the array, starting from the beginning if @a bFromEnd
131 is @false or from end otherwise. If @a bCase, comparison is case sensitive
132 (default), otherwise the case is ignored.
133
134 This function uses linear search for wxArrayString.
135 Returns index of the first item matched or @c wxNOT_FOUND if there is no match.
136 */
137 int Index(const wxString& sz, bool bCase = true, bool bFromEnd = false) const;
138
139 /**
140 Insert the given number of @a copies of the new element in the array before the
141 position @a nIndex. Thus, for example, to insert the string in the beginning of
142 the array you would write:
143
144 @code
145 Insert("foo", 0);
146 @endcode
147
148 If @a nIndex is equal to GetCount() this function behaves as Add().
149 */
150 void Insert(const wxString& str, size_t nIndex,
151 size_t copies = 1);
152
153 /**
154 Returns @true if the array is empty, @false otherwise. This function returns the
155 same result as GetCount() == 0 but is probably easier to read.
156 */
157 bool IsEmpty() const;
158
159 /**
160 Return the array element at position @a nIndex. An assert failure will
161 result from an attempt to access an element beyond the end of array in debug
162 mode, but no check is done in release mode.
163
164 @see operator[] for the operator version.
165 */
166 wxString& Item(size_t nIndex) const;
167
168 /**
169 Returns the last element of the array. Attempt to access the last element of
170 an empty array will result in assert failure in debug build, however no checks
171 are done in release mode.
172 */
173 wxString& Last() const;
174
175 /**
176 Removes the first item matching this value. An assert failure is provoked by
177 an attempt to remove an element which does not exist in debug build.
178
179 @see Index()
180 */
181 void Remove(const wxString& sz);
182
183 /**
184 Removes @a count items starting at position @a nIndex from the array.
185 */
186 void RemoveAt(size_t nIndex, size_t count = 1);
187
188 /**
189 Releases the extra memory allocated by the array. This function is useful to
190 minimize the array memory consumption.
191
192 @todo FIX THIS LINK
193
194 @see Alloc(), @ref wxArray::memorymanagement "Dynamic array memory
195 management"
196 */
197 void Shrink();
198
199 /**
200 Sorts the array in alphabetical order or in reverse alphabetical order if
201 @a reverseOrder is @true. The sort is case-sensitive.
202 */
203 void Sort(bool reverseOrder = false);
204
205 /**
206 Sorts the array using the specified @a compareFunction for item comparison.
207 @a CompareFunction is defined as a function taking two @e const wxString
208 parameters and returning an @e int value less than, equal to or greater
209 than 0 if the first string is less than, equal to or greater than the
210 second one.
211
212 Example:
213 The following example sorts strings by their length.
214
215 @code
216 static int CompareStringLen(const wxString& first, const wxString& second)
217 {
218 return first.length() - second.length();
219 }
220
221 ...
222
223 wxArrayString array;
224
225 array.Add("one");
226 array.Add("two");
227 array.Add("three");
228 array.Add("four");
229
230 array.Sort(CompareStringLen);
231 @endcode
232 */
233 void Sort(CompareFunction compareFunction);
234
235 /**
236 Compares 2 arrays respecting the case. Returns @true if the arrays have
237 different number of elements or if the elements don't match pairwise.
238 */
239 bool operator !=(const wxArrayString& array) const;
240
241 /**
242 Assignment operator.
243 */
244 wxArrayString& operator=(const wxArrayString&);
245
246 /**
247 Compares 2 arrays respecting the case. Returns @true only if the arrays have
248 the same number of elements and the same strings in the same order.
249 */
250 bool operator ==(const wxArrayString& array) const;
251
252 /**
253 Return the array element at position @a nIndex. An assert failure will
254 result from an attempt to access an element beyond the end of array in
255 debug mode, but no check is done in release mode.
256
257 This is the operator version of the Item() method.
258 */
259 wxString& operator[](size_t nIndex) const;
260 };
261
262
263 /**
264 @class wxSortedArrayString
265
266 wxSortedArrayString is an efficient container for storing wxString objects
267 which always keeps the string in alphabetical order.
268
269 wxSortedArrayString uses binary search in its wxArrayString::Index() function
270 (instead of linear search for wxArrayString::Index()) which makes it much more
271 efficient if you add strings to the array rarely (because, of course, you have
272 to pay for Index() efficiency by having Add() be slower) but search for them
273 often. Several methods should not be used with sorted array (basically, all
274 those which break the order of items) which is mentioned in their description.
275
276 @todo what about STL? who does it integrates?
277
278 @library{wxbase}
279 @category{containers}
280
281 @see wxArray, wxString, @ref overview_string
282 */
283 class wxSortedArrayString : public wxArrayString
284 {
285 public:
286
287 /**
288 Copy constructor. Note that when an array is assigned to a sorted array,
289 its contents is automatically sorted during construction.
290 */
291 wxArrayString(const wxArrayString& array);
292
293 /**
294 @copydoc wxArrayString::Add()
295
296 @warning
297 For sorted arrays, the index of the inserted item will not be, in general,
298 equal to GetCount() - 1 because the item is inserted at the correct position
299 to keep the array sorted and not appended.
300 */
301 size_t Add(const wxString& str, size_t copies = 1);
302
303
304 /**
305 @copydoc wxArrayString::Index()
306
307 This function uses binary search for wxSortedArrayString, but it ignores
308 the @a bCase and @a bFromEnd parameters.
309 */
310 int Index(const wxString& sz, bool bCase = true,
311 bool bFromEnd = false);
312
313 /**
314 @warning this function should not be used with sorted arrays because it
315 could break the order of items and, for example, subsequent calls
316 to Index() would then not work!
317 */
318 void Insert(const wxString& str, size_t nIndex,
319 size_t copies = 1);
320
321 //@{
322 /**
323 @warning this function should not be used with sorted array because it could
324 break the order of items and, for example, subsequent calls to Index()
325 would then not work! Also, sorting a wxSortedArrayString doesn't make
326 sense because its elements are always already sorted.
327 */
328 void Sort(bool reverseOrder = false);
329 void Sort(CompareFunction compareFunction);
330 //@}
331 };
332
333
334 // ============================================================================
335 // Global functions/macros
336 // ============================================================================
337
338 /** @ingroup group_funcmacro_string */
339 //@{
340
341 /**
342 Splits the given wxString object using the separator @a sep and returns the
343 result as a wxArrayString.
344
345 If the @a escape character is non-@NULL, then the occurrences of @a sep
346 immediately prefixed with @a escape are not considered as separators.
347 Note that empty tokens will be generated if there are two or more adjacent
348 separators.
349
350 @see wxJoin()
351
352 @header{wx/arrstr.h}
353 */
354 wxArrayString wxSplit(const wxString& str, const wxChar sep,
355 const wxChar escape = '\\');
356
357 /**
358 Concatenate all lines of the given wxArrayString object using the separator
359 @a sep and returns the result as a wxString.
360
361 If the @a escape character is non-@NULL, then it's used as prefix for each
362 occurrence of @a sep in the strings contained in @a arr before joining them
363 which is necessary in order to be able to recover the original array
364 contents from the string later using wxSplit().
365
366 @see wxSplit()
367
368 @header{wx/arrstr.h}
369 */
370 wxString wxJoin(const wxArrayString& arr, const wxChar sep,
371 const wxChar escape = '\\');
372
373 //@}
374