]>
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>
8 // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // This class closely follows the implementation of the boost
13 // library scoped_ptr and is an adaption for c++ macro's in
14 // the wxWidgets project. The original authors of the boost
15 // scoped_ptr are given below with their respective copyrights.
17 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
18 // Copyright (c) 2001, 2002 Peter Dimov
20 // Permission to copy, use, modify, sell and distribute this software
21 // is granted provided this copyright notice appears in all copies.
22 // This software is provided "as is" without express or implied
23 // warranty, and with no claim as to its suitability for any purpose.
25 // See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
28 #ifndef __WX_SCOPED_POINTER__
29 #define __WX_SCOPED_POINTER__
33 // ----------------------------------------------------------------------------
34 // wxScopedPtr: A scoped pointer
35 // ----------------------------------------------------------------------------
43 wxScopedPtr(wxScopedPtr
const &);
44 wxScopedPtr
& operator=(wxScopedPtr
const &);
47 typedef T element_type
;
49 wxEXPLICIT
wxScopedPtr(T
* ptr
= NULL
)
58 // test for pointer validity: defining conversion to unspecified_bool_type
59 // and not more obvious bool to avoid implicit conversions to integer types
60 typedef T
*(wxScopedPtr
<T
>::*unspecified_bool_type
)() const;
61 operator unspecified_bool_type() const
63 return m_ptr
? &wxScopedPtr
<T
>::get
: NULL
;
66 void reset(T
* ptr
= NULL
)
84 wxASSERT(m_ptr
!= NULL
);
88 T
* operator->() const
90 wxASSERT(m_ptr
!= NULL
);
99 void swap(wxScopedPtr
& ot
)
107 // ----------------------------------------------------------------------------
108 // old macro based implementation
109 // ----------------------------------------------------------------------------
112 checked deleters are used to make sure that the type being deleted is really
113 a complete type.: otherwise sizeof() would result in a compile-time error
115 do { ... } while ( 0 ) construct is used to have an anonymous scope
116 (otherwise we could have name clashes between different "complete"s) but
117 still force a semicolon after the macro
121 #define wxFOR_ONCE(name) for(int name=0; name<1; name++)
122 #define wxPRE_NO_WARNING_SCOPE(name) wxFOR_ONCE(wxMAKE_UNIQUE_NAME(name))
123 #define wxPOST_NO_WARNING_SCOPE(name)
125 #define wxPRE_NO_WARNING_SCOPE(name) do
126 #define wxPOST_NO_WARNING_SCOPE(name) while ( wxFalse )
129 #define wxCHECKED_DELETE(ptr) \
130 wxPRE_NO_WARNING_SCOPE(scope_var1) \
132 typedef char complete[sizeof(*ptr)]; \
134 } wxPOST_NO_WARNING_SCOPE(scope_var1)
136 #define wxCHECKED_DELETE_ARRAY(ptr) \
137 wxPRE_NO_WARNING_SCOPE(scope_var2) \
139 typedef char complete[sizeof(*ptr)]; \
141 } wxPOST_NO_WARNING_SCOPE(scope_var2)
143 /* The type being used *must* be complete at the time
144 that wxDEFINE_SCOPED_* is called or a compiler error will result.
145 This is because the class checks for the completeness of the type
148 #define wxDECLARE_SCOPED_PTR(T, name) \
154 name(name const &); \
155 name & operator=(name const &); \
158 wxEXPLICIT name(T * ptr = NULL) \
163 void reset(T * ptr = NULL) \
179 T & operator*() const \
181 wxASSERT(m_ptr != NULL); \
185 T * operator->() const \
187 wxASSERT(m_ptr != NULL); \
196 void swap(name & ot) \
198 T * tmp = ot.m_ptr; \
204 #define wxDEFINE_SCOPED_PTR(T, name)\
207 wxCHECKED_DELETE(m_ptr); \
210 // this macro can be used for the most common case when you want to declare and
211 // define the scoped pointer at the same time and want to use the standard
212 // naming convention: auto pointer to Foo is called FooPtr
213 #define wxDEFINE_SCOPED_PTR_TYPE(T) \
214 wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
215 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
217 // the same but for arrays instead of simple pointers
218 #define wxDECLARE_SCOPED_ARRAY(T, name)\
223 name(name const &); \
224 name & operator=(name const &); \
227 wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
231 void reset(T * p = NULL); \
233 T & operator[](long int i) const\
235 wxASSERT(m_ptr != NULL); \
245 void swap(name & ot) \
247 T * tmp = ot.m_ptr; \
253 #define wxDEFINE_SCOPED_ARRAY(T, name) \
256 wxCHECKED_DELETE_ARRAY(m_ptr); \
258 void name::reset(T * p){ \
261 wxCHECKED_DELETE_ARRAY(m_ptr); \
266 // ----------------------------------------------------------------------------
267 // "Tied" scoped pointer: same as normal one but also sets the value of
268 // some other variable to the pointer value
269 // ----------------------------------------------------------------------------
271 #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
272 wxDEFINE_SCOPED_PTR_TYPE(T) \
273 class T ## TiedPtr : public T ## Ptr \
276 T ## TiedPtr(T **pp, T *p) \
277 : T ## Ptr(p), m_pp(pp) \
293 #endif // __WX_SCOPED_POINTER__