]>
Commit | Line | Data |
---|---|---|
23324ae1 FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: arrstr.h | |
3 | // Purpose: documentation for wxArrayString class | |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /** | |
10 | @class wxArrayString | |
11 | @wxheader{arrstr.h} | |
12 | ||
13 | wxArrayString is an efficient container for storing | |
14 | wxString objects. It has the same features as all | |
15 | wxArray classes, i.e. it dynamically expands when new items | |
16 | are added to it (so it is as easy to use as a linked list), but the access | |
17 | time to the elements is constant, instead of being linear in number of | |
18 | elements as in the case of linked lists. It is also very size efficient and | |
19 | doesn't take more space than a C array @e wxString[] type (wxArrayString | |
20 | uses its knowledge of internals of wxString class to achieve this). | |
21 | ||
22 | This class is used in the same way as other dynamic arrays, | |
23 | except that no @e WX_DEFINE_ARRAY declaration is needed for it. When a | |
24 | string is added or inserted in the array, a copy of the string is created, so | |
25 | the original string may be safely deleted (e.g. if it was a @e wxChar * | |
26 | pointer the memory it was using can be freed immediately after this). In | |
27 | general, there is no need to worry about string memory deallocation when using | |
28 | this class - it will always free the memory it uses itself. | |
29 | ||
30 | The references returned by wxArrayString::Item, | |
31 | wxArrayString::Last or | |
32 | @ref wxArrayString::operatorindex operator[] are not constant, so the | |
33 | array elements may be modified in place like this | |
34 | ||
35 | @code | |
36 | array.Last().MakeUpper(); | |
37 | @endcode | |
38 | ||
39 | There is also a variant of wxArrayString called wxSortedArrayString which has | |
40 | exactly the same methods as wxArrayString, but which always keeps the string | |
41 | in it in (alphabetical) order. wxSortedArrayString uses binary search in its | |
42 | wxArrayString::Index function (instead of linear search for | |
43 | wxArrayString::Index) which makes it much more efficient if you add strings to | |
44 | the array rarely (because, of course, you have to pay for Index() efficiency | |
45 | by having Add() be slower) but search for them often. Several methods should | |
46 | not be used with sorted array (basically, all which break the order of items) | |
47 | which is mentioned in their description. | |
48 | ||
49 | Final word: none of the methods of wxArrayString is virtual including its | |
50 | destructor, so this class should not be used as a base class. | |
51 | ||
52 | @library{wxbase} | |
53 | @category{containers} | |
54 | ||
55 | @seealso | |
56 | wxArray, wxString, @ref overview_wxstringoverview "wxString overview" | |
57 | */ | |
58 | class wxArrayString : public wxArray | |
59 | { | |
60 | public: | |
61 | //@{ | |
62 | /** | |
63 | Constructor from a wxString array. Pass a size @e sz and array @e arr. | |
64 | */ | |
65 | wxArrayString(); | |
66 | wxArrayString(const wxArrayString& array); | |
67 | wxArrayString(size_t sz, const char** arr); | |
68 | wxArrayString(size_t sz, const wchar_t** arr); | |
69 | wxArrayString(size_t sz, const wxString* arr); | |
70 | //@} | |
71 | ||
72 | /** | |
73 | Destructor frees memory occupied by the array strings. For the performance | |
74 | reasons it is not virtual, so this class should not be derived from. | |
75 | */ | |
76 | ~wxArrayString(); | |
77 | ||
78 | /** | |
79 | Appends the given number of @e copies of the new item @e str to the | |
80 | array and returns the index of the first new item in the array. | |
81 | ||
82 | @b Warning: For sorted arrays, the index of the inserted item will not be, | |
83 | in general, equal to GetCount() - 1 because | |
84 | the item is inserted at the correct position to keep the array sorted and not | |
85 | appended. | |
86 | ||
87 | See also: Insert() | |
88 | */ | |
89 | #define size_t Add(const wxString& str, size_t copies = 1) /* implementation is private */ | |
90 | ||
91 | /** | |
92 | Preallocates enough memory to store @e nCount items. This function may be | |
93 | used to improve array class performance before adding a known number of items | |
94 | consecutively. | |
95 | ||
96 | See also: @ref wxArray::memorymanagement "Dynamic array memory management" | |
97 | */ | |
98 | void Alloc(size_t nCount); | |
99 | ||
100 | /** | |
101 | Clears the array contents and frees memory. | |
102 | ||
103 | See also: Empty() | |
104 | */ | |
105 | void Clear(); | |
106 | ||
107 | /** | |
108 | Empties the array: after a call to this function | |
109 | GetCount() will return 0. However, this | |
110 | function does not free the memory used by the array and so should be used when | |
111 | the array is going to be reused for storing other strings. Otherwise, you | |
112 | should use Clear() to empty the array and free | |
113 | memory. | |
114 | */ | |
115 | void Empty(); | |
116 | ||
117 | /** | |
118 | Returns the number of items in the array. | |
119 | */ | |
120 | size_t GetCount(); | |
121 | ||
122 | /** | |
123 | Search the element in the array, starting from the beginning if | |
124 | @e bFromEnd is @false or from end otherwise. If @e bCase, comparison is | |
125 | case sensitive (default), otherwise the case is ignored. | |
126 | ||
127 | This function uses linear search for wxArrayString and binary search for | |
128 | wxSortedArrayString, but it ignores the @e bCase and @e bFromEnd | |
129 | parameters in the latter case. | |
130 | ||
131 | Returns index of the first item matched or @c wxNOT_FOUND if there is no match. | |
132 | */ | |
133 | int Index(const wxString& sz, bool bCase = @true, | |
134 | bool bFromEnd = @false); | |
135 | ||
136 | /** | |
137 | Insert the given number of @e copies of the new element in the array before the | |
138 | position @e nIndex. Thus, for | |
139 | example, to insert the string in the beginning of the array you would write | |
140 | If @e nIndex is equal to @e GetCount() this function behaves as | |
141 | Add(). | |
142 | ||
143 | @b Warning: this function should not be used with sorted arrays because it | |
144 | could break the order of items and, for example, subsequent calls to | |
145 | Index() would then not work! | |
146 | */ | |
147 | void Insert(const wxString& str, size_t nIndex, | |
148 | size_t copies = 1); | |
149 | ||
150 | /** | |
151 | Returns @true if the array is empty, @false otherwise. This function returns the | |
152 | same result as @e GetCount() == 0 but is probably easier to read. | |
153 | */ | |
154 | bool IsEmpty(); | |
155 | ||
156 | /** | |
157 | Return the array element at position @e nIndex. An assert failure will | |
158 | result from an attempt to access an element beyond the end of array in debug | |
159 | mode, but no check is done in release mode. | |
160 | ||
161 | See also @ref operatorindex() operator[] for the operator | |
162 | version. | |
163 | */ | |
164 | wxString Item(size_t nIndex); | |
165 | ||
166 | /** | |
167 | Returns the last element of the array. Attempt to access the last element of | |
168 | an empty array will result in assert failure in debug build, however no checks | |
169 | are done in release mode. | |
170 | */ | |
171 | wxString Last(); | |
172 | ||
173 | /** | |
174 | Removes the first item matching this value. An assert failure is provoked by | |
175 | an attempt to remove an element which does not exist in debug build. | |
176 | ||
177 | See also: Index() | |
178 | */ | |
179 | void Remove(const wxString& sz); | |
180 | ||
181 | /** | |
182 | Removes @e count items starting at position @e nIndex from the array. | |
183 | */ | |
184 | void RemoveAt(size_t nIndex, size_t count = 1); | |
185 | ||
186 | /** | |
187 | Releases the extra memory allocated by the array. This function is useful to | |
188 | minimize the array memory consumption. | |
189 | ||
190 | See also: Alloc(), @ref wxArray::memorymanagement "Dynamic array memory | |
191 | management" | |
192 | */ | |
193 | void Shrink(); | |
194 | ||
195 | //@{ | |
196 | /** | |
197 | Sorts the array using the specified @e compareFunction for item comparison. | |
198 | @e CompareFunction is defined as a function taking two @e const | |
199 | wxString parameters and returning an @e int value less than, equal to or | |
200 | greater than 0 if the first string is less than, equal to or greater than the | |
201 | second one. | |
202 | */ | |
203 | void Sort(bool reverseOrder = @false); | |
204 | Warning: void Sort(CompareFunction compareFunction); | |
205 | //@} | |
206 | ||
207 | /** | |
208 | Compares 2 arrays respecting the case. Returns @true if the arrays have | |
209 | different number of elements or if the elements don't match pairwise. | |
210 | */ | |
211 | bool operator !=(const wxArrayString& array); | |
212 | ||
213 | /** | |
214 | Assignment operator. | |
215 | */ | |
216 | wxArrayString operator =(const wxArrayString& array); | |
217 | ||
218 | /** | |
219 | Compares 2 arrays respecting the case. Returns @true only if the arrays have | |
220 | the same number of elements and the same strings in the same order. | |
221 | */ | |
222 | bool operator ==(const wxArrayString& array); | |
223 | ||
224 | /** | |
225 | Return the array element at position @e nIndex. An assert failure will | |
226 | result from an attempt to access an element beyond the end of array in debug | |
227 | mode, but no check is done in release mode. | |
228 | ||
229 | This is the operator version of Item() method. | |
230 | */ | |
231 | wxString operator[](size_t nIndex); | |
232 | }; | |
233 | ||
234 | ||
235 | // ============================================================================ | |
236 | // Global functions/macros | |
237 | // ============================================================================ | |
238 | ||
239 | /** | |
240 | Splits the given wxString object using the separator @e sep and returns the | |
241 | result as a wxArrayString. | |
242 | ||
243 | If the @e escape character is non-@NULL, then the occurrences of @e sep | |
244 | immediately prefixed | |
245 | with @e escape are not considered as separators. | |
246 | ||
247 | Note that empty tokens will be generated if there are two or more adjacent | |
248 | separators. | |
249 | ||
250 | @sa wxJoin | |
251 | */ | |
252 | wxArrayString wxSplit(const wxString& str, const wxChar sep, | |
253 | const wxChar escape = ' | |
254 | '); | |
255 | ||
256 | /** | |
257 | Concatenate all lines of the given wxArrayString object using the separator @e | |
258 | sep and returns | |
259 | the result as a wxString. | |
260 | ||
261 | If the @e escape character is non-@NULL, then it's used as prefix for each | |
262 | occurrence of @e sep | |
263 | in the strings contained in @e arr before joining them which is necessary | |
264 | in order to be able to recover the original array contents from the string | |
265 | later using wxSplit. | |
266 | */ | |
267 | wxString wxJoin(const wxArrayString& arr, const wxChar sep, | |
268 | const wxChar escape = '\'); | |
269 |