]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
adding open and close hand cursor constants, implementation currently OSX only, close...
[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"
2e8fd8cc 16#include "wx/dataobj.h"
3c648a82 17
df4aed1c 18#if wxUSE_STL
e966f815 19
e966f815
VS
20#include <vector>
21#define wxVector std::vector
22
23#else // !wxUSE_STL
24
7df6b37a 25#include "wx/utils.h"
fc31181d 26#include "wx/scopeguard.h"
6712283c
VS
27#include "wx/meta/movable.h"
28#include "wx/meta/if.h"
7df6b37a 29
c157b27e
VS
30#include "wx/beforestd.h"
31#include <new> // for placement new
32#include "wx/afterstd.h"
33
6712283c
VS
34namespace wxPrivate
35{
36
37// These templates encapsulate memory operations for use by wxVector; there are
38// two implementations, both in generic way for any C++ types and as an
39// optimized version for "movable" types that uses realloc() and memmove().
40
41// version for movable types:
42template<typename T>
43struct wxVectorMemOpsMovable
44{
45 static void Free(T* array)
46 { free(array); }
47
48 static T* Realloc(T* old, size_t newCapacity, size_t WXUNUSED(occupiedSize))
49 { return (T*)realloc(old, newCapacity * sizeof(T)); }
50
51 static void MemmoveBackward(T* dest, T* source, size_t count)
52 { memmove(dest, source, count * sizeof(T)); }
53
54 static void MemmoveForward(T* dest, T* source, size_t count)
55 { memmove(dest, source, count * sizeof(T)); }
56};
57
58// generic version for non-movable types:
59template<typename T>
60struct wxVectorMemOpsGeneric
61{
62 static void Free(T* array)
63 { ::operator delete(array); }
64
65 static T* Realloc(T* old, size_t newCapacity, size_t occupiedSize)
66 {
67 T *mem = (T*)::operator new(newCapacity * sizeof(T));
68 for ( size_t i = 0; i < occupiedSize; i++ )
69 {
54742e34 70 ::new(mem + i) T(old[i]);
6712283c
VS
71 old[i].~T();
72 }
73 ::operator delete(old);
74 return mem;
75 }
76
77 static void MemmoveBackward(T* dest, T* source, size_t count)
78 {
79 wxASSERT( dest < source );
80 T* destptr = dest;
81 T* sourceptr = source;
82 for ( size_t i = count; i > 0; --i, ++destptr, ++sourceptr )
83 {
0e82d270 84 ::new(destptr) T(*sourceptr);
6712283c
VS
85 sourceptr->~T();
86 }
87 }
88
89 static void MemmoveForward(T* dest, T* source, size_t count)
90 {
91 wxASSERT( dest > source );
92 T* destptr = dest + count - 1;
93 T* sourceptr = source + count - 1;
94 for ( size_t i = count; i > 0; --i, --destptr, --sourceptr )
95 {
0e82d270 96 ::new(destptr) T(*sourceptr);
6712283c
VS
97 sourceptr->~T();
98 }
99 }
100};
101
102
103} // namespace wxPrivate
104
e966f815
VS
105template<typename T>
106class wxVector
3c648a82 107{
c9faa9e9 108private:
0c73d133 109 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
c9faa9e9
VS
110 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
111 //
112 // Note that we use typedef instead of privately deriving from this (which
113 // would allowed us to omit "Ops::" prefixes below) to keep VC6 happy,
114 // it can't compile code that derives from wxIf<...>::value.
115 typedef typename wxIf< wxIsMovable<T>::value,
116 wxPrivate::wxVectorMemOpsMovable<T>,
117 wxPrivate::wxVectorMemOpsGeneric<T> >::value
118 Ops;
119
2d6c58d6 120public:
5fd588d2 121 typedef size_t size_type;
946954d3 122 typedef size_t difference_type;
e966f815 123 typedef T value_type;
946954d3 124 typedef value_type* pointer;
df4aed1c 125 typedef value_type* iterator;
0516de2c 126 typedef const value_type* const_iterator;
df4aed1c 127 typedef value_type& reference;
e966f815 128
946954d3
RR
129 class reverse_iterator
130 {
131 public:
132 reverse_iterator() : m_ptr(NULL) { }
133 wxEXPLICIT reverse_iterator(iterator it) : m_ptr(it) { }
134 reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
deb0c402 135
946954d3
RR
136 reference operator*() const { return *m_ptr; }
137 pointer operator->() const { return m_ptr; }
deb0c402 138
946954d3 139 iterator base() const { return m_ptr; }
deb0c402
VZ
140
141 reverse_iterator& operator++()
142 { --m_ptr; return *this; }
946954d3 143 reverse_iterator operator++(int)
deb0c402
VZ
144 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
145 reverse_iterator& operator--()
146 { ++m_ptr; return *this; }
147 reverse_iterator operator--(int)
148 { reverse_iterator tmp = *this; ++m_ptr; return tmp; }
149
946954d3 150 reverse_iterator operator+(difference_type n) const
deb0c402 151 { return reverse_iterator(m_ptr - n); }
946954d3 152 reverse_iterator& operator+=(difference_type n)
deb0c402 153 { m_ptr -= n; return *this; }
946954d3 154 reverse_iterator operator-(difference_type n) const
deb0c402 155 { return reverse_iterator(m_ptr + n); }
946954d3 156 reverse_iterator& operator-=(difference_type n)
deb0c402
VZ
157 { m_ptr += n; return *this; }
158
946954d3 159 reference operator[](difference_type n) const
deb0c402
VZ
160 { return *(*this + n); }
161
162 bool operator ==(const reverse_iterator& it) const
163 { return m_ptr == it.m_ptr; }
946954d3 164 bool operator !=(const reverse_iterator& it) const
deb0c402
VZ
165 { return m_ptr != it.m_ptr; }
166
946954d3
RR
167 private:
168 value_type *m_ptr;
169 };
170
0516de2c 171 wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
e966f815 172
664e5ff9 173 wxVector(size_type size)
e068310a 174 : m_size(0), m_capacity(0), m_values(NULL)
664e5ff9
VZ
175 {
176 reserve(size);
177 for ( size_t n = 0; n < size; n++ )
178 push_back(value_type());
179 }
180
181 wxVector(size_type size, const value_type& v)
e068310a 182 : m_size(0), m_capacity(0), m_values(NULL)
664e5ff9 183 {
8d00e347 184 reserve(size);
664e5ff9
VZ
185 for ( size_t n = 0; n < size; n++ )
186 push_back(v);
187 }
188
a470957a 189 wxVector(const wxVector& c) : m_size(0), m_capacity(0), m_values(NULL)
e966f815 190 {
0516de2c 191 Copy(c);
e966f815
VS
192 }
193
194 ~wxVector()
195 {
196 clear();
197 }
198
199 void clear()
200 {
c157b27e
VS
201 // call destructors of stored objects:
202 for ( size_type i = 0; i < m_size; i++ )
203 {
82b6efd2 204 m_values[i].~T();
c157b27e
VS
205 }
206
c9faa9e9 207 Ops::Free(m_values);
0516de2c 208 m_values = NULL;
a470957a
VZ
209 m_size =
210 m_capacity = 0;
e966f815
VS
211 }
212
213 void reserve(size_type n)
214 {
0516de2c
VS
215 if ( n <= m_capacity )
216 return;
217
218 // increase the size twice, unless we're already too big or unless
219 // more is requested
f5851311 220 //
c157b27e 221 // NB: casts to size_type are needed to suppress mingw32 warnings about
f5851311
VZ
222 // mixing enums and ints in the same expression
223 const size_type increment = m_size > 0
224 ? wxMin(m_size, (size_type)ALLOC_MAX_SIZE)
225 : (size_type)ALLOC_INITIAL_SIZE;
0516de2c
VS
226 if ( m_capacity + increment > n )
227 n = m_capacity + increment;
228
c9faa9e9 229 m_values = Ops::Realloc(m_values, n * sizeof(value_type), m_size);
0516de2c 230 m_capacity = n;
e966f815
VS
231 }
232
e068310a
VZ
233 void resize(size_type n)
234 {
235 if ( n < m_size )
236 Shrink(n);
237 else if ( n > m_size )
238 Extend(n, value_type());
239 }
240
241 void resize(size_type n, const value_type& v)
242 {
243 if ( n < m_size )
244 Shrink(n);
245 else if ( n > m_size )
246 Extend(n, v);
247 }
248
e966f815
VS
249 size_type size() const
250 {
251 return m_size;
252 }
253
254 size_type capacity() const
255 {
256 return m_capacity;
257 }
258
259 bool empty() const
260 {
261 return size() == 0;
262 }
263
264 wxVector& operator=(const wxVector& vb)
265 {
a09307ab
PC
266 if (this != &vb)
267 {
268 clear();
269 Copy(vb);
270 }
e966f815
VS
271 return *this;
272 }
273
0516de2c 274 void push_back(const value_type& v)
e966f815 275 {
0516de2c 276 reserve(size() + 1);
c157b27e
VS
277
278 // use placement new to initialize new object in preallocated place in
279 // m_values and store 'v' in it:
280 void* const place = m_values + m_size;
54742e34 281 ::new(place) value_type(v);
c157b27e
VS
282
283 // only increase m_size if the ctor didn't throw an exception; notice
284 // that if it _did_ throw, everything is OK, because we only increased
285 // vector's capacity so far and possibly written some data to
286 // uninitialized memory at the end of m_values
287 m_size++;
e966f815
VS
288 }
289
290 void pop_back()
291 {
0516de2c 292 erase(end() - 1);
e966f815
VS
293 }
294
295 const value_type& at(size_type idx) const
296 {
297 wxASSERT(idx < m_size);
0516de2c 298 return m_values[idx];
e966f815
VS
299 }
300
301 value_type& at(size_type idx)
302 {
303 wxASSERT(idx < m_size);
0516de2c 304 return m_values[idx];
e966f815 305 }
3c648a82 306
e966f815
VS
307 const value_type& operator[](size_type idx) const { return at(idx); }
308 value_type& operator[](size_type idx) { return at(idx); }
309 const value_type& front() const { return at(0); }
310 value_type& front() { return at(0); }
311 const value_type& back() const { return at(size() - 1); }
312 value_type& back() { return at(size() - 1); }
313
0516de2c
VS
314 const_iterator begin() const { return m_values; }
315 iterator begin() { return m_values; }
316 const_iterator end() const { return m_values + size(); }
317 iterator end() { return m_values + size(); }
df4aed1c 318
946954d3
RR
319 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
320 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
321
0516de2c 322 iterator insert(iterator it, const value_type& v = value_type())
f631cd8e 323 {
c157b27e
VS
324 // NB: this must be done before reserve(), because reserve()
325 // invalidates iterators!
326 const size_t idx = it - begin();
327 const size_t after = end() - it;
f631cd8e 328
0516de2c 329 reserve(size() + 1);
9cf33372 330
fc31181d
VZ
331 // the place where the new element is going to be inserted
332 value_type * const place = m_values + idx;
333
c157b27e 334 // unless we're inserting at the end, move following elements out of
0516de2c 335 // the way:
c157b27e 336 if ( after > 0 )
fc31181d 337 Ops::MemmoveForward(place + 1, place, after);
c157b27e 338
fc31181d
VZ
339 // if the ctor called below throws an exception, we need to move all
340 // the elements back to their original positions in m_values
341 wxScopeGuard moveBack = wxMakeGuard(
342 Ops::MemmoveBackward, place, place + 1, after);
343 if ( !after )
344 moveBack.Dismiss();
345
346 // use placement new to initialize new object in preallocated place in
347 // m_values and store 'v' in it:
0e82d270 348 ::new(place) value_type(v);
e966f815 349
fc31181d
VZ
350 // now that we did successfully add the new element, increment the size
351 // and disable moving the items back
352 moveBack.Dismiss();
0516de2c 353 m_size++;
3c648a82 354
0516de2c 355 return begin() + idx;
1f32f585 356 }
3c648a82 357
0516de2c 358 iterator erase(iterator it)
3c648a82 359 {
0516de2c 360 return erase(it, it + 1);
1f32f585 361 }
3c648a82 362
0516de2c 363 iterator erase(iterator first, iterator last)
df4aed1c 364 {
0516de2c
VS
365 if ( first == last )
366 return first;
367 wxASSERT( first < end() && last <= end() );
f631cd8e 368
c157b27e
VS
369 const size_type idx = first - begin();
370 const size_type count = last - first;
371 const size_type after = end() - last;
df4aed1c 372
c157b27e
VS
373 // erase elements by calling their destructors:
374 for ( iterator i = first; i < last; ++i )
6712283c 375 i->~T();
0516de2c 376
c157b27e
VS
377 // once that's done, move following elements over to the freed space:
378 if ( after > 0 )
379 {
c9faa9e9 380 Ops::MemmoveBackward(m_values + idx, m_values + idx + count, after);
c157b27e 381 }
f631cd8e 382
df4aed1c 383 m_size -= count;
0516de2c 384
c157b27e 385 return begin() + idx;
df4aed1c 386 }
0516de2c
VS
387
388#if WXWIN_COMPATIBILITY_2_8
389 wxDEPRECATED( size_type erase(size_type n) );
390#endif // WXWIN_COMPATIBILITY_2_8
391
392private:
f9bf06ac
VS
393 // VC6 can't compile static const int members
394 enum { ALLOC_INITIAL_SIZE = 16 };
395 enum { ALLOC_MAX_SIZE = 4096 };
0516de2c
VS
396
397 void Copy(const wxVector& vb)
3c648a82 398 {
0516de2c 399 reserve(vb.size());
3c648a82 400
0516de2c
VS
401 for ( const_iterator i = vb.begin(); i != vb.end(); ++i )
402 push_back(*i);
1f32f585 403 }
3c648a82 404
e966f815 405private:
e068310a
VZ
406 void Shrink(size_type n)
407 {
408 for ( size_type i = n; i < m_size; i++ )
409 m_values[i].~T();
410 m_size = n;
411 }
412
413 void Extend(size_type n, const value_type& v)
414 {
415 reserve(n);
416 for ( size_type i = m_size; i < n; i++ )
417 push_back(v);
418 }
419
e966f815
VS
420 size_type m_size,
421 m_capacity;
0516de2c 422 value_type *m_values;
3c648a82
VZ
423};
424
9cf33372
VS
425#if WXWIN_COMPATIBILITY_2_8
426template<typename T>
32aa5bda 427inline typename wxVector<T>::size_type wxVector<T>::erase(size_type n)
9cf33372 428{
0516de2c 429 erase(begin() + n);
9cf33372
VS
430 return n;
431}
432#endif // WXWIN_COMPATIBILITY_2_8
433
e966f815 434#endif // wxUSE_STL/!wxUSE_STL
5fd588d2 435
e966f815
VS
436#if WXWIN_COMPATIBILITY_2_8
437 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
438 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
439 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
440#endif // WXWIN_COMPATIBILITY_2_8
3c648a82 441
e966f815 442#endif // _WX_VECTOR_H_