| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: wx/arrimpl.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 | #define _WX_ERROR_REMOVE2(x) wxT("bad index in ") wxT(#x) wxT("::RemoveAt()") |
| 26 | |
| 27 | // macro implements remaining (not inline) methods of template list |
| 28 | // (it's private to this file) |
| 29 | #undef _DEFINE_OBJARRAY |
| 30 | #define _DEFINE_OBJARRAY(T, name) \ |
| 31 | name::~name() \ |
| 32 | { \ |
| 33 | Empty(); \ |
| 34 | } \ |
| 35 | \ |
| 36 | void name::DoCopy(const name& src) \ |
| 37 | { \ |
| 38 | for ( size_t ui = 0; ui < src.size(); ui++ ) \ |
| 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 | \ |
| 50 | name::name(const name& src) : wxArrayPtrVoid() \ |
| 51 | { \ |
| 52 | DoCopy(src); \ |
| 53 | } \ |
| 54 | \ |
| 55 | void name::DoEmpty() \ |
| 56 | { \ |
| 57 | for ( size_t ui = 0; ui < size(); ui++ ) \ |
| 58 | delete (T*)base_array::operator[](ui); \ |
| 59 | } \ |
| 60 | \ |
| 61 | void name::RemoveAt(size_t uiIndex, size_t nRemove) \ |
| 62 | { \ |
| 63 | wxCHECK_RET( uiIndex < size(), _WX_ERROR_REMOVE2(name) ); \ |
| 64 | \ |
| 65 | for (size_t i = 0; i < nRemove; i++ ) \ |
| 66 | delete (T*)base_array::operator[](uiIndex + i); \ |
| 67 | \ |
| 68 | base_array::erase(begin() + uiIndex, begin() + uiIndex + nRemove); \ |
| 69 | } \ |
| 70 | \ |
| 71 | void name::Add(const T& item, size_t nInsert) \ |
| 72 | { \ |
| 73 | if (nInsert == 0) \ |
| 74 | return; \ |
| 75 | T* pItem = new T(item); \ |
| 76 | size_t nOldSize = size(); \ |
| 77 | if ( pItem != NULL ) \ |
| 78 | base_array::insert(end(), nInsert, pItem); \ |
| 79 | for (size_t i = 1; i < nInsert; i++) \ |
| 80 | base_array::operator[](nOldSize + i) = new T(item); \ |
| 81 | } \ |
| 82 | \ |
| 83 | void name::Insert(const T& item, size_t uiIndex, size_t nInsert) \ |
| 84 | { \ |
| 85 | if (nInsert == 0) \ |
| 86 | return; \ |
| 87 | T* pItem = new T(item); \ |
| 88 | if ( pItem != NULL ) \ |
| 89 | base_array::insert(begin() + uiIndex, nInsert, pItem); \ |
| 90 | for (size_t i = 1; i < nInsert; i++) \ |
| 91 | base_array::operator[](uiIndex + i) = new T(item); \ |
| 92 | } \ |
| 93 | \ |
| 94 | int name::Index(const T& Item, bool bFromEnd) const \ |
| 95 | { \ |
| 96 | if ( bFromEnd ) { \ |
| 97 | if ( size() > 0 ) { \ |
| 98 | size_t ui = size() - 1; \ |
| 99 | do { \ |
| 100 | if ( (T*)base_array::operator[](ui) == &Item ) \ |
| 101 | return wx_static_cast(int, ui); \ |
| 102 | ui--; \ |
| 103 | } \ |
| 104 | while ( ui != 0 ); \ |
| 105 | } \ |
| 106 | } \ |
| 107 | else { \ |
| 108 | for( size_t ui = 0; ui < size(); ui++ ) { \ |
| 109 | if( (T*)base_array::operator[](ui) == &Item ) \ |
| 110 | return wx_static_cast(int, ui); \ |
| 111 | } \ |
| 112 | } \ |
| 113 | \ |
| 114 | return wxNOT_FOUND; \ |
| 115 | } |
| 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 |
| 120 | #define WX_DEFINE_OBJARRAY(name) _DEFINE_OBJARRAY(_wxObjArray##name, name) |