]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/scopeguard.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/scopeguard.h
3 // Purpose: declares wxwxScopeGuard and related macros
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
12 Acknowledgements: this header is heavily based on (well, almost the exact
13 copy of) ScopeGuard.h by Andrei Alexandrescu and Petru Marginean published
14 in December 2000 issue of C/C++ Users Journal.
15 http://www.cuj.com/documents/cujcexp1812alexandr/
18 #ifndef _WX_SCOPEGUARD_H_
19 #define _WX_SCOPEGUARD_H_
23 #include "wx/except.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
31 // WATCOM-FIXME: C++ of Open Watcom 1.3 doesn't like OnScopeExit() created
32 // through template so it must be workarounded with dedicated inlined macro.
33 // For compatibility with Watcom compilers wxPrivate::OnScopeExit must be
34 // replaced with wxPrivateOnScopeExit but in user code (for everyone who
35 // doesn't care about OW compatibility) wxPrivate::OnScopeExit still works.
37 #define wxPrivateOnScopeExit(guard) \
39 if ( !(guard).WasDismissed() ) \
49 #define wxPrivateUse(n) wxUnusedVar(n)
55 // in the original implementation this was a member template function of
56 // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't
57 // support member templates and so we must make it global
58 template <class ScopeGuardImpl
>
59 void OnScopeExit(ScopeGuardImpl
& guard
)
61 if ( !guard
.WasDismissed() )
63 // we're called from ScopeGuardImpl dtor and so we must not throw
68 wxCATCH_ALL(;) // do nothing, just eat the exception
72 // just to avoid the warning about unused variables
74 void Use(const T
& WXUNUSED(t
))
77 } // namespace wxPrivate
79 #define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n)
80 #define wxPrivateUse(n) wxPrivate::Use(n)
84 // ============================================================================
85 // wxScopeGuard for functions and functors
86 // ============================================================================
88 // ----------------------------------------------------------------------------
89 // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
90 // ----------------------------------------------------------------------------
92 class wxScopeGuardImplBase
95 wxScopeGuardImplBase() : m_wasDismissed(false) { }
97 wxScopeGuardImplBase(const wxScopeGuardImplBase
& other
)
98 : m_wasDismissed(other
.m_wasDismissed
)
103 void Dismiss() const { m_wasDismissed
= true; }
105 // for OnScopeExit() only (we can't make it friend, unfortunately)!
106 bool WasDismissed() const { return m_wasDismissed
; }
109 ~wxScopeGuardImplBase() { }
111 // must be mutable for copy ctor to work
112 mutable bool m_wasDismissed
;
115 wxScopeGuardImplBase
& operator=(const wxScopeGuardImplBase
&);
118 // wxScopeGuard is just a reference, see the explanation in CUJ article
119 typedef const wxScopeGuardImplBase
& wxScopeGuard
;
121 // ----------------------------------------------------------------------------
122 // wxScopeGuardImpl0: scope guard for actions without parameters
123 // ----------------------------------------------------------------------------
126 class wxScopeGuardImpl0
: public wxScopeGuardImplBase
129 static wxScopeGuardImpl0
<F
> MakeGuard(F fun
)
131 return wxScopeGuardImpl0
<F
>(fun
);
134 ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
136 void Execute() { m_fun(); }
139 wxScopeGuardImpl0(F fun
) : m_fun(fun
) { }
143 wxScopeGuardImpl0
& operator=(const wxScopeGuardImpl0
&);
147 inline wxScopeGuardImpl0
<F
> wxMakeGuard(F fun
)
149 return wxScopeGuardImpl0
<F
>::MakeGuard(fun
);
152 // ----------------------------------------------------------------------------
153 // wxScopeGuardImpl1: scope guard for actions with 1 parameter
154 // ----------------------------------------------------------------------------
156 template <class F
, class P1
>
157 class wxScopeGuardImpl1
: public wxScopeGuardImplBase
160 static wxScopeGuardImpl1
<F
, P1
> MakeGuard(F fun
, P1 p1
)
162 return wxScopeGuardImpl1
<F
, P1
>(fun
, p1
);
165 ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
167 void Execute() { m_fun(m_p1
); }
170 wxScopeGuardImpl1(F fun
, P1 p1
) : m_fun(fun
), m_p1(p1
) { }
175 wxScopeGuardImpl1
& operator=(const wxScopeGuardImpl1
&);
178 template <class F
, class P1
>
179 inline wxScopeGuardImpl1
<F
, P1
> wxMakeGuard(F fun
, P1 p1
)
181 return wxScopeGuardImpl1
<F
, P1
>::MakeGuard(fun
, p1
);
184 // ----------------------------------------------------------------------------
185 // wxScopeGuardImpl2: scope guard for actions with 2 parameters
186 // ----------------------------------------------------------------------------
188 template <class F
, class P1
, class P2
>
189 class wxScopeGuardImpl2
: public wxScopeGuardImplBase
192 static wxScopeGuardImpl2
<F
, P1
, P2
> MakeGuard(F fun
, P1 p1
, P2 p2
)
194 return wxScopeGuardImpl2
<F
, P1
, P2
>(fun
, p1
, p2
);
197 ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
199 void Execute() { m_fun(m_p1
, m_p2
); }
202 wxScopeGuardImpl2(F fun
, P1 p1
, P2 p2
) : m_fun(fun
), m_p1(p1
), m_p2(p2
) { }
208 wxScopeGuardImpl2
& operator=(const wxScopeGuardImpl2
&);
211 template <class F
, class P1
, class P2
>
212 inline wxScopeGuardImpl2
<F
, P1
, P2
> wxMakeGuard(F fun
, P1 p1
, P2 p2
)
214 return wxScopeGuardImpl2
<F
, P1
, P2
>::MakeGuard(fun
, p1
, p2
);
217 // ----------------------------------------------------------------------------
218 // wxScopeGuardImpl3: scope guard for actions with 3 parameters
219 // ----------------------------------------------------------------------------
221 template <class F
, class P1
, class P2
, class P3
>
222 class wxScopeGuardImpl3
: public wxScopeGuardImplBase
225 static wxScopeGuardImpl3
<F
, P1
, P2
, P3
> MakeGuard(F fun
, P1 p1
, P2 p2
, P3 p3
)
227 return wxScopeGuardImpl3
<F
, P1
, P2
, P3
>(fun
, p1
, p2
, p3
);
230 ~wxScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
232 void Execute() { m_fun(m_p1
, m_p2
, m_p3
); }
235 wxScopeGuardImpl3(F fun
, P1 p1
, P2 p2
, P3 p3
)
236 : m_fun(fun
), m_p1(p1
), m_p2(p2
), m_p3(p3
) { }
243 wxScopeGuardImpl3
& operator=(const wxScopeGuardImpl3
&);
246 template <class F
, class P1
, class P2
, class P3
>
247 inline wxScopeGuardImpl3
<F
, P1
, P2
, P3
> wxMakeGuard(F fun
, P1 p1
, P2 p2
, P3 p3
)
249 return wxScopeGuardImpl3
<F
, P1
, P2
, P3
>::MakeGuard(fun
, p1
, p2
, p3
);
252 // ============================================================================
253 // wxScopeGuards for object methods
254 // ============================================================================
256 // ----------------------------------------------------------------------------
257 // wxObjScopeGuardImpl0
258 // ----------------------------------------------------------------------------
260 template <class Obj
, class MemFun
>
261 class wxObjScopeGuardImpl0
: public wxScopeGuardImplBase
264 static wxObjScopeGuardImpl0
<Obj
, MemFun
>
265 MakeObjGuard(Obj
& obj
, MemFun memFun
)
267 return wxObjScopeGuardImpl0
<Obj
, MemFun
>(obj
, memFun
);
270 ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
272 void Execute() { (m_obj
.*m_memfun
)(); }
275 wxObjScopeGuardImpl0(Obj
& obj
, MemFun memFun
)
276 : m_obj(obj
), m_memfun(memFun
) { }
282 template <class Obj
, class MemFun
>
283 inline wxObjScopeGuardImpl0
<Obj
, MemFun
> wxMakeObjGuard(Obj
& obj
, MemFun memFun
)
285 return wxObjScopeGuardImpl0
<Obj
, MemFun
>::MakeObjGuard(obj
, memFun
);
288 template <class Obj
, class MemFun
, class P1
>
289 class wxObjScopeGuardImpl1
: public wxScopeGuardImplBase
292 static wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
293 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
295 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>(obj
, memFun
, p1
);
298 ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
300 void Execute() { (m_obj
.*m_memfun
)(m_p1
); }
303 wxObjScopeGuardImpl1(Obj
& obj
, MemFun memFun
, P1 p1
)
304 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
) { }
311 template <class Obj
, class MemFun
, class P1
>
312 inline wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
313 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
315 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>::MakeObjGuard(obj
, memFun
, p1
);
318 template <class Obj
, class MemFun
, class P1
, class P2
>
319 class wxObjScopeGuardImpl2
: public wxScopeGuardImplBase
322 static wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
323 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
325 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>(obj
, memFun
, p1
, p2
);
328 ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
330 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
); }
333 wxObjScopeGuardImpl2(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
334 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
) { }
342 template <class Obj
, class MemFun
, class P1
, class P2
>
343 inline wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
344 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
346 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>::
347 MakeObjGuard(obj
, memFun
, p1
, p2
);
350 template <class Obj
, class MemFun
, class P1
, class P2
, class P3
>
351 class wxObjScopeGuardImpl3
: public wxScopeGuardImplBase
354 static wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>
355 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
357 return wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>(obj
, memFun
, p1
, p2
, p3
);
360 ~wxObjScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
362 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
, m_p3
); }
365 wxObjScopeGuardImpl3(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
366 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
), m_p3(p3
) { }
375 template <class Obj
, class MemFun
, class P1
, class P2
, class P3
>
376 inline wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>
377 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
379 return wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>::
380 MakeObjGuard(obj
, memFun
, p1
, p2
, p3
);
383 // ----------------------------------------------------------------------------
384 // wxVariableSetter: use the same technique as for wxScopeGuard to allow
385 // setting a variable to some value on block exit
386 // ----------------------------------------------------------------------------
391 // empty class just to be able to define a reference to it
392 class VariableSetterBase
: public wxScopeGuardImplBase
{ };
394 typedef const VariableSetterBase
& VariableSetter
;
396 template <typename T
, typename U
>
397 class VariableSetterImpl
: public VariableSetterBase
400 VariableSetterImpl(T
& var
, U value
)
406 ~VariableSetterImpl() { wxPrivateOnScopeExit(*this); }
408 void Execute() { m_var
= m_value
; }
414 // suppress the warning about assignment operator not being generated
415 VariableSetterImpl
<T
, U
>& operator=(const VariableSetterImpl
<T
, U
>&);
418 template <typename T
>
419 class VariableNullerImpl
: public VariableSetterBase
422 VariableNullerImpl(T
& var
)
427 ~VariableNullerImpl() { wxPrivateOnScopeExit(*this); }
429 void Execute() { m_var
= NULL
; }
434 VariableNullerImpl
<T
>& operator=(const VariableNullerImpl
<T
>&);
437 } // namespace wxPrivate
439 template <typename T
, typename U
>
441 wxPrivate::VariableSetterImpl
<T
, U
> wxMakeVarSetter(T
& var
, U value
)
443 return wxPrivate::VariableSetterImpl
<T
, U
>(var
, value
);
446 // calling wxMakeVarSetter(ptr, NULL) doesn't work because U is deduced to be
447 // "int" and subsequent assignment of "U" to "T *" fails, so provide a special
448 // function for this special case
449 template <typename T
>
451 wxPrivate::VariableNullerImpl
<T
> wxMakeVarNuller(T
& var
)
453 return wxPrivate::VariableNullerImpl
<T
>(var
);
456 // ============================================================================
457 // macros for declaring unnamed scoped guards (which can't be dismissed)
458 // ============================================================================
460 // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
461 // but this results in compiler warnings about unused variables and I
462 // didn't find a way to work around this other than by having different
463 // macros with different names or using a less natural syntax for passing
464 // the arguments (e.g. as Boost preprocessor sequences, which would mean
465 // having to write wxON_BLOCK_EXIT(fwrite, (buf)(size)(n)(fp)) instead of
466 // wxON_BLOCK_EXIT4(fwrite, buf, size, n, fp)).
468 #define wxGuardName wxMAKE_UNIQUE_NAME(wxScopeGuard)
470 #define wxON_BLOCK_EXIT0_IMPL(n, f) \
471 wxScopeGuard n = wxMakeGuard(f); \
473 #define wxON_BLOCK_EXIT0(f) \
474 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
476 #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
477 wxScopeGuard n = wxMakeObjGuard(o, m); \
479 #define wxON_BLOCK_EXIT_OBJ0(o, m) \
480 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
482 #define wxON_BLOCK_EXIT_THIS0(m) \
483 wxON_BLOCK_EXIT_OBJ0(*this, m)
486 #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
487 wxScopeGuard n = wxMakeGuard(f, p1); \
489 #define wxON_BLOCK_EXIT1(f, p1) \
490 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
492 #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
493 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
495 #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
496 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
498 #define wxON_BLOCK_EXIT_THIS1(m, p1) \
499 wxON_BLOCK_EXIT_OBJ1(*this, m, p1)
502 #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
503 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
505 #define wxON_BLOCK_EXIT2(f, p1, p2) \
506 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
508 #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
509 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
511 #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
512 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
514 #define wxON_BLOCK_EXIT_THIS2(m, p1, p2) \
515 wxON_BLOCK_EXIT_OBJ2(*this, m, p1, p2)
518 #define wxON_BLOCK_EXIT3_IMPL(n, f, p1, p2, p3) \
519 wxScopeGuard n = wxMakeGuard(f, p1, p2, p3); \
521 #define wxON_BLOCK_EXIT3(f, p1, p2, p3) \
522 wxON_BLOCK_EXIT3_IMPL(wxGuardName, f, p1, p2, p3)
524 #define wxON_BLOCK_EXIT_OBJ3_IMPL(n, o, m, p1, p2, p3) \
525 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2, p3); \
527 #define wxON_BLOCK_EXIT_OBJ3(o, m, p1, p2, p3) \
528 wxON_BLOCK_EXIT_OBJ3_IMPL(wxGuardName, o, &m, p1, p2, p3)
530 #define wxON_BLOCK_EXIT_THIS3(m, p1, p2, p3) \
531 wxON_BLOCK_EXIT_OBJ3(*this, m, p1, p2, p3)
534 #define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter)
536 #define wxON_BLOCK_EXIT_SET_IMPL(n, var, value) \
537 wxPrivate::VariableSetter n = wxMakeVarSetter(var, value); \
540 #define wxON_BLOCK_EXIT_SET(var, value) \
541 wxON_BLOCK_EXIT_SET_IMPL(wxSetterName, var, value)
543 #define wxON_BLOCK_EXIT_NULL_IMPL(n, var) \
544 wxPrivate::VariableSetter n = wxMakeVarNuller(var); \
547 #define wxON_BLOCK_EXIT_NULL(ptr) \
548 wxON_BLOCK_EXIT_NULL_IMPL(wxSetterName, ptr)
550 #endif // _WX_SCOPEGUARD_H_