]>
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
6 // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
7 // (c) 2009 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_SCOPED_ARRAY_H_
12 #define _WX_SCOPED_ARRAY_H_
15 #include "wx/checkeddelete.h"
17 // ----------------------------------------------------------------------------
18 // wxScopedArray: A scoped array
19 // ----------------------------------------------------------------------------
25 typedef T element_type
;
27 wxEXPLICIT
wxScopedArray(T
* array
= NULL
) : m_array(array
) { }
29 ~wxScopedArray() { delete [] m_array
; }
31 // test for pointer validity: defining conversion to unspecified_bool_type
32 // and not more obvious bool to avoid implicit conversions to integer types
33 typedef T
*(wxScopedArray
<T
>::*unspecified_bool_type
)() const;
34 operator unspecified_bool_type() const
36 return m_array
? &wxScopedArray
<T
>::get
: NULL
;
39 void reset(T
*array
= NULL
)
41 if ( array
!= m_array
)
48 T
& operator[](size_t n
) const { return m_array
[n
]; }
50 T
*get() const { return m_array
; }
52 void swap(wxScopedArray
&other
)
54 T
* const tmp
= other
.m_array
;
55 other
.m_array
= m_array
;
62 DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedArray
, T
)
65 // ----------------------------------------------------------------------------
66 // old macro based implementation
67 // ----------------------------------------------------------------------------
69 // the same but for arrays instead of simple pointers
70 #define wxDECLARE_SCOPED_ARRAY(T, name)\
76 name & operator=(name const &); \
79 wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
83 void reset(T * p = NULL); \
85 T & operator[](long int i) const\
87 wxASSERT(m_ptr != NULL); \
97 void swap(name & ot) \
105 #define wxDEFINE_SCOPED_ARRAY(T, name) \
108 wxCHECKED_DELETE_ARRAY(m_ptr); \
110 void name::reset(T * p){ \
113 wxCHECKED_DELETE_ARRAY(m_ptr); \
118 #endif // _WX_SCOPED_ARRAY_H_