]>
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__
34 checked deleters are used to make sure that the type being deleted is really
35 a complete type.: otherwise sizeof() would result in a compile-time error
37 do { ... } while ( 0 ) construct is used to have an anonymous scope
38 (otherwise we could have name clashes between different "complete"s) but
39 still force a semicolon after the macro
43 #define wxPRE_NO_WARNING_SCOPE for(int i=0;i<1;i++)
44 #define wxPOST_NO_WARNING_SCOPE
46 #define wxPRE_NO_WARNING_SCOPE do
47 #define wxPOST_NO_WARNING_SCOPE while ( 0 )
50 #define wxCHECKED_DELETE(ptr) \
51 wxPRE_NO_WARNING_SCOPE \
53 typedef char complete[sizeof(*ptr)]; \
55 } wxPOST_NO_WARNING_SCOPE
57 #define wxCHECKED_DELETE_ARRAY(ptr) \
58 wxPRE_NO_WARNING_SCOPE \
60 typedef char complete[sizeof(*ptr)]; \
62 } wxPOST_NO_WARNING_SCOPE
64 /* These scoped pointers are *not* assignable and cannot be used
65 within a container. Look for wxDECLARE_SHARED_PTR for this
68 In addition, the type being used *must* be complete at the time
69 that wxDEFINE_SCOPED_* is called or a compiler error will result.
70 This is because the class checks for the completeness of the type
75 #define wxDECLARE_SCOPED_PTR(T, name) \
82 name & operator=(name const &); \
85 wxEXPLICIT name(T * ptr = NULL) \
90 void reset(T * ptr = NULL) \
106 T & operator*() const \
108 wxASSERT(m_ptr != NULL); \
112 T * operator->() const \
114 wxASSERT(m_ptr != NULL); \
123 void swap(name & ot) \
125 T * tmp = ot.m_ptr; \
131 #define wxDEFINE_SCOPED_PTR(T, name)\
134 wxCHECKED_DELETE(m_ptr); \
137 // this macro can be used for the most common case when you want to declare and
138 // define the scoped pointer at the same time and want to use the standard
139 // naming convention: auto pointer to Foo is called FooPtr
140 #define wxDEFINE_SCOPED_PTR_TYPE(T) \
141 wxDECLARE_SCOPED_PTR(T, T ## Ptr); \
142 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
144 // the same but for arrays instead of simple pointers
145 #define wxDECLARE_SCOPED_ARRAY(T, name)\
150 name(name const &); \
151 name & operator=(name const &); \
154 wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
158 void reset(T * p = NULL); \
160 T & operator[](long int i) const\
162 wxASSERT(m_ptr != NULL); \
172 void swap(name & ot) \
174 T * tmp = ot.m_ptr; \
180 #define wxDEFINE_SCOPED_ARRAY(T, name) \
183 wxCHECKED_DELETE_ARRAY(m_ptr); \
185 void name::reset(T * p){ \
188 wxCHECKED_DELETE_ARRAY(m_ptr); \
193 // ----------------------------------------------------------------------------
194 // "Tied" scoped pointer: same as normal one but also sets the value of
195 // some other variable to the pointer value
196 // ----------------------------------------------------------------------------
198 #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
199 wxDEFINE_SCOPED_PTR_TYPE(T); \
200 class T ## TiedPtr : public T ## Ptr \
203 T ## TiedPtr(T **pp, T *p) \
204 : T ## Ptr(p), m_pp(pp) \
220 #endif // __WX_SCOPED_POINTER__