]> git.saurik.com Git - wxWidgets.git/blame - include/wx/ptr_scpd.h
Removed SetPropertiesFlag() (high-level function using 'undocumented' wxPGProperty...
[wxWidgets.git] / include / wx / ptr_scpd.h
CommitLineData
5b222f1c
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: wx/ptr_scpd.h
3// Purpose: scoped smart pointer class
4// Author: Jesse Lovelace <jllovela@eos.ncsu.edu>
5// Modified by:
6// Created: 06/01/02
7// RCS-ID: $Id$
8// Copyright: (c) Jesse Lovelace and original Boost authors (see below)
65571936 9// Licence: wxWindows licence
5b222f1c
JS
10/////////////////////////////////////////////////////////////////////////////
11
12// This class closely follows the implementation of the boost
7e548f6b 13// library scoped_ptr and is an adaption for c++ macro's in
77ffb593 14// the wxWidgets project. The original authors of the boost
5b222f1c
JS
15// scoped_ptr are given below with their respective copyrights.
16
17// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
18// Copyright (c) 2001, 2002 Peter Dimov
19//
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.
24//
25// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
26//
27
28#ifndef __WX_SCOPED_POINTER__
29#define __WX_SCOPED_POINTER__
30
31#include "wx/defs.h"
32
89969a91
RR
33// ----------------------------------------------------------------------------
34// wxScopedPtr: A scoped pointer
35// ----------------------------------------------------------------------------
36
37template <class T>
38class wxScopedPtr
39{
40private:
41 T * m_ptr;
42
43 wxScopedPtr(wxScopedPtr const &);
44 wxScopedPtr & operator=(wxScopedPtr const &);
45
46public:
47 typedef T element_type;
48
49 wxEXPLICIT wxScopedPtr(T * ptr = NULL)
50 : m_ptr(ptr) { }
51
52 ~wxScopedPtr()
53 {
54 if (m_ptr)
55 delete m_ptr;
56 }
57
a60b0ddc
RR
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
62 {
63 return m_ptr ? &wxScopedPtr<T>::get : NULL;
64 }
65
89969a91
RR
66 void reset(T * ptr = NULL)
67 {
68 if (m_ptr != ptr)
69 {
70 delete m_ptr;
71 m_ptr = ptr;
72 }
73 }
74
75 T *release()
76 {
77 T *ptr = m_ptr;
78 m_ptr = NULL;
79 return ptr;
80 }
81
82 T & operator*() const
83 {
84 wxASSERT(m_ptr != NULL);
85 return *m_ptr;
86 }
87
88 T * operator->() const
89 {
90 wxASSERT(m_ptr != NULL);
91 return m_ptr;
92 }
93
94 T * get() const
95 {
96 return m_ptr;
97 }
98
99 void swap(wxScopedPtr & ot)
100 {
101 T * tmp = ot.m_ptr;
102 ot.m_ptr = m_ptr;
103 m_ptr = tmp;
104 }
105};
106
107// ----------------------------------------------------------------------------
108// old macro based implementation
109// ----------------------------------------------------------------------------
110
d566b182
VZ
111/*
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
114
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
5b222f1c
JS
118*/
119
373a5fb3 120#ifdef __WATCOMC__
5c519b6c
WS
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)
373a5fb3 124#else
5c519b6c 125 #define wxPRE_NO_WARNING_SCOPE(name) do
17a1ebd1 126 #define wxPOST_NO_WARNING_SCOPE(name) while ( wxFalse )
373a5fb3
WS
127#endif
128
d566b182 129#define wxCHECKED_DELETE(ptr) \
5c519b6c 130 wxPRE_NO_WARNING_SCOPE(scope_var1) \
d566b182
VZ
131 { \
132 typedef char complete[sizeof(*ptr)]; \
133 delete ptr; \
5c519b6c 134 } wxPOST_NO_WARNING_SCOPE(scope_var1)
d566b182
VZ
135
136#define wxCHECKED_DELETE_ARRAY(ptr) \
5c519b6c 137 wxPRE_NO_WARNING_SCOPE(scope_var2) \
d566b182
VZ
138 { \
139 typedef char complete[sizeof(*ptr)]; \
140 delete [] ptr; \
5c519b6c 141 } wxPOST_NO_WARNING_SCOPE(scope_var2)
5b222f1c 142
89969a91 143/* The type being used *must* be complete at the time
5b222f1c
JS
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
89969a91 146 being used. */
5b222f1c
JS
147
148#define wxDECLARE_SCOPED_PTR(T, name) \
149class name \
150{ \
151private: \
152 T * m_ptr; \
153 \
154 name(name const &); \
155 name & operator=(name const &); \
156 \
157public: \
5455e227 158 wxEXPLICIT name(T * ptr = NULL) \
5b222f1c
JS
159 : m_ptr(ptr) { } \
160 \
161 ~name(); \
162 \
163 void reset(T * ptr = NULL) \
164 { \
165 if (m_ptr != ptr) \
166 { \
167 delete m_ptr; \
168 m_ptr = ptr; \
169 } \
170 } \
171 \
5455e227
VZ
172 T *release() \
173 { \
174 T *ptr = m_ptr; \
175 m_ptr = NULL; \
176 return ptr; \
177 } \
178 \
5b222f1c
JS
179 T & operator*() const \
180 { \
181 wxASSERT(m_ptr != NULL); \
182 return *m_ptr; \
183 } \
184 \
185 T * operator->() const \
186 { \
187 wxASSERT(m_ptr != NULL); \
188 return m_ptr; \
189 } \
190 \
191 T * get() const \
192 { \
193 return m_ptr; \
194 } \
195 \
196 void swap(name & ot) \
197 { \
198 T * tmp = ot.m_ptr; \
199 ot.m_ptr = m_ptr; \
200 m_ptr = tmp; \
201 } \
202};
203
204#define wxDEFINE_SCOPED_PTR(T, name)\
205name::~name() \
206{ \
bb0a3c4a 207 wxCHECKED_DELETE(m_ptr); \
5b222f1c
JS
208}
209
d83eeece
VZ
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
d0ee33f5
WS
213#define wxDEFINE_SCOPED_PTR_TYPE(T) \
214 wxDECLARE_SCOPED_PTR(T, T ## Ptr) \
d83eeece
VZ
215 wxDEFINE_SCOPED_PTR(T, T ## Ptr)
216
217// the same but for arrays instead of simple pointers
5b222f1c
JS
218#define wxDECLARE_SCOPED_ARRAY(T, name)\
219class name \
220{ \
221private: \
222 T * m_ptr; \
223 name(name const &); \
224 name & operator=(name const &); \
225 \
226public: \
227 wxEXPLICIT name(T * p = NULL) : m_ptr(p) \
228 {} \
229 \
230 ~name(); \
231 void reset(T * p = NULL); \
232 \
233 T & operator[](long int i) const\
234 { \
235 wxASSERT(m_ptr != NULL); \
236 wxASSERT(i >= 0); \
237 return m_ptr[i]; \
238 } \
239 \
240 T * get() const \
241 { \
242 return m_ptr; \
243 } \
244 \
245 void swap(name & ot) \
246 { \
247 T * tmp = ot.m_ptr; \
248 ot.m_ptr = m_ptr; \
249 m_ptr = tmp; \
250 } \
251};
252
253#define wxDEFINE_SCOPED_ARRAY(T, name) \
bb0a3c4a
VZ
254name::~name() \
255{ \
256 wxCHECKED_DELETE_ARRAY(m_ptr); \
257} \
258void name::reset(T * p){ \
259 if (m_ptr != p) \
260 { \
261 wxCHECKED_DELETE_ARRAY(m_ptr); \
262 m_ptr = p; \
263 } \
5b222f1c
JS
264}
265
38f15267
VZ
266// ----------------------------------------------------------------------------
267// "Tied" scoped pointer: same as normal one but also sets the value of
268// some other variable to the pointer value
269// ----------------------------------------------------------------------------
270
271#define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \
259c43f6 272 wxDEFINE_SCOPED_PTR_TYPE(T) \
38f15267
VZ
273 class T ## TiedPtr : public T ## Ptr \
274 { \
275 public: \
276 T ## TiedPtr(T **pp, T *p) \
277 : T ## Ptr(p), m_pp(pp) \
278 { \
279 m_pOld = *pp; \
280 *pp = p; \
281 } \
282 \
283 ~ T ## TiedPtr() \
284 { \
285 *m_pp = m_pOld; \
286 } \
287 \
288 private: \
289 T **m_pp; \
290 T *m_pOld; \
259c43f6 291 };
38f15267 292
d83eeece
VZ
293#endif // __WX_SCOPED_POINTER__
294