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