]>
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)
56 // in the original implementation this was a member template function of
57 // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't
58 // support member templates and so we must make it global
59 template <class ScopeGuardImpl
>
60 void OnScopeExit(ScopeGuardImpl
& guard
)
62 if ( !guard
.WasDismissed() )
64 // we're called from ScopeGuardImpl dtor and so we must not throw
69 wxCATCH_ALL(;) // do nothing, just eat the exception
73 // just to avoid the warning about unused variables
75 void Use(const T
& WXUNUSED(t
))
78 } // namespace wxPrivate
80 #define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n)
81 #define wxPrivateUse(n) wxPrivate::Use(n)
85 // ============================================================================
86 // wxScopeGuard for functions and functors
87 // ============================================================================
89 // ----------------------------------------------------------------------------
90 // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
91 // ----------------------------------------------------------------------------
93 class wxScopeGuardImplBase
96 wxScopeGuardImplBase() : m_wasDismissed(false) { }
98 wxScopeGuardImplBase(const wxScopeGuardImplBase
& other
)
99 : m_wasDismissed(other
.m_wasDismissed
)
104 void Dismiss() const { m_wasDismissed
= true; }
106 // for OnScopeExit() only (we can't make it friend, unfortunately)!
107 bool WasDismissed() const { return m_wasDismissed
; }
110 ~wxScopeGuardImplBase() { }
112 // must be mutable for copy ctor to work
113 mutable bool m_wasDismissed
;
116 wxScopeGuardImplBase
& operator=(const wxScopeGuardImplBase
&);
119 // wxScopeGuard is just a reference, see the explanation in CUJ article
120 typedef const wxScopeGuardImplBase
& wxScopeGuard
;
122 // ----------------------------------------------------------------------------
123 // wxScopeGuardImpl0: scope guard for actions without parameters
124 // ----------------------------------------------------------------------------
127 class wxScopeGuardImpl0
: public wxScopeGuardImplBase
130 static wxScopeGuardImpl0
<F
> MakeGuard(F fun
)
132 return wxScopeGuardImpl0
<F
>(fun
);
135 ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
137 void Execute() { m_fun(); }
140 wxScopeGuardImpl0(F fun
) : m_fun(fun
) { }
144 wxScopeGuardImpl0
& operator=(const wxScopeGuardImpl0
&);
148 inline wxScopeGuardImpl0
<F
> wxMakeGuard(F fun
)
150 return wxScopeGuardImpl0
<F
>::MakeGuard(fun
);
153 // ----------------------------------------------------------------------------
154 // wxScopeGuardImpl1: scope guard for actions with 1 parameter
155 // ----------------------------------------------------------------------------
157 template <class F
, class P1
>
158 class wxScopeGuardImpl1
: public wxScopeGuardImplBase
161 static wxScopeGuardImpl1
<F
, P1
> MakeGuard(F fun
, P1 p1
)
163 return wxScopeGuardImpl1
<F
, P1
>(fun
, p1
);
166 ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
168 void Execute() { m_fun(m_p1
); }
171 wxScopeGuardImpl1(F fun
, P1 p1
) : m_fun(fun
), m_p1(p1
) { }
176 wxScopeGuardImpl1
& operator=(const wxScopeGuardImpl1
&);
179 template <class F
, class P1
>
180 inline wxScopeGuardImpl1
<F
, P1
> wxMakeGuard(F fun
, P1 p1
)
182 return wxScopeGuardImpl1
<F
, P1
>::MakeGuard(fun
, p1
);
185 // ----------------------------------------------------------------------------
186 // wxScopeGuardImpl2: scope guard for actions with 2 parameters
187 // ----------------------------------------------------------------------------
189 template <class F
, class P1
, class P2
>
190 class wxScopeGuardImpl2
: public wxScopeGuardImplBase
193 static wxScopeGuardImpl2
<F
, P1
, P2
> MakeGuard(F fun
, P1 p1
, P2 p2
)
195 return wxScopeGuardImpl2
<F
, P1
, P2
>(fun
, p1
, p2
);
198 ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
200 void Execute() { m_fun(m_p1
, m_p2
); }
203 wxScopeGuardImpl2(F fun
, P1 p1
, P2 p2
) : m_fun(fun
), m_p1(p1
), m_p2(p2
) { }
209 wxScopeGuardImpl2
& operator=(const wxScopeGuardImpl2
&);
212 template <class F
, class P1
, class P2
>
213 inline wxScopeGuardImpl2
<F
, P1
, P2
> wxMakeGuard(F fun
, P1 p1
, P2 p2
)
215 return wxScopeGuardImpl2
<F
, P1
, P2
>::MakeGuard(fun
, p1
, p2
);
218 // ----------------------------------------------------------------------------
219 // wxScopeGuardImpl3: scope guard for actions with 3 parameters
220 // ----------------------------------------------------------------------------
222 template <class F
, class P1
, class P2
, class P3
>
223 class wxScopeGuardImpl3
: public wxScopeGuardImplBase
226 static wxScopeGuardImpl3
<F
, P1
, P2
, P3
> MakeGuard(F fun
, P1 p1
, P2 p2
, P3 p3
)
228 return wxScopeGuardImpl3
<F
, P1
, P2
, P3
>(fun
, p1
, p2
, p3
);
231 ~wxScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
233 void Execute() { m_fun(m_p1
, m_p2
, m_p3
); }
236 wxScopeGuardImpl3(F fun
, P1 p1
, P2 p2
, P3 p3
)
237 : m_fun(fun
), m_p1(p1
), m_p2(p2
), m_p3(p3
) { }
244 wxScopeGuardImpl3
& operator=(const wxScopeGuardImpl3
&);
247 template <class F
, class P1
, class P2
, class P3
>
248 inline wxScopeGuardImpl3
<F
, P1
, P2
, P3
> wxMakeGuard(F fun
, P1 p1
, P2 p2
, P3 p3
)
250 return wxScopeGuardImpl3
<F
, P1
, P2
, P3
>::MakeGuard(fun
, p1
, p2
, p3
);
253 // ============================================================================
254 // wxScopeGuards for object methods
255 // ============================================================================
257 // ----------------------------------------------------------------------------
258 // wxObjScopeGuardImpl0
259 // ----------------------------------------------------------------------------
261 template <class Obj
, class MemFun
>
262 class wxObjScopeGuardImpl0
: public wxScopeGuardImplBase
265 static wxObjScopeGuardImpl0
<Obj
, MemFun
>
266 MakeObjGuard(Obj
& obj
, MemFun memFun
)
268 return wxObjScopeGuardImpl0
<Obj
, MemFun
>(obj
, memFun
);
271 ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
273 void Execute() { (m_obj
.*m_memfun
)(); }
276 wxObjScopeGuardImpl0(Obj
& obj
, MemFun memFun
)
277 : m_obj(obj
), m_memfun(memFun
) { }
283 template <class Obj
, class MemFun
>
284 inline wxObjScopeGuardImpl0
<Obj
, MemFun
> wxMakeObjGuard(Obj
& obj
, MemFun memFun
)
286 return wxObjScopeGuardImpl0
<Obj
, MemFun
>::MakeObjGuard(obj
, memFun
);
289 template <class Obj
, class MemFun
, class P1
>
290 class wxObjScopeGuardImpl1
: public wxScopeGuardImplBase
293 static wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
294 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
296 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>(obj
, memFun
, p1
);
299 ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
301 void Execute() { (m_obj
.*m_memfun
)(m_p1
); }
304 wxObjScopeGuardImpl1(Obj
& obj
, MemFun memFun
, P1 p1
)
305 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
) { }
312 template <class Obj
, class MemFun
, class P1
>
313 inline wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
314 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
316 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>::MakeObjGuard(obj
, memFun
, p1
);
319 template <class Obj
, class MemFun
, class P1
, class P2
>
320 class wxObjScopeGuardImpl2
: public wxScopeGuardImplBase
323 static wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
324 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
326 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>(obj
, memFun
, p1
, p2
);
329 ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
331 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
); }
334 wxObjScopeGuardImpl2(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
335 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
) { }
343 template <class Obj
, class MemFun
, class P1
, class P2
>
344 inline wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
345 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
347 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>::
348 MakeObjGuard(obj
, memFun
, p1
, p2
);
351 template <class Obj
, class MemFun
, class P1
, class P2
, class P3
>
352 class wxObjScopeGuardImpl3
: public wxScopeGuardImplBase
355 static wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>
356 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
358 return wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>(obj
, memFun
, p1
, p2
, p3
);
361 ~wxObjScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
363 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
, m_p3
); }
366 wxObjScopeGuardImpl3(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
367 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
), m_p3(p3
) { }
376 template <class Obj
, class MemFun
, class P1
, class P2
, class P3
>
377 inline wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>
378 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
, P3 p3
)
380 return wxObjScopeGuardImpl3
<Obj
, MemFun
, P1
, P2
, P3
>::
381 MakeObjGuard(obj
, memFun
, p1
, p2
, p3
);
384 // ----------------------------------------------------------------------------
385 // wxVariableSetter: use the same technique as for wxScopeGuard to allow
386 // setting a variable to some value on block exit
387 // ----------------------------------------------------------------------------
392 // empty class just to be able to define a reference to it
393 class VariableSetterBase
: public wxScopeGuardImplBase
{ };
395 typedef const VariableSetterBase
& VariableSetter
;
397 template <typename T
, typename U
>
398 class VariableSetterImpl
: public VariableSetterBase
401 VariableSetterImpl(T
& var
, U value
)
407 ~VariableSetterImpl() { wxPrivateOnScopeExit(*this); }
409 void Execute() { m_var
= m_value
; }
415 // suppress the warning about assignment operator not being generated
416 VariableSetterImpl
<T
, U
>& operator=(const VariableSetterImpl
<T
, U
>&);
419 template <typename T
>
420 class VariableNullerImpl
: public VariableSetterBase
423 VariableNullerImpl(T
& var
)
428 ~VariableNullerImpl() { wxPrivateOnScopeExit(*this); }
430 void Execute() { m_var
= NULL
; }
435 VariableNullerImpl
<T
>& operator=(const VariableNullerImpl
<T
>&);
438 } // namespace wxPrivate
440 template <typename T
, typename U
>
442 wxPrivate::VariableSetterImpl
<T
, U
> wxMakeVarSetter(T
& var
, U value
)
444 return wxPrivate::VariableSetterImpl
<T
, U
>(var
, value
);
447 // calling wxMakeVarSetter(ptr, NULL) doesn't work because U is deduced to be
448 // "int" and subsequent assignment of "U" to "T *" fails, so provide a special
449 // function for this special case
450 template <typename T
>
452 wxPrivate::VariableNullerImpl
<T
> wxMakeVarNuller(T
& var
)
454 return wxPrivate::VariableNullerImpl
<T
>(var
);
457 // ============================================================================
458 // macros for declaring unnamed scoped guards (which can't be dismissed)
459 // ============================================================================
461 // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
462 // but this results in compiler warnings about unused variables and I
463 // didn't find a way to work around this other than by having different
464 // macros with different names or using a less natural syntax for passing
465 // the arguments (e.g. as Boost preprocessor sequences, which would mean
466 // having to write wxON_BLOCK_EXIT(fwrite, (buf)(size)(n)(fp)) instead of
467 // wxON_BLOCK_EXIT4(fwrite, buf, size, n, fp)).
469 #define wxGuardName wxMAKE_UNIQUE_NAME(wxScopeGuard)
471 #define wxON_BLOCK_EXIT0_IMPL(n, f) \
472 wxScopeGuard n = wxMakeGuard(f); \
474 #define wxON_BLOCK_EXIT0(f) \
475 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
477 #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
478 wxScopeGuard n = wxMakeObjGuard(o, m); \
480 #define wxON_BLOCK_EXIT_OBJ0(o, m) \
481 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
483 #define wxON_BLOCK_EXIT_THIS0(m) \
484 wxON_BLOCK_EXIT_OBJ0(*this, m)
487 #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
488 wxScopeGuard n = wxMakeGuard(f, p1); \
490 #define wxON_BLOCK_EXIT1(f, p1) \
491 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
493 #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
494 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
496 #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
497 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
499 #define wxON_BLOCK_EXIT_THIS1(m, p1) \
500 wxON_BLOCK_EXIT_OBJ1(*this, m, p1)
503 #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
504 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
506 #define wxON_BLOCK_EXIT2(f, p1, p2) \
507 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
509 #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
510 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
512 #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
513 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
515 #define wxON_BLOCK_EXIT_THIS2(m, p1, p2) \
516 wxON_BLOCK_EXIT_OBJ2(*this, m, p1, p2)
519 #define wxON_BLOCK_EXIT3_IMPL(n, f, p1, p2, p3) \
520 wxScopeGuard n = wxMakeGuard(f, p1, p2, p3); \
522 #define wxON_BLOCK_EXIT3(f, p1, p2, p3) \
523 wxON_BLOCK_EXIT3_IMPL(wxGuardName, f, p1, p2, p3)
525 #define wxON_BLOCK_EXIT_OBJ3_IMPL(n, o, m, p1, p2, p3) \
526 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2, p3); \
528 #define wxON_BLOCK_EXIT_OBJ3(o, m, p1, p2, p3) \
529 wxON_BLOCK_EXIT_OBJ3_IMPL(wxGuardName, o, &m, p1, p2, p3)
531 #define wxON_BLOCK_EXIT_THIS3(m, p1, p2, p3) \
532 wxON_BLOCK_EXIT_OBJ3(*this, m, p1, p2, p3)
535 #define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter)
537 #define wxON_BLOCK_EXIT_SET_IMPL(n, var, value) \
538 wxPrivate::VariableSetter n = wxMakeVarSetter(var, value); \
541 #define wxON_BLOCK_EXIT_SET(var, value) \
542 wxON_BLOCK_EXIT_SET_IMPL(wxSetterName, var, value)
544 #define wxON_BLOCK_EXIT_NULL_IMPL(n, var) \
545 wxPrivate::VariableSetter n = wxMakeVarNuller(var); \
548 #define wxON_BLOCK_EXIT_NULL(ptr) \
549 wxON_BLOCK_EXIT_NULL_IMPL(wxSetterName, ptr)
551 #endif // _WX_SCOPEGUARD_H_