]>
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>
7 // Copyright: (c) Jesse Lovelace and original Boost authors (see below)
8 // (c) 2009 Vadim Zeitlin
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_PTR_H_
29 #define _WX_SCOPED_PTR_H_
32 #include "wx/checkeddelete.h"
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 // old macro based implementation
104 // ----------------------------------------------------------------------------
106 /* The type being used *must* be complete at the time
107 that wxDEFINE_SCOPED_* is called or a compiler error will result.
108 This is because the class checks for the completeness of the type
111 #define wxDECLARE_SCOPED_PTR(T, name) \
117 name(name const &); \
118 name & operator=(name const &); \
121 wxEXPLICIT name(T * ptr = NULL) \
126 void reset(T * ptr = NULL) \
142 T & operator*() const \
144 wxASSERT(m_ptr != NULL); \
148 T * operator->() const \
150 wxASSERT(m_ptr != NULL); \
159 void swap(name & ot) \
161 T * tmp = ot.m_ptr; \
167 #define wxDEFINE_SCOPED_PTR(T, name)\
170 wxCHECKED_DELETE(m_ptr); \
173 // this macro can be used for the most common case when you want to declare and
174 // define the scoped pointer at the same time and want to use the standard
175 // naming convention: auto pointer to Foo is called FooPtr
176 #define wxDEFINE_SCOPED_PTR_TYPE(T) \
177 wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
178 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
180 // ----------------------------------------------------------------------------
181 // "Tied" scoped pointer: same as normal one but also sets the value of
182 // some other variable to the pointer value
183 // ----------------------------------------------------------------------------
185 #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
186 wxDEFINE_SCOPED_PTR_TYPE(T) \
187 class T ## TiedPtr : public T ## Ptr \
190 T ## TiedPtr(T **pp, T *p) \
191 : T ## Ptr(p), m_pp(pp) \
207 #endif // _WX_SCOPED_PTR_H_