]> git.saurik.com Git - wxWidgets.git/blob - include/wx/scopeguard.h
added convenient wxON_BLOCK_EXIT_SET() macro
[wxWidgets.git] / include / wx / scopeguard.h
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$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 /*
13 Acknowledgements: this header is heavily based on (well, almost the exact
14 copy of) ScopeGuard.h by Andrei Alexandrescu and Petru Marginean published
15 in December 2000 issue of C/C++ Users Journal.
16 http://www.cuj.com/documents/cujcexp1812alexandr/
17 */
18
19 #ifndef _WX_SCOPEGUARD_H_
20 #define _WX_SCOPEGUARD_H_
21
22 #include "wx/defs.h"
23
24 #include "wx/except.h"
25
26 // ----------------------------------------------------------------------------
27 // helpers
28 // ----------------------------------------------------------------------------
29
30 #ifdef __WATCOMC__
31
32 // WATCOM-FIXME: C++ of Open Watcom 1.3 doesn't like OnScopeExit() created
33 // through template so it must be workarounded with dedicated inlined macro.
34 // For compatibility with Watcom compilers wxPrivate::OnScopeExit must be
35 // replaced with wxPrivateOnScopeExit but in user code (for everyone who
36 // doesn't care about OW compatibility) wxPrivate::OnScopeExit still works.
37
38 #define wxPrivateOnScopeExit(guard) \
39 { \
40 if ( !(guard).WasDismissed() ) \
41 { \
42 wxTRY \
43 { \
44 (guard).Execute(); \
45 } \
46 wxCATCH_ALL(;) \
47 } \
48 }
49
50 #define wxPrivateUse(n) wxUnusedVar(n)
51
52 #else
53
54 // namespace support was first implemented in gcc-2.95,
55 // so avoid using it for older versions.
56 #if !defined(__GNUC__) || wxCHECK_GCC_VERSION(2, 95)
57
58 #define wxHAS_NAMESPACES
59
60 namespace wxPrivate
61 {
62 #else
63 #define wxPrivate
64 #endif
65 // in the original implementation this was a member template function of
66 // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't
67 // support member templates and so we must make it global
68 template <class ScopeGuardImpl>
69 void OnScopeExit(ScopeGuardImpl& guard)
70 {
71 if ( !guard.WasDismissed() )
72 {
73 // we're called from ScopeGuardImpl dtor and so we must not throw
74 wxTRY
75 {
76 guard.Execute();
77 }
78 wxCATCH_ALL(;) // do nothing, just eat the exception
79 }
80 }
81
82 // just to avoid the warning about unused variables
83 template <class T>
84 void Use(const T& WXUNUSED(t))
85 {
86 }
87 #if !defined(__GNUC__) || wxCHECK_GCC_VERSION(2, 95)
88 } // namespace wxPrivate
89 #endif
90
91 #define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n)
92 #define wxPrivateUse(n) wxPrivate::Use(n)
93
94 #endif
95
96 // ============================================================================
97 // wxScopeGuard for functions and functors
98 // ============================================================================
99
100 // ----------------------------------------------------------------------------
101 // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
102 // ----------------------------------------------------------------------------
103
104 class wxScopeGuardImplBase
105 {
106 public:
107 wxScopeGuardImplBase() : m_wasDismissed(false) { }
108
109 wxScopeGuardImplBase(const wxScopeGuardImplBase& other)
110 : m_wasDismissed(other.m_wasDismissed)
111 {
112 other.Dismiss();
113 }
114
115 void Dismiss() const { m_wasDismissed = true; }
116
117 // for OnScopeExit() only (we can't make it friend, unfortunately)!
118 bool WasDismissed() const { return m_wasDismissed; }
119
120 protected:
121 ~wxScopeGuardImplBase() { }
122
123 // must be mutable for copy ctor to work
124 mutable bool m_wasDismissed;
125
126 private:
127 wxScopeGuardImplBase& operator=(const wxScopeGuardImplBase&);
128 };
129
130 // wxScopeGuard is just a reference, see the explanation in CUJ article
131 typedef const wxScopeGuardImplBase& wxScopeGuard;
132
133 // ----------------------------------------------------------------------------
134 // wxScopeGuardImpl0: scope guard for actions without parameters
135 // ----------------------------------------------------------------------------
136
137 template <class F>
138 class wxScopeGuardImpl0 : public wxScopeGuardImplBase
139 {
140 public:
141 static wxScopeGuardImpl0<F> MakeGuard(F fun)
142 {
143 return wxScopeGuardImpl0<F>(fun);
144 }
145
146 ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
147
148 void Execute() { m_fun(); }
149
150 protected:
151 wxScopeGuardImpl0(F fun) : m_fun(fun) { }
152
153 F m_fun;
154
155 wxScopeGuardImpl0& operator=(const wxScopeGuardImpl0&);
156 };
157
158 template <class F>
159 inline wxScopeGuardImpl0<F> wxMakeGuard(F fun)
160 {
161 return wxScopeGuardImpl0<F>::MakeGuard(fun);
162 }
163
164 // ----------------------------------------------------------------------------
165 // wxScopeGuardImpl1: scope guard for actions with 1 parameter
166 // ----------------------------------------------------------------------------
167
168 template <class F, class P1>
169 class wxScopeGuardImpl1 : public wxScopeGuardImplBase
170 {
171 public:
172 static wxScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
173 {
174 return wxScopeGuardImpl1<F, P1>(fun, p1);
175 }
176
177 ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
178
179 void Execute() { m_fun(m_p1); }
180
181 protected:
182 wxScopeGuardImpl1(F fun, P1 p1) : m_fun(fun), m_p1(p1) { }
183
184 F m_fun;
185 const P1 m_p1;
186
187 wxScopeGuardImpl1& operator=(const wxScopeGuardImpl1&);
188 };
189
190 template <class F, class P1>
191 inline wxScopeGuardImpl1<F, P1> wxMakeGuard(F fun, P1 p1)
192 {
193 return wxScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
194 }
195
196 // ----------------------------------------------------------------------------
197 // wxScopeGuardImpl2: scope guard for actions with 2 parameters
198 // ----------------------------------------------------------------------------
199
200 template <class F, class P1, class P2>
201 class wxScopeGuardImpl2 : public wxScopeGuardImplBase
202 {
203 public:
204 static wxScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
205 {
206 return wxScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
207 }
208
209 ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
210
211 void Execute() { m_fun(m_p1, m_p2); }
212
213 protected:
214 wxScopeGuardImpl2(F fun, P1 p1, P2 p2) : m_fun(fun), m_p1(p1), m_p2(p2) { }
215
216 F m_fun;
217 const P1 m_p1;
218 const P2 m_p2;
219
220 wxScopeGuardImpl2& operator=(const wxScopeGuardImpl2&);
221 };
222
223 template <class F, class P1, class P2>
224 inline wxScopeGuardImpl2<F, P1, P2> wxMakeGuard(F fun, P1 p1, P2 p2)
225 {
226 return wxScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
227 }
228
229 // ============================================================================
230 // wxScopeGuards for object methods
231 // ============================================================================
232
233 // ----------------------------------------------------------------------------
234 // wxObjScopeGuardImpl0
235 // ----------------------------------------------------------------------------
236
237 template <class Obj, class MemFun>
238 class wxObjScopeGuardImpl0 : public wxScopeGuardImplBase
239 {
240 public:
241 static wxObjScopeGuardImpl0<Obj, MemFun>
242 MakeObjGuard(Obj& obj, MemFun memFun)
243 {
244 return wxObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
245 }
246
247 ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
248
249 void Execute() { (m_obj.*m_memfun)(); }
250
251 protected:
252 wxObjScopeGuardImpl0(Obj& obj, MemFun memFun)
253 : m_obj(obj), m_memfun(memFun) { }
254
255 Obj& m_obj;
256 MemFun m_memfun;
257 };
258
259 template <class Obj, class MemFun>
260 inline wxObjScopeGuardImpl0<Obj, MemFun> wxMakeObjGuard(Obj& obj, MemFun memFun)
261 {
262 return wxObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun);
263 }
264
265 template <class Obj, class MemFun, class P1>
266 class wxObjScopeGuardImpl1 : public wxScopeGuardImplBase
267 {
268 public:
269 static wxObjScopeGuardImpl1<Obj, MemFun, P1>
270 MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
271 {
272 return wxObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
273 }
274
275 ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
276
277 void Execute() { (m_obj.*m_memfun)(m_p1); }
278
279 protected:
280 wxObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1)
281 : m_obj(obj), m_memfun(memFun), m_p1(p1) { }
282
283 Obj& m_obj;
284 MemFun m_memfun;
285 const P1 m_p1;
286 };
287
288 template <class Obj, class MemFun, class P1>
289 inline wxObjScopeGuardImpl1<Obj, MemFun, P1>
290 wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
291 {
292 return wxObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1);
293 }
294
295 template <class Obj, class MemFun, class P1, class P2>
296 class wxObjScopeGuardImpl2 : public wxScopeGuardImplBase
297 {
298 public:
299 static wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
300 MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
301 {
302 return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
303 }
304
305 ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
306
307 void Execute() { (m_obj.*m_memfun)(m_p1, m_p2); }
308
309 protected:
310 wxObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2)
311 : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2) { }
312
313 Obj& m_obj;
314 MemFun m_memfun;
315 const P1 m_p1;
316 const P2 m_p2;
317 };
318
319 template <class Obj, class MemFun, class P1, class P2>
320 inline wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
321 wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
322 {
323 return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>::
324 MakeObjGuard(obj, memFun, p1, p2);
325 }
326
327 // ----------------------------------------------------------------------------
328 // wxVariableSetter: use the same technique as for wxScopeGuard to allow
329 // setting a variable to some value on block exit
330 // ----------------------------------------------------------------------------
331
332 #ifdef wxHAS_NAMESPACES
333
334 namespace wxPrivate
335 {
336
337 // empty class just to be able to define a reference to it
338 class VariableSetterBase { };
339
340 typedef const VariableSetterBase& VariableSetter;
341
342 template <typename T, typename U>
343 class VariableSetterImpl : public VariableSetterBase
344 {
345 public:
346 VariableSetterImpl(T& var, const U& value)
347 : m_var(var),
348 m_value(value)
349 {
350 }
351
352 ~VariableSetterImpl()
353 {
354 m_var = m_value;
355 }
356
357 private:
358 T& m_var;
359 const U& m_value;
360
361 // suppress the warning about assignment operator not being generated
362 VariableSetterImpl<T, U>& operator=(const VariableSetterImpl<T, U>&);
363 };
364
365 template <typename T>
366 class VariableNullerImpl : public VariableSetterBase
367 {
368 public:
369 VariableNullerImpl(T& var)
370 : m_var(var)
371 {
372 }
373
374 ~VariableNullerImpl()
375 {
376 m_var = NULL;
377 }
378
379 private:
380 T& m_var;
381
382 VariableNullerImpl<T>& operator=(const VariableNullerImpl<T>&);
383 };
384
385 } // namespace wxPrivate
386
387 template <typename T, typename U>
388 inline
389 wxPrivate::VariableSetterImpl<T, U> wxMakeVarSetter(T& var, const U& value)
390 {
391 return wxPrivate::VariableSetterImpl<T, U>(var, value);
392 }
393
394 // calling wxMakeVarSetter(ptr, NULL) doesn't work because U is deduced to be
395 // "int" and subsequent assignment of "U" to "T *" fails, so provide a special
396 // function for this special case
397 template <typename T>
398 inline
399 wxPrivate::VariableNullerImpl<T> wxMakeVarNuller(T& var)
400 {
401 return wxPrivate::VariableNullerImpl<T>(var);
402 }
403
404 #endif // wxHAS_NAMESPACES
405
406 // ============================================================================
407 // macros for declaring unnamed scoped guards (which can't be dismissed)
408 // ============================================================================
409
410 // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
411 // but this results in compiler warnings about unused variables and I
412 // didn't find a way to work around this other than by having different
413 // macros with different names or using a less natural syntax for passing
414 // the arguments (e.g. as Boost preprocessor sequences, which would mean
415 // having to write wxON_BLOCK_EXIT(fwrite, (buf)(size)(n)(fp)) instead of
416 // wxON_BLOCK_EXIT4(fwrite, buf, size, n, fp)).
417
418 #define wxGuardName wxMAKE_UNIQUE_NAME(wxScopeGuard)
419
420 #define wxON_BLOCK_EXIT0_IMPL(n, f) \
421 wxScopeGuard n = wxMakeGuard(f); \
422 wxPrivateUse(n)
423 #define wxON_BLOCK_EXIT0(f) \
424 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
425
426 #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
427 wxScopeGuard n = wxMakeObjGuard(o, m); \
428 wxPrivateUse(n)
429 #define wxON_BLOCK_EXIT_OBJ0(o, m) \
430 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
431
432 #define wxON_BLOCK_EXIT_THIS0(m) \
433 wxON_BLOCK_EXIT_OBJ0(*this, m)
434
435
436 #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
437 wxScopeGuard n = wxMakeGuard(f, p1); \
438 wxPrivateUse(n)
439 #define wxON_BLOCK_EXIT1(f, p1) \
440 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
441
442 #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
443 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
444 wxPrivateUse(n)
445 #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
446 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
447
448 #define wxON_BLOCK_EXIT_THIS1(m, p1) \
449 wxON_BLOCK_EXIT_OBJ1(*this, m, p1)
450
451
452 #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
453 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
454 wxPrivateUse(n)
455 #define wxON_BLOCK_EXIT2(f, p1, p2) \
456 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
457
458 #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
459 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
460 wxPrivateUse(n)
461 #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
462 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
463
464 #define wxON_BLOCK_EXIT_THIS2(m, p1, p2) \
465 wxON_BLOCK_EXIT_OBJ2(*this, m, p1, p2)
466
467
468 #define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter)
469
470 #define wxON_BLOCK_EXIT_SET_IMPL(n, var, value) \
471 wxPrivate::VariableSetter n = wxMakeVarSetter(var, value); \
472 wxPrivateUse(n)
473
474 #define wxON_BLOCK_EXIT_SET(var, value) \
475 wxON_BLOCK_EXIT_SET_IMPL(wxSetterName, var, value)
476
477 #define wxON_BLOCK_EXIT_NULL_IMPL(n, var) \
478 wxPrivate::VariableSetter n = wxMakeVarNuller(var); \
479 wxPrivateUse(n)
480
481 #define wxON_BLOCK_EXIT_NULL(ptr) \
482 wxON_BLOCK_EXIT_NULL_IMPL(wxSetterName, ptr)
483
484 #endif // _WX_SCOPEGUARD_H_