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