]>
Commit | Line | Data |
---|---|---|
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 | ||
37 | template <class T> | |
38 | class wxScopedPtr | |
39 | { | |
40 | private: | |
41 | T * m_ptr; | |
42 | ||
43 | wxScopedPtr(wxScopedPtr const &); | |
44 | wxScopedPtr & operator=(wxScopedPtr const &); | |
45 | ||
46 | public: | |
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 | ||
58 | void reset(T * ptr = NULL) | |
59 | { | |
60 | if (m_ptr != ptr) | |
61 | { | |
62 | delete m_ptr; | |
63 | m_ptr = ptr; | |
64 | } | |
65 | } | |
66 | ||
67 | T *release() | |
68 | { | |
69 | T *ptr = m_ptr; | |
70 | m_ptr = NULL; | |
71 | return ptr; | |
72 | } | |
73 | ||
74 | T & operator*() const | |
75 | { | |
76 | wxASSERT(m_ptr != NULL); | |
77 | return *m_ptr; | |
78 | } | |
79 | ||
80 | T * operator->() const | |
81 | { | |
82 | wxASSERT(m_ptr != NULL); | |
83 | return m_ptr; | |
84 | } | |
85 | ||
86 | T * get() const | |
87 | { | |
88 | return m_ptr; | |
89 | } | |
90 | ||
91 | void swap(wxScopedPtr & ot) | |
92 | { | |
93 | T * tmp = ot.m_ptr; | |
94 | ot.m_ptr = m_ptr; | |
95 | m_ptr = tmp; | |
96 | } | |
97 | }; | |
98 | ||
99 | // ---------------------------------------------------------------------------- | |
100 | // old macro based implementation | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
d566b182 VZ |
103 | /* |
104 | checked deleters are used to make sure that the type being deleted is really | |
105 | a complete type.: otherwise sizeof() would result in a compile-time error | |
106 | ||
107 | do { ... } while ( 0 ) construct is used to have an anonymous scope | |
108 | (otherwise we could have name clashes between different "complete"s) but | |
109 | still force a semicolon after the macro | |
5b222f1c JS |
110 | */ |
111 | ||
373a5fb3 | 112 | #ifdef __WATCOMC__ |
5c519b6c WS |
113 | #define wxFOR_ONCE(name) for(int name=0; name<1; name++) |
114 | #define wxPRE_NO_WARNING_SCOPE(name) wxFOR_ONCE(wxMAKE_UNIQUE_NAME(name)) | |
115 | #define wxPOST_NO_WARNING_SCOPE(name) | |
373a5fb3 | 116 | #else |
5c519b6c | 117 | #define wxPRE_NO_WARNING_SCOPE(name) do |
17a1ebd1 | 118 | #define wxPOST_NO_WARNING_SCOPE(name) while ( wxFalse ) |
373a5fb3 WS |
119 | #endif |
120 | ||
d566b182 | 121 | #define wxCHECKED_DELETE(ptr) \ |
5c519b6c | 122 | wxPRE_NO_WARNING_SCOPE(scope_var1) \ |
d566b182 VZ |
123 | { \ |
124 | typedef char complete[sizeof(*ptr)]; \ | |
125 | delete ptr; \ | |
5c519b6c | 126 | } wxPOST_NO_WARNING_SCOPE(scope_var1) |
d566b182 VZ |
127 | |
128 | #define wxCHECKED_DELETE_ARRAY(ptr) \ | |
5c519b6c | 129 | wxPRE_NO_WARNING_SCOPE(scope_var2) \ |
d566b182 VZ |
130 | { \ |
131 | typedef char complete[sizeof(*ptr)]; \ | |
132 | delete [] ptr; \ | |
5c519b6c | 133 | } wxPOST_NO_WARNING_SCOPE(scope_var2) |
5b222f1c | 134 | |
89969a91 | 135 | /* The type being used *must* be complete at the time |
5b222f1c JS |
136 | that wxDEFINE_SCOPED_* is called or a compiler error will result. |
137 | This is because the class checks for the completeness of the type | |
89969a91 | 138 | being used. */ |
5b222f1c JS |
139 | |
140 | #define wxDECLARE_SCOPED_PTR(T, name) \ | |
141 | class name \ | |
142 | { \ | |
143 | private: \ | |
144 | T * m_ptr; \ | |
145 | \ | |
146 | name(name const &); \ | |
147 | name & operator=(name const &); \ | |
148 | \ | |
149 | public: \ | |
5455e227 | 150 | wxEXPLICIT name(T * ptr = NULL) \ |
5b222f1c JS |
151 | : m_ptr(ptr) { } \ |
152 | \ | |
153 | ~name(); \ | |
154 | \ | |
155 | void reset(T * ptr = NULL) \ | |
156 | { \ | |
157 | if (m_ptr != ptr) \ | |
158 | { \ | |
159 | delete m_ptr; \ | |
160 | m_ptr = ptr; \ | |
161 | } \ | |
162 | } \ | |
163 | \ | |
5455e227 VZ |
164 | T *release() \ |
165 | { \ | |
166 | T *ptr = m_ptr; \ | |
167 | m_ptr = NULL; \ | |
168 | return ptr; \ | |
169 | } \ | |
170 | \ | |
5b222f1c JS |
171 | T & operator*() const \ |
172 | { \ | |
173 | wxASSERT(m_ptr != NULL); \ | |
174 | return *m_ptr; \ | |
175 | } \ | |
176 | \ | |
177 | T * operator->() const \ | |
178 | { \ | |
179 | wxASSERT(m_ptr != NULL); \ | |
180 | return m_ptr; \ | |
181 | } \ | |
182 | \ | |
183 | T * get() const \ | |
184 | { \ | |
185 | return m_ptr; \ | |
186 | } \ | |
187 | \ | |
188 | void swap(name & ot) \ | |
189 | { \ | |
190 | T * tmp = ot.m_ptr; \ | |
191 | ot.m_ptr = m_ptr; \ | |
192 | m_ptr = tmp; \ | |
193 | } \ | |
194 | }; | |
195 | ||
196 | #define wxDEFINE_SCOPED_PTR(T, name)\ | |
197 | name::~name() \ | |
198 | { \ | |
bb0a3c4a | 199 | wxCHECKED_DELETE(m_ptr); \ |
5b222f1c JS |
200 | } |
201 | ||
d83eeece VZ |
202 | // this macro can be used for the most common case when you want to declare and |
203 | // define the scoped pointer at the same time and want to use the standard | |
204 | // naming convention: auto pointer to Foo is called FooPtr | |
d0ee33f5 WS |
205 | #define wxDEFINE_SCOPED_PTR_TYPE(T) \ |
206 | wxDECLARE_SCOPED_PTR(T, T ## Ptr) \ | |
d83eeece VZ |
207 | wxDEFINE_SCOPED_PTR(T, T ## Ptr) |
208 | ||
209 | // the same but for arrays instead of simple pointers | |
5b222f1c JS |
210 | #define wxDECLARE_SCOPED_ARRAY(T, name)\ |
211 | class name \ | |
212 | { \ | |
213 | private: \ | |
214 | T * m_ptr; \ | |
215 | name(name const &); \ | |
216 | name & operator=(name const &); \ | |
217 | \ | |
218 | public: \ | |
219 | wxEXPLICIT name(T * p = NULL) : m_ptr(p) \ | |
220 | {} \ | |
221 | \ | |
222 | ~name(); \ | |
223 | void reset(T * p = NULL); \ | |
224 | \ | |
225 | T & operator[](long int i) const\ | |
226 | { \ | |
227 | wxASSERT(m_ptr != NULL); \ | |
228 | wxASSERT(i >= 0); \ | |
229 | return m_ptr[i]; \ | |
230 | } \ | |
231 | \ | |
232 | T * get() const \ | |
233 | { \ | |
234 | return m_ptr; \ | |
235 | } \ | |
236 | \ | |
237 | void swap(name & ot) \ | |
238 | { \ | |
239 | T * tmp = ot.m_ptr; \ | |
240 | ot.m_ptr = m_ptr; \ | |
241 | m_ptr = tmp; \ | |
242 | } \ | |
243 | }; | |
244 | ||
245 | #define wxDEFINE_SCOPED_ARRAY(T, name) \ | |
bb0a3c4a VZ |
246 | name::~name() \ |
247 | { \ | |
248 | wxCHECKED_DELETE_ARRAY(m_ptr); \ | |
249 | } \ | |
250 | void name::reset(T * p){ \ | |
251 | if (m_ptr != p) \ | |
252 | { \ | |
253 | wxCHECKED_DELETE_ARRAY(m_ptr); \ | |
254 | m_ptr = p; \ | |
255 | } \ | |
5b222f1c JS |
256 | } |
257 | ||
38f15267 VZ |
258 | // ---------------------------------------------------------------------------- |
259 | // "Tied" scoped pointer: same as normal one but also sets the value of | |
260 | // some other variable to the pointer value | |
261 | // ---------------------------------------------------------------------------- | |
262 | ||
263 | #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \ | |
259c43f6 | 264 | wxDEFINE_SCOPED_PTR_TYPE(T) \ |
38f15267 VZ |
265 | class T ## TiedPtr : public T ## Ptr \ |
266 | { \ | |
267 | public: \ | |
268 | T ## TiedPtr(T **pp, T *p) \ | |
269 | : T ## Ptr(p), m_pp(pp) \ | |
270 | { \ | |
271 | m_pOld = *pp; \ | |
272 | *pp = p; \ | |
273 | } \ | |
274 | \ | |
275 | ~ T ## TiedPtr() \ | |
276 | { \ | |
277 | *m_pp = m_pOld; \ | |
278 | } \ | |
279 | \ | |
280 | private: \ | |
281 | T **m_pp; \ | |
282 | T *m_pOld; \ | |
259c43f6 | 283 | }; |
38f15267 | 284 | |
d83eeece VZ |
285 | #endif // __WX_SCOPED_POINTER__ |
286 |