]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/scopedarray.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/scopedarray.h
3 // Purpose: scoped smart pointer class
4 // Author: Vadim Zeitlin
7 // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
8 // (c) 2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_SCOPED_ARRAY_H_
13 #define _WX_SCOPED_ARRAY_H_
16 #include "wx/checkeddelete.h"
18 // ----------------------------------------------------------------------------
19 // wxScopedArray: A scoped array
20 // ----------------------------------------------------------------------------
26 typedef T element_type
;
28 wxEXPLICIT
wxScopedArray(T
* array
= NULL
) : m_array(array
) { }
30 ~wxScopedArray() { delete [] m_array
; }
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
37 return m_array
? &wxScopedArray
<T
>::get
: NULL
;
40 void reset(T
*array
= NULL
)
42 if ( array
!= m_array
)
49 T
& operator[](size_t n
) const { return m_array
[n
]; }
51 T
*get() const { return m_array
; }
53 void swap(wxScopedArray
&other
)
55 T
* const tmp
= other
.m_array
;
56 other
.m_array
= m_array
;
63 DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedArray
, T
)
66 // ----------------------------------------------------------------------------
67 // old macro based implementation
68 // ----------------------------------------------------------------------------
70 // the same but for arrays instead of simple pointers
71 #define wxDECLARE_SCOPED_ARRAY(T, name)\
77 name & operator=(name const &); \
80 wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
84 void reset(T * p = NULL); \
86 T & operator[](long int i) const\
88 wxASSERT(m_ptr != NULL); \
98 void swap(name & ot) \
100 T * tmp = ot.m_ptr; \
106 #define wxDEFINE_SCOPED_ARRAY(T, name) \
109 wxCHECKED_DELETE_ARRAY(m_ptr); \
111 void name::reset(T * p){ \
114 wxCHECKED_DELETE_ARRAY(m_ptr); \
119 #endif // _WX_SCOPED_ARRAY_H_