]>
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 | 24 | |
eee3624b | 25 | #define _WX_ERROR_REMOVE2(x) wxT("bad index in ") wxT(#x) wxT("::RemoveAt()") |
e90c1d2a | 26 | |
0297f5c6 VZ |
27 | // macro implements remaining (not inline) methods of template list |
28 | // (it's private to this file) | |
22692cf6 VZ |
29 | #undef _DEFINE_OBJARRAY |
30 | #define _DEFINE_OBJARRAY(T, name) \ | |
0297f5c6 VZ |
31 | name::~name() \ |
32 | { \ | |
33 | Empty(); \ | |
34 | } \ | |
35 | \ | |
36 | void name::DoCopy(const name& src) \ | |
37 | { \ | |
22692cf6 | 38 | for ( size_t ui = 0; ui < src.Count(); ui++ ) \ |
0297f5c6 VZ |
39 | Add(src[ui]); \ |
40 | } \ | |
41 | \ | |
42 | name& name::operator=(const name& src) \ | |
43 | { \ | |
44 | Empty(); \ | |
45 | DoCopy(src); \ | |
46 | \ | |
47 | return *this; \ | |
48 | } \ | |
49 | \ | |
d84afea9 | 50 | name::name(const name& src) : wxArrayPtrVoid() \ |
0297f5c6 VZ |
51 | { \ |
52 | DoCopy(src); \ | |
53 | } \ | |
54 | \ | |
f6bcfd97 | 55 | void name::DoEmpty() \ |
0297f5c6 | 56 | { \ |
22692cf6 | 57 | for ( size_t ui = 0; ui < Count(); ui++ ) \ |
5a1cad6e | 58 | delete (T*)wxBaseArrayPtrVoid::Item(ui); \ |
0297f5c6 VZ |
59 | } \ |
60 | \ | |
3b0b5f13 | 61 | void name::RemoveAt(size_t uiIndex, size_t nRemove) \ |
0297f5c6 | 62 | { \ |
e90c1d2a | 63 | wxCHECK_RET( uiIndex < Count(), _WX_ERROR_REMOVE2(name) ); \ |
0297f5c6 | 64 | \ |
3b0b5f13 VZ |
65 | for (size_t i = 0; i < nRemove; i++ ) \ |
66 | delete (T*)wxBaseArrayPtrVoid::Item(uiIndex + i); \ | |
0297f5c6 | 67 | \ |
3b0b5f13 | 68 | wxBaseArrayPtrVoid::RemoveAt(uiIndex, nRemove); \ |
0297f5c6 VZ |
69 | } \ |
70 | \ | |
3b0b5f13 | 71 | void name::Add(const T& item, size_t nInsert) \ |
0297f5c6 | 72 | { \ |
2b5f62a0 VZ |
73 | if (nInsert == 0) \ |
74 | return; \ | |
0297f5c6 | 75 | T* pItem = new T(item); \ |
3b0b5f13 | 76 | size_t nOldSize = GetCount(); \ |
0297f5c6 | 77 | if ( pItem != NULL ) \ |
3b0b5f13 VZ |
78 | wxBaseArrayPtrVoid::Add(pItem, nInsert); \ |
79 | for (size_t i = 1; i < nInsert; i++) \ | |
80 | wxBaseArrayPtrVoid::Item(nOldSize + i) = new T(item); \ | |
0297f5c6 VZ |
81 | } \ |
82 | \ | |
3b0b5f13 | 83 | void name::Insert(const T& item, size_t uiIndex, size_t nInsert) \ |
0297f5c6 | 84 | { \ |
2b5f62a0 VZ |
85 | if (nInsert == 0) \ |
86 | return; \ | |
0297f5c6 VZ |
87 | T* pItem = new T(item); \ |
88 | if ( pItem != NULL ) \ | |
3b0b5f13 VZ |
89 | wxBaseArrayPtrVoid::Insert(pItem, uiIndex, nInsert); \ |
90 | for (size_t i = 1; i < nInsert; i++) \ | |
91 | wxBaseArrayPtrVoid::Item(uiIndex + i) = new T(item); \ | |
0297f5c6 VZ |
92 | } \ |
93 | \ | |
22692cf6 | 94 | int name::Index(const T& Item, bool bFromEnd) const \ |
0297f5c6 VZ |
95 | { \ |
96 | if ( bFromEnd ) { \ | |
97 | if ( Count() > 0 ) { \ | |
22692cf6 | 98 | size_t ui = Count() - 1; \ |
0297f5c6 | 99 | do { \ |
5a1cad6e | 100 | if ( (T*)wxBaseArrayPtrVoid::Item(ui) == &Item ) \ |
0297f5c6 VZ |
101 | return ui; \ |
102 | ui--; \ | |
103 | } \ | |
104 | while ( ui != 0 ); \ | |
105 | } \ | |
106 | } \ | |
107 | else { \ | |
22692cf6 | 108 | for( size_t ui = 0; ui < Count(); ui++ ) { \ |
5a1cad6e | 109 | if( (T*)wxBaseArrayPtrVoid::Item(ui) == &Item ) \ |
0297f5c6 VZ |
110 | return ui; \ |
111 | } \ | |
112 | } \ | |
113 | \ | |
e90c1d2a VZ |
114 | return wxNOT_FOUND; \ |
115 | } | |
0297f5c6 VZ |
116 | |
117 | // redefine the macro so that now it will generate the class implementation | |
118 | // old value would provoke a compile-time error if this file is not included | |
119 | #undef WX_DEFINE_OBJARRAY | |
1a931653 | 120 | #define WX_DEFINE_OBJARRAY(name) _DEFINE_OBJARRAY(_wxObjArray##name, name) |