]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/vector.h
Fixed to work with the new wxMacControl system.
[wxWidgets.git] / include / wx / vector.h
... / ...
CommitLineData
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>
8// Licence: wxWindows licence
9///////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_VECTOR_H_
12#define _WX_VECTOR_H_
13
14#include "wx/defs.h"
15
16class WXDLLIMPEXP_BASE wxVectorBase
17{
18public:
19 typedef size_t size_type;
20private:
21 size_type m_allocsize;
22 size_type m_size,
23 m_capacity;
24 void **m_objects;
25
26protected:
27 bool Alloc(size_type sz)
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;
42 };
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
49 const void *GetItem(size_type idx) const
50 {
51 wxASSERT(idx >= 0 && idx < m_size);
52 return m_objects[idx];
53 };
54
55 void Append(void *obj)
56 {
57 wxASSERT(m_size < m_capacity);
58 m_objects[m_size] = obj;
59 m_size++;
60 };
61
62 void RemoveAt(size_type idx)
63 {
64 wxASSERT(idx >= 0 && idx < m_size);
65 Free(m_objects[idx]);
66 if (idx < m_size - 1)
67 memcpy(
68 m_objects + idx,
69 m_objects + idx + 1,
70 ( m_size - idx - 1 ) * sizeof(void*) );
71 m_size--;
72 };
73
74 bool copy(const wxVectorBase& vb)
75 {
76 clear();
77 if (! Alloc(vb.size()))
78 return false;
79
80 for (size_type i = 0; i < vb.size(); i++)
81 {
82 void *o = vb.Copy(vb.GetItem(i));
83 if (! o)
84 return false;
85 Append(o);
86 };
87
88 return true;
89 };
90
91public:
92 wxVectorBase() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {}
93 virtual ~wxVectorBase() {} // calm down GCC
94
95 void clear()
96 {
97 for (size_type i = 0; i < size(); i++)
98 Free(m_objects[i]);
99 free(m_objects);
100 m_objects = 0;
101 m_size = m_capacity = 0;
102 };
103
104 void reserve(size_type n)
105 {
106 if ( !Alloc(n) )
107 {
108 wxFAIL_MSG( _T("out of memory in wxVector::reserve()") );
109 }
110 };
111
112 size_type size() const
113 {
114 return m_size;
115 }
116
117 size_type capacity() const
118 {
119 return m_capacity;
120 };
121
122 bool empty() const
123 {
124 return size() == 0;
125 };
126
127 wxVectorBase& operator = (const wxVectorBase& vb)
128 {
129 wxCHECK(copy(vb), *this);
130 return *this;
131 }
132};
133
134#define WX_DECLARE_VECTORBASE(obj, cls)\
135private:\
136 virtual void Free(void *o)\
137 {\
138 delete (obj *) o;\
139 };\
140 virtual void *Copy(const void *o) const\
141 {\
142 return new obj(*(obj *) o);\
143 };\
144public:\
145 cls() {}\
146 cls(const cls& c)\
147 {\
148 wxCHECK2(copy(c), return);\
149 }\
150 ~cls()\
151 {\
152 clear();\
153 }
154
155#define _WX_DECLARE_VECTOR(obj, cls, exp)\
156class exp cls : public wxVectorBase\
157{\
158 WX_DECLARE_VECTORBASE(obj, cls);\
159public:\
160 void push_back(const obj& o)\
161 {\
162 wxCHECK2(Alloc(size() + 1), return);\
163 Append(new obj(o));\
164 };\
165 void pop_back()\
166 {\
167 RemoveAt(size() - 1);\
168 };\
169 const obj& at(size_type idx) const\
170 {\
171 return *(obj *) GetItem(idx);\
172 };\
173 obj& at(size_type idx)\
174 {\
175 return *(obj *) GetItem(idx);\
176 };\
177 const obj& operator[](size_type idx) const\
178 {\
179 return at(idx);\
180 };\
181 obj& operator[](size_type idx)\
182 {\
183 return at(idx);\
184 };\
185 const obj& front() const\
186 {\
187 return at(0);\
188 };\
189 obj& front()\
190 {\
191 return at(0);\
192 };\
193 const obj& back() const\
194 {\
195 return at(size() - 1);\
196 };\
197 obj& back()\
198 {\
199 return at(size() - 1);\
200 };\
201 size_type erase(size_type idx)\
202 {\
203 RemoveAt(idx);\
204 return idx;\
205 };\
206}
207
208#define WX_DECLARE_VECTOR(obj, cls) \
209 _WX_DECLARE_VECTOR(obj, cls, WXDLLEXPORT)
210
211#endif // _WX_VECTOR_H_
212