]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
make wxDateTime::ParseXXX() functions char*-friendly
[wxWidgets.git] / include / wx / vector.h
CommitLineData
3c648a82
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/vector.h
3// Purpose: STL vector clone
4// Author: Lindsay Mathieson
e966f815 5// Modified by: Vaclav Slavik - make it a template
3c648a82 6// Created: 30.07.2001
e966f815
VS
7// Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>,
8// 2007 Vaclav Slavik <vslavik@fastmail.fm>
65571936 9// Licence: wxWindows licence
3c648a82
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_VECTOR_H_
13#define _WX_VECTOR_H_
14
15#include "wx/defs.h"
16
df4aed1c 17#if wxUSE_STL
e966f815 18
e966f815
VS
19#include <vector>
20#define wxVector std::vector
21
22#else // !wxUSE_STL
23
2d615fe9
JS
24#include "wx/utils.h"
25
c157b27e
VS
26#include "wx/beforestd.h"
27#include <new> // for placement new
28#include "wx/afterstd.h"
29
e966f815
VS
30template<typename T>
31class wxVector
3c648a82 32{
2d6c58d6 33public:
5fd588d2 34 typedef size_t size_type;
e966f815 35 typedef T value_type;
df4aed1c 36 typedef value_type* iterator;
0516de2c 37 typedef const value_type* const_iterator;
df4aed1c 38 typedef value_type& reference;
e966f815 39
0516de2c 40 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
e966f815
VS
41
42 wxVector(const wxVector& c)
43 {
0516de2c 44 Copy(c);
e966f815
VS
45 }
46
47 ~wxVector()
48 {
49 clear();
50 }
51
52 void clear()
53 {
c157b27e
VS
54 // call destructors of stored objects:
55 for ( size_type i = 0; i < m_size; i++ )
56 {
57 m_values[i].~value_type();
58 }
59
60 free(m_values);
0516de2c 61 m_values = NULL;
e966f815
VS
62 m_size = m_capacity = 0;
63 }
64
65 void reserve(size_type n)
66 {
0516de2c
VS
67 if ( n <= m_capacity )
68 return;
69
70 // increase the size twice, unless we're already too big or unless
71 // more is requested
f5851311 72 //
c157b27e 73 // NB: casts to size_type are needed to suppress mingw32 warnings about
f5851311
VZ
74 // mixing enums and ints in the same expression
75 const size_type increment = m_size > 0
76 ? wxMin(m_size, (size_type)ALLOC_MAX_SIZE)
77 : (size_type)ALLOC_INITIAL_SIZE;
0516de2c
VS
78 if ( m_capacity + increment > n )
79 n = m_capacity + increment;
80
c157b27e 81 m_values = (value_type*)realloc(m_values, n * sizeof(value_type));
0516de2c 82
0516de2c 83 m_capacity = n;
e966f815
VS
84 }
85
86 size_type size() const
87 {
88 return m_size;
89 }
90
91 size_type capacity() const
92 {
93 return m_capacity;
94 }
95
96 bool empty() const
97 {
98 return size() == 0;
99 }
100
101 wxVector& operator=(const wxVector& vb)
102 {
0516de2c 103 Copy(vb);
e966f815
VS
104 return *this;
105 }
106
0516de2c 107 void push_back(const value_type& v)
e966f815 108 {
0516de2c 109 reserve(size() + 1);
c157b27e
VS
110
111 // use placement new to initialize new object in preallocated place in
112 // m_values and store 'v' in it:
113 void* const place = m_values + m_size;
114 new(place) value_type(v);
115
116 // only increase m_size if the ctor didn't throw an exception; notice
117 // that if it _did_ throw, everything is OK, because we only increased
118 // vector's capacity so far and possibly written some data to
119 // uninitialized memory at the end of m_values
120 m_size++;
e966f815
VS
121 }
122
123 void pop_back()
124 {
0516de2c 125 erase(end() - 1);
e966f815
VS
126 }
127
128 const value_type& at(size_type idx) const
129 {
130 wxASSERT(idx < m_size);
0516de2c 131 return m_values[idx];
e966f815
VS
132 }
133
134 value_type& at(size_type idx)
135 {
136 wxASSERT(idx < m_size);
0516de2c 137 return m_values[idx];
e966f815 138 }
3c648a82 139
e966f815
VS
140 const value_type& operator[](size_type idx) const { return at(idx); }
141 value_type& operator[](size_type idx) { return at(idx); }
142 const value_type& front() const { return at(0); }
143 value_type& front() { return at(0); }
144 const value_type& back() const { return at(size() - 1); }
145 value_type& back() { return at(size() - 1); }
146
0516de2c
VS
147 const_iterator begin() const { return m_values; }
148 iterator begin() { return m_values; }
149 const_iterator end() const { return m_values + size(); }
150 iterator end() { return m_values + size(); }
df4aed1c 151
0516de2c 152 iterator insert(iterator it, const value_type& v = value_type())
f631cd8e 153 {
c157b27e
VS
154 // NB: this must be done before reserve(), because reserve()
155 // invalidates iterators!
156 const size_t idx = it - begin();
157 const size_t after = end() - it;
f631cd8e 158
0516de2c 159 reserve(size() + 1);
9cf33372 160
c157b27e 161 // unless we're inserting at the end, move following elements out of
0516de2c 162 // the way:
c157b27e
VS
163 if ( after > 0 )
164 {
165 memmove(m_values + idx + 1,
166 m_values + idx,
167 after * sizeof(value_type));
168 }
169
170#if wxUSE_EXCEPTIONS
171 try
172 {
173#endif
174 // use placement new to initialize new object in preallocated place
175 // in m_values and store 'v' in it:
176 void* const place = m_values + idx;
177 new(place) value_type(v);
178#if wxUSE_EXCEPTIONS
179 }
180 catch ( ... )
181 {
182 // if the ctor threw an exception, we need to move all the elements
183 // back to their original positions in m_values
184 if ( after > 0 )
185 {
186 memmove(m_values + idx,
187 m_values + idx + 1,
188 after * sizeof(value_type));
189 }
190
191 throw; // rethrow the exception
192 }
193#endif // wxUSE_EXCEPTIONS
e966f815 194
c157b27e
VS
195 // increment m_size only if ctor didn't throw -- if it did, we'll be
196 // left with m_values larger than necessary, but number of elements will
197 // be the same
0516de2c 198 m_size++;
3c648a82 199
0516de2c 200 return begin() + idx;
1f32f585 201 }
3c648a82 202
0516de2c 203 iterator erase(iterator it)
3c648a82 204 {
0516de2c 205 return erase(it, it + 1);
1f32f585 206 }
3c648a82 207
0516de2c 208 iterator erase(iterator first, iterator last)
df4aed1c 209 {
0516de2c
VS
210 if ( first == last )
211 return first;
212 wxASSERT( first < end() && last <= end() );
f631cd8e 213
c157b27e
VS
214 const size_type idx = first - begin();
215 const size_type count = last - first;
216 const size_type after = end() - last;
df4aed1c 217
c157b27e
VS
218 // erase elements by calling their destructors:
219 for ( iterator i = first; i < last; ++i )
220 i->~value_type();
0516de2c 221
c157b27e
VS
222 // once that's done, move following elements over to the freed space:
223 if ( after > 0 )
224 {
225 memmove(m_values + idx,
226 m_values + idx + count,
227 after * sizeof(value_type));
228 }
f631cd8e 229
df4aed1c 230 m_size -= count;
0516de2c 231
c157b27e 232 return begin() + idx;
df4aed1c 233 }
0516de2c
VS
234
235#if WXWIN_COMPATIBILITY_2_8
236 wxDEPRECATED( size_type erase(size_type n) );
237#endif // WXWIN_COMPATIBILITY_2_8
238
239private:
f9bf06ac
VS
240 // VC6 can't compile static const int members
241 enum { ALLOC_INITIAL_SIZE = 16 };
242 enum { ALLOC_MAX_SIZE = 4096 };
0516de2c
VS
243
244 void Copy(const wxVector& vb)
3c648a82
VZ
245 {
246 clear();
0516de2c 247 reserve(vb.size());
3c648a82 248
0516de2c
VS
249 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
250 push_back(*i);
1f32f585 251 }
3c648a82 252
e966f815 253private:
e966f815
VS
254 size_type m_size,
255 m_capacity;
0516de2c 256 value_type *m_values;
3c648a82
VZ
257};
258
9cf33372
VS
259#if WXWIN_COMPATIBILITY_2_8
260template<typename T>
32aa5bda 261inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
9cf33372 262{
0516de2c 263 erase(begin() + n);
9cf33372
VS
264 return n;
265}
266#endif // WXWIN_COMPATIBILITY_2_8
267
e966f815 268#endif // wxUSE_STL/!wxUSE_STL
5fd588d2 269
e966f815
VS
270#if WXWIN_COMPATIBILITY_2_8
271 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
272 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
273 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
274#endif // WXWIN_COMPATIBILITY_2_8
3c648a82 275
e966f815 276#endif // _WX_VECTOR_H_