]>
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 #if !defined(__GNUC__) || wxCHECK_GCC_VERSION(2, 95)
55 // namespace support was first implemented in gcc-2.95,
56 // so avoid using it for older versions.
62 // in the original implementation this was a member template function of
63 // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't
64 // support member templates and so we must make it global
65 template <class ScopeGuardImpl
>
66 void OnScopeExit(ScopeGuardImpl
& guard
)
68 if ( !guard
.WasDismissed() )
70 // we're called from ScopeGuardImpl dtor and so we must not throw
75 wxCATCH_ALL(;) // do nothing, just eat the exception
79 // just to avoid the warning about unused variables
81 void Use(const T
& WXUNUSED(t
))
84 #if !defined(__GNUC__) || wxCHECK_GCC_VERSION(2, 95)
85 } // namespace wxPrivate
88 #define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n)
89 #define wxPrivateUse(n) wxPrivate::Use(n)
93 // ============================================================================
94 // wxScopeGuard for functions and functors
95 // ============================================================================
97 // ----------------------------------------------------------------------------
98 // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
99 // ----------------------------------------------------------------------------
101 class wxScopeGuardImplBase
104 wxScopeGuardImplBase() : m_wasDismissed(false) { }
106 wxScopeGuardImplBase(const wxScopeGuardImplBase
& other
)
107 : m_wasDismissed(other
.m_wasDismissed
)
112 void Dismiss() const { m_wasDismissed
= true; }
114 // for OnScopeExit() only (we can't make it friend, unfortunately)!
115 bool WasDismissed() const { return m_wasDismissed
; }
118 ~wxScopeGuardImplBase() { }
120 // must be mutable for copy ctor to work
121 mutable bool m_wasDismissed
;
124 wxScopeGuardImplBase
& operator=(const wxScopeGuardImplBase
&);
127 // ----------------------------------------------------------------------------
128 // wxScopeGuardImpl0: scope guard for actions without parameters
129 // ----------------------------------------------------------------------------
132 class wxScopeGuardImpl0
: public wxScopeGuardImplBase
135 static wxScopeGuardImpl0
<F
> MakeGuard(F fun
)
137 return wxScopeGuardImpl0
<F
>(fun
);
140 ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
142 void Execute() { m_fun(); }
145 wxScopeGuardImpl0(F fun
) : m_fun(fun
) { }
149 wxScopeGuardImpl0
& operator=(const wxScopeGuardImpl0
&);
153 inline wxScopeGuardImpl0
<F
> wxMakeGuard(F fun
)
155 return wxScopeGuardImpl0
<F
>::MakeGuard(fun
);
158 // ----------------------------------------------------------------------------
159 // wxScopeGuardImpl1: scope guard for actions with 1 parameter
160 // ----------------------------------------------------------------------------
162 template <class F
, class P1
>
163 class wxScopeGuardImpl1
: public wxScopeGuardImplBase
166 static wxScopeGuardImpl1
<F
, P1
> MakeGuard(F fun
, P1 p1
)
168 return wxScopeGuardImpl1
<F
, P1
>(fun
, p1
);
171 ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
173 void Execute() { m_fun(m_p1
); }
176 wxScopeGuardImpl1(F fun
, P1 p1
) : m_fun(fun
), m_p1(p1
) { }
181 wxScopeGuardImpl1
& operator=(const wxScopeGuardImpl1
&);
184 template <class F
, class P1
>
185 inline wxScopeGuardImpl1
<F
, P1
> wxMakeGuard(F fun
, P1 p1
)
187 return wxScopeGuardImpl1
<F
, P1
>::MakeGuard(fun
, p1
);
190 // ----------------------------------------------------------------------------
191 // wxScopeGuardImpl2: scope guard for actions with 2 parameters
192 // ----------------------------------------------------------------------------
194 template <class F
, class P1
, class P2
>
195 class wxScopeGuardImpl2
: public wxScopeGuardImplBase
198 static wxScopeGuardImpl2
<F
, P1
, P2
> MakeGuard(F fun
, P1 p1
, P2 p2
)
200 return wxScopeGuardImpl2
<F
, P1
, P2
>(fun
, p1
, p2
);
203 ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
205 void Execute() { m_fun(m_p1
, m_p2
); }
208 wxScopeGuardImpl2(F fun
, P1 p1
, P2 p2
) : m_fun(fun
), m_p1(p1
), m_p2(p2
) { }
214 wxScopeGuardImpl2
& operator=(const wxScopeGuardImpl2
&);
217 template <class F
, class P1
, class P2
>
218 inline wxScopeGuardImpl2
<F
, P1
, P2
> wxMakeGuard(F fun
, P1 p1
, P2 p2
)
220 return wxScopeGuardImpl2
<F
, P1
, P2
>::MakeGuard(fun
, p1
, p2
);
223 // ============================================================================
224 // wxScopeGuards for object methods
225 // ============================================================================
227 // ----------------------------------------------------------------------------
228 // wxObjScopeGuardImpl0
229 // ----------------------------------------------------------------------------
231 template <class Obj
, class MemFun
>
232 class wxObjScopeGuardImpl0
: public wxScopeGuardImplBase
235 static wxObjScopeGuardImpl0
<Obj
, MemFun
>
236 MakeObjGuard(Obj
& obj
, MemFun memFun
)
238 return wxObjScopeGuardImpl0
<Obj
, MemFun
>(obj
, memFun
);
241 ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
243 void Execute() { (m_obj
.*m_memfun
)(); }
246 wxObjScopeGuardImpl0(Obj
& obj
, MemFun memFun
)
247 : m_obj(obj
), m_memfun(memFun
) { }
253 template <class Obj
, class MemFun
>
254 inline wxObjScopeGuardImpl0
<Obj
, MemFun
> wxMakeObjGuard(Obj
& obj
, MemFun memFun
)
256 return wxObjScopeGuardImpl0
<Obj
, MemFun
>::MakeObjGuard(obj
, memFun
);
259 template <class Obj
, class MemFun
, class P1
>
260 class wxObjScopeGuardImpl1
: public wxScopeGuardImplBase
263 static wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
264 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
266 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>(obj
, memFun
, p1
);
269 ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
271 void Execute() { (m_obj
.*m_memfun
)(m_p1
); }
274 wxObjScopeGuardImpl1(Obj
& obj
, MemFun memFun
, P1 p1
)
275 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
) { }
282 template <class Obj
, class MemFun
, class P1
>
283 inline wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
284 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
286 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>::MakeObjGuard(obj
, memFun
, p1
);
289 template <class Obj
, class MemFun
, class P1
, class P2
>
290 class wxObjScopeGuardImpl2
: public wxScopeGuardImplBase
293 static wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
294 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
296 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>(obj
, memFun
, p1
, p2
);
299 ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
301 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
); }
304 wxObjScopeGuardImpl2(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
305 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
) { }
313 template <class Obj
, class MemFun
, class P1
, class P2
>
314 inline wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
315 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
317 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>::
318 MakeObjGuard(obj
, memFun
, p1
, p2
);
321 // ============================================================================
323 // ============================================================================
325 // wxScopeGuard is just a reference, see the explanation in CUJ article
326 typedef const wxScopeGuardImplBase
& wxScopeGuard
;
328 // when an unnamed scope guard is needed, the macros below may be used
330 // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
331 // but this results in compiler warnings about unused variables and I
332 // didn't find a way to work around this other than by having different
333 // macros with different names
335 #define wxGuardName wxMAKE_UNIQUE_NAME(scopeGuard)
337 #define wxON_BLOCK_EXIT0_IMPL(n, f) \
338 wxScopeGuard n = wxMakeGuard(f); \
340 #define wxON_BLOCK_EXIT0(f) \
341 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
343 #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
344 wxScopeGuard n = wxMakeObjGuard(o, m); \
346 #define wxON_BLOCK_EXIT_OBJ0(o, m) \
347 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
349 #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
350 wxScopeGuard n = wxMakeGuard(f, p1); \
352 #define wxON_BLOCK_EXIT1(f, p1) \
353 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
355 #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
356 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
358 #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
359 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
361 #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
362 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
364 #define wxON_BLOCK_EXIT2(f, p1, p2) \
365 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
367 #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
368 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
370 #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
371 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
373 #endif // _WX_SCOPEGUARD_H_