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