]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: STL vector clone
4 // Author: Lindsay Mathieson
7 // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
16 class WXDLLEXPORT wxVectorBase
19 typedef size_t size_type
;
21 size_type m_allocsize
;
27 bool Alloc(size_type sz
)
29 // work in multiples of m_allocsize;
30 sz
= (sz
/ m_allocsize
+ 1) * m_allocsize
;
35 void *mem
= realloc(m_objects
, sizeof(void *) * sz
);
37 return false; // failed
39 m_objects
= (void **) mem
;
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;
49 const void *GetItem(size_type idx
) const
51 wxASSERT(idx
>= 0 && idx
< m_size
);
52 return m_objects
[idx
];
55 void Append(void *obj
)
57 wxASSERT(m_size
< m_capacity
);
58 m_objects
[m_size
] = obj
;
62 void RemoveAt(size_type idx
)
64 wxASSERT(idx
>= 0 && idx
< m_size
);
70 ( m_size
- idx
- 1 ) * sizeof(void*) );
74 bool copy(const wxVectorBase
& vb
)
77 if (! Alloc(vb
.size()))
80 for (size_type i
= 0; i
< vb
.size(); i
++)
82 void *o
= vb
.Copy(vb
.GetItem(i
));
92 wxVectorBase() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {}
93 virtual ~wxVectorBase() {} // calm down GCC
97 for (size_type i
= 0; i
< size(); i
++)
101 m_size
= m_capacity
= 0;
104 void reserve(size_type n
)
108 wxFAIL_MSG( _T("out of memory in wxVector::reserve()") );
112 size_type
size() const
117 size_type
capacity() const
127 wxVectorBase
& operator = (const wxVectorBase
& vb
)
135 #define WX_DECLARE_VECTORBASE(obj, cls)\
137 virtual void Free(void *o)\
141 virtual void *Copy(const void *o) const\
143 return new obj(*(obj *) o);\
157 #define _WX_DECLARE_VECTOR(obj, cls, exp)\
158 class exp cls : public wxVectorBase\
160 WX_DECLARE_VECTORBASE(obj, cls);\
162 void push_back(const obj& o)\
164 bool rc = Alloc(size() + 1);\
170 RemoveAt(size() - 1);\
172 const obj& at(size_type idx) const\
174 return *(obj *) GetItem(idx);\
176 obj& at(size_type idx)\
178 return *(obj *) GetItem(idx);\
180 const obj& operator[](size_type idx) const\
184 obj& operator[](size_type idx)\
188 const obj& front() const\
196 const obj& back() const\
198 return at(size() - 1);\
202 return at(size() - 1);\
204 size_type erase(size_type idx)\
211 #define WX_DECLARE_VECTOR(obj, cls) \
212 _WX_DECLARE_VECTOR(obj, cls, WXDLLEXPORT)
214 #endif // _WX_VECTOR_H_