]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/ptr_scpd.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: scoped smart pointer class
4 // Author: Jesse Lovelace <jllovela@eos.ncsu.edu>
5 // Modified by: Vadim Zeitlin to add template wxScopedArray
8 // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
9 // (c) 2009 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // This class closely follows the implementation of the boost
14 // library scoped_ptr and is an adaption for c++ macro's in
15 // the wxWidgets project. The original authors of the boost
16 // scoped_ptr are given below with their respective copyrights.
18 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
19 // Copyright (c) 2001, 2002 Peter Dimov
21 // Permission to copy, use, modify, sell and distribute this software
22 // is granted provided this copyright notice appears in all copies.
23 // This software is provided "as is" without express or implied
24 // warranty, and with no claim as to its suitability for any purpose.
26 // See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
29 #ifndef __WX_SCOPED_POINTER__
30 #define __WX_SCOPED_POINTER__
34 // ----------------------------------------------------------------------------
35 // wxScopedPtr: A scoped pointer
36 // ----------------------------------------------------------------------------
42 typedef T element_type
;
44 wxEXPLICIT
wxScopedPtr(T
* ptr
= NULL
) : m_ptr(ptr
) { }
46 ~wxScopedPtr() { delete m_ptr
; }
48 // test for pointer validity: defining conversion to unspecified_bool_type
49 // and not more obvious bool to avoid implicit conversions to integer types
50 typedef T
*(wxScopedPtr
<T
>::*unspecified_bool_type
)() const;
51 operator unspecified_bool_type() const
53 return m_ptr
? &wxScopedPtr
<T
>::get
: NULL
;
56 void reset(T
* ptr
= NULL
)
74 wxASSERT(m_ptr
!= NULL
);
78 T
* operator->() const
80 wxASSERT(m_ptr
!= NULL
);
89 void swap(wxScopedPtr
& other
)
91 T
* const tmp
= other
.m_ptr
;
99 DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr
, T
)
102 // ----------------------------------------------------------------------------
103 // wxScopedArray: A scoped array
104 // ----------------------------------------------------------------------------
110 typedef T element_type
;
112 wxEXPLICIT
wxScopedArray(T
* array
= NULL
) : m_array(array
) { }
114 ~wxScopedArray() { delete [] m_array
; }
116 // test for pointer validity: defining conversion to unspecified_bool_type
117 // and not more obvious bool to avoid implicit conversions to integer types
118 typedef T
*(wxScopedArray
<T
>::*unspecified_bool_type
)() const;
119 operator unspecified_bool_type() const
121 return m_array
? &wxScopedArray
<T
>::get
: NULL
;
124 void reset(T
*array
= NULL
)
126 if ( array
!= m_array
)
133 T
& operator[](size_t n
) const { return m_array
[n
]; }
135 T
*get() const { return m_array
; }
137 void swap(wxScopedArray
&other
)
139 T
* const tmp
= other
.m_array
;
140 other
.m_array
= m_array
;
147 DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedArray
, T
)
150 // ----------------------------------------------------------------------------
151 // old macro based implementation
152 // ----------------------------------------------------------------------------
155 checked deleters are used to make sure that the type being deleted is really
156 a complete type.: otherwise sizeof() would result in a compile-time error
158 do { ... } while ( 0 ) construct is used to have an anonymous scope
159 (otherwise we could have name clashes between different "complete"s) but
160 still force a semicolon after the macro
164 #define wxFOR_ONCE(name) for(int name=0; name<1; name++)
165 #define wxPRE_NO_WARNING_SCOPE(name) wxFOR_ONCE(wxMAKE_UNIQUE_NAME(name))
166 #define wxPOST_NO_WARNING_SCOPE(name)
168 #define wxPRE_NO_WARNING_SCOPE(name) do
169 #define wxPOST_NO_WARNING_SCOPE(name) while ( wxFalse )
172 #define wxCHECKED_DELETE(ptr) \
173 wxPRE_NO_WARNING_SCOPE(scope_var1) \
175 typedef char complete[sizeof(*ptr)]; \
177 } wxPOST_NO_WARNING_SCOPE(scope_var1)
179 #define wxCHECKED_DELETE_ARRAY(ptr) \
180 wxPRE_NO_WARNING_SCOPE(scope_var2) \
182 typedef char complete[sizeof(*ptr)]; \
184 } wxPOST_NO_WARNING_SCOPE(scope_var2)
186 /* The type being used *must* be complete at the time
187 that wxDEFINE_SCOPED_* is called or a compiler error will result.
188 This is because the class checks for the completeness of the type
191 #define wxDECLARE_SCOPED_PTR(T, name) \
197 name(name const &); \
198 name & operator=(name const &); \
201 wxEXPLICIT name(T * ptr = NULL) \
206 void reset(T * ptr = NULL) \
222 T & operator*() const \
224 wxASSERT(m_ptr != NULL); \
228 T * operator->() const \
230 wxASSERT(m_ptr != NULL); \
239 void swap(name & ot) \
241 T * tmp = ot.m_ptr; \
247 #define wxDEFINE_SCOPED_PTR(T, name)\
250 wxCHECKED_DELETE(m_ptr); \
253 // this macro can be used for the most common case when you want to declare and
254 // define the scoped pointer at the same time and want to use the standard
255 // naming convention: auto pointer to Foo is called FooPtr
256 #define wxDEFINE_SCOPED_PTR_TYPE(T) \
257 wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
258 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
260 // the same but for arrays instead of simple pointers
261 #define wxDECLARE_SCOPED_ARRAY(T, name)\
266 name(name const &); \
267 name & operator=(name const &); \
270 wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
274 void reset(T * p = NULL); \
276 T & operator[](long int i) const\
278 wxASSERT(m_ptr != NULL); \
288 void swap(name & ot) \
290 T * tmp = ot.m_ptr; \
296 #define wxDEFINE_SCOPED_ARRAY(T, name) \
299 wxCHECKED_DELETE_ARRAY(m_ptr); \
301 void name::reset(T * p){ \
304 wxCHECKED_DELETE_ARRAY(m_ptr); \
309 // ----------------------------------------------------------------------------
310 // "Tied" scoped pointer: same as normal one but also sets the value of
311 // some other variable to the pointer value
312 // ----------------------------------------------------------------------------
314 #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
315 wxDEFINE_SCOPED_PTR_TYPE(T) \
316 class T ## TiedPtr : public T ## Ptr \
319 T ## TiedPtr(T **pp, T *p) \
320 : T ## Ptr(p), m_pp(pp) \
336 #endif // __WX_SCOPED_POINTER__