]>
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 license
9 ///////////////////////////////////////////////////////////////////////////////
27 // work in multiples of m_allocsize;
28 sz
= (sz
/ m_allocsize
+ 1) * m_allocsize
;
33 void *mem
= realloc(m_objects
, sizeof(void *) * sz
);
35 return false; // failed
37 m_objects
= (void **) mem
;
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;
47 const void *GetItem(int idx
) const
49 wxASSERT(idx
>= 0 && idx
< m_size
);
50 return m_objects
[idx
];
53 void Append(void *obj
)
55 wxASSERT(m_size
< m_capacity
);
56 m_objects
[m_size
] = obj
;
60 void RemoveAt(int idx
)
62 wxASSERT(idx
>= 0 && idx
< m_size
);
66 m_objects
+ idx
* sizeof(void *),
67 m_objects
+ (idx
+ 1) * sizeof(void *),
72 bool copy(const wxVectorBase
& vb
)
75 if (! Alloc(vb
.size()))
78 for (int i
= 0; i
< vb
.size(); i
++)
80 void *o
= vb
.Copy(vb
.GetItem(i
));
90 wxVectorBase() : m_objects(0), m_allocsize(16), m_size(0), m_capacity(0) {}
93 for (int i
= 0; i
< size(); i
++)
97 m_size
= m_capacity
= 0;
104 wxFAIL_MSG( _T("out of memory in wxVector::reserve()") );
123 wxVectorBase
& operator = (const wxVectorBase
& vb
)
131 #define WX_DECLARE_VECTORBASE(obj, cls)\
133 virtual void Free(void *o)\
137 virtual void *Copy(const void *o) const\
139 return new obj(*(obj *) o);\
153 #define WX_DECLARE_VECTOR(obj, cls)\
154 class cls : public wxVectorBase\
156 WX_DECLARE_VECTORBASE(obj, cls);\
158 void push_back(const obj& o)\
160 bool rc = Alloc(size() + 1);\
166 RemoveAt(size() - 1);\
168 const obj& at(int idx) const\
170 return *(obj *) GetItem(idx);\
174 return *(obj *) GetItem(idx);\
176 const obj& operator[](int idx) const\
180 obj& operator[](int idx)\
184 const obj& front() const\
192 const obj& back() const\
194 return at(size() - 1);\
198 return at(size() - 1);\
207 #endif // _WX_VECTOR_H_