]>
Commit | Line | Data |
---|---|---|
3c648a82 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/vector.h | |
3 | // Purpose: STL vector clone | |
4 | // Author: Lindsay Mathieson | |
5 | // Modified by: | |
6 | // Created: 30.07.2001 | |
7 | // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org> | |
65571936 | 8 | // Licence: wxWindows licence |
3c648a82 VZ |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #ifndef _WX_VECTOR_H_ | |
12 | #define _WX_VECTOR_H_ | |
13 | ||
14 | #include "wx/defs.h" | |
15 | ||
bddd7a8d | 16 | class WXDLLIMPEXP_BASE wxVectorBase |
3c648a82 | 17 | { |
2d6c58d6 | 18 | public: |
5fd588d2 | 19 | typedef size_t size_type; |
2d6c58d6 | 20 | private: |
5fd588d2 MB |
21 | size_type m_allocsize; |
22 | size_type m_size, | |
23 | m_capacity; | |
3c648a82 VZ |
24 | void **m_objects; |
25 | ||
26 | protected: | |
5fd588d2 | 27 | bool Alloc(size_type sz) |
3c648a82 VZ |
28 | { |
29 | // work in multiples of m_allocsize; | |
30 | sz = (sz / m_allocsize + 1) * m_allocsize; | |
31 | if (sz <= m_capacity) | |
32 | return true; | |
33 | ||
34 | // try to realloc | |
35 | void *mem = realloc(m_objects, sizeof(void *) * sz); | |
36 | if (! mem) | |
37 | return false; // failed | |
38 | // success | |
39 | m_objects = (void **) mem; | |
40 | m_capacity = sz; | |
41 | return true; | |
1f32f585 | 42 | } |
3c648a82 VZ |
43 | |
44 | // untyped destructor of elements - must be overriden | |
45 | virtual void Free(void *) = 0; | |
46 | // untyped copy constructor of elements - must be overriden | |
47 | virtual void *Copy(const void *) const = 0; | |
48 | ||
5fd588d2 | 49 | const void *GetItem(size_type idx) const |
3c648a82 | 50 | { |
fa776a58 | 51 | wxASSERT(idx < m_size); |
3c648a82 | 52 | return m_objects[idx]; |
1f32f585 | 53 | } |
3c648a82 VZ |
54 | |
55 | void Append(void *obj) | |
56 | { | |
57 | wxASSERT(m_size < m_capacity); | |
58 | m_objects[m_size] = obj; | |
59 | m_size++; | |
1f32f585 | 60 | } |
3c648a82 | 61 | |
5fd588d2 | 62 | void RemoveAt(size_type idx) |
3c648a82 | 63 | { |
fa776a58 | 64 | wxASSERT(idx < m_size); |
3c648a82 VZ |
65 | Free(m_objects[idx]); |
66 | if (idx < m_size - 1) | |
67 | memcpy( | |
5fd588d2 MB |
68 | m_objects + idx, |
69 | m_objects + idx + 1, | |
70 | ( m_size - idx - 1 ) * sizeof(void*) ); | |
3c648a82 | 71 | m_size--; |
1f32f585 | 72 | } |
3c648a82 VZ |
73 | |
74 | bool copy(const wxVectorBase& vb) | |
75 | { | |
76 | clear(); | |
77 | if (! Alloc(vb.size())) | |
78 | return false; | |
79 | ||
5fd588d2 | 80 | for (size_type i = 0; i < vb.size(); i++) |
3c648a82 VZ |
81 | { |
82 | void *o = vb.Copy(vb.GetItem(i)); | |
83 | if (! o) | |
84 | return false; | |
85 | Append(o); | |
1f32f585 | 86 | } |
3c648a82 VZ |
87 | |
88 | return true; | |
1f32f585 | 89 | } |
3c648a82 VZ |
90 | |
91 | public: | |
5fd588d2 MB |
92 | wxVectorBase() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {} |
93 | virtual ~wxVectorBase() {} // calm down GCC | |
94 | ||
3c648a82 VZ |
95 | void clear() |
96 | { | |
5fd588d2 | 97 | for (size_type i = 0; i < size(); i++) |
3c648a82 VZ |
98 | Free(m_objects[i]); |
99 | free(m_objects); | |
100 | m_objects = 0; | |
101 | m_size = m_capacity = 0; | |
1f32f585 | 102 | } |
3c648a82 | 103 | |
5fd588d2 | 104 | void reserve(size_type n) |
3c648a82 VZ |
105 | { |
106 | if ( !Alloc(n) ) | |
107 | { | |
108 | wxFAIL_MSG( _T("out of memory in wxVector::reserve()") ); | |
109 | } | |
1f32f585 | 110 | } |
3c648a82 | 111 | |
5fd588d2 | 112 | size_type size() const |
3c648a82 VZ |
113 | { |
114 | return m_size; | |
115 | } | |
116 | ||
5fd588d2 | 117 | size_type capacity() const |
3c648a82 | 118 | { |
e18e17f9 | 119 | return m_capacity; |
1f32f585 | 120 | } |
3c648a82 VZ |
121 | |
122 | bool empty() const | |
123 | { | |
124 | return size() == 0; | |
1f32f585 | 125 | } |
3c648a82 VZ |
126 | |
127 | wxVectorBase& operator = (const wxVectorBase& vb) | |
128 | { | |
02a48e3b | 129 | wxCHECK(copy(vb), *this); |
3c648a82 VZ |
130 | return *this; |
131 | } | |
132 | }; | |
133 | ||
134 | #define WX_DECLARE_VECTORBASE(obj, cls)\ | |
6f02a879 | 135 | protected:\ |
3c648a82 VZ |
136 | virtual void Free(void *o)\ |
137 | {\ | |
138 | delete (obj *) o;\ | |
1f32f585 | 139 | }\ |
3c648a82 VZ |
140 | virtual void *Copy(const void *o) const\ |
141 | {\ | |
142 | return new obj(*(obj *) o);\ | |
1f32f585 | 143 | }\ |
3c648a82 VZ |
144 | public:\ |
145 | cls() {}\ | |
fa776a58 | 146 | cls(const cls& c) : wxVectorBase()\ |
3c648a82 | 147 | {\ |
02a48e3b | 148 | wxCHECK2(copy(c), return);\ |
3c648a82 VZ |
149 | }\ |
150 | ~cls()\ | |
151 | {\ | |
152 | clear();\ | |
153 | } | |
154 | ||
5fd588d2 MB |
155 | #define _WX_DECLARE_VECTOR(obj, cls, exp)\ |
156 | class exp cls : public wxVectorBase\ | |
3c648a82 | 157 | {\ |
47b378bd | 158 | WX_DECLARE_VECTORBASE(obj, cls)\ |
3c648a82 VZ |
159 | public:\ |
160 | void push_back(const obj& o)\ | |
161 | {\ | |
02a48e3b | 162 | wxCHECK2(Alloc(size() + 1), return);\ |
3c648a82 | 163 | Append(new obj(o));\ |
1f32f585 | 164 | }\ |
3c648a82 VZ |
165 | void pop_back()\ |
166 | {\ | |
167 | RemoveAt(size() - 1);\ | |
1f32f585 | 168 | }\ |
5fd588d2 | 169 | const obj& at(size_type idx) const\ |
3c648a82 VZ |
170 | {\ |
171 | return *(obj *) GetItem(idx);\ | |
1f32f585 | 172 | }\ |
5fd588d2 | 173 | obj& at(size_type idx)\ |
3c648a82 VZ |
174 | {\ |
175 | return *(obj *) GetItem(idx);\ | |
1f32f585 | 176 | }\ |
5fd588d2 | 177 | const obj& operator[](size_type idx) const\ |
3c648a82 VZ |
178 | {\ |
179 | return at(idx);\ | |
1f32f585 | 180 | }\ |
5fd588d2 | 181 | obj& operator[](size_type idx)\ |
3c648a82 VZ |
182 | {\ |
183 | return at(idx);\ | |
1f32f585 | 184 | }\ |
3c648a82 VZ |
185 | const obj& front() const\ |
186 | {\ | |
187 | return at(0);\ | |
1f32f585 | 188 | }\ |
3c648a82 VZ |
189 | obj& front()\ |
190 | {\ | |
191 | return at(0);\ | |
1f32f585 | 192 | }\ |
3c648a82 VZ |
193 | const obj& back() const\ |
194 | {\ | |
195 | return at(size() - 1);\ | |
1f32f585 | 196 | }\ |
3c648a82 VZ |
197 | obj& back()\ |
198 | {\ | |
199 | return at(size() - 1);\ | |
1f32f585 | 200 | }\ |
5fd588d2 | 201 | size_type erase(size_type idx)\ |
3c648a82 VZ |
202 | {\ |
203 | RemoveAt(idx);\ | |
204 | return idx;\ | |
1f32f585 | 205 | }\ |
3c648a82 VZ |
206 | } |
207 | ||
5fd588d2 MB |
208 | #define WX_DECLARE_VECTOR(obj, cls) \ |
209 | _WX_DECLARE_VECTOR(obj, cls, WXDLLEXPORT) | |
210 | ||
3c648a82 VZ |
211 | #endif // _WX_VECTOR_H_ |
212 |