]>
Commit | Line | Data |
---|---|---|
c66cca2a VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/scopeguard.h | |
3 | // Purpose: declares wxwxScopeGuard and related macros | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 03.07.2003 | |
7 | // RCS-ID: $Id$ | |
77ffb593 | 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
65571936 | 9 | // Licence: wxWindows licence |
c66cca2a VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | /* | |
13 | Acknowledgements: this header is heavily based on (well, almost the exact | |
14 | copy of) wxScopeGuard.h by Andrei Alexandrescu and Petru Marginean published | |
15 | in December 2000 issue of C/C++ Users Journal. | |
16 | */ | |
17 | ||
18 | #ifndef _WX_SCOPEGUARD_H_ | |
19 | #define _WX_SCOPEGUARD_H_ | |
20 | ||
21 | #include "wx/defs.h" | |
22 | ||
5800b5be VZ |
23 | #include "wx/except.h" |
24 | ||
c66cca2a VZ |
25 | // ---------------------------------------------------------------------------- |
26 | // helpers | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
29 | namespace wxPrivate | |
30 | { | |
31 | // in the original implementation this was a member template function of | |
32 | // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't | |
33 | // support member templates and so we must make it global | |
17e22c50 | 34 | template <class ScopeGuardImpl> |
d775fa82 | 35 | void OnScopeExit(ScopeGuardImpl& guard) |
c66cca2a VZ |
36 | { |
37 | if ( !guard.WasDismissed() ) | |
38 | { | |
39 | // we're called from ScopeGuardImpl dtor and so we must not throw | |
5800b5be | 40 | wxTRY |
c66cca2a VZ |
41 | { |
42 | guard.Execute(); | |
43 | } | |
f0658b1e | 44 | wxCATCH_ALL(;) // do nothing, just eat the exception |
c66cca2a VZ |
45 | } |
46 | } | |
47 | ||
48 | // just to avoid the warning about unused variables | |
17e22c50 | 49 | template <class T> |
c66cca2a VZ |
50 | void Use(const T& WXUNUSED(t)) |
51 | { | |
52 | } | |
53 | } // namespace wxPrivate | |
d775fa82 | 54 | |
c66cca2a VZ |
55 | // ============================================================================ |
56 | // wxScopeGuard for functions and functors | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | class wxScopeGuardImplBase | |
64 | { | |
65 | public: | |
66 | wxScopeGuardImplBase() : m_wasDismissed(false) { } | |
67 | ||
68 | void Dismiss() const { m_wasDismissed = true; } | |
69 | ||
426a63a3 | 70 | // for OnScopeExit() only (we can't make it friend, unfortunately)! |
c66cca2a VZ |
71 | bool WasDismissed() const { return m_wasDismissed; } |
72 | ||
73 | protected: | |
74 | ~wxScopeGuardImplBase() { } | |
75 | ||
d775fa82 | 76 | wxScopeGuardImplBase(const wxScopeGuardImplBase& other) |
c66cca2a VZ |
77 | : m_wasDismissed(other.m_wasDismissed) |
78 | { | |
79 | other.Dismiss(); | |
80 | } | |
81 | ||
82 | // must be mutable for copy ctor to work | |
83 | mutable bool m_wasDismissed; | |
84 | ||
85 | private: | |
86 | wxScopeGuardImplBase& operator=(const wxScopeGuardImplBase&); | |
87 | }; | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // wxScopeGuardImpl0: scope guard for actions without parameters | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
17e22c50 | 93 | template <class F> |
c66cca2a VZ |
94 | class wxScopeGuardImpl0 : public wxScopeGuardImplBase |
95 | { | |
96 | public: | |
97 | static wxScopeGuardImpl0<F> MakeGuard(F fun) | |
98 | { | |
99 | return wxScopeGuardImpl0<F>(fun); | |
100 | } | |
101 | ||
102 | ~wxScopeGuardImpl0() { wxPrivate::OnScopeExit(*this); } | |
103 | ||
104 | void Execute() { m_fun(); } | |
105 | ||
106 | protected: | |
107 | wxScopeGuardImpl0(F fun) : m_fun(fun) { } | |
108 | ||
109 | F m_fun; | |
110 | ||
111 | wxScopeGuardImpl0& operator=(const wxScopeGuardImpl0&); | |
112 | }; | |
113 | ||
17e22c50 | 114 | template <class F> |
c66cca2a VZ |
115 | inline wxScopeGuardImpl0<F> wxMakeGuard(F fun) |
116 | { | |
117 | return wxScopeGuardImpl0<F>::MakeGuard(fun); | |
118 | } | |
119 | ||
120 | // ---------------------------------------------------------------------------- | |
121 | // wxScopeGuardImpl1: scope guard for actions with 1 parameter | |
122 | // ---------------------------------------------------------------------------- | |
123 | ||
17e22c50 | 124 | template <class F, class P1> |
c66cca2a VZ |
125 | class wxScopeGuardImpl1 : public wxScopeGuardImplBase |
126 | { | |
127 | public: | |
128 | static wxScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1) | |
129 | { | |
130 | return wxScopeGuardImpl1<F, P1>(fun, p1); | |
131 | } | |
132 | ||
133 | ~wxScopeGuardImpl1() { wxPrivate::OnScopeExit(*this); } | |
134 | ||
135 | void Execute() { m_fun(m_p1); } | |
136 | ||
137 | protected: | |
138 | wxScopeGuardImpl1(F fun, P1 p1) : m_fun(fun), m_p1(p1) { } | |
139 | ||
140 | F m_fun; | |
141 | const P1 m_p1; | |
142 | ||
143 | wxScopeGuardImpl1& operator=(const wxScopeGuardImpl1&); | |
144 | }; | |
145 | ||
17e22c50 | 146 | template <class F, class P1> |
c66cca2a VZ |
147 | inline wxScopeGuardImpl1<F, P1> wxMakeGuard(F fun, P1 p1) |
148 | { | |
149 | return wxScopeGuardImpl1<F, P1>::MakeGuard(fun, p1); | |
150 | } | |
151 | ||
152 | // ---------------------------------------------------------------------------- | |
153 | // wxScopeGuardImpl2: scope guard for actions with 2 parameters | |
154 | // ---------------------------------------------------------------------------- | |
155 | ||
17e22c50 | 156 | template <class F, class P1, class P2> |
c66cca2a VZ |
157 | class wxScopeGuardImpl2 : public wxScopeGuardImplBase |
158 | { | |
159 | public: | |
160 | static wxScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2) | |
161 | { | |
162 | return wxScopeGuardImpl2<F, P1, P2>(fun, p1, p2); | |
163 | } | |
164 | ||
165 | ~wxScopeGuardImpl2() { wxPrivate::OnScopeExit(*this); } | |
166 | ||
167 | void Execute() { m_fun(m_p1, m_p2); } | |
168 | ||
169 | protected: | |
170 | wxScopeGuardImpl2(F fun, P1 p1, P2 p2) : m_fun(fun), m_p1(p1), m_p2(p2) { } | |
171 | ||
172 | F m_fun; | |
173 | const P1 m_p1; | |
174 | const P2 m_p2; | |
175 | ||
176 | wxScopeGuardImpl2& operator=(const wxScopeGuardImpl2&); | |
177 | }; | |
178 | ||
17e22c50 | 179 | template <class F, class P1, class P2> |
c66cca2a VZ |
180 | inline wxScopeGuardImpl2<F, P1, P2> wxMakeGuard(F fun, P1 p1, P2 p2) |
181 | { | |
182 | return wxScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2); | |
183 | } | |
184 | ||
185 | // ============================================================================ | |
186 | // wxScopeGuards for object methods | |
187 | // ============================================================================ | |
188 | ||
189 | // ---------------------------------------------------------------------------- | |
190 | // wxObjScopeGuardImpl0 | |
191 | // ---------------------------------------------------------------------------- | |
192 | ||
17e22c50 | 193 | template <class Obj, class MemFun> |
c66cca2a VZ |
194 | class wxObjScopeGuardImpl0 : public wxScopeGuardImplBase |
195 | { | |
196 | public: | |
197 | static wxObjScopeGuardImpl0<Obj, MemFun> | |
198 | MakeObjGuard(Obj& obj, MemFun memFun) | |
199 | { | |
200 | return wxObjScopeGuardImpl0<Obj, MemFun>(obj, memFun); | |
201 | } | |
202 | ||
203 | ~wxObjScopeGuardImpl0() { wxPrivate::OnScopeExit(*this); } | |
204 | ||
205 | void Execute() { (m_obj.*m_memfun)(); } | |
206 | ||
207 | protected: | |
208 | wxObjScopeGuardImpl0(Obj& obj, MemFun memFun) | |
209 | : m_obj(obj), m_memfun(memFun) { } | |
210 | ||
211 | Obj& m_obj; | |
212 | MemFun m_memfun; | |
213 | }; | |
214 | ||
17e22c50 | 215 | template <class Obj, class MemFun> |
c66cca2a VZ |
216 | inline wxObjScopeGuardImpl0<Obj, MemFun> wxMakeObjGuard(Obj& obj, MemFun memFun) |
217 | { | |
218 | return wxObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun); | |
219 | } | |
220 | ||
17e22c50 | 221 | template <class Obj, class MemFun, class P1> |
c66cca2a VZ |
222 | class wxObjScopeGuardImpl1 : public wxScopeGuardImplBase |
223 | { | |
224 | public: | |
225 | static wxObjScopeGuardImpl1<Obj, MemFun, P1> | |
226 | MakeObjGuard(Obj& obj, MemFun memFun, P1 p1) | |
227 | { | |
228 | return wxObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1); | |
229 | } | |
230 | ||
231 | ~wxObjScopeGuardImpl1() { wxPrivate::OnScopeExit(*this); } | |
232 | ||
233 | void Execute() { (m_obj.*m_memfun)(m_p1); } | |
234 | ||
235 | protected: | |
d775fa82 | 236 | wxObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1) |
c66cca2a VZ |
237 | : m_obj(obj), m_memfun(memFun), m_p1(p1) { } |
238 | ||
239 | Obj& m_obj; | |
240 | MemFun m_memfun; | |
241 | const P1 m_p1; | |
242 | }; | |
243 | ||
17e22c50 | 244 | template <class Obj, class MemFun, class P1> |
c66cca2a VZ |
245 | inline wxObjScopeGuardImpl1<Obj, MemFun, P1> |
246 | wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1) | |
247 | { | |
248 | return wxObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1); | |
249 | } | |
250 | ||
17e22c50 | 251 | template <class Obj, class MemFun, class P1, class P2> |
c66cca2a VZ |
252 | class wxObjScopeGuardImpl2 : public wxScopeGuardImplBase |
253 | { | |
254 | public: | |
255 | static wxObjScopeGuardImpl2<Obj, MemFun, P1, P2> | |
256 | MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2) | |
257 | { | |
258 | return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2); | |
259 | } | |
260 | ||
261 | ~wxObjScopeGuardImpl2() { wxPrivate::OnScopeExit(*this); } | |
262 | ||
263 | void Execute() { (m_obj.*m_memfun)(m_p1, m_p2); } | |
264 | ||
265 | protected: | |
d775fa82 | 266 | wxObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2) |
c66cca2a VZ |
267 | : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2) { } |
268 | ||
269 | Obj& m_obj; | |
270 | MemFun m_memfun; | |
271 | const P1 m_p1; | |
272 | const P2 m_p2; | |
273 | }; | |
274 | ||
17e22c50 | 275 | template <class Obj, class MemFun, class P1, class P2> |
c66cca2a VZ |
276 | inline wxObjScopeGuardImpl2<Obj, MemFun, P1, P2> |
277 | wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2) | |
278 | { | |
279 | return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>:: | |
280 | MakeObjGuard(obj, memFun, p1, p2); | |
281 | } | |
282 | ||
283 | // ============================================================================ | |
284 | // public stuff | |
285 | // ============================================================================ | |
286 | ||
287 | // wxScopeGuard is just a reference, see the explanation in CUJ article | |
288 | typedef const wxScopeGuardImplBase& wxScopeGuard; | |
289 | ||
290 | // when an unnamed scope guard is needed, the macros below may be used | |
291 | // | |
292 | // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro | |
293 | // but this results in compiler warnings about unused variables and I | |
294 | // didn't find a way to work around this other than by having different | |
295 | // macros with different names | |
426a63a3 VZ |
296 | |
297 | #define wxGuardName wxMAKE_UNIQUE_NAME(scopeGuard) | |
298 | ||
299 | #define wxON_BLOCK_EXIT0_IMPL(n, f) \ | |
300 | wxScopeGuard n = wxMakeGuard(f); \ | |
301 | wxPrivate::Use(n) | |
5800b5be | 302 | #define wxON_BLOCK_EXIT0(f) \ |
426a63a3 | 303 | wxON_BLOCK_EXIT0_IMPL(wxGuardName, f) |
c66cca2a | 304 | |
426a63a3 VZ |
305 | #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \ |
306 | wxScopeGuard n = wxMakeObjGuard(o, m); \ | |
307 | wxPrivate::Use(n) | |
5800b5be | 308 | #define wxON_BLOCK_EXIT_OBJ0(o, m) \ |
426a63a3 | 309 | wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, m) |
c66cca2a | 310 | |
426a63a3 VZ |
311 | #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \ |
312 | wxScopeGuard n = wxMakeGuard(f, p1); \ | |
313 | wxPrivate::Use(n) | |
5800b5be | 314 | #define wxON_BLOCK_EXIT1(f, p1) \ |
426a63a3 | 315 | wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1) |
c66cca2a | 316 | |
426a63a3 VZ |
317 | #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \ |
318 | wxScopeGuard n = wxMakeObjGuard(o, m, p1); \ | |
319 | wxPrivate::Use(n) | |
5800b5be | 320 | #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \ |
426a63a3 | 321 | wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, m, p1) |
c66cca2a | 322 | |
426a63a3 VZ |
323 | #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \ |
324 | wxScopeGuard n = wxMakeGuard(f, p1, p2); \ | |
325 | wxPrivate::Use(n) | |
5800b5be | 326 | #define wxON_BLOCK_EXIT2(f, p1, p2) \ |
426a63a3 | 327 | wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2) |
c66cca2a | 328 | |
426a63a3 VZ |
329 | #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \ |
330 | wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \ | |
331 | wxPrivate::Use(n) | |
5800b5be | 332 | #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \ |
426a63a3 | 333 | wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, m, p1, p2) |
c66cca2a VZ |
334 | |
335 | #endif // _WX_SCOPEGUARD_H_ |