]> git.saurik.com Git - wxWidgets.git/blame - include/wx/arrstr.h
Always define SIZEOF_WCHAR_T if it's not defined under Windows.
[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;
795ac80e
VZ
90
91private:
92 void Insert()
93 {
94 wxFAIL_MSG( "wxSortedArrayString::Insert() is not to be used" );
95 }
96
97 void Sort()
98 {
99 wxFAIL_MSG( "wxSortedArrayString::Sort() is not to be used" );
100 }
df5168c4
MB
101};
102
103#else // if !wxUSE_STL
104
d11c9d86 105// this shouldn't be defined for compilers not supporting template methods or
93652557
VZ
106// without std::distance()
107//
108// FIXME-VC6: currently it's only not defined for VC6 in DLL build as it
109// doesn't export template methods from DLL correctly so even though
110// it compiles them fine, we get link errors when using wxArrayString
0330301c 111#if !defined(__VISUALC6__) || !(defined(WXMAKINGDLL) || defined(WXUSINGDLL))
93652557
VZ
112 #define wxHAS_VECTOR_TEMPLATE_ASSIGN
113#endif
d11c9d86
VZ
114
115#ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
116 #include "wx/beforestd.h"
117 #include <iterator>
118 #include "wx/afterstd.h"
119#endif // wxHAS_VECTOR_TEMPLATE_ASSIGN
120
df5168c4
MB
121class WXDLLIMPEXP_BASE wxArrayString
122{
123public:
124 // type of function used by wxArrayString::Sort()
39318636 125 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
df5168c4
MB
126 const wxString& second);
127 // type of function used by wxArrayString::Sort(), for compatibility with
128 // wxArray
39318636 129 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
df5168c4
MB
130 wxString* second);
131
132 // constructors and destructor
133 // default ctor
584ad2a3
MB
134 wxArrayString() { Init(false); }
135 // if autoSort is true, the array is always sorted (in alphabetical order)
df5168c4
MB
136 //
137 // NB: the reason for using int and not bool is that like this we can avoid
138 // using this ctor for implicit conversions from "const char *" (which
139 // we'd like to be implicitly converted to wxString instead!)
140 //
141 // of course, using explicit would be even better - if all compilers
142 // supported it...
584ad2a3 143 wxArrayString(int autoSort) { Init(autoSort != 0); }
d44d0cbd 144 // C string array ctor
cfa3d7ba
VS
145 wxArrayString(size_t sz, const char** a);
146 wxArrayString(size_t sz, const wchar_t** a);
d44d0cbd
JS
147 // wxString string array ctor
148 wxArrayString(size_t sz, const wxString* a);
df5168c4
MB
149 // copy ctor
150 wxArrayString(const wxArrayString& array);
151 // assignment operator
152 wxArrayString& operator=(const wxArrayString& src);
153 // not virtual, this class should not be derived from
154 ~wxArrayString();
155
156 // memory management
157 // empties the list, but doesn't release memory
158 void Empty();
159 // empties the list and releases memory
160 void Clear();
161 // preallocates memory for given number of items
162 void Alloc(size_t nCount);
163 // minimzes the memory usage (by freeing all extra memory)
164 void Shrink();
165
166 // simple accessors
167 // number of elements in the array
168 size_t GetCount() const { return m_nCount; }
169 // is it empty?
170 bool IsEmpty() const { return m_nCount == 0; }
171 // number of elements in the array (GetCount is preferred API)
172 size_t Count() const { return m_nCount; }
173
174 // items access (range checking is done in debug version)
175 // get item at position uiIndex
176 wxString& Item(size_t nIndex) const
177 {
178 wxASSERT_MSG( nIndex < m_nCount,
9a83f860 179 wxT("wxArrayString: index out of bounds") );
df5168c4 180
b7452b3a 181 return m_pItems[nIndex];
df5168c4
MB
182 }
183
184 // same as Item()
185 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
186 // get last item
187 wxString& Last() const
188 {
189 wxASSERT_MSG( !IsEmpty(),
9a83f860 190 wxT("wxArrayString: index out of bounds") );
b4a980f4 191 return Item(GetCount() - 1);
df5168c4
MB
192 }
193
df5168c4
MB
194
195 // item management
196 // Search the element in the array, starting from the beginning if
584ad2a3 197 // bFromEnd is false or from end otherwise. If bCase, comparison is case
df5168c4
MB
198 // sensitive (default). Returns index of the first item matched or
199 // wxNOT_FOUND
86501081 200 int Index (const wxString& str, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
201 // add new element at the end (if the array is not sorted), return its
202 // index
203 size_t Add(const wxString& str, size_t nInsert = 1);
204 // add new element at given position
205 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
206 // expand the array to have count elements
207 void SetCount(size_t count);
208 // remove first item matching this value
cfa3d7ba 209 void Remove(const wxString& sz);
df5168c4 210 // remove item by index
df5168c4
MB
211 void RemoveAt(size_t nIndex, size_t nRemove = 1);
212
213 // sorting
214 // sort array elements in alphabetical order (or reversed alphabetical
584ad2a3
MB
215 // order if reverseOrder parameter is true)
216 void Sort(bool reverseOrder = false);
eaf6da07 217 // sort array elements using specified comparison function
df5168c4
MB
218 void Sort(CompareFunction compareFunction);
219 void Sort(CompareFunction2 compareFunction);
220
221 // comparison
222 // compare two arrays case sensitively
223 bool operator==(const wxArrayString& a) const;
224 // compare two arrays case sensitively
225 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
226
227 // STL-like interface
228 typedef wxString value_type;
229 typedef value_type* pointer;
230 typedef const value_type* const_pointer;
231 typedef value_type* iterator;
232 typedef const value_type* const_iterator;
233 typedef value_type& reference;
234 typedef const value_type& const_reference;
235 typedef int difference_type;
236 typedef size_t size_type;
237
3c7896fd 238 // TODO: this code duplicates the one in dynarray.h
df5168c4
MB
239 class reverse_iterator
240 {
37589419
MB
241 typedef wxString value_type;
242 typedef value_type* pointer;
243 typedef value_type& reference;
df5168c4
MB
244 typedef reverse_iterator itor;
245 friend itor operator+(int o, const itor& it);
246 friend itor operator+(const itor& it, int o);
247 friend itor operator-(const itor& it, int o);
248 friend difference_type operator -(const itor& i1, const itor& i2);
249 public:
250 pointer m_ptr;
251 reverse_iterator() : m_ptr(NULL) { }
252 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
253 reverse_iterator(const itor& 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 { reverse_iterator 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
265 class const_reverse_iterator
266 {
37589419
MB
267 typedef wxString value_type;
268 typedef const value_type* pointer;
269 typedef const value_type& reference;
df5168c4
MB
270 typedef const_reverse_iterator itor;
271 friend itor operator+(int o, const itor& it);
272 friend itor operator+(const itor& it, int o);
273 friend itor operator-(const itor& it, int o);
274 friend difference_type operator -(const itor& i1, const itor& i2);
275 public:
276 pointer m_ptr;
277 const_reverse_iterator() : m_ptr(NULL) { }
278 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
279 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
280 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
281 reference operator*() const { return *m_ptr; }
282 pointer operator->() const { return m_ptr; }
60d8e886
VZ
283 itor& operator++() { --m_ptr; return *this; }
284 const itor operator++(int)
df5168c4 285 { itor tmp = *this; --m_ptr; return tmp; }
60d8e886
VZ
286 itor& operator--() { ++m_ptr; return *this; }
287 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
288 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
289 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
df5168c4
MB
290 };
291
584ad2a3
MB
292 wxArrayString(const_iterator first, const_iterator last)
293 { Init(false); assign(first, last); }
294 wxArrayString(size_type n, const_reference v) { Init(false); assign(n, v); }
d11c9d86
VZ
295
296#ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
297 template <class Iterator>
298 void assign(Iterator first, Iterator last)
299 {
300 clear();
301 reserve(std::distance(first, last));
302 for(; first != last; ++first)
303 push_back(*first);
304 }
305#else // !wxHAS_VECTOR_TEMPLATE_ASSIGN
306 void assign(const_iterator first, const_iterator last)
307 {
308 clear();
309 reserve(last - first);
310 for(; first != last; ++first)
311 push_back(*first);
312 }
313#endif // wxHAS_VECTOR_TEMPLATE_ASSIGN/!wxHAS_VECTOR_TEMPLATE_ASSIGN
314
df5168c4
MB
315 void assign(size_type n, const_reference v)
316 { clear(); Add(v, n); }
317 reference back() { return *(end() - 1); }
318 const_reference back() const { return *(end() - 1); }
b7452b3a
VS
319 iterator begin() { return m_pItems; }
320 const_iterator begin() const { return m_pItems; }
df5168c4
MB
321 size_type capacity() const { return m_nSize; }
322 void clear() { Clear(); }
323 bool empty() const { return IsEmpty(); }
324 iterator end() { return begin() + GetCount(); }
325 const_iterator end() const { return begin() + GetCount(); }
326 iterator erase(iterator first, iterator last)
327 {
328 size_t idx = first - begin();
329 RemoveAt(idx, last - first);
330 return begin() + idx;
331 }
332 iterator erase(iterator it) { return erase(it, it + 1); }
333 reference front() { return *begin(); }
334 const_reference front() const { return *begin(); }
335 void insert(iterator it, size_type n, const_reference v)
336 { Insert(v, it - begin(), n); }
337 iterator insert(iterator it, const_reference v = value_type())
338 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
339 void insert(iterator it, const_iterator first, const_iterator last);
340 size_type max_size() const { return INT_MAX; }
341 void pop_back() { RemoveAt(GetCount() - 1); }
342 void push_back(const_reference v) { Add(v); }
343 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
d3960469
VZ
344 const_reverse_iterator rbegin() const
345 { return const_reverse_iterator(end() - 1); }
df5168c4 346 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
d3960469
VZ
347 const_reverse_iterator rend() const
348 { return const_reverse_iterator(begin() - 1); }
df5168c4
MB
349 void reserve(size_type n) /* base::reserve*/;
350 void resize(size_type n, value_type v = value_type());
351 size_type size() const { return GetCount(); }
91276868
VZ
352 void swap(wxArrayString& other)
353 {
1372f8cc
VZ
354 wxSwap(m_nSize, other.m_nSize);
355 wxSwap(m_nCount, other.m_nCount);
356 wxSwap(m_pItems, other.m_pItems);
357 wxSwap(m_autoSort, other.m_autoSort);
91276868 358 }
df5168c4
MB
359
360protected:
361 void Init(bool autoSort); // common part of all ctors
362 void Copy(const wxArrayString& src); // copies the contents of another array
363
364private:
365 void Grow(size_t nIncrement = 0); // makes array bigger if needed
df5168c4 366
df5168c4
MB
367 size_t m_nSize, // current size of the array
368 m_nCount; // current number of elements
369
b7452b3a 370 wxString *m_pItems; // pointer to data
df5168c4 371
584ad2a3 372 bool m_autoSort; // if true, keep the array always sorted
df5168c4
MB
373};
374
375class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
376{
377public:
584ad2a3 378 wxSortedArrayString() : wxArrayString(true)
df5168c4 379 { }
584ad2a3 380 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
df5168c4
MB
381 { Copy(array); }
382};
383
384#endif // !wxUSE_STL
385
584ad2a3
MB
386// this class provides a temporary wxString* from a
387// wxArrayString
388class WXDLLIMPEXP_BASE wxCArrayString
389{
390public:
391 wxCArrayString( const wxArrayString& array )
392 : m_array( array ), m_strings( NULL )
393 { }
394 ~wxCArrayString() { delete[] m_strings; }
395
396 size_t GetCount() const { return m_array.GetCount(); }
397 wxString* GetStrings()
398 {
399 if( m_strings ) return m_strings;
400 size_t count = m_array.GetCount();
401 m_strings = new wxString[count];
402 for( size_t i = 0; i < count; ++i )
403 m_strings[i] = m_array[i];
404 return m_strings;
405 }
4f24cbbd
VS
406
407 wxString* Release()
408 {
409 wxString *r = GetStrings();
410 m_strings = NULL;
411 return r;
412 }
413
584ad2a3
MB
414private:
415 const wxArrayString& m_array;
416 wxString* m_strings;
417};
418
abbb59e8
VZ
419
420// ----------------------------------------------------------------------------
421// helper functions for working with arrays
422// ----------------------------------------------------------------------------
423
424// by default, these functions use the escape character to escape the
425// separators occuring inside the string to be joined, this can be disabled by
426// passing '\0' as escape
427
428WXDLLIMPEXP_BASE wxString wxJoin(const wxArrayString& arr,
429 const wxChar sep,
430 const wxChar escape = wxT('\\'));
431
432WXDLLIMPEXP_BASE wxArrayString wxSplit(const wxString& str,
433 const wxChar sep,
434 const wxChar escape = wxT('\\'));
435
a236aa20
VZ
436
437// ----------------------------------------------------------------------------
438// This helper class allows to pass both C array of wxStrings or wxArrayString
439// using the same interface.
440//
441// Use it when you have two methods taking wxArrayString or (int, wxString[]),
442// that do the same thing. This class lets you iterate over input data in the
443// same way whether it is a raw array of strings or wxArrayString.
444//
445// The object does not take ownership of the data -- internally it keeps
446// pointers to the data, therefore the data must be disposed of by user
447// and only after this object is destroyed. Usually it is not a problem as
448// only temporary objects of this class are used.
449// ----------------------------------------------------------------------------
450
451class wxArrayStringsAdapter
452{
453public:
454 // construct an adapter from a wxArrayString
455 wxArrayStringsAdapter(const wxArrayString& strings)
456 : m_type(wxSTRING_ARRAY), m_size(strings.size())
457 {
458 m_data.array = &strings;
459 }
460
461 // construct an adapter from a wxString[]
462 wxArrayStringsAdapter(unsigned int n, const wxString *strings)
463 : m_type(wxSTRING_POINTER), m_size(n)
464 {
465 m_data.ptr = strings;
466 }
467
468 // construct an adapter from a single wxString
469 wxArrayStringsAdapter(const wxString& s)
470 : m_type(wxSTRING_POINTER), m_size(1)
471 {
472 m_data.ptr = &s;
473 }
474
475 // default copy constructor is ok
476
477 // iteration interface
1b6d87d0 478 size_t GetCount() const { return m_size; }
a236aa20
VZ
479 bool IsEmpty() const { return GetCount() == 0; }
480 const wxString& operator[] (unsigned int i) const
481 {
482 wxASSERT_MSG( i < GetCount(), wxT("index out of bounds") );
483 if(m_type == wxSTRING_POINTER)
484 return m_data.ptr[i];
485 return m_data.array->Item(i);
486 }
487 wxArrayString AsArrayString() const
488 {
489 if(m_type == wxSTRING_ARRAY)
490 return *m_data.array;
491 return wxArrayString(GetCount(), m_data.ptr);
492 }
493
494private:
495 // type of the data being held
496 enum wxStringContainerType
497 {
498 wxSTRING_ARRAY, // wxArrayString
499 wxSTRING_POINTER // wxString[]
500 };
501
502 wxStringContainerType m_type;
1b6d87d0 503 size_t m_size;
a236aa20
VZ
504 union
505 {
506 const wxString * ptr;
507 const wxArrayString * array;
508 } m_data;
509
c0c133e1 510 wxDECLARE_NO_ASSIGN_CLASS(wxArrayStringsAdapter);
a236aa20
VZ
511};
512
abbb59e8 513#endif // _WX_ARRSTR_H