]> git.saurik.com Git - wxWidgets.git/blame - include/wx/vector.h
support for using DIBs for wxBitmap implementation (patch 649866)
[wxWidgets.git] / include / wx / vector.h
CommitLineData
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>
8// Licence: wxWindows license
9///////////////////////////////////////////////////////////////////////////////
10
11#ifndef _WX_VECTOR_H_
12#define _WX_VECTOR_H_
13
14#include "wx/defs.h"
15
5fd588d2 16class WXDLLEXPORT wxVectorBase
3c648a82 17{
2d6c58d6 18public:
5fd588d2 19 typedef size_t size_type;
2d6c58d6 20private:
5fd588d2
MB
21 size_type m_allocsize;
22 size_type m_size,
23 m_capacity;
3c648a82
VZ
24 void **m_objects;
25
26protected:
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;
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
5fd588d2 49 const void *GetItem(size_type idx) const
3c648a82
VZ
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
5fd588d2 62 void RemoveAt(size_type idx)
3c648a82
VZ
63 {
64 wxASSERT(idx >= 0 && idx < m_size);
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
VZ
71 m_size--;
72 };
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);
86 };
87
88 return true;
89 };
90
91public:
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;
102 };
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 }
110 };
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;
3c648a82
VZ
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)\
136private:\
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 };\
145public:\
146 cls() {}\
147 cls(const cls& c)\
148 {\
149 bool rc = copy(c);\
150 wxASSERT(rc);\
151 }\
152 ~cls()\
153 {\
154 clear();\
155 }
156
5fd588d2
MB
157#define _WX_DECLARE_VECTOR(obj, cls, exp)\
158class exp cls : public wxVectorBase\
3c648a82 159{\
e18e17f9 160 WX_DECLARE_VECTORBASE(obj, cls);\
3c648a82
VZ
161public:\
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 };\
5fd588d2 172 const obj& at(size_type idx) const\
3c648a82
VZ
173 {\
174 return *(obj *) GetItem(idx);\
175 };\
5fd588d2 176 obj& at(size_type idx)\
3c648a82
VZ
177 {\
178 return *(obj *) GetItem(idx);\
179 };\
5fd588d2 180 const obj& operator[](size_type idx) const\
3c648a82
VZ
181 {\
182 return at(idx);\
183 };\
5fd588d2 184 obj& operator[](size_type idx)\
3c648a82
VZ
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 };\
5fd588d2 204 size_type erase(size_type idx)\
3c648a82
VZ
205 {\
206 RemoveAt(idx);\
207 return idx;\
208 };\
209}
210
5fd588d2
MB
211#define WX_DECLARE_VECTOR(obj, cls) \
212 _WX_DECLARE_VECTOR(obj, cls, WXDLLEXPORT)
213
3c648a82
VZ
214#endif // _WX_VECTOR_H_
215