]>
Commit | Line | Data |
---|---|---|
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) | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
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. | |
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 | ||
33 | /* | |
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 | |
36 | ||
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 | |
40 | */ | |
41 | ||
42 | #define wxCHECKED_DELETE(ptr) \ | |
43 | do \ | |
44 | { \ | |
45 | typedef char complete[sizeof(*ptr)]; \ | |
46 | delete ptr; \ | |
47 | } while ( 0 ) | |
48 | ||
49 | #define wxCHECKED_DELETE_ARRAY(ptr) \ | |
50 | do \ | |
51 | { \ | |
52 | typedef char complete[sizeof(*ptr)]; \ | |
53 | delete [] ptr; \ | |
54 | } while ( 0 ) | |
55 | ||
56 | /* These scoped pointers are *not* assignable and cannot be used | |
57 | within a container. Look for wxDECLARE_SHARED_PTR for this | |
58 | functionality. | |
59 | ||
60 | In addition, the type being used *must* be complete at the time | |
61 | that wxDEFINE_SCOPED_* is called or a compiler error will result. | |
62 | This is because the class checks for the completeness of the type | |
63 | being used. | |
64 | */ | |
65 | ||
66 | ||
67 | #define wxDECLARE_SCOPED_PTR(T, name) \ | |
68 | class name \ | |
69 | { \ | |
70 | private: \ | |
71 | T * m_ptr; \ | |
72 | \ | |
73 | name(name const &); \ | |
74 | name & operator=(name const &); \ | |
75 | \ | |
76 | public: \ | |
77 | wxEXPLICIT name(T * ptr = NULL) \ | |
78 | : m_ptr(ptr) { } \ | |
79 | \ | |
80 | ~name(); \ | |
81 | \ | |
82 | void reset(T * ptr = NULL) \ | |
83 | { \ | |
84 | if (m_ptr != ptr) \ | |
85 | { \ | |
86 | delete m_ptr; \ | |
87 | m_ptr = ptr; \ | |
88 | } \ | |
89 | } \ | |
90 | \ | |
91 | T *release() \ | |
92 | { \ | |
93 | T *ptr = m_ptr; \ | |
94 | m_ptr = NULL; \ | |
95 | return ptr; \ | |
96 | } \ | |
97 | \ | |
98 | T & operator*() const \ | |
99 | { \ | |
100 | wxASSERT(m_ptr != NULL); \ | |
101 | return *m_ptr; \ | |
102 | } \ | |
103 | \ | |
104 | T * operator->() const \ | |
105 | { \ | |
106 | wxASSERT(m_ptr != NULL); \ | |
107 | return m_ptr; \ | |
108 | } \ | |
109 | \ | |
110 | T * get() const \ | |
111 | { \ | |
112 | return m_ptr; \ | |
113 | } \ | |
114 | \ | |
115 | void swap(name & ot) \ | |
116 | { \ | |
117 | T * tmp = ot.m_ptr; \ | |
118 | ot.m_ptr = m_ptr; \ | |
119 | m_ptr = tmp; \ | |
120 | } \ | |
121 | }; | |
122 | ||
123 | #define wxDEFINE_SCOPED_PTR(T, name)\ | |
124 | name::~name() \ | |
125 | { \ | |
126 | wxCHECKED_DELETE(m_ptr); \ | |
127 | } | |
128 | ||
129 | // this macro can be used for the most common case when you want to declare and | |
130 | // define the scoped pointer at the same time and want to use the standard | |
131 | // naming convention: auto pointer to Foo is called FooPtr | |
132 | #define wxDEFINE_SCOPED_PTR_TYPE(T) \ | |
133 | wxDECLARE_SCOPED_PTR(T, T ## Ptr); \ | |
134 | wxDEFINE_SCOPED_PTR(T, T ## Ptr) | |
135 | ||
136 | // the same but for arrays instead of simple pointers | |
137 | #define wxDECLARE_SCOPED_ARRAY(T, name)\ | |
138 | class name \ | |
139 | { \ | |
140 | private: \ | |
141 | T * m_ptr; \ | |
142 | name(name const &); \ | |
143 | name & operator=(name const &); \ | |
144 | \ | |
145 | public: \ | |
146 | wxEXPLICIT name(T * p = NULL) : m_ptr(p) \ | |
147 | {} \ | |
148 | \ | |
149 | ~name(); \ | |
150 | void reset(T * p = NULL); \ | |
151 | \ | |
152 | T & operator[](long int i) const\ | |
153 | { \ | |
154 | wxASSERT(m_ptr != NULL); \ | |
155 | wxASSERT(i >= 0); \ | |
156 | return m_ptr[i]; \ | |
157 | } \ | |
158 | \ | |
159 | T * get() const \ | |
160 | { \ | |
161 | return m_ptr; \ | |
162 | } \ | |
163 | \ | |
164 | void swap(name & ot) \ | |
165 | { \ | |
166 | T * tmp = ot.m_ptr; \ | |
167 | ot.m_ptr = m_ptr; \ | |
168 | m_ptr = tmp; \ | |
169 | } \ | |
170 | }; | |
171 | ||
172 | #define wxDEFINE_SCOPED_ARRAY(T, name) \ | |
173 | name::~name() \ | |
174 | { \ | |
175 | wxCHECKED_DELETE_ARRAY(m_ptr); \ | |
176 | } \ | |
177 | void name::reset(T * p){ \ | |
178 | if (m_ptr != p) \ | |
179 | { \ | |
180 | wxCHECKED_DELETE_ARRAY(m_ptr); \ | |
181 | m_ptr = p; \ | |
182 | } \ | |
183 | } | |
184 | ||
185 | // ---------------------------------------------------------------------------- | |
186 | // "Tied" scoped pointer: same as normal one but also sets the value of | |
187 | // some other variable to the pointer value | |
188 | // ---------------------------------------------------------------------------- | |
189 | ||
190 | #define wxDEFINE_TIED_SCOPED_PTR_TYPE(T) \ | |
191 | wxDEFINE_SCOPED_PTR_TYPE(T); \ | |
192 | class T ## TiedPtr : public T ## Ptr \ | |
193 | { \ | |
194 | public: \ | |
195 | T ## TiedPtr(T **pp, T *p) \ | |
196 | : T ## Ptr(p), m_pp(pp) \ | |
197 | { \ | |
198 | m_pOld = *pp; \ | |
199 | *pp = p; \ | |
200 | } \ | |
201 | \ | |
202 | ~ T ## TiedPtr() \ | |
203 | { \ | |
204 | *m_pp = m_pOld; \ | |
205 | } \ | |
206 | \ | |
207 | private: \ | |
208 | T **m_pp; \ | |
209 | T *m_pOld; \ | |
210 | } | |
211 | ||
212 | #endif // __WX_SCOPED_POINTER__ | |
213 |