scopedarray.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/scopedarray.h
  3. // Purpose: scoped smart pointer class
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-02-03
  6. // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
  7. // (c) 2009 Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_SCOPED_ARRAY_H_
  11. #define _WX_SCOPED_ARRAY_H_
  12. #include "wx/defs.h"
  13. #include "wx/checkeddelete.h"
  14. // ----------------------------------------------------------------------------
  15. // wxScopedArray: A scoped array
  16. // ----------------------------------------------------------------------------
  17. template <class T>
  18. class wxScopedArray
  19. {
  20. public:
  21. typedef T element_type;
  22. wxEXPLICIT wxScopedArray(T * array = NULL) : m_array(array) { }
  23. ~wxScopedArray() { delete [] m_array; }
  24. // test for pointer validity: defining conversion to unspecified_bool_type
  25. // and not more obvious bool to avoid implicit conversions to integer types
  26. typedef T *(wxScopedArray<T>::*unspecified_bool_type)() const;
  27. operator unspecified_bool_type() const
  28. {
  29. return m_array ? &wxScopedArray<T>::get : NULL;
  30. }
  31. void reset(T *array = NULL)
  32. {
  33. if ( array != m_array )
  34. {
  35. delete [] m_array;
  36. m_array = array;
  37. }
  38. }
  39. T& operator[](size_t n) const { return m_array[n]; }
  40. T *get() const { return m_array; }
  41. void swap(wxScopedArray &other)
  42. {
  43. T * const tmp = other.m_array;
  44. other.m_array = m_array;
  45. m_array = tmp;
  46. }
  47. private:
  48. T *m_array;
  49. DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedArray, T)
  50. };
  51. // ----------------------------------------------------------------------------
  52. // old macro based implementation
  53. // ----------------------------------------------------------------------------
  54. // the same but for arrays instead of simple pointers
  55. #define wxDECLARE_SCOPED_ARRAY(T, name)\
  56. class name \
  57. { \
  58. private: \
  59. T * m_ptr; \
  60. name(name const &); \
  61. name & operator=(name const &); \
  62. \
  63. public: \
  64. wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
  65. {} \
  66. \
  67. ~name(); \
  68. void reset(T * p = NULL); \
  69. \
  70. T & operator[](long int i) const\
  71. { \
  72. wxASSERT(m_ptr != NULL); \
  73. wxASSERT(i >= 0); \
  74. return m_ptr[i]; \
  75. } \
  76. \
  77. T * get() const \
  78. { \
  79. return m_ptr; \
  80. } \
  81. \
  82. void swap(name & ot) \
  83. { \
  84. T * tmp = ot.m_ptr; \
  85. ot.m_ptr = m_ptr; \
  86. m_ptr = tmp; \
  87. } \
  88. };
  89. #define wxDEFINE_SCOPED_ARRAY(T, name) \
  90. name::~name() \
  91. { \
  92. wxCHECKED_DELETE_ARRAY(m_ptr); \
  93. } \
  94. void name::reset(T * p){ \
  95. if (m_ptr != p) \
  96. { \
  97. wxCHECKED_DELETE_ARRAY(m_ptr); \
  98. m_ptr = p; \
  99. } \
  100. }
  101. #endif // _WX_SCOPED_ARRAY_H_