]>
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
)
129 wxCHECK(copy(vb
), *this);
134 #define WX_DECLARE_VECTORBASE(obj, cls)\
136 virtual void Free(void *o)\
140 virtual void *Copy(const void *o) const\
142 return new obj(*(obj *) o);\
148 wxCHECK2(copy(c), return);\
155 #define _WX_DECLARE_VECTOR(obj, cls, exp)\
156 class exp cls : public wxVectorBase\
158 WX_DECLARE_VECTORBASE(obj, cls);\
160 void push_back(const obj& o)\
162 wxCHECK2(Alloc(size() + 1), return);\
167 RemoveAt(size() - 1);\
169 const obj& at(size_type idx) const\
171 return *(obj *) GetItem(idx);\
173 obj& at(size_type idx)\
175 return *(obj *) GetItem(idx);\
177 const obj& operator[](size_type idx) const\
181 obj& operator[](size_type idx)\
185 const obj& front() const\
193 const obj& back() const\
195 return at(size() - 1);\
199 return at(size() - 1);\
201 size_type erase(size_type idx)\
208 #define WX_DECLARE_VECTOR(obj, cls) \
209 _WX_DECLARE_VECTOR(obj, cls, WXDLLEXPORT)
211 #endif // _WX_VECTOR_H_