/src/wxwidgets/include/wx/dynarray.h
Line | Count | Source (jump to first uncovered line) |
1 | | /////////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/dynarray.h |
3 | | // Purpose: auto-resizable (i.e. dynamic) array support |
4 | | // Author: Vadim Zeitlin |
5 | | // Created: 12.09.97 |
6 | | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
7 | | // Licence: wxWindows licence |
8 | | /////////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | #ifndef _DYNARRAY_H |
11 | | #define _DYNARRAY_H |
12 | | |
13 | | #include "wx/defs.h" |
14 | | |
15 | | #include "wx/vector.h" |
16 | | |
17 | | #include <initializer_list> |
18 | | |
19 | | /* |
20 | | This header defines legacy dynamic arrays and object arrays (i.e. arrays |
21 | | which own their elements) classes. |
22 | | |
23 | | Do *NOT* use them in the new code, these classes exist for compatibility |
24 | | only. Simply use standard container, e.g. std::vector<>, in your own code. |
25 | | */ |
26 | | |
27 | | #define _WX_ERROR_REMOVE "removing inexistent element in wxArray::Remove" |
28 | | |
29 | | // ---------------------------------------------------------------------------- |
30 | | // types |
31 | | // ---------------------------------------------------------------------------- |
32 | | |
33 | | /* |
34 | | Callback compare function for quick sort. |
35 | | |
36 | | It must return negative value, 0 or positive value if the first item is |
37 | | less than, equal to or greater than the second one. |
38 | | */ |
39 | | extern "C" |
40 | | { |
41 | | typedef int (wxCMPFUNC_CONV *CMPFUNC)(const void* pItem1, const void* pItem2); |
42 | | } |
43 | | |
44 | | // ---------------------------------------------------------------------------- |
45 | | // Array class providing legacy dynamic arrays API on top of wxVector<> |
46 | | // ---------------------------------------------------------------------------- |
47 | | |
48 | | // For some reasons lost in the depths of time, sort functions with different |
49 | | // signatures are used to sort normal arrays and to keep sorted arrays sorted. |
50 | | // These two functors can be used as predicates with std::sort() adapting the |
51 | | // sort function to it, whichever signature it uses. |
52 | | |
53 | | template<class T> |
54 | | class wxArray_SortFunction |
55 | | { |
56 | | public: |
57 | | typedef int (wxCMPFUNC_CONV *CMPFUNC)(T* pItem1, T* pItem2); |
58 | | |
59 | | wxArray_SortFunction(CMPFUNC f) : m_f(f) { } |
60 | | bool operator()(const T& i1, const T& i2) |
61 | 0 | { return m_f(const_cast<T*>(&i1), const_cast<T*>(&i2)) < 0; } |
62 | | private: |
63 | | CMPFUNC m_f; |
64 | | }; |
65 | | |
66 | | template<class T> |
67 | | class wxSortedArray_SortFunction |
68 | | { |
69 | | public: |
70 | | typedef int (wxCMPFUNC_CONV *CMPFUNC)(T, T); |
71 | | |
72 | 0 | wxSortedArray_SortFunction(CMPFUNC f) : m_f(f) { } Unexecuted instantiation: wxSortedArray_SortFunction<wxFileConfigGroup*>::wxSortedArray_SortFunction(int (*)(wxFileConfigGroup*, wxFileConfigGroup*)) Unexecuted instantiation: wxSortedArray_SortFunction<wxFileConfigEntry*>::wxSortedArray_SortFunction(int (*)(wxFileConfigEntry*, wxFileConfigEntry*)) Unexecuted instantiation: wxSortedArray_SortFunction<wxDateTime>::wxSortedArray_SortFunction(int (*)(wxDateTime, wxDateTime)) |
73 | | bool operator()(const T& i1, const T& i2) |
74 | 0 | { return m_f(i1, i2) < 0; } Unexecuted instantiation: wxSortedArray_SortFunction<wxFileConfigGroup*>::operator()(wxFileConfigGroup* const&, wxFileConfigGroup* const&) Unexecuted instantiation: wxSortedArray_SortFunction<wxFileConfigEntry*>::operator()(wxFileConfigEntry* const&, wxFileConfigEntry* const&) Unexecuted instantiation: wxSortedArray_SortFunction<wxDateTime>::operator()(wxDateTime const&, wxDateTime const&) |
75 | | private: |
76 | | CMPFUNC m_f; |
77 | | }; |
78 | | |
79 | | template <typename T, typename Sorter = wxSortedArray_SortFunction<T> > |
80 | | class wxBaseArray : public wxVector<T> |
81 | | { |
82 | | public: |
83 | | typedef typename Sorter::CMPFUNC SCMPFUNC; |
84 | | typedef typename wxArray_SortFunction<T>::CMPFUNC CMPFUNC; |
85 | | |
86 | | typedef wxVector<T> base_vec; |
87 | | |
88 | | typedef typename base_vec::value_type value_type; |
89 | | typedef typename base_vec::reference reference; |
90 | | typedef typename base_vec::const_reference const_reference; |
91 | | typedef typename base_vec::iterator iterator; |
92 | | typedef typename base_vec::const_iterator const_iterator; |
93 | | typedef typename base_vec::const_reverse_iterator const_reverse_iterator; |
94 | | typedef typename base_vec::difference_type difference_type; |
95 | | typedef typename base_vec::size_type size_type; |
96 | | |
97 | | public: |
98 | | typedef T base_type; |
99 | | |
100 | 8 | wxBaseArray() : base_vec() { } wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::wxBaseArray() Line | Count | Source | 100 | 4 | wxBaseArray() : base_vec() { } |
Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::wxBaseArray() Unexecuted instantiation: wxBaseArray<wxTextFileType, wxSortedArray_SortFunction<wxTextFileType> >::wxBaseArray() Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::wxBaseArray() Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::wxBaseArray() wxBaseArray<wxThread*, wxSortedArray_SortFunction<wxThread*> >::wxBaseArray() Line | Count | Source | 100 | 2 | wxBaseArray() : base_vec() { } |
Unexecuted instantiation: wxBaseArray<wxEventTableEntry const*, wxSortedArray_SortFunction<wxEventTableEntry const*> >::wxBaseArray() Unexecuted instantiation: wxBaseArray<int, wxSortedArray_SortFunction<int> >::wxBaseArray() wxBaseArray<wxDateTimeHolidayAuthority*, wxSortedArray_SortFunction<wxDateTimeHolidayAuthority*> >::wxBaseArray() Line | Count | Source | 100 | 2 | wxBaseArray() : base_vec() { } |
Unexecuted instantiation: wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> >::wxBaseArray() Unexecuted instantiation: wxBaseArray<wxFontEncoding, wxSortedArray_SortFunction<wxFontEncoding> >::wxBaseArray() |
101 | | explicit wxBaseArray(size_t n) : base_vec(n) { } |
102 | | wxBaseArray(size_t n, const_reference v) : base_vec(n, v) { } |
103 | | |
104 | | template <class InputIterator> |
105 | | wxBaseArray(InputIterator first, InputIterator last) |
106 | | : base_vec(first, last) |
107 | | { } |
108 | | |
109 | | template<typename U> |
110 | | wxBaseArray(std::initializer_list<U> list) : base_vec(list.begin(), list.end()) {} |
111 | | |
112 | 0 | wxBaseArray(const std::vector<T>& vec) : base_vec(vec) { } |
113 | | wxBaseArray(std::vector<T>&& vec) : base_vec(std::move(vec)) { } |
114 | | |
115 | 0 | void Empty() { this->clear(); } Unexecuted instantiation: wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::Empty() Unexecuted instantiation: wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> >::Empty() |
116 | 0 | void Clear() { this->clear(); } Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::Clear() Unexecuted instantiation: wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::Clear() Unexecuted instantiation: wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> >::Clear() |
117 | | void Alloc(size_t uiSize) { this->reserve(uiSize); } |
118 | 0 | void Shrink() { this->shrink_to_fit(); } |
119 | | |
120 | 0 | size_t GetCount() const { return this->size(); } Unexecuted instantiation: wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::GetCount() const Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::GetCount() const Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::GetCount() const Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::GetCount() const Unexecuted instantiation: wxBaseArray<wxThread*, wxSortedArray_SortFunction<wxThread*> >::GetCount() const Unexecuted instantiation: wxBaseArray<wxEventTableEntry const*, wxSortedArray_SortFunction<wxEventTableEntry const*> >::GetCount() const Unexecuted instantiation: wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> >::GetCount() const |
121 | | void SetCount(size_t n, T v = T()) { this->resize(n, v); } |
122 | 0 | bool IsEmpty() const { return this->empty(); } Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::IsEmpty() const Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::IsEmpty() const Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::IsEmpty() const Unexecuted instantiation: wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::IsEmpty() const |
123 | | size_t Count() const { return this->size(); } |
124 | | |
125 | | T& Item(size_t uiIndex) const |
126 | 0 | { |
127 | 0 | wxASSERT( uiIndex < this->size() ); |
128 | 0 | return const_cast<T&>((*this)[uiIndex]); |
129 | 0 | } |
130 | | |
131 | | T& Last() const { return Item(this->size() - 1); } |
132 | | |
133 | | int Index(T item, bool bFromEnd = false) const |
134 | 0 | { |
135 | 0 | if ( bFromEnd ) |
136 | 0 | { |
137 | 0 | const const_reverse_iterator b = this->rbegin(), |
138 | 0 | e = this->rend(); |
139 | 0 | for ( const_reverse_iterator i = b; i != e; ++i ) |
140 | 0 | if ( *i == item ) |
141 | 0 | return (int)(e - i - 1); |
142 | 0 | } |
143 | 0 | else |
144 | 0 | { |
145 | 0 | const const_iterator b = this->begin(), |
146 | 0 | e = this->end(); |
147 | 0 | for ( const_iterator i = b; i != e; ++i ) |
148 | 0 | if ( *i == item ) |
149 | 0 | return (int)(i - b); |
150 | 0 | } |
151 | | |
152 | 0 | return wxNOT_FOUND; |
153 | 0 | } Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::Index(wxEvtHandler*, bool) const Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::Index(wxFileConfigGroup*, bool) const Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::Index(wxFileConfigEntry*, bool) const Unexecuted instantiation: wxBaseArray<wxFontEncoding, wxSortedArray_SortFunction<wxFontEncoding> >::Index(wxFontEncoding, bool) const Unexecuted instantiation: wxBaseArray<wxThread*, wxSortedArray_SortFunction<wxThread*> >::Index(wxThread*, bool) const Unexecuted instantiation: wxBaseArray<int, wxSortedArray_SortFunction<int> >::Index(int, bool) const |
154 | | |
155 | | int Index(T lItem, SCMPFUNC fnCompare) const |
156 | | { |
157 | | Sorter p(fnCompare); |
158 | | const_iterator i = std::lower_bound(this->begin(), this->end(), lItem, p); |
159 | | return i != this->end() && !p(lItem, *i) ? (int)(i - this->begin()) |
160 | | : wxNOT_FOUND; |
161 | | } |
162 | | |
163 | | size_t IndexForInsert(T lItem, SCMPFUNC fnCompare) const |
164 | 0 | { |
165 | 0 | Sorter p(fnCompare); |
166 | 0 | const_iterator i = std::lower_bound(this->begin(), this->end(), lItem, p); |
167 | 0 | return i - this->begin(); |
168 | 0 | } Unexecuted instantiation: wxBaseArray<wxString, wxSortedArrayString_SortFunction>::IndexForInsert(wxString, int (*)(wxString const&, wxString const&)) const Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::IndexForInsert(wxFileConfigGroup*, int (*)(wxFileConfigGroup*, wxFileConfigGroup*)) const Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::IndexForInsert(wxFileConfigEntry*, int (*)(wxFileConfigEntry*, wxFileConfigEntry*)) const |
169 | | |
170 | | void Add(T lItem, size_t nInsert = 1) |
171 | 0 | { |
172 | 0 | this->insert(this->end(), nInsert, lItem); |
173 | 0 | } Unexecuted instantiation: wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::Add(wxString, unsigned long) Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::Add(wxEvtHandler*, unsigned long) Unexecuted instantiation: wxBaseArray<wxThread*, wxSortedArray_SortFunction<wxThread*> >::Add(wxThread*, unsigned long) Unexecuted instantiation: wxBaseArray<wxEventTableEntry const*, wxSortedArray_SortFunction<wxEventTableEntry const*> >::Add(wxEventTableEntry const*, unsigned long) Unexecuted instantiation: wxBaseArray<int, wxSortedArray_SortFunction<int> >::Add(int, unsigned long) Unexecuted instantiation: wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> >::Add(wxDateTime, unsigned long) |
174 | | |
175 | | size_t Add(T lItem, SCMPFUNC fnCompare) |
176 | 0 | { |
177 | 0 | size_t n = IndexForInsert(lItem, fnCompare); |
178 | 0 | Insert(lItem, n); |
179 | 0 | return n; |
180 | 0 | } Unexecuted instantiation: wxBaseArray<wxString, wxSortedArrayString_SortFunction>::Add(wxString, int (*)(wxString const&, wxString const&)) Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::Add(wxFileConfigGroup*, int (*)(wxFileConfigGroup*, wxFileConfigGroup*)) Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::Add(wxFileConfigEntry*, int (*)(wxFileConfigEntry*, wxFileConfigEntry*)) |
181 | | |
182 | | void Insert(T lItem, size_t uiIndex, size_t nInsert = 1) |
183 | 0 | { |
184 | 0 | this->insert(this->begin() + uiIndex, nInsert, lItem); |
185 | 0 | } Unexecuted instantiation: wxBaseArray<wxString, wxSortedArrayString_SortFunction>::Insert(wxString, unsigned long, unsigned long) Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::Insert(wxFileConfigGroup*, unsigned long, unsigned long) Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::Insert(wxFileConfigEntry*, unsigned long, unsigned long) Unexecuted instantiation: wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::Insert(wxString, unsigned long, unsigned long) |
186 | | |
187 | | void Remove(T lItem) |
188 | 0 | { |
189 | 0 | int n = Index(lItem); |
190 | 0 | wxCHECK_RET( n != wxNOT_FOUND, _WX_ERROR_REMOVE ); |
191 | 0 | RemoveAt((size_t)n); |
192 | 0 | } Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::Remove(wxEvtHandler*) Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::Remove(wxFileConfigGroup*) Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::Remove(wxFileConfigEntry*) Unexecuted instantiation: wxBaseArray<wxThread*, wxSortedArray_SortFunction<wxThread*> >::Remove(wxThread*) |
193 | | |
194 | | void RemoveAt(size_t uiIndex, size_t nRemove = 1) |
195 | 0 | { |
196 | 0 | this->erase(this->begin() + uiIndex, this->begin() + uiIndex + nRemove); |
197 | 0 | } Unexecuted instantiation: wxBaseArray<wxEvtHandler*, wxSortedArray_SortFunction<wxEvtHandler*> >::RemoveAt(unsigned long, unsigned long) Unexecuted instantiation: wxBaseArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::RemoveAt(unsigned long, unsigned long) Unexecuted instantiation: wxBaseArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::RemoveAt(unsigned long, unsigned long) Unexecuted instantiation: wxBaseArray<wxString, wxSortedArray_SortFunction<wxString> >::RemoveAt(unsigned long, unsigned long) Unexecuted instantiation: wxBaseArray<wxThread*, wxSortedArray_SortFunction<wxThread*> >::RemoveAt(unsigned long, unsigned long) |
198 | | |
199 | | void Sort(CMPFUNC fCmp) |
200 | 0 | { |
201 | 0 | wxArray_SortFunction<T> p(fCmp); |
202 | 0 | std::sort(this->begin(), this->end(), p); |
203 | 0 | } |
204 | | |
205 | | void Sort(SCMPFUNC fCmp) |
206 | 0 | { |
207 | 0 | Sorter p(fCmp); |
208 | 0 | std::sort(this->begin(), this->end(), p); |
209 | 0 | } |
210 | | }; |
211 | | |
212 | | // ============================================================================ |
213 | | // The private helper macros containing the core of the array classes |
214 | | // ============================================================================ |
215 | | |
216 | | // ---------------------------------------------------------------------------- |
217 | | // _WX_DEFINE_SORTED_TYPEARRAY: sorted array for simple data types |
218 | | // cannot handle types with size greater than pointer because of sorting |
219 | | // ---------------------------------------------------------------------------- |
220 | | |
221 | | // Note that "classdecl" here is intentionally not used because this class has |
222 | | // only inline methods and so never needs to be exported from a DLL. |
223 | | #define _WX_DEFINE_SORTED_TYPEARRAY_2(T, name, base, defcomp, classdecl) \ |
224 | | typedef wxBaseSortedArray<T> wxBaseSortedArrayFor##name; \ |
225 | | class name : public wxBaseSortedArrayFor##name \ |
226 | | { \ |
227 | | public: \ |
228 | | name(wxBaseSortedArrayFor##name::SCMPFUNC fn defcomp) \ |
229 | 0 | : wxBaseSortedArrayFor##name(fn) { } \ Unexecuted instantiation: ArrayEntries::ArrayEntries(int (*)(wxFileConfigEntry*, wxFileConfigEntry*)) Unexecuted instantiation: ArrayGroups::ArrayGroups(int (*)(wxFileConfigGroup*, wxFileConfigGroup*)) |
230 | | } |
231 | | |
232 | | |
233 | | template <typename T, typename Sorter = wxSortedArray_SortFunction<T> > |
234 | | class wxBaseSortedArray : public wxBaseArray<T, Sorter> |
235 | | { |
236 | | public: |
237 | | typedef typename Sorter::CMPFUNC SCMPFUNC; |
238 | | |
239 | 0 | explicit wxBaseSortedArray(SCMPFUNC fn) : m_fnCompare(fn) { } Unexecuted instantiation: wxBaseSortedArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::wxBaseSortedArray(int (*)(wxFileConfigEntry*, wxFileConfigEntry*)) Unexecuted instantiation: wxBaseSortedArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::wxBaseSortedArray(int (*)(wxFileConfigGroup*, wxFileConfigGroup*)) |
240 | | |
241 | | size_t IndexForInsert(T item) const |
242 | | { |
243 | | return this->wxBaseArray<T, Sorter>::IndexForInsert(item, m_fnCompare); |
244 | | } |
245 | | |
246 | | void AddAt(T item, size_t index) |
247 | | { |
248 | | this->insert(this->begin() + index, item); |
249 | | } |
250 | | |
251 | | size_t Add(T item) |
252 | 0 | { |
253 | 0 | return this->wxBaseArray<T, Sorter>::Add(item, m_fnCompare); |
254 | 0 | } Unexecuted instantiation: wxBaseSortedArray<wxString, wxSortedArrayString_SortFunction>::Add(wxString) Unexecuted instantiation: wxBaseSortedArray<wxFileConfigGroup*, wxSortedArray_SortFunction<wxFileConfigGroup*> >::Add(wxFileConfigGroup*) Unexecuted instantiation: wxBaseSortedArray<wxFileConfigEntry*, wxSortedArray_SortFunction<wxFileConfigEntry*> >::Add(wxFileConfigEntry*) |
255 | | |
256 | | void push_back(T item) |
257 | | { |
258 | | Add(item); |
259 | | } |
260 | | |
261 | | protected: |
262 | 0 | SCMPFUNC GetCompareFunction() const noexcept { return m_fnCompare; } |
263 | | |
264 | | private: |
265 | | SCMPFUNC m_fnCompare; |
266 | | }; |
267 | | |
268 | | |
269 | | // ---------------------------------------------------------------------------- |
270 | | // _WX_DECLARE_OBJARRAY: an array for pointers to type T with owning semantics |
271 | | // ---------------------------------------------------------------------------- |
272 | | |
273 | | // Trivial default implementation of the traits used by wxBaseObjectArray. |
274 | | // It can only be used if the class T is complete. |
275 | | template <typename T> |
276 | | class wxDefaultBaseObjectArrayTraits |
277 | | { |
278 | | public: |
279 | | wxNODISCARD static T* Clone(const T& value) { return new T{value}; } |
280 | | static void Free(T* p) { delete p; } |
281 | | }; |
282 | | |
283 | | // This class must be able to be declared with incomplete types, so it doesn't |
284 | | // actually use type T in its definition, and relies on a helper template |
285 | | // parameter, which is declared by WX_DECLARE_OBJARRAY() and defined by |
286 | | // WX_DEFINE_OBJARRAY(), for providing a way to create and destroy objects of |
287 | | // type T |
288 | | // |
289 | | // If the class T happens to be complete, Traits can be left unspecified and a |
290 | | // trivial implementation using copy ctor directly is used. |
291 | | template <typename T, typename Traits = wxDefaultBaseObjectArrayTraits<T>> |
292 | | class wxBaseObjectArray : private wxBaseArray<T*> |
293 | | { |
294 | | typedef wxBaseArray<T*> base; |
295 | | |
296 | | public: |
297 | | typedef T value_type; |
298 | | |
299 | | typedef int (wxCMPFUNC_CONV *CMPFUNC)(T **pItem1, T **pItem2); |
300 | | |
301 | | wxBaseObjectArray() |
302 | | { |
303 | | } |
304 | | |
305 | | wxBaseObjectArray(const wxBaseObjectArray& src) : base() |
306 | | { |
307 | | DoCopy(src); |
308 | | } |
309 | | |
310 | | wxBaseObjectArray& operator=(const wxBaseObjectArray& src) |
311 | | { |
312 | | Empty(); |
313 | | DoCopy(src); |
314 | | |
315 | | return *this; |
316 | | } |
317 | | |
318 | | ~wxBaseObjectArray() |
319 | | { |
320 | | Empty(); |
321 | | } |
322 | | |
323 | | void Alloc(size_t count) { base::reserve(count); } |
324 | | void reserve(size_t count) { base::reserve(count); } |
325 | | size_t GetCount() const { return base::size(); } |
326 | | size_t size() const { return base::size(); } |
327 | | bool IsEmpty() const { return base::empty(); } |
328 | | bool empty() const { return base::empty(); } |
329 | | size_t Count() const { return base::size(); } |
330 | | void Shrink() { base::Shrink(); } |
331 | | |
332 | | T& operator[](size_t uiIndex) const |
333 | | { |
334 | | return *base::operator[](uiIndex); |
335 | | } |
336 | | |
337 | | T& Item(size_t uiIndex) const |
338 | | { |
339 | | return *base::operator[](uiIndex); |
340 | | } |
341 | | |
342 | | T& Last() const |
343 | | { |
344 | | return *(base::operator[](size() - 1)); |
345 | | } |
346 | | |
347 | | int Index(const T& item, bool bFromEnd = false) const |
348 | | { |
349 | | if ( bFromEnd ) |
350 | | { |
351 | | if ( size() > 0 ) |
352 | | { |
353 | | size_t ui = size() - 1; |
354 | | do |
355 | | { |
356 | | if ( base::operator[](ui) == &item ) |
357 | | return static_cast<int>(ui); |
358 | | ui--; |
359 | | } |
360 | | while ( ui != 0 ); |
361 | | } |
362 | | } |
363 | | else |
364 | | { |
365 | | for ( size_t ui = 0; ui < size(); ++ui ) |
366 | | { |
367 | | if( base::operator[](ui) == &item ) |
368 | | return static_cast<int>(ui); |
369 | | } |
370 | | } |
371 | | |
372 | | return wxNOT_FOUND; |
373 | | } |
374 | | |
375 | | void Add(const T& item, size_t nInsert = 1) |
376 | | { |
377 | | if ( nInsert == 0 ) |
378 | | return; |
379 | | |
380 | | T* const pItem = Traits::Clone(item); |
381 | | |
382 | | const size_t nOldSize = size(); |
383 | | if ( pItem != nullptr ) |
384 | | base::insert(base::end(), nInsert, pItem); |
385 | | |
386 | | for ( size_t i = 1; i < nInsert; i++ ) |
387 | | base::operator[](nOldSize + i) = Traits::Clone(item); |
388 | | } |
389 | | |
390 | | void Add(const T* pItem) |
391 | | { |
392 | | base::push_back(const_cast<T*>(pItem)); |
393 | | } |
394 | | |
395 | | void push_back(const T* pItem) { Add(pItem); } |
396 | | void push_back(const T& item) { Add(item); } |
397 | | |
398 | | void Insert(const T& item, size_t uiIndex, size_t nInsert = 1) |
399 | | { |
400 | | if ( nInsert == 0 ) |
401 | | return; |
402 | | |
403 | | T* const pItem = Traits::Clone(item); |
404 | | if ( pItem != nullptr ) |
405 | | base::insert(base::begin() + uiIndex, nInsert, pItem); |
406 | | |
407 | | for ( size_t i = 1; i < nInsert; ++i ) |
408 | | base::operator[](uiIndex + i) = Traits::Clone(item); |
409 | | } |
410 | | |
411 | | void Insert(const T* pItem, size_t uiIndex) |
412 | | { |
413 | | base::insert(base::begin() + uiIndex, const_cast<T*>(pItem)); |
414 | | } |
415 | | |
416 | | void Empty() { DoEmpty(); base::clear(); } |
417 | | void Clear() { DoEmpty(); base::clear(); } |
418 | | |
419 | | T* Detach(size_t uiIndex) |
420 | | { |
421 | | T* const p = base::operator[](uiIndex); |
422 | | |
423 | | base::erase(base::begin() + uiIndex); |
424 | | return p; |
425 | | } |
426 | | |
427 | | void RemoveAt(size_t uiIndex, size_t nRemove = 1) |
428 | | { |
429 | | wxCHECK_RET( uiIndex < size(), "bad index in RemoveAt()" ); |
430 | | |
431 | | for ( size_t i = 0; i < nRemove; ++i ) |
432 | | Traits::Free(base::operator[](uiIndex + i)); |
433 | | |
434 | | base::erase(base::begin() + uiIndex, base::begin() + uiIndex + nRemove); |
435 | | } |
436 | | |
437 | | void Sort(CMPFUNC fCmp) { base::Sort(fCmp); } |
438 | | |
439 | | void swap(wxBaseObjectArray& other) { base::swap(other); } |
440 | | |
441 | | // Provide a way to iterate over the stored objects using range-based for. |
442 | | class ObjectIterator |
443 | | { |
444 | | public: |
445 | | using base_iter = typename base::const_iterator; |
446 | | |
447 | | using value_type = T; |
448 | | using reference = T&; |
449 | | using pointer = T*; |
450 | | using difference_type = typename std::iterator_traits<base_iter>::difference_type; |
451 | | using iterator_category = std::random_access_iterator_tag; |
452 | | |
453 | | ObjectIterator() = default; |
454 | | ObjectIterator(base_iter const& it) : m_it(it) { } |
455 | | |
456 | | bool operator==(ObjectIterator const& other) const { return m_it == other.m_it; } |
457 | | bool operator!=(ObjectIterator const& other) const { return m_it != other.m_it; } |
458 | | |
459 | | reference operator*() const { return **m_it; } |
460 | | ObjectIterator& operator++() { ++m_it; return *this; } |
461 | | ObjectIterator& operator--() { --m_it; return *this; } |
462 | | |
463 | | private: |
464 | | typename base::const_iterator m_it; |
465 | | }; |
466 | | |
467 | | wxNODISCARD ObjectIterator begin() const { return base::begin(); } |
468 | | wxNODISCARD ObjectIterator end() const { return base::end(); } |
469 | | |
470 | | private: |
471 | | void DoEmpty() |
472 | | { |
473 | | for ( size_t n = 0; n < size(); ++n ) |
474 | | Traits::Free(base::operator[](n)); |
475 | | } |
476 | | |
477 | | void DoCopy(const wxBaseObjectArray& src) |
478 | | { |
479 | | reserve(src.size()); |
480 | | for ( size_t n = 0; n < src.size(); ++n ) |
481 | | Add(src[n]); |
482 | | } |
483 | | }; |
484 | | |
485 | | // ============================================================================ |
486 | | // The public macros for declaration and definition of the dynamic arrays |
487 | | // ============================================================================ |
488 | | |
489 | | // Please note that for each macro WX_FOO_ARRAY we also have |
490 | | // WX_FOO_EXPORTED_ARRAY and WX_FOO_USER_EXPORTED_ARRAY which are exactly the |
491 | | // same except that they use an additional __declspec(dllexport) or equivalent |
492 | | // under Windows if needed. |
493 | | // |
494 | | // The first (just EXPORTED) macros do it if wxWidgets was compiled as a DLL |
495 | | // and so must be used inside the library. The second kind (USER_EXPORTED) |
496 | | // allow the user code to do it when it wants. This is needed if you have a dll |
497 | | // that wants to export a wxArray daubed with your own import/export goo. |
498 | | // |
499 | | // Finally, you can define the macro below as something special to modify the |
500 | | // arrays defined by a simple WX_FOO_ARRAY as well. By default is empty. |
501 | | #define wxARRAY_DEFAULT_EXPORT |
502 | | |
503 | | // ---------------------------------------------------------------------------- |
504 | | // WX_DECLARE_BASEARRAY(T, name): now is the same as WX_DEFINE_TYPEARRAY() |
505 | | // below, only preserved for compatibility. |
506 | | // ---------------------------------------------------------------------------- |
507 | | |
508 | | #define wxARRAY_DUMMY_BASE |
509 | | |
510 | | #define WX_DECLARE_BASEARRAY(T, name) \ |
511 | | WX_DEFINE_TYPEARRAY(T, name) |
512 | | |
513 | | #define WX_DECLARE_EXPORTED_BASEARRAY(T, name) \ |
514 | | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, WXDLLIMPEXP_CORE) |
515 | | |
516 | | #define WX_DECLARE_USER_EXPORTED_BASEARRAY(T, name, expmode) \ |
517 | | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxARRAY_DUMMY_BASE, class expmode) |
518 | | |
519 | | // ---------------------------------------------------------------------------- |
520 | | // WX_DEFINE_TYPEARRAY(T, name, base) define an array class named "name" |
521 | | // containing the elements of type T. Note that the argument "base" is unused |
522 | | // and is preserved for compatibility only. Also, macros with and without |
523 | | // "_PTR" suffix are identical, and the latter ones are also kept only for |
524 | | // compatibility. |
525 | | // ---------------------------------------------------------------------------- |
526 | | |
527 | | #define WX_DEFINE_TYPEARRAY(T, name, base) \ |
528 | | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class wxARRAY_DEFAULT_EXPORT) |
529 | | |
530 | | #define WX_DEFINE_TYPEARRAY_PTR(T, name, base) \ |
531 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class wxARRAY_DEFAULT_EXPORT) |
532 | | |
533 | | #define WX_DEFINE_EXPORTED_TYPEARRAY(T, name, base) \ |
534 | | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class WXDLLIMPEXP_CORE) |
535 | | |
536 | | #define WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, base) \ |
537 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class WXDLLIMPEXP_CORE) |
538 | | |
539 | | #define WX_DEFINE_USER_EXPORTED_TYPEARRAY(T, name, base, expdecl) \ |
540 | | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, class expdecl) |
541 | | |
542 | | #define WX_DEFINE_USER_EXPORTED_TYPEARRAY_PTR(T, name, base, expdecl) \ |
543 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class expdecl) |
544 | | |
545 | | // This is the only non-trivial macro, which actually defines the array class |
546 | | // with the given name containing the elements of the specified type. |
547 | | // |
548 | | // Note that "name" must be a class and not just a typedef because it can be |
549 | | // (and is) forward declared in the existing code. |
550 | | // |
551 | | // As mentioned above, "base" is unused and so is "classdecl" as this class has |
552 | | // only inline methods and so never needs to be exported from MSW DLLs. |
553 | | // |
554 | | // Note about apparently redundant wxBaseArray##name typedef: this is needed to |
555 | | // avoid clashes between T and symbols defined in wxBaseArray<> scope, e.g. if |
556 | | // we didn't do this, we would have compilation problems with arrays of type |
557 | | // "Item" (which is also the name of a method in wxBaseArray<>). |
558 | | #define WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, classdecl) \ |
559 | | typedef wxBaseArray<T> wxBaseArrayFor##name; \ |
560 | | class name : public wxBaseArrayFor##name \ |
561 | | { \ |
562 | | typedef wxBaseArrayFor##name Base; \ |
563 | | public: \ |
564 | 4 | name() : Base() { } \ Unexecuted instantiation: wxArrayInt::wxArrayInt() wxHolidayAuthoritiesArray::wxHolidayAuthoritiesArray() Line | Count | Source | 564 | 2 | name() : Base() { } \ |
Unexecuted instantiation: wxEvtHandlerArray::wxEvtHandlerArray() Unexecuted instantiation: wxEventTableEntryPointerArray::wxEventTableEntryPointerArray() Unexecuted instantiation: wxArrayLinesType::wxArrayLinesType() Unexecuted instantiation: wxFontEncodingArray::wxFontEncodingArray() wxArrayThread::wxArrayThread() Line | Count | Source | 564 | 2 | name() : Base() { } \ |
Unexecuted instantiation: wxBaseArrayPtrVoid::wxBaseArrayPtrVoid() Unexecuted instantiation: wxBaseArrayChar::wxBaseArrayChar() Unexecuted instantiation: wxBaseArrayShort::wxBaseArrayShort() Unexecuted instantiation: wxBaseArrayInt::wxBaseArrayInt() Unexecuted instantiation: wxBaseArrayLong::wxBaseArrayLong() Unexecuted instantiation: wxBaseArraySizeT::wxBaseArraySizeT() Unexecuted instantiation: wxBaseArrayDouble::wxBaseArrayDouble() Unexecuted instantiation: wxArrayShort::wxArrayShort() Unexecuted instantiation: wxArrayDouble::wxArrayDouble() Unexecuted instantiation: wxArrayLong::wxArrayLong() Unexecuted instantiation: wxArrayPtrVoid::wxArrayPtrVoid() |
565 | 0 | explicit name(size_t n) : Base(n) { } \ Unexecuted instantiation: wxBaseArrayPtrVoid::wxBaseArrayPtrVoid(unsigned long) Unexecuted instantiation: wxBaseArrayChar::wxBaseArrayChar(unsigned long) Unexecuted instantiation: wxBaseArrayShort::wxBaseArrayShort(unsigned long) Unexecuted instantiation: wxBaseArrayInt::wxBaseArrayInt(unsigned long) Unexecuted instantiation: wxBaseArrayLong::wxBaseArrayLong(unsigned long) Unexecuted instantiation: wxBaseArraySizeT::wxBaseArraySizeT(unsigned long) Unexecuted instantiation: wxBaseArrayDouble::wxBaseArrayDouble(unsigned long) Unexecuted instantiation: wxArrayShort::wxArrayShort(unsigned long) Unexecuted instantiation: wxArrayInt::wxArrayInt(unsigned long) Unexecuted instantiation: wxArrayDouble::wxArrayDouble(unsigned long) Unexecuted instantiation: wxArrayLong::wxArrayLong(unsigned long) Unexecuted instantiation: wxArrayPtrVoid::wxArrayPtrVoid(unsigned long) Unexecuted instantiation: wxHolidayAuthoritiesArray::wxHolidayAuthoritiesArray(unsigned long) Unexecuted instantiation: wxEventTableEntryPointerArray::wxEventTableEntryPointerArray(unsigned long) Unexecuted instantiation: wxEvtHandlerArray::wxEvtHandlerArray(unsigned long) Unexecuted instantiation: wxArrayLinesType::wxArrayLinesType(unsigned long) Unexecuted instantiation: wxFontEncodingArray::wxFontEncodingArray(unsigned long) Unexecuted instantiation: wxArrayThread::wxArrayThread(unsigned long) |
566 | 0 | name(size_t n, Base::const_reference v) : Base(n, v) { } \ Unexecuted instantiation: wxBaseArrayPtrVoid::wxBaseArrayPtrVoid(unsigned long, void const* const&) Unexecuted instantiation: wxBaseArrayChar::wxBaseArrayChar(unsigned long, char const&) Unexecuted instantiation: wxBaseArrayShort::wxBaseArrayShort(unsigned long, short const&) Unexecuted instantiation: wxBaseArrayInt::wxBaseArrayInt(unsigned long, int const&) Unexecuted instantiation: wxBaseArrayLong::wxBaseArrayLong(unsigned long, long const&) Unexecuted instantiation: wxBaseArraySizeT::wxBaseArraySizeT(unsigned long, unsigned long const&) Unexecuted instantiation: wxBaseArrayDouble::wxBaseArrayDouble(unsigned long, double const&) Unexecuted instantiation: wxArrayShort::wxArrayShort(unsigned long, short const&) Unexecuted instantiation: wxArrayInt::wxArrayInt(unsigned long, int const&) Unexecuted instantiation: wxArrayDouble::wxArrayDouble(unsigned long, double const&) Unexecuted instantiation: wxArrayLong::wxArrayLong(unsigned long, long const&) Unexecuted instantiation: wxArrayPtrVoid::wxArrayPtrVoid(unsigned long, void* const&) Unexecuted instantiation: wxHolidayAuthoritiesArray::wxHolidayAuthoritiesArray(unsigned long, wxDateTimeHolidayAuthority* const&) Unexecuted instantiation: wxEventTableEntryPointerArray::wxEventTableEntryPointerArray(unsigned long, wxEventTableEntry const* const&) Unexecuted instantiation: wxEvtHandlerArray::wxEvtHandlerArray(unsigned long, wxEvtHandler* const&) Unexecuted instantiation: wxArrayLinesType::wxArrayLinesType(unsigned long, wxTextFileType const&) Unexecuted instantiation: wxFontEncodingArray::wxFontEncodingArray(unsigned long, wxFontEncoding const&) Unexecuted instantiation: wxArrayThread::wxArrayThread(unsigned long, wxThread* const&) |
567 | | template <class InputIterator> \ |
568 | | name(InputIterator first, InputIterator last) : Base(first, last) { } \ |
569 | | template<typename U> \ |
570 | | name(std::initializer_list<U> list) : Base(list.begin(), list.end()) { } \ |
571 | | } |
572 | | |
573 | | |
574 | | #define WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, classdecl) \ |
575 | | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, base, classdecl) |
576 | | |
577 | | // ---------------------------------------------------------------------------- |
578 | | // WX_DEFINE_SORTED_TYPEARRAY: this is the same as the previous macro, but it |
579 | | // defines a sorted array. |
580 | | // |
581 | | // Differences: |
582 | | // 1) it must be given a COMPARE function in ctor which takes 2 items of type |
583 | | // T* and should return -1, 0 or +1 if the first one is less/greater |
584 | | // than/equal to the second one. |
585 | | // 2) the Add() method inserts the item in such was that the array is always |
586 | | // sorted (it uses the COMPARE function) |
587 | | // 3) it has no Sort() method because it's always sorted |
588 | | // 4) Index() method is much faster (the sorted arrays use binary search |
589 | | // instead of linear one), but Add() is slower. |
590 | | // 5) there is no Insert() method because you can't insert an item into the |
591 | | // given position in a sorted array but there is IndexForInsert()/AddAt() |
592 | | // pair which may be used to optimize a common operation of "insert only if |
593 | | // not found" |
594 | | // |
595 | | // Note that you have to specify the comparison function when creating the |
596 | | // objects of this array type. If, as in 99% of cases, the comparison function |
597 | | // is the same for all objects of a class, WX_DEFINE_SORTED_TYPEARRAY_CMP below |
598 | | // is more convenient. |
599 | | // |
600 | | // Summary: use this class when the speed of Index() function is important, use |
601 | | // the normal arrays otherwise. |
602 | | // ---------------------------------------------------------------------------- |
603 | | |
604 | | // we need a macro which expands to nothing to pass correct number of |
605 | | // parameters to a nested macro invocation even when we don't have anything to |
606 | | // pass it |
607 | | #define wxARRAY_EMPTY |
608 | | |
609 | | #define WX_DEFINE_SORTED_TYPEARRAY(T, name, base) \ |
610 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, \ |
611 | | wxARRAY_DEFAULT_EXPORT) |
612 | | |
613 | | #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, base) \ |
614 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, WXDLLIMPEXP_CORE) |
615 | | |
616 | | #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, base, expmode) \ |
617 | | typedef T _wxArray##name; \ |
618 | | _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, \ |
619 | | wxARRAY_EMPTY, class expmode) |
620 | | |
621 | | // ---------------------------------------------------------------------------- |
622 | | // WX_DEFINE_SORTED_TYPEARRAY_CMP: exactly the same as above but the comparison |
623 | | // function is provided by this macro and the objects of this class have a |
624 | | // default constructor which just uses it. |
625 | | // |
626 | | // The arguments are: the element type, the comparison function and the array |
627 | | // name |
628 | | // |
629 | | // NB: this is, of course, how WX_DEFINE_SORTED_TYPEARRAY() should have worked |
630 | | // from the very beginning - unfortunately I didn't think about this earlier |
631 | | // ---------------------------------------------------------------------------- |
632 | | |
633 | | #define WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ |
634 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ |
635 | | wxARRAY_DEFAULT_EXPORT) |
636 | | |
637 | | #define WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base) \ |
638 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ |
639 | | WXDLLIMPEXP_CORE) |
640 | | |
641 | | #define WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, base, \ |
642 | | expmode) \ |
643 | | typedef T _wxArray##name; \ |
644 | | _WX_DEFINE_SORTED_TYPEARRAY_2(_wxArray##name, name, base, = cmpfunc, \ |
645 | | class expmode) |
646 | | |
647 | | // ---------------------------------------------------------------------------- |
648 | | // WX_DECLARE_OBJARRAY(T, name): this macro generates a new array class |
649 | | // named "name" which owns the objects of type T it contains, i.e. it will |
650 | | // delete them when it is destroyed. |
651 | | // |
652 | | // An element is of type T*, but arguments of type T& are taken (see below!) |
653 | | // and T& is returned. |
654 | | // |
655 | | // Don't use this for simple types such as "int" or "long"! |
656 | | // |
657 | | // Note on Add/Insert functions: |
658 | | // 1) function(T*) gives the object to the array, i.e. it will delete the |
659 | | // object when it's removed or in the array's dtor |
660 | | // 2) function(T&) will create a copy of the object and work with it |
661 | | // |
662 | | // Also: |
663 | | // 1) Remove() will delete the object after removing it from the array |
664 | | // 2) Detach() just removes the object from the array (returning pointer to it) |
665 | | // |
666 | | // NB1: Base type T should have an accessible copy ctor if Add(T&) is used |
667 | | // NB2: Never ever cast an array to it's base type: as dtor is not virtual |
668 | | // and so you risk having at least the memory leaks and probably worse |
669 | | // |
670 | | // Some functions of this class are not inline, so it takes some space to |
671 | | // define new class from this template even if you don't use it - which is not |
672 | | // the case for the simple (non-object) array classes |
673 | | // |
674 | | // To use an objarray class you must |
675 | | // #include "dynarray.h" |
676 | | // WX_DECLARE_OBJARRAY(element_type, list_class_name) |
677 | | // #include "arrimpl.cpp" |
678 | | // WX_DEFINE_OBJARRAY(list_class_name) // name must be the same as above! |
679 | | // |
680 | | // This is necessary because at the moment of DEFINE_OBJARRAY class parsing the |
681 | | // element_type must be fully defined (i.e. forward declaration is not |
682 | | // enough), while WX_DECLARE_OBJARRAY may be done anywhere. The separation of |
683 | | // two allows to break circcular dependencies with classes which have member |
684 | | // variables of objarray type. |
685 | | // ---------------------------------------------------------------------------- |
686 | | |
687 | | #define WX_DECLARE_OBJARRAY(T, name) \ |
688 | | WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, wxARRAY_DEFAULT_EXPORT) |
689 | | |
690 | | #define WX_DECLARE_EXPORTED_OBJARRAY(T, name) \ |
691 | | WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, WXDLLIMPEXP_CORE) |
692 | | |
693 | | #define WX_DECLARE_OBJARRAY_WITH_DECL(T, name, classdecl) \ |
694 | | classdecl wxObjectArrayTraitsFor##name \ |
695 | | { \ |
696 | | public: \ |
697 | | wxNODISCARD static T* Clone(T const& item); \ |
698 | | static void Free(T* p); \ |
699 | | }; \ |
700 | | typedef wxBaseObjectArray<T, wxObjectArrayTraitsFor##name> \ |
701 | | wxBaseObjectArrayFor##name; \ |
702 | | classdecl name : public wxBaseObjectArrayFor##name \ |
703 | | { \ |
704 | | } |
705 | | |
706 | | #define WX_DECLARE_USER_EXPORTED_OBJARRAY(T, name, expmode) \ |
707 | | WX_DECLARE_OBJARRAY_WITH_DECL(T, name, class expmode) |
708 | | |
709 | | // WX_DEFINE_OBJARRAY is going to be redefined when arrimpl.cpp is included, |
710 | | // try to provoke a human-understandable error if it used incorrectly. |
711 | | // |
712 | | // there is no real need for 3 different macros in the DEFINE case but do it |
713 | | // anyhow for consistency |
714 | | #define WX_DEFINE_OBJARRAY(name) DidYouIncludeArrimplCpp |
715 | | #define WX_DEFINE_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) |
716 | | #define WX_DEFINE_USER_EXPORTED_OBJARRAY(name) WX_DEFINE_OBJARRAY(name) |
717 | | |
718 | | // ---------------------------------------------------------------------------- |
719 | | // Some commonly used predefined base arrays |
720 | | // ---------------------------------------------------------------------------- |
721 | | |
722 | | WX_DECLARE_USER_EXPORTED_BASEARRAY(const void *, wxBaseArrayPtrVoid, |
723 | | WXDLLIMPEXP_BASE); |
724 | | WX_DECLARE_USER_EXPORTED_BASEARRAY(char, wxBaseArrayChar, WXDLLIMPEXP_BASE); |
725 | | WX_DECLARE_USER_EXPORTED_BASEARRAY(short, wxBaseArrayShort, WXDLLIMPEXP_BASE); |
726 | | WX_DECLARE_USER_EXPORTED_BASEARRAY(int, wxBaseArrayInt, WXDLLIMPEXP_BASE); |
727 | | WX_DECLARE_USER_EXPORTED_BASEARRAY(long, wxBaseArrayLong, WXDLLIMPEXP_BASE); |
728 | | WX_DECLARE_USER_EXPORTED_BASEARRAY(size_t, wxBaseArraySizeT, WXDLLIMPEXP_BASE); |
729 | | WX_DECLARE_USER_EXPORTED_BASEARRAY(double, wxBaseArrayDouble, WXDLLIMPEXP_BASE); |
730 | | |
731 | | // ---------------------------------------------------------------------------- |
732 | | // Convenience macros to define arrays from base arrays |
733 | | // ---------------------------------------------------------------------------- |
734 | | |
735 | | #define WX_DEFINE_ARRAY(T, name) \ |
736 | | WX_DEFINE_TYPEARRAY(T, name, wxBaseArrayPtrVoid) |
737 | | #define WX_DEFINE_ARRAY_PTR(T, name) \ |
738 | | WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid) |
739 | | #define WX_DEFINE_EXPORTED_ARRAY(T, name) \ |
740 | | WX_DEFINE_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) |
741 | | #define WX_DEFINE_EXPORTED_ARRAY_PTR(T, name) \ |
742 | | WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayPtrVoid) |
743 | | #define WX_DEFINE_ARRAY_WITH_DECL_PTR(T, name, decl) \ |
744 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, decl) |
745 | | #define WX_DEFINE_USER_EXPORTED_ARRAY(T, name, expmode) \ |
746 | | WX_DEFINE_TYPEARRAY_WITH_DECL(T, name, wxBaseArrayPtrVoid, wxARRAY_EMPTY expmode) |
747 | | #define WX_DEFINE_USER_EXPORTED_ARRAY_PTR(T, name, expmode) \ |
748 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayPtrVoid, wxARRAY_EMPTY expmode) |
749 | | |
750 | | #define WX_DEFINE_ARRAY_CHAR(T, name) \ |
751 | | WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayChar) |
752 | | #define WX_DEFINE_EXPORTED_ARRAY_CHAR(T, name) \ |
753 | | WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayChar) |
754 | | #define WX_DEFINE_USER_EXPORTED_ARRAY_CHAR(T, name, expmode) \ |
755 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayChar, wxARRAY_EMPTY expmode) |
756 | | |
757 | | #define WX_DEFINE_ARRAY_SHORT(T, name) \ |
758 | | WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayShort) |
759 | | #define WX_DEFINE_EXPORTED_ARRAY_SHORT(T, name) \ |
760 | | WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayShort) |
761 | | #define WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ |
762 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayShort, wxARRAY_EMPTY expmode) |
763 | | |
764 | | #define WX_DEFINE_ARRAY_INT(T, name) \ |
765 | | WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayInt) |
766 | | #define WX_DEFINE_EXPORTED_ARRAY_INT(T, name) \ |
767 | | WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayInt) |
768 | | #define WX_DEFINE_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ |
769 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayInt, wxARRAY_EMPTY expmode) |
770 | | |
771 | | #define WX_DEFINE_ARRAY_LONG(T, name) \ |
772 | | WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayLong) |
773 | | #define WX_DEFINE_EXPORTED_ARRAY_LONG(T, name) \ |
774 | | WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayLong) |
775 | | #define WX_DEFINE_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ |
776 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayLong, wxARRAY_EMPTY expmode) |
777 | | |
778 | | #define WX_DEFINE_ARRAY_SIZE_T(T, name) \ |
779 | | WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArraySizeT) |
780 | | #define WX_DEFINE_EXPORTED_ARRAY_SIZE_T(T, name) \ |
781 | | WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArraySizeT) |
782 | | #define WX_DEFINE_USER_EXPORTED_ARRAY_SIZE_T(T, name, expmode) \ |
783 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArraySizeT, wxARRAY_EMPTY expmode) |
784 | | |
785 | | #define WX_DEFINE_ARRAY_DOUBLE(T, name) \ |
786 | | WX_DEFINE_TYPEARRAY_PTR(T, name, wxBaseArrayDouble) |
787 | | #define WX_DEFINE_EXPORTED_ARRAY_DOUBLE(T, name) \ |
788 | | WX_DEFINE_EXPORTED_TYPEARRAY_PTR(T, name, wxBaseArrayDouble) |
789 | | #define WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(T, name, expmode) \ |
790 | | WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, wxBaseArrayDouble, wxARRAY_EMPTY expmode) |
791 | | |
792 | | // ---------------------------------------------------------------------------- |
793 | | // Convenience macros to define sorted arrays from base arrays |
794 | | // ---------------------------------------------------------------------------- |
795 | | |
796 | | #define WX_DEFINE_SORTED_ARRAY(T, name) \ |
797 | | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) |
798 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY(T, name) \ |
799 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid) |
800 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY(T, name, expmode) \ |
801 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayPtrVoid, wxARRAY_EMPTY expmode) |
802 | | |
803 | | #define WX_DEFINE_SORTED_ARRAY_CHAR(T, name) \ |
804 | | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayChar) |
805 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CHAR(T, name) \ |
806 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayChar) |
807 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CHAR(T, name, expmode) \ |
808 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayChar, wxARRAY_EMPTY expmode) |
809 | | |
810 | | #define WX_DEFINE_SORTED_ARRAY_SHORT(T, name) \ |
811 | | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayShort) |
812 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_SHORT(T, name) \ |
813 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort) |
814 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SHORT(T, name, expmode) \ |
815 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayShort, wxARRAY_EMPTY expmode) |
816 | | |
817 | | #define WX_DEFINE_SORTED_ARRAY_INT(T, name) \ |
818 | | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayInt) |
819 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_INT(T, name) \ |
820 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt) |
821 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_INT(T, name, expmode) \ |
822 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayInt, expmode) |
823 | | |
824 | | #define WX_DEFINE_SORTED_ARRAY_LONG(T, name) \ |
825 | | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArrayLong) |
826 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(T, name) \ |
827 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong) |
828 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_LONG(T, name, expmode) \ |
829 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArrayLong, expmode) |
830 | | |
831 | | #define WX_DEFINE_SORTED_ARRAY_SIZE_T(T, name) \ |
832 | | WX_DEFINE_SORTED_TYPEARRAY(T, name, wxBaseArraySizeT) |
833 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_SIZE_T(T, name) \ |
834 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY(T, name, wxBaseArraySizeT) |
835 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(T, name, expmode) \ |
836 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY(T, name, wxBaseArraySizeT, wxARRAY_EMPTY expmode) |
837 | | |
838 | | // ---------------------------------------------------------------------------- |
839 | | // Convenience macros to define sorted arrays from base arrays |
840 | | // ---------------------------------------------------------------------------- |
841 | | |
842 | | #define WX_DEFINE_SORTED_ARRAY_CMP(T, cmpfunc, name) \ |
843 | | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) |
844 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP(T, cmpfunc, name) \ |
845 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayPtrVoid) |
846 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP(T, cmpfunc, \ |
847 | | name, expmode) \ |
848 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ |
849 | | wxBaseArrayPtrVoid, \ |
850 | | wxARRAY_EMPTY expmode) |
851 | | |
852 | | #define WX_DEFINE_SORTED_ARRAY_CMP_CHAR(T, cmpfunc, name) \ |
853 | | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayChar) |
854 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_CHAR(T, cmpfunc, name) \ |
855 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayChar) |
856 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_CHAR(T, cmpfunc, \ |
857 | | name, expmode) \ |
858 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ |
859 | | wxBaseArrayChar, \ |
860 | | wxARRAY_EMPTY expmode) |
861 | | |
862 | | #define WX_DEFINE_SORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ |
863 | | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) |
864 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, name) \ |
865 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayShort) |
866 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SHORT(T, cmpfunc, \ |
867 | | name, expmode) \ |
868 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ |
869 | | wxBaseArrayShort, \ |
870 | | wxARRAY_EMPTY expmode) |
871 | | |
872 | | #define WX_DEFINE_SORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ |
873 | | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) |
874 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, name) \ |
875 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayInt) |
876 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_INT(T, cmpfunc, \ |
877 | | name, expmode) \ |
878 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ |
879 | | wxBaseArrayInt, \ |
880 | | wxARRAY_EMPTY expmode) |
881 | | |
882 | | #define WX_DEFINE_SORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ |
883 | | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) |
884 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, name) \ |
885 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArrayLong) |
886 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_LONG(T, cmpfunc, \ |
887 | | name, expmode) \ |
888 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ |
889 | | wxBaseArrayLong, \ |
890 | | wxARRAY_EMPTY expmode) |
891 | | |
892 | | #define WX_DEFINE_SORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, name) \ |
893 | | WX_DEFINE_SORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArraySizeT) |
894 | | #define WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, name) \ |
895 | | WX_DEFINE_SORTED_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, wxBaseArraySizeT) |
896 | | #define WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_CMP_SIZE_T(T, cmpfunc, \ |
897 | | name, expmode) \ |
898 | | WX_DEFINE_SORTED_USER_EXPORTED_TYPEARRAY_CMP(T, cmpfunc, name, \ |
899 | | wxBaseArraySizeT, \ |
900 | | wxARRAY_EMPTY expmode) |
901 | | |
902 | | // ---------------------------------------------------------------------------- |
903 | | // Some commonly used predefined arrays |
904 | | // ---------------------------------------------------------------------------- |
905 | | |
906 | | WX_DEFINE_USER_EXPORTED_ARRAY_SHORT(short, wxArrayShort, class WXDLLIMPEXP_BASE); |
907 | | WX_DEFINE_USER_EXPORTED_ARRAY_INT(int, wxArrayInt, class WXDLLIMPEXP_BASE); |
908 | | WX_DEFINE_USER_EXPORTED_ARRAY_DOUBLE(double, wxArrayDouble, class WXDLLIMPEXP_BASE); |
909 | | WX_DEFINE_USER_EXPORTED_ARRAY_LONG(long, wxArrayLong, class WXDLLIMPEXP_BASE); |
910 | | WX_DEFINE_USER_EXPORTED_ARRAY_PTR(void *, wxArrayPtrVoid, class WXDLLIMPEXP_BASE); |
911 | | |
912 | | // ----------------------------------------------------------------------------- |
913 | | // convenience functions: they used to be macros, hence the naming convention |
914 | | // ----------------------------------------------------------------------------- |
915 | | |
916 | | // prepend all element of one array to another one; e.g. if first array contains |
917 | | // elements X,Y,Z and the second contains A,B,C (in those orders), then the |
918 | | // first array will be result as A,B,C,X,Y,Z |
919 | | template <typename A1, typename A2> |
920 | | inline void WX_PREPEND_ARRAY(A1& array, const A2& other) |
921 | 0 | { |
922 | 0 | const size_t size = other.size(); |
923 | 0 | array.reserve(size); |
924 | 0 | for ( size_t n = 0; n < size; n++ ) |
925 | 0 | { |
926 | 0 | array.Insert(other[n], n); |
927 | 0 | } |
928 | 0 | } |
929 | | |
930 | | // append all element of one array to another one |
931 | | template <typename A1, typename A2> |
932 | | inline void WX_APPEND_ARRAY(A1& array, const A2& other) |
933 | 0 | { |
934 | 0 | size_t size = other.size(); |
935 | 0 | array.reserve(size); |
936 | 0 | for ( size_t n = 0; n < size; n++ ) |
937 | 0 | { |
938 | 0 | array.push_back(other[n]); |
939 | 0 | } |
940 | 0 | } Unexecuted instantiation: void WX_APPEND_ARRAY<wxEvtHandlerArray, wxEvtHandlerArray>(wxEvtHandlerArray&, wxEvtHandlerArray const&) Unexecuted instantiation: void WX_APPEND_ARRAY<wxArrayString, wxArrayString>(wxArrayString&, wxArrayString const&) Unexecuted instantiation: void WX_APPEND_ARRAY<wxPathList, wxArrayString>(wxPathList&, wxArrayString const&) Unexecuted instantiation: void WX_APPEND_ARRAY<wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> >, wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> > >(wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> >&, wxBaseArray<wxDateTime, wxSortedArray_SortFunction<wxDateTime> > const&) |
941 | | |
942 | | // delete all array elements |
943 | | // |
944 | | // NB: the class declaration of the array elements must be visible from the |
945 | | // place where you use this macro, otherwise the proper destructor may not |
946 | | // be called (a decent compiler should give a warning about it, but don't |
947 | | // count on it)! |
948 | | template <typename A> |
949 | | inline void WX_CLEAR_ARRAY(A& array) |
950 | 0 | { |
951 | 0 | size_t size = array.size(); |
952 | 0 | for ( size_t n = 0; n < size; n++ ) |
953 | 0 | { |
954 | 0 | delete array[n]; |
955 | 0 | } |
956 | |
|
957 | 0 | array.clear(); |
958 | 0 | } |
959 | | |
960 | | #endif // _DYNARRAY_H |