]>
Commit | Line | Data |
---|---|---|
0297f5c6 | 1 | /////////////////////////////////////////////////////////////////////////////// |
1a931653 | 2 | // Name: wx/arrimpl.cpp |
0297f5c6 VZ |
3 | // Purpose: helper file for implementation of dynamic lists |
4 | // Author: Vadim Zeitlin | |
e90c1d2a | 5 | // Modified by: |
0297f5c6 VZ |
6 | // Created: 16.10.97 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1997 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | /***************************************************************************** | |
13 | * Purpose: implements methods of "template" class declared in * | |
14 | * DECLARE_OBJARRAY macro and which couldn't be implemented inline * | |
15 | * (because they need the full definition of type T in scope) * | |
16 | * * | |
17 | * Usage: 1) #include dynarray.h * | |
18 | * 2) WX_DECLARE_OBJARRAY * | |
19 | * 3) #include arrimpl.cpp * | |
20 | * 4) WX_DEFINE_OBJARRAY * | |
21 | *****************************************************************************/ | |
22 | ||
e90c1d2a | 23 | // needed to resolve the conflict between global T and macro parameter T |
fbdcff4a JS |
24 | |
25 | // VC++ can't cope with string concatenation in Unicode mode | |
26 | #if defined(wxUSE_UNICODE) && wxUSE_UNICODE | |
27 | #define _WX_ERROR_REMOVE2(x) wxT("bad index in ::RemoveAt()") | |
28 | #else | |
8a729bb8 | 29 | #define _WX_ERROR_REMOVE2(x) wxT("bad index in " #x "::RemoveAt()") |
fbdcff4a | 30 | #endif |
e90c1d2a | 31 | |
0297f5c6 VZ |
32 | // macro implements remaining (not inline) methods of template list |
33 | // (it's private to this file) | |
22692cf6 VZ |
34 | #undef _DEFINE_OBJARRAY |
35 | #define _DEFINE_OBJARRAY(T, name) \ | |
0297f5c6 VZ |
36 | name::~name() \ |
37 | { \ | |
38 | Empty(); \ | |
39 | } \ | |
40 | \ | |
41 | void name::DoCopy(const name& src) \ | |
42 | { \ | |
22692cf6 | 43 | for ( size_t ui = 0; ui < src.Count(); ui++ ) \ |
0297f5c6 VZ |
44 | Add(src[ui]); \ |
45 | } \ | |
46 | \ | |
47 | name& name::operator=(const name& src) \ | |
48 | { \ | |
49 | Empty(); \ | |
50 | DoCopy(src); \ | |
51 | \ | |
52 | return *this; \ | |
53 | } \ | |
54 | \ | |
d84afea9 | 55 | name::name(const name& src) : wxArrayPtrVoid() \ |
0297f5c6 VZ |
56 | { \ |
57 | DoCopy(src); \ | |
58 | } \ | |
59 | \ | |
f6bcfd97 | 60 | void name::DoEmpty() \ |
0297f5c6 | 61 | { \ |
22692cf6 | 62 | for ( size_t ui = 0; ui < Count(); ui++ ) \ |
5a1cad6e | 63 | delete (T*)wxBaseArrayPtrVoid::Item(ui); \ |
0297f5c6 VZ |
64 | } \ |
65 | \ | |
3b0b5f13 | 66 | void name::RemoveAt(size_t uiIndex, size_t nRemove) \ |
0297f5c6 | 67 | { \ |
e90c1d2a | 68 | wxCHECK_RET( uiIndex < Count(), _WX_ERROR_REMOVE2(name) ); \ |
0297f5c6 | 69 | \ |
3b0b5f13 VZ |
70 | for (size_t i = 0; i < nRemove; i++ ) \ |
71 | delete (T*)wxBaseArrayPtrVoid::Item(uiIndex + i); \ | |
0297f5c6 | 72 | \ |
3b0b5f13 | 73 | wxBaseArrayPtrVoid::RemoveAt(uiIndex, nRemove); \ |
0297f5c6 VZ |
74 | } \ |
75 | \ | |
3b0b5f13 | 76 | void name::Add(const T& item, size_t nInsert) \ |
0297f5c6 | 77 | { \ |
2b5f62a0 VZ |
78 | if (nInsert == 0) \ |
79 | return; \ | |
0297f5c6 | 80 | T* pItem = new T(item); \ |
3b0b5f13 | 81 | size_t nOldSize = GetCount(); \ |
0297f5c6 | 82 | if ( pItem != NULL ) \ |
3b0b5f13 VZ |
83 | wxBaseArrayPtrVoid::Add(pItem, nInsert); \ |
84 | for (size_t i = 1; i < nInsert; i++) \ | |
85 | wxBaseArrayPtrVoid::Item(nOldSize + i) = new T(item); \ | |
0297f5c6 VZ |
86 | } \ |
87 | \ | |
3b0b5f13 | 88 | void name::Insert(const T& item, size_t uiIndex, size_t nInsert) \ |
0297f5c6 | 89 | { \ |
2b5f62a0 VZ |
90 | if (nInsert == 0) \ |
91 | return; \ | |
0297f5c6 VZ |
92 | T* pItem = new T(item); \ |
93 | if ( pItem != NULL ) \ | |
3b0b5f13 VZ |
94 | wxBaseArrayPtrVoid::Insert(pItem, uiIndex, nInsert); \ |
95 | for (size_t i = 1; i < nInsert; i++) \ | |
96 | wxBaseArrayPtrVoid::Item(uiIndex + i) = new T(item); \ | |
0297f5c6 VZ |
97 | } \ |
98 | \ | |
22692cf6 | 99 | int name::Index(const T& Item, bool bFromEnd) const \ |
0297f5c6 VZ |
100 | { \ |
101 | if ( bFromEnd ) { \ | |
102 | if ( Count() > 0 ) { \ | |
22692cf6 | 103 | size_t ui = Count() - 1; \ |
0297f5c6 | 104 | do { \ |
5a1cad6e | 105 | if ( (T*)wxBaseArrayPtrVoid::Item(ui) == &Item ) \ |
0297f5c6 VZ |
106 | return ui; \ |
107 | ui--; \ | |
108 | } \ | |
109 | while ( ui != 0 ); \ | |
110 | } \ | |
111 | } \ | |
112 | else { \ | |
22692cf6 | 113 | for( size_t ui = 0; ui < Count(); ui++ ) { \ |
5a1cad6e | 114 | if( (T*)wxBaseArrayPtrVoid::Item(ui) == &Item ) \ |
0297f5c6 VZ |
115 | return ui; \ |
116 | } \ | |
117 | } \ | |
118 | \ | |
e90c1d2a VZ |
119 | return wxNOT_FOUND; \ |
120 | } | |
0297f5c6 VZ |
121 | |
122 | // redefine the macro so that now it will generate the class implementation | |
123 | // old value would provoke a compile-time error if this file is not included | |
124 | #undef WX_DEFINE_OBJARRAY | |
1a931653 | 125 | #define WX_DEFINE_OBJARRAY(name) _DEFINE_OBJARRAY(_wxObjArray##name, name) |