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