]>
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
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
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/
19 #ifndef _WX_SCOPEGUARD_H_
20 #define _WX_SCOPEGUARD_H_
24 #include "wx/except.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
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.
38 #define wxPrivateOnScopeExit(guard) \
40 if ( !(guard).WasDismissed() ) \
50 #define wxPrivateUse(n) wxUnusedVar(n)
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)
58 #define wxHAS_NAMESPACES
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
)
71 if ( !guard
.WasDismissed() )
73 // we're called from ScopeGuardImpl dtor and so we must not throw
78 wxCATCH_ALL(;) // do nothing, just eat the exception
82 // just to avoid the warning about unused variables
84 void Use(const T
& WXUNUSED(t
))
87 #if !defined(__GNUC__) || wxCHECK_GCC_VERSION(2, 95)
88 } // namespace wxPrivate
91 #define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n)
92 #define wxPrivateUse(n) wxPrivate::Use(n)
96 // ============================================================================
97 // wxScopeGuard for functions and functors
98 // ============================================================================
100 // ----------------------------------------------------------------------------
101 // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
102 // ----------------------------------------------------------------------------
104 class wxScopeGuardImplBase
107 wxScopeGuardImplBase() : m_wasDismissed(false) { }
109 wxScopeGuardImplBase(const wxScopeGuardImplBase
& other
)
110 : m_wasDismissed(other
.m_wasDismissed
)
115 void Dismiss() const { m_wasDismissed
= true; }
117 // for OnScopeExit() only (we can't make it friend, unfortunately)!
118 bool WasDismissed() const { return m_wasDismissed
; }
121 ~wxScopeGuardImplBase() { }
123 // must be mutable for copy ctor to work
124 mutable bool m_wasDismissed
;
127 wxScopeGuardImplBase
& operator=(const wxScopeGuardImplBase
&);
130 // wxScopeGuard is just a reference, see the explanation in CUJ article
131 typedef const wxScopeGuardImplBase
& wxScopeGuard
;
133 // ----------------------------------------------------------------------------
134 // wxScopeGuardImpl0: scope guard for actions without parameters
135 // ----------------------------------------------------------------------------
138 class wxScopeGuardImpl0
: public wxScopeGuardImplBase
141 static wxScopeGuardImpl0
<F
> MakeGuard(F fun
)
143 return wxScopeGuardImpl0
<F
>(fun
);
146 ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
148 void Execute() { m_fun(); }
151 wxScopeGuardImpl0(F fun
) : m_fun(fun
) { }
155 wxScopeGuardImpl0
& operator=(const wxScopeGuardImpl0
&);
159 inline wxScopeGuardImpl0
<F
> wxMakeGuard(F fun
)
161 return wxScopeGuardImpl0
<F
>::MakeGuard(fun
);
164 // ----------------------------------------------------------------------------
165 // wxScopeGuardImpl1: scope guard for actions with 1 parameter
166 // ----------------------------------------------------------------------------
168 template <class F
, class P1
>
169 class wxScopeGuardImpl1
: public wxScopeGuardImplBase
172 static wxScopeGuardImpl1
<F
, P1
> MakeGuard(F fun
, P1 p1
)
174 return wxScopeGuardImpl1
<F
, P1
>(fun
, p1
);
177 ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
179 void Execute() { m_fun(m_p1
); }
182 wxScopeGuardImpl1(F fun
, P1 p1
) : m_fun(fun
), m_p1(p1
) { }
187 wxScopeGuardImpl1
& operator=(const wxScopeGuardImpl1
&);
190 template <class F
, class P1
>
191 inline wxScopeGuardImpl1
<F
, P1
> wxMakeGuard(F fun
, P1 p1
)
193 return wxScopeGuardImpl1
<F
, P1
>::MakeGuard(fun
, p1
);
196 // ----------------------------------------------------------------------------
197 // wxScopeGuardImpl2: scope guard for actions with 2 parameters
198 // ----------------------------------------------------------------------------
200 template <class F
, class P1
, class P2
>
201 class wxScopeGuardImpl2
: public wxScopeGuardImplBase
204 static wxScopeGuardImpl2
<F
, P1
, P2
> MakeGuard(F fun
, P1 p1
, P2 p2
)
206 return wxScopeGuardImpl2
<F
, P1
, P2
>(fun
, p1
, p2
);
209 ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
211 void Execute() { m_fun(m_p1
, m_p2
); }
214 wxScopeGuardImpl2(F fun
, P1 p1
, P2 p2
) : m_fun(fun
), m_p1(p1
), m_p2(p2
) { }
220 wxScopeGuardImpl2
& operator=(const wxScopeGuardImpl2
&);
223 template <class F
, class P1
, class P2
>
224 inline wxScopeGuardImpl2
<F
, P1
, P2
> wxMakeGuard(F fun
, P1 p1
, P2 p2
)
226 return wxScopeGuardImpl2
<F
, P1
, P2
>::MakeGuard(fun
, p1
, p2
);
229 // ----------------------------------------------------------------------------
230 // wxScopeGuardImpl3: scope guard for actions with 3 parameters
231 // ----------------------------------------------------------------------------
233 template <class F
, class P1
, class P2
, class P3
>
234 class wxScopeGuardImpl3
: public wxScopeGuardImplBase
237 static wxScopeGuardImpl3
<F
, P1
, P2
, P3
> MakeGuard(F fun
, P1 p1
, P2 p2
, P3 p3
)
239 return wxScopeGuardImpl3
<F
, P1
, P2
, P3
>(fun
, p1
, p2
, p3
);
242 ~wxScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
244 void Execute() { m_fun(m_p1
, m_p2
, m_p3
); }
247 wxScopeGuardImpl3(F fun
, P1 p1
, P2 p2
, P3 p3
)
248 : m_fun(fun
), m_p1(p1
), m_p2(p2
), m_p3(p3
) { }
255 wxScopeGuardImpl3
& operator=(const wxScopeGuardImpl3
&);
258 template <class F
, class P1
, class P2
, class P3
>
259 inline wxScopeGuardImpl3
<F
, P1
, P2
, P3
> wxMakeGuard(F fun
, P1 p1
, P2 p2
, P3 p3
)
261 return wxScopeGuardImpl3
<F
, P1
, P2
, P3
>::MakeGuard(fun
, p1
, p2
, p3
);
264 // ============================================================================
265 // wxScopeGuards for object methods
266 // ============================================================================
268 // ----------------------------------------------------------------------------
269 // wxObjScopeGuardImpl0
270 // ----------------------------------------------------------------------------
272 template <class Obj
, class MemFun
>
273 class wxObjScopeGuardImpl0
: public wxScopeGuardImplBase
276 static wxObjScopeGuardImpl0
<Obj
, MemFun
>
277 MakeObjGuard(Obj
& obj
, MemFun memFun
)
279 return wxObjScopeGuardImpl0
<Obj
, MemFun
>(obj
, memFun
);
282 ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
284 void Execute() { (m_obj
.*m_memfun
)(); }
287 wxObjScopeGuardImpl0(Obj
& obj
, MemFun memFun
)
288 : m_obj(obj
), m_memfun(memFun
) { }
294 template <class Obj
, class MemFun
>
295 inline wxObjScopeGuardImpl0
<Obj
, MemFun
> wxMakeObjGuard(Obj
& obj
, MemFun memFun
)
297 return wxObjScopeGuardImpl0
<Obj
, MemFun
>::MakeObjGuard(obj
, memFun
);
300 template <class Obj
, class MemFun
, class P1
>
301 class wxObjScopeGuardImpl1
: public wxScopeGuardImplBase
304 static wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
305 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
307 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>(obj
, memFun
, p1
);
310 ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
312 void Execute() { (m_obj
.*m_memfun
)(m_p1
); }
315 wxObjScopeGuardImpl1(Obj
& obj
, MemFun memFun
, P1 p1
)
316 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
) { }
323 template <class Obj
, class MemFun
, class P1
>
324 inline wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
325 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
327 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>::MakeObjGuard(obj
, memFun
, p1
);
330 template <class Obj
, class MemFun
, class P1
, class P2
>
331 class wxObjScopeGuardImpl2
: public wxScopeGuardImplBase
334 static wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
335 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
337 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>(obj
, memFun
, p1
, p2
);
340 ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
342 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
); }
345 wxObjScopeGuardImpl2(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
346 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
) { }
354 template <class Obj
, class MemFun
, class P1
, class P2
>
355 inline wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
356 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
358 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>::
359 MakeObjGuard(obj
, memFun
, p1
, p2
);
362 template <class Obj
, class MemFun
, class P1
, class P2
, class P3
>
363 class wxObjScopeGuardImpl3
: public wxScopeGuardImplBase
366 static wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>
367 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
369 return wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>(obj
, memFun
, p1
, p2
, p3
);
372 ~wxObjScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
374 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
, m_p3
); }
377 wxObjScopeGuardImpl3(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
378 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
), m_p3(p3
) { }
387 template <class Obj
, class MemFun
, class P1
, class P2
, class P3
>
388 inline wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>
389 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
391 return wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>::
392 MakeObjGuard(obj
, memFun
, p1
, p2
, p3
);
395 // ----------------------------------------------------------------------------
396 // wxVariableSetter: use the same technique as for wxScopeGuard to allow
397 // setting a variable to some value on block exit
398 // ----------------------------------------------------------------------------
400 #ifdef wxHAS_NAMESPACES
405 // empty class just to be able to define a reference to it
406 class VariableSetterBase
{ };
408 typedef const VariableSetterBase
& VariableSetter
;
410 template <typename T
, typename U
>
411 class VariableSetterImpl
: public VariableSetterBase
414 VariableSetterImpl(T
& var
, U value
)
420 ~VariableSetterImpl()
429 // suppress the warning about assignment operator not being generated
430 VariableSetterImpl
<T
, U
>& operator=(const VariableSetterImpl
<T
, U
>&);
433 template <typename T
>
434 class VariableNullerImpl
: public VariableSetterBase
437 VariableNullerImpl(T
& var
)
442 ~VariableNullerImpl()
450 VariableNullerImpl
<T
>& operator=(const VariableNullerImpl
<T
>&);
453 } // namespace wxPrivate
455 template <typename T
, typename U
>
457 wxPrivate::VariableSetterImpl
<T
, U
> wxMakeVarSetter(T
& var
, U value
)
459 return wxPrivate::VariableSetterImpl
<T
, U
>(var
, value
);
462 // calling wxMakeVarSetter(ptr, NULL) doesn't work because U is deduced to be
463 // "int" and subsequent assignment of "U" to "T *" fails, so provide a special
464 // function for this special case
465 template <typename T
>
467 wxPrivate::VariableNullerImpl
<T
> wxMakeVarNuller(T
& var
)
469 return wxPrivate::VariableNullerImpl
<T
>(var
);
472 #endif // wxHAS_NAMESPACES
474 // ============================================================================
475 // macros for declaring unnamed scoped guards (which can't be dismissed)
476 // ============================================================================
478 // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
479 // but this results in compiler warnings about unused variables and I
480 // didn't find a way to work around this other than by having different
481 // macros with different names or using a less natural syntax for passing
482 // the arguments (e.g. as Boost preprocessor sequences, which would mean
483 // having to write wxON_BLOCK_EXIT(fwrite, (buf)(size)(n)(fp)) instead of
484 // wxON_BLOCK_EXIT4(fwrite, buf, size, n, fp)).
486 #define wxGuardName wxMAKE_UNIQUE_NAME(wxScopeGuard)
488 #define wxON_BLOCK_EXIT0_IMPL(n, f) \
489 wxScopeGuard n = wxMakeGuard(f); \
491 #define wxON_BLOCK_EXIT0(f) \
492 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
494 #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
495 wxScopeGuard n = wxMakeObjGuard(o, m); \
497 #define wxON_BLOCK_EXIT_OBJ0(o, m) \
498 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
500 #define wxON_BLOCK_EXIT_THIS0(m) \
501 wxON_BLOCK_EXIT_OBJ0(*this, m)
504 #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
505 wxScopeGuard n = wxMakeGuard(f, p1); \
507 #define wxON_BLOCK_EXIT1(f, p1) \
508 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
510 #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
511 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
513 #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
514 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
516 #define wxON_BLOCK_EXIT_THIS1(m, p1) \
517 wxON_BLOCK_EXIT_OBJ1(*this, m, p1)
520 #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
521 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
523 #define wxON_BLOCK_EXIT2(f, p1, p2) \
524 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
526 #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
527 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
529 #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
530 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
532 #define wxON_BLOCK_EXIT_THIS2(m, p1, p2) \
533 wxON_BLOCK_EXIT_OBJ2(*this, m, p1, p2)
536 #define wxON_BLOCK_EXIT3_IMPL(n, f, p1, p2, p3) \
537 wxScopeGuard n = wxMakeGuard(f, p1, p2, p3); \
539 #define wxON_BLOCK_EXIT3(f, p1, p2, p3) \
540 wxON_BLOCK_EXIT3_IMPL(wxGuardName, f, p1, p2, p3)
542 #define wxON_BLOCK_EXIT_OBJ3_IMPL(n, o, m, p1, p2, p3) \
543 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2, p3); \
545 #define wxON_BLOCK_EXIT_OBJ3(o, m, p1, p2, p3) \
546 wxON_BLOCK_EXIT_OBJ3_IMPL(wxGuardName, o, &m, p1, p2, p3)
548 #define wxON_BLOCK_EXIT_THIS3(m, p1, p2, p3) \
549 wxON_BLOCK_EXIT_OBJ3(*this, m, p1, p2, p3)
552 #define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter)
554 #define wxON_BLOCK_EXIT_SET_IMPL(n, var, value) \
555 wxPrivate::VariableSetter n = wxMakeVarSetter(var, value); \
558 #define wxON_BLOCK_EXIT_SET(var, value) \
559 wxON_BLOCK_EXIT_SET_IMPL(wxSetterName, var, value)
561 #define wxON_BLOCK_EXIT_NULL_IMPL(n, var) \
562 wxPrivate::VariableSetter n = wxMakeVarNuller(var); \
565 #define wxON_BLOCK_EXIT_NULL(ptr) \
566 wxON_BLOCK_EXIT_NULL_IMPL(wxSetterName, ptr)
568 #endif // _WX_SCOPEGUARD_H_