]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
use HandleWindowEvent() in ProcessCommand(); return the value this function returns...
[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
7df6b37a 24#include "wx/utils.h"
6712283c
VS
25#include "wx/meta/movable.h"
26#include "wx/meta/if.h"
7df6b37a 27
c157b27e
VS
28#include "wx/beforestd.h"
29#include <new> // for placement new
30#include "wx/afterstd.h"
31
6712283c
VS
32namespace wxPrivate
33{
34
35// These templates encapsulate memory operations for use by wxVector; there are
36// two implementations, both in generic way for any C++ types and as an
37// optimized version for "movable" types that uses realloc() and memmove().
38
39// version for movable types:
40template<typename T>
41struct wxVectorMemOpsMovable
42{
43 static void Free(T* array)
44 { free(array); }
45
46 static T* Realloc(T* old, size_t newCapacity, size_t WXUNUSED(occupiedSize))
47 { return (T*)realloc(old, newCapacity * sizeof(T)); }
48
49 static void MemmoveBackward(T* dest, T* source, size_t count)
50 { memmove(dest, source, count * sizeof(T)); }
51
52 static void MemmoveForward(T* dest, T* source, size_t count)
53 { memmove(dest, source, count * sizeof(T)); }
54};
55
56// generic version for non-movable types:
57template<typename T>
58struct wxVectorMemOpsGeneric
59{
60 static void Free(T* array)
61 { ::operator delete(array); }
62
63 static T* Realloc(T* old, size_t newCapacity, size_t occupiedSize)
64 {
65 T *mem = (T*)::operator new(newCapacity * sizeof(T));
66 for ( size_t i = 0; i < occupiedSize; i++ )
67 {
68 new(mem + i) T(old[i]);
69 old[i].~T();
70 }
71 ::operator delete(old);
72 return mem;
73 }
74
75 static void MemmoveBackward(T* dest, T* source, size_t count)
76 {
77 wxASSERT( dest < source );
78 T* destptr = dest;
79 T* sourceptr = source;
80 for ( size_t i = count; i > 0; --i, ++destptr, ++sourceptr )
81 {
82 new(destptr) T(*sourceptr);
83 sourceptr->~T();
84 }
85 }
86
87 static void MemmoveForward(T* dest, T* source, size_t count)
88 {
89 wxASSERT( dest > source );
90 T* destptr = dest + count - 1;
91 T* sourceptr = source + count - 1;
92 for ( size_t i = count; i > 0; --i, --destptr, --sourceptr )
93 {
94 new(destptr) T(*sourceptr);
95 sourceptr->~T();
96 }
97 }
98};
99
100
101} // namespace wxPrivate
102
e966f815
VS
103template<typename T>
104class wxVector
6712283c
VS
105 // this cryptic expression means "derive from wxVectorMemOpsMovable if
106 // type T is movable type, otherwise derive from wxVectorMemOpsGeneric
107 : private wxIf< wxIsMovable<T>::value,
108 wxPrivate::wxVectorMemOpsMovable<T>,
109 wxPrivate::wxVectorMemOpsGeneric<T> >::value
3c648a82 110{
2d6c58d6 111public:
5fd588d2 112 typedef size_t size_type;
e966f815 113 typedef T value_type;
df4aed1c 114 typedef value_type* iterator;
0516de2c 115 typedef const value_type* const_iterator;
df4aed1c 116 typedef value_type& reference;
e966f815 117
0516de2c 118 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
e966f815
VS
119
120 wxVector(const wxVector& c)
121 {
0516de2c 122 Copy(c);
e966f815
VS
123 }
124
125 ~wxVector()
126 {
127 clear();
128 }
129
130 void clear()
131 {
c157b27e
VS
132 // call destructors of stored objects:
133 for ( size_type i = 0; i < m_size; i++ )
134 {
82b6efd2 135 m_values[i].~T();
c157b27e
VS
136 }
137
6712283c 138 Free(m_values);
0516de2c 139 m_values = NULL;
e966f815
VS
140 m_size = m_capacity = 0;
141 }
142
143 void reserve(size_type n)
144 {
0516de2c
VS
145 if ( n <= m_capacity )
146 return;
147
148 // increase the size twice, unless we're already too big or unless
149 // more is requested
f5851311 150 //
c157b27e 151 // NB: casts to size_type are needed to suppress mingw32 warnings about
f5851311
VZ
152 // mixing enums and ints in the same expression
153 const size_type increment = m_size > 0
154 ? wxMin(m_size, (size_type)ALLOC_MAX_SIZE)
155 : (size_type)ALLOC_INITIAL_SIZE;
0516de2c
VS
156 if ( m_capacity + increment > n )
157 n = m_capacity + increment;
158
6712283c 159 m_values = Realloc(m_values, n * sizeof(value_type), m_size);
0516de2c 160 m_capacity = n;
e966f815
VS
161 }
162
163 size_type size() const
164 {
165 return m_size;
166 }
167
168 size_type capacity() const
169 {
170 return m_capacity;
171 }
172
173 bool empty() const
174 {
175 return size() == 0;
176 }
177
178 wxVector& operator=(const wxVector& vb)
179 {
0516de2c 180 Copy(vb);
e966f815
VS
181 return *this;
182 }
183
0516de2c 184 void push_back(const value_type& v)
e966f815 185 {
0516de2c 186 reserve(size() + 1);
c157b27e
VS
187
188 // use placement new to initialize new object in preallocated place in
189 // m_values and store 'v' in it:
190 void* const place = m_values + m_size;
191 new(place) value_type(v);
192
193 // only increase m_size if the ctor didn't throw an exception; notice
194 // that if it _did_ throw, everything is OK, because we only increased
195 // vector's capacity so far and possibly written some data to
196 // uninitialized memory at the end of m_values
197 m_size++;
e966f815
VS
198 }
199
200 void pop_back()
201 {
0516de2c 202 erase(end() - 1);
e966f815
VS
203 }
204
205 const value_type& at(size_type idx) const
206 {
207 wxASSERT(idx < m_size);
0516de2c 208 return m_values[idx];
e966f815
VS
209 }
210
211 value_type& at(size_type idx)
212 {
213 wxASSERT(idx < m_size);
0516de2c 214 return m_values[idx];
e966f815 215 }
3c648a82 216
e966f815
VS
217 const value_type& operator[](size_type idx) const { return at(idx); }
218 value_type& operator[](size_type idx) { return at(idx); }
219 const value_type& front() const { return at(0); }
220 value_type& front() { return at(0); }
221 const value_type& back() const { return at(size() - 1); }
222 value_type& back() { return at(size() - 1); }
223
0516de2c
VS
224 const_iterator begin() const { return m_values; }
225 iterator begin() { return m_values; }
226 const_iterator end() const { return m_values + size(); }
227 iterator end() { return m_values + size(); }
df4aed1c 228
0516de2c 229 iterator insert(iterator it, const value_type& v = value_type())
f631cd8e 230 {
c157b27e
VS
231 // NB: this must be done before reserve(), because reserve()
232 // invalidates iterators!
233 const size_t idx = it - begin();
234 const size_t after = end() - it;
f631cd8e 235
0516de2c 236 reserve(size() + 1);
9cf33372 237
c157b27e 238 // unless we're inserting at the end, move following elements out of
0516de2c 239 // the way:
c157b27e
VS
240 if ( after > 0 )
241 {
6712283c 242 MemmoveForward(m_values + idx + 1, m_values + idx, after);
c157b27e
VS
243 }
244
245#if wxUSE_EXCEPTIONS
246 try
247 {
248#endif
249 // use placement new to initialize new object in preallocated place
250 // in m_values and store 'v' in it:
251 void* const place = m_values + idx;
252 new(place) value_type(v);
253#if wxUSE_EXCEPTIONS
254 }
255 catch ( ... )
256 {
257 // if the ctor threw an exception, we need to move all the elements
258 // back to their original positions in m_values
259 if ( after > 0 )
260 {
6712283c 261 MemmoveBackward(m_values + idx, m_values + idx + 1, after);
c157b27e
VS
262 }
263
264 throw; // rethrow the exception
265 }
266#endif // wxUSE_EXCEPTIONS
e966f815 267
c157b27e
VS
268 // increment m_size only if ctor didn't throw -- if it did, we'll be
269 // left with m_values larger than necessary, but number of elements will
270 // be the same
0516de2c 271 m_size++;
3c648a82 272
0516de2c 273 return begin() + idx;
1f32f585 274 }
3c648a82 275
0516de2c 276 iterator erase(iterator it)
3c648a82 277 {
0516de2c 278 return erase(it, it + 1);
1f32f585 279 }
3c648a82 280
0516de2c 281 iterator erase(iterator first, iterator last)
df4aed1c 282 {
0516de2c
VS
283 if ( first == last )
284 return first;
285 wxASSERT( first < end() && last <= end() );
f631cd8e 286
c157b27e
VS
287 const size_type idx = first - begin();
288 const size_type count = last - first;
289 const size_type after = end() - last;
df4aed1c 290
c157b27e
VS
291 // erase elements by calling their destructors:
292 for ( iterator i = first; i < last; ++i )
6712283c 293 i->~T();
0516de2c 294
c157b27e
VS
295 // once that's done, move following elements over to the freed space:
296 if ( after > 0 )
297 {
6712283c 298 MemmoveBackward(m_values + idx, m_values + idx + count, after);
c157b27e 299 }
f631cd8e 300
df4aed1c 301 m_size -= count;
0516de2c 302
c157b27e 303 return begin() + idx;
df4aed1c 304 }
0516de2c
VS
305
306#if WXWIN_COMPATIBILITY_2_8
307 wxDEPRECATED( size_type erase(size_type n) );
308#endif // WXWIN_COMPATIBILITY_2_8
309
310private:
f9bf06ac
VS
311 // VC6 can't compile static const int members
312 enum { ALLOC_INITIAL_SIZE = 16 };
313 enum { ALLOC_MAX_SIZE = 4096 };
0516de2c
VS
314
315 void Copy(const wxVector& vb)
3c648a82
VZ
316 {
317 clear();
0516de2c 318 reserve(vb.size());
3c648a82 319
0516de2c
VS
320 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
321 push_back(*i);
1f32f585 322 }
3c648a82 323
e966f815 324private:
e966f815
VS
325 size_type m_size,
326 m_capacity;
0516de2c 327 value_type *m_values;
3c648a82
VZ
328};
329
9cf33372
VS
330#if WXWIN_COMPATIBILITY_2_8
331template<typename T>
32aa5bda 332inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
9cf33372 333{
0516de2c 334 erase(begin() + n);
9cf33372
VS
335 return n;
336}
337#endif // WXWIN_COMPATIBILITY_2_8
338
e966f815 339#endif // wxUSE_STL/!wxUSE_STL
5fd588d2 340
e966f815
VS
341#if WXWIN_COMPATIBILITY_2_8
342 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
343 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
344 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
345#endif // WXWIN_COMPATIBILITY_2_8
3c648a82 346
e966f815 347#endif // _WX_VECTOR_H_