Removed erroneous copyright names and corrected licence spelling
[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 licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_VECTOR_H_
12 #define _WX_VECTOR_H_
13
14 #include "wx/defs.h"
15
16 class WXDLLEXPORT wxVectorBase
17 {
18 public:
19 typedef size_t size_type;
20 private:
21 size_type m_allocsize;
22 size_type m_size,
23 m_capacity;
24 void **m_objects;
25
26 protected:
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
91 public:
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 bool rc = copy(vb);
130 wxASSERT(rc);
131 return *this;
132 }
133 };
134
135 #define WX_DECLARE_VECTORBASE(obj, cls)\
136 private:\
137 virtual void Free(void *o)\
138 {\
139 delete (obj *) o;\
140 };\
141 virtual void *Copy(const void *o) const\
142 {\
143 return new obj(*(obj *) o);\
144 };\
145 public:\
146 cls() {}\
147 cls(const cls& c)\
148 {\
149 bool rc = copy(c);\
150 wxASSERT(rc);\
151 }\
152 ~cls()\
153 {\
154 clear();\
155 }
156
157 #define _WX_DECLARE_VECTOR(obj, cls, exp)\
158 class exp cls : public wxVectorBase\
159 {\
160 WX_DECLARE_VECTORBASE(obj, cls);\
161 public:\
162 void push_back(const obj& o)\
163 {\
164 bool rc = Alloc(size() + 1);\
165 wxASSERT(rc);\
166 Append(new obj(o));\
167 };\
168 void pop_back()\
169 {\
170 RemoveAt(size() - 1);\
171 };\
172 const obj& at(size_type idx) const\
173 {\
174 return *(obj *) GetItem(idx);\
175 };\
176 obj& at(size_type idx)\
177 {\
178 return *(obj *) GetItem(idx);\
179 };\
180 const obj& operator[](size_type idx) const\
181 {\
182 return at(idx);\
183 };\
184 obj& operator[](size_type idx)\
185 {\
186 return at(idx);\
187 };\
188 const obj& front() const\
189 {\
190 return at(0);\
191 };\
192 obj& front()\
193 {\
194 return at(0);\
195 };\
196 const obj& back() const\
197 {\
198 return at(size() - 1);\
199 };\
200 obj& back()\
201 {\
202 return at(size() - 1);\
203 };\
204 size_type erase(size_type idx)\
205 {\
206 RemoveAt(idx);\
207 return idx;\
208 };\
209 }
210
211 #define WX_DECLARE_VECTOR(obj, cls) \
212 _WX_DECLARE_VECTOR(obj, cls, WXDLLEXPORT)
213
214 #endif // _WX_VECTOR_H_
215