Misc fixes
[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 // needed to resolve the conflict between global T and macro parameter T
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
29 #define _WX_ERROR_REMOVE2(x) wxT("bad index in " #x "::RemoveAt()")
30 #endif
31
32 // macro implements remaining (not inline) methods of template list
33 // (it's private to this file)
34 #undef _DEFINE_OBJARRAY
35 #define _DEFINE_OBJARRAY(T, name) \
36 name::~name() \
37 { \
38 Empty(); \
39 } \
40 \
41 void name::DoCopy(const name& src) \
42 { \
43 for ( size_t ui = 0; ui < src.Count(); ui++ ) \
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 \
55 name::name(const name& src) \
56 { \
57 DoCopy(src); \
58 } \
59 \
60 void name::Empty() \
61 { \
62 for ( size_t ui = 0; ui < Count(); ui++ ) \
63 delete (T*)wxBaseArray::Item(ui); \
64 \
65 wxBaseArray::Clear(); \
66 } \
67 \
68 void name::RemoveAt(size_t uiIndex) \
69 { \
70 wxCHECK_RET( uiIndex < Count(), _WX_ERROR_REMOVE2(name) ); \
71 \
72 delete (T*)wxBaseArray::Item(uiIndex); \
73 \
74 wxBaseArray::RemoveAt(uiIndex); \
75 } \
76 \
77 void name::Add(const T& item) \
78 { \
79 T* pItem = new T(item); \
80 if ( pItem != NULL ) \
81 Add(pItem); \
82 } \
83 \
84 void name::Insert(const T& item, size_t uiIndex) \
85 { \
86 T* pItem = new T(item); \
87 if ( pItem != NULL ) \
88 Insert(pItem, uiIndex); \
89 } \
90 \
91 int name::Index(const T& Item, bool bFromEnd) const \
92 { \
93 if ( bFromEnd ) { \
94 if ( Count() > 0 ) { \
95 size_t ui = Count() - 1; \
96 do { \
97 if ( (T*)wxBaseArray::Item(ui) == &Item ) \
98 return ui; \
99 ui--; \
100 } \
101 while ( ui != 0 ); \
102 } \
103 } \
104 else { \
105 for( size_t ui = 0; ui < Count(); ui++ ) { \
106 if( (T*)wxBaseArray::Item(ui) == &Item ) \
107 return ui; \
108 } \
109 } \
110 \
111 return wxNOT_FOUND; \
112 }
113
114 // redefine the macro so that now it will generate the class implementation
115 // old value would provoke a compile-time error if this file is not included
116 #undef WX_DEFINE_OBJARRAY
117 #define WX_DEFINE_OBJARRAY(name) _DEFINE_OBJARRAY(_L##name, name)