arrimpl.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Copyright: (c) 1997 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. /*****************************************************************************
  11. * Purpose: implements methods of "template" class declared in *
  12. * DECLARE_OBJARRAY macro and which couldn't be implemented inline *
  13. * (because they need the full definition of type T in scope) *
  14. * *
  15. * Usage: 1) #include dynarray.h *
  16. * 2) WX_DECLARE_OBJARRAY *
  17. * 3) #include arrimpl.cpp *
  18. * 4) WX_DEFINE_OBJARRAY *
  19. *****************************************************************************/
  20. // needed to resolve the conflict between global T and macro parameter T
  21. #define _WX_ERROR_REMOVE2(x) wxT("bad index in ") wxT(#x) wxT("::RemoveAt()")
  22. // macro implements remaining (not inline) methods of template list
  23. // (it's private to this file)
  24. #undef _DEFINE_OBJARRAY
  25. #define _DEFINE_OBJARRAY(T, name) \
  26. name::~name() \
  27. { \
  28. Empty(); \
  29. } \
  30. \
  31. void name::DoCopy(const name& src) \
  32. { \
  33. for ( size_t ui = 0; ui < src.size(); ui++ ) \
  34. Add(src[ui]); \
  35. } \
  36. \
  37. name& name::operator=(const name& src) \
  38. { \
  39. Empty(); \
  40. DoCopy(src); \
  41. \
  42. return *this; \
  43. } \
  44. \
  45. name::name(const name& src) : wxArrayPtrVoid() \
  46. { \
  47. DoCopy(src); \
  48. } \
  49. \
  50. void name::DoEmpty() \
  51. { \
  52. for ( size_t ui = 0; ui < size(); ui++ ) \
  53. delete (T*)base_array::operator[](ui); \
  54. } \
  55. \
  56. void name::RemoveAt(size_t uiIndex, size_t nRemove) \
  57. { \
  58. wxCHECK_RET( uiIndex < size(), _WX_ERROR_REMOVE2(name) ); \
  59. \
  60. for (size_t i = 0; i < nRemove; i++ ) \
  61. delete (T*)base_array::operator[](uiIndex + i); \
  62. \
  63. base_array::erase(begin() + uiIndex, begin() + uiIndex + nRemove); \
  64. } \
  65. \
  66. void name::Add(const T& item, size_t nInsert) \
  67. { \
  68. if (nInsert == 0) \
  69. return; \
  70. T* pItem = new T(item); \
  71. size_t nOldSize = size(); \
  72. if ( pItem != NULL ) \
  73. base_array::insert(end(), nInsert, pItem); \
  74. for (size_t i = 1; i < nInsert; i++) \
  75. base_array::operator[](nOldSize + i) = new T(item); \
  76. } \
  77. \
  78. void name::Insert(const T& item, size_t uiIndex, size_t nInsert) \
  79. { \
  80. if (nInsert == 0) \
  81. return; \
  82. T* pItem = new T(item); \
  83. if ( pItem != NULL ) \
  84. base_array::insert(begin() + uiIndex, nInsert, pItem); \
  85. for (size_t i = 1; i < nInsert; i++) \
  86. base_array::operator[](uiIndex + i) = new T(item); \
  87. } \
  88. \
  89. int name::Index(const T& item, bool bFromEnd) const \
  90. { \
  91. if ( bFromEnd ) { \
  92. if ( size() > 0 ) { \
  93. size_t ui = size() - 1; \
  94. do { \
  95. if ( (T*)base_array::operator[](ui) == &item ) \
  96. return static_cast<int>(ui); \
  97. ui--; \
  98. } \
  99. while ( ui != 0 ); \
  100. } \
  101. } \
  102. else { \
  103. for( size_t ui = 0; ui < size(); ui++ ) { \
  104. if( (T*)base_array::operator[](ui) == &item ) \
  105. return static_cast<int>(ui); \
  106. } \
  107. } \
  108. \
  109. return wxNOT_FOUND; \
  110. }
  111. // redefine the macro so that now it will generate the class implementation
  112. // old value would provoke a compile-time error if this file is not included
  113. #undef WX_DEFINE_OBJARRAY
  114. #define WX_DEFINE_OBJARRAY(name) _DEFINE_OBJARRAY(_wxObjArray##name, name)