]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/scopedptr.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/scopedptr.h
3 // Purpose: scoped smart pointer class
4 // Author: Jesse Lovelace <jllovela@eos.ncsu.edu>
6 // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
7 // (c) 2009 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // This class closely follows the implementation of the boost
12 // library scoped_ptr and is an adaption for c++ macro's in
13 // the wxWidgets project. The original authors of the boost
14 // scoped_ptr are given below with their respective copyrights.
16 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
17 // Copyright (c) 2001, 2002 Peter Dimov
19 // Permission to copy, use, modify, sell and distribute this software
20 // is granted provided this copyright notice appears in all copies.
21 // This software is provided "as is" without express or implied
22 // warranty, and with no claim as to its suitability for any purpose.
24 // See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
27 #ifndef _WX_SCOPED_PTR_H_
28 #define _WX_SCOPED_PTR_H_
31 #include "wx/checkeddelete.h"
33 // ----------------------------------------------------------------------------
34 // wxScopedPtr: A scoped pointer
35 // ----------------------------------------------------------------------------
41 typedef T element_type
;
43 wxEXPLICIT
wxScopedPtr(T
* ptr
= NULL
) : m_ptr(ptr
) { }
45 ~wxScopedPtr() { wxCHECKED_DELETE(m_ptr
); }
47 // test for pointer validity: defining conversion to unspecified_bool_type
48 // and not more obvious bool to avoid implicit conversions to integer types
50 // this compiler is too dumb to use unspecified_bool_type operator in tests
51 // of the form "if ( !ptr )"
52 typedef bool unspecified_bool_type
;
54 typedef T
*(wxScopedPtr
<T
>::*unspecified_bool_type
)() const;
55 #endif // __BORLANDC__
56 operator unspecified_bool_type() const
58 return m_ptr
? &wxScopedPtr
<T
>::get
: NULL
;
61 void reset(T
* ptr
= NULL
)
65 wxCHECKED_DELETE(m_ptr
);
79 wxASSERT(m_ptr
!= NULL
);
83 T
* operator->() const
85 wxASSERT(m_ptr
!= NULL
);
94 void swap(wxScopedPtr
& other
)
96 T
* const tmp
= other
.m_ptr
;
104 DECLARE_NO_COPY_TEMPLATE_CLASS(wxScopedPtr
, T
)
107 // ----------------------------------------------------------------------------
108 // old macro based implementation
109 // ----------------------------------------------------------------------------
111 /* The type being used *must* be complete at the time
112 that wxDEFINE_SCOPED_* is called or a compiler error will result.
113 This is because the class checks for the completeness of the type
116 #define wxDECLARE_SCOPED_PTR(T, name) \
122 name(name const &); \
123 name & operator=(name const &); \
126 wxEXPLICIT name(T * ptr = NULL) \
131 void reset(T * ptr = NULL); \
140 T & operator*() const \
142 wxASSERT(m_ptr != NULL); \
146 T * operator->() const \
148 wxASSERT(m_ptr != NULL); \
157 void swap(name & ot) \
159 T * tmp = ot.m_ptr; \
165 #define wxDEFINE_SCOPED_PTR(T, name)\
166 void name::reset(T * ptr) \
170 wxCHECKED_DELETE(m_ptr); \
176 wxCHECKED_DELETE(m_ptr); \
179 // this macro can be used for the most common case when you want to declare and
180 // define the scoped pointer at the same time and want to use the standard
181 // naming convention: auto pointer to Foo is called FooPtr
182 #define wxDEFINE_SCOPED_PTR_TYPE(T) \
183 wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
184 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
186 // ----------------------------------------------------------------------------
187 // "Tied" scoped pointer: same as normal one but also sets the value of
188 // some other variable to the pointer value
189 // ----------------------------------------------------------------------------
191 #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
192 wxDEFINE_SCOPED_PTR_TYPE(T) \
193 class T ## TiedPtr : public T ## Ptr \
196 T ## TiedPtr(T **pp, T *p) \
197 : T ## Ptr(p), m_pp(pp) \
213 #endif // _WX_SCOPED_PTR_H_