]> git.saurik.com Git - wxWidgets.git/blob - include/wx/arrimpl.cpp
e52d81def300c47f7e1224c188c11082f14f0061
[wxWidgets.git] / include / wx / arrimpl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: listimpl.cpp
3 // Purpose: helper file for implementation of dynamic lists
4 // Author: Vadim Zeitlin
5 // Modified by:
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
23 // macro implements remaining (not inline) methods of template list
24 // (it's private to this file)
25 #undef _DEFINE_OBJARRAY
26 #define _DEFINE_OBJARRAY(T, name) \
27 name::~name() \
28 { \
29 Empty(); \
30 } \
31 \
32 void name::DoCopy(const name& src) \
33 { \
34 for ( size_t ui = 0; ui < src.Count(); ui++ ) \
35 Add(src[ui]); \
36 } \
37 \
38 name& name::operator=(const name& src) \
39 { \
40 Empty(); \
41 DoCopy(src); \
42 \
43 return *this; \
44 } \
45 \
46 name::name(const name& src) \
47 { \
48 DoCopy(src); \
49 } \
50 \
51 void name::Empty() \
52 { \
53 for ( size_t ui = 0; ui < Count(); ui++ ) \
54 delete (T*)wxBaseArray::Item(ui); \
55 \
56 wxBaseArray::Clear(); \
57 } \
58 \
59 void name::Remove(size_t uiIndex) \
60 { \
61 wxCHECK_RET( uiIndex < Count(), "bad index in " #name "::Remove()" ); \
62 \
63 delete (T*)wxBaseArray::Item(uiIndex); \
64 \
65 wxBaseArray::Remove(uiIndex); \
66 } \
67 \
68 void name::Add(const T& item) \
69 { \
70 T* pItem = new T(item); \
71 if ( pItem != NULL ) \
72 Add(pItem); \
73 } \
74 \
75 void name::Insert(const T& item, size_t uiIndex) \
76 { \
77 T* pItem = new T(item); \
78 if ( pItem != NULL ) \
79 Insert(pItem, uiIndex); \
80 } \
81 \
82 int name::Index(const T& Item, bool bFromEnd) const \
83 { \
84 if ( bFromEnd ) { \
85 if ( Count() > 0 ) { \
86 size_t ui = Count() - 1; \
87 do { \
88 if ( (T*)wxBaseArray::Item(ui) == &Item ) \
89 return ui; \
90 ui--; \
91 } \
92 while ( ui != 0 ); \
93 } \
94 } \
95 else { \
96 for( size_t ui = 0; ui < Count(); ui++ ) { \
97 if( (T*)wxBaseArray::Item(ui) == &Item ) \
98 return ui; \
99 } \
100 } \
101 \
102 return NOT_FOUND; \
103 }
104
105 // redefine the macro so that now it will generate the class implementation
106 // old value would provoke a compile-time error if this file is not included
107 #undef WX_DEFINE_OBJARRAY
108 #define WX_DEFINE_OBJARRAY(name) _DEFINE_OBJARRAY(_L##name, name)