]>
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) wxScopeGuard.h by Andrei Alexandrescu and Petru Marginean published
15 in December 2000 issue of C/C++ Users Journal.
18 #ifndef _WX_SCOPEGUARD_H_
19 #define _WX_SCOPEGUARD_H_
23 #include "wx/except.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
31 // in the original implementation this was a member template function of
32 // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't
33 // support member templates and so we must make it global
34 template <typename ScopeGuardImpl
>
35 void OnScopeExit(ScopeGuardImpl
& guard
)
37 if ( !guard
.WasDismissed() )
39 // we're called from ScopeGuardImpl dtor and so we must not throw
44 wxCATCH_ALL(;) // do nothing, just eat the exception
48 // just to avoid the warning about unused variables
50 void Use(const T
& WXUNUSED(t
))
53 } // namespace wxPrivate
55 // ============================================================================
56 // wxScopeGuard for functions and functors
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
61 // ----------------------------------------------------------------------------
63 class wxScopeGuardImplBase
66 wxScopeGuardImplBase() : m_wasDismissed(false) { }
68 void Dismiss() const { m_wasDismissed
= true; }
70 // for OnScopeExit() only (we can't make it friend, unfortunately)!
71 bool WasDismissed() const { return m_wasDismissed
; }
74 ~wxScopeGuardImplBase() { }
76 wxScopeGuardImplBase(const wxScopeGuardImplBase
& other
)
77 : m_wasDismissed(other
.m_wasDismissed
)
82 // must be mutable for copy ctor to work
83 mutable bool m_wasDismissed
;
86 wxScopeGuardImplBase
& operator=(const wxScopeGuardImplBase
&);
89 // ----------------------------------------------------------------------------
90 // wxScopeGuardImpl0: scope guard for actions without parameters
91 // ----------------------------------------------------------------------------
94 class wxScopeGuardImpl0
: public wxScopeGuardImplBase
97 static wxScopeGuardImpl0
<F
> MakeGuard(F fun
)
99 return wxScopeGuardImpl0
<F
>(fun
);
102 ~wxScopeGuardImpl0() { wxPrivate::OnScopeExit(*this); }
104 void Execute() { m_fun(); }
107 wxScopeGuardImpl0(F fun
) : m_fun(fun
) { }
111 wxScopeGuardImpl0
& operator=(const wxScopeGuardImpl0
&);
114 template <typename F
>
115 inline wxScopeGuardImpl0
<F
> wxMakeGuard(F fun
)
117 return wxScopeGuardImpl0
<F
>::MakeGuard(fun
);
120 // ----------------------------------------------------------------------------
121 // wxScopeGuardImpl1: scope guard for actions with 1 parameter
122 // ----------------------------------------------------------------------------
124 template <typename F
, typename P1
>
125 class wxScopeGuardImpl1
: public wxScopeGuardImplBase
128 static wxScopeGuardImpl1
<F
, P1
> MakeGuard(F fun
, P1 p1
)
130 return wxScopeGuardImpl1
<F
, P1
>(fun
, p1
);
133 ~wxScopeGuardImpl1() { wxPrivate::OnScopeExit(*this); }
135 void Execute() { m_fun(m_p1
); }
138 wxScopeGuardImpl1(F fun
, P1 p1
) : m_fun(fun
), m_p1(p1
) { }
143 wxScopeGuardImpl1
& operator=(const wxScopeGuardImpl1
&);
146 template <typename F
, typename P1
>
147 inline wxScopeGuardImpl1
<F
, P1
> wxMakeGuard(F fun
, P1 p1
)
149 return wxScopeGuardImpl1
<F
, P1
>::MakeGuard(fun
, p1
);
152 // ----------------------------------------------------------------------------
153 // wxScopeGuardImpl2: scope guard for actions with 2 parameters
154 // ----------------------------------------------------------------------------
156 template <typename F
, typename P1
, typename P2
>
157 class wxScopeGuardImpl2
: public wxScopeGuardImplBase
160 static wxScopeGuardImpl2
<F
, P1
, P2
> MakeGuard(F fun
, P1 p1
, P2 p2
)
162 return wxScopeGuardImpl2
<F
, P1
, P2
>(fun
, p1
, p2
);
165 ~wxScopeGuardImpl2() { wxPrivate::OnScopeExit(*this); }
167 void Execute() { m_fun(m_p1
, m_p2
); }
170 wxScopeGuardImpl2(F fun
, P1 p1
, P2 p2
) : m_fun(fun
), m_p1(p1
), m_p2(p2
) { }
176 wxScopeGuardImpl2
& operator=(const wxScopeGuardImpl2
&);
179 template <typename F
, typename P1
, typename P2
>
180 inline wxScopeGuardImpl2
<F
, P1
, P2
> wxMakeGuard(F fun
, P1 p1
, P2 p2
)
182 return wxScopeGuardImpl2
<F
, P1
, P2
>::MakeGuard(fun
, p1
, p2
);
185 // ============================================================================
186 // wxScopeGuards for object methods
187 // ============================================================================
189 // ----------------------------------------------------------------------------
190 // wxObjScopeGuardImpl0
191 // ----------------------------------------------------------------------------
193 template <class Obj
, typename MemFun
>
194 class wxObjScopeGuardImpl0
: public wxScopeGuardImplBase
197 static wxObjScopeGuardImpl0
<Obj
, MemFun
>
198 MakeObjGuard(Obj
& obj
, MemFun memFun
)
200 return wxObjScopeGuardImpl0
<Obj
, MemFun
>(obj
, memFun
);
203 ~wxObjScopeGuardImpl0() { wxPrivate::OnScopeExit(*this); }
205 void Execute() { (m_obj
.*m_memfun
)(); }
208 wxObjScopeGuardImpl0(Obj
& obj
, MemFun memFun
)
209 : m_obj(obj
), m_memfun(memFun
) { }
215 template <class Obj
, typename MemFun
>
216 inline wxObjScopeGuardImpl0
<Obj
, MemFun
> wxMakeObjGuard(Obj
& obj
, MemFun memFun
)
218 return wxObjScopeGuardImpl0
<Obj
, MemFun
>::MakeObjGuard(obj
, memFun
);
221 template <class Obj
, typename MemFun
, typename P1
>
222 class wxObjScopeGuardImpl1
: public wxScopeGuardImplBase
225 static wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
226 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
228 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>(obj
, memFun
, p1
);
231 ~wxObjScopeGuardImpl1() { wxPrivate::OnScopeExit(*this); }
233 void Execute() { (m_obj
.*m_memfun
)(m_p1
); }
236 wxObjScopeGuardImpl1(Obj
& obj
, MemFun memFun
, P1 p1
)
237 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
) { }
244 template <class Obj
, typename MemFun
, typename P1
>
245 inline wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>
246 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
)
248 return wxObjScopeGuardImpl1
<Obj
, MemFun
, P1
>::MakeObjGuard(obj
, memFun
, p1
);
251 template <class Obj
, typename MemFun
, typename P1
, typename P2
>
252 class wxObjScopeGuardImpl2
: public wxScopeGuardImplBase
255 static wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
256 MakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
258 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>(obj
, memFun
, p1
, p2
);
261 ~wxObjScopeGuardImpl2() { wxPrivate::OnScopeExit(*this); }
263 void Execute() { (m_obj
.*m_memfun
)(m_p1
, m_p2
); }
266 wxObjScopeGuardImpl2(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
267 : m_obj(obj
), m_memfun(memFun
), m_p1(p1
), m_p2(p2
) { }
275 template <class Obj
, typename MemFun
, typename P1
, typename P2
>
276 inline wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>
277 wxMakeObjGuard(Obj
& obj
, MemFun memFun
, P1 p1
, P2 p2
)
279 return wxObjScopeGuardImpl2
<Obj
, MemFun
, P1
, P2
>::
280 MakeObjGuard(obj
, memFun
, p1
, p2
);
283 // ============================================================================
285 // ============================================================================
287 // wxScopeGuard is just a reference, see the explanation in CUJ article
288 typedef const wxScopeGuardImplBase
& wxScopeGuard
;
290 // when an unnamed scope guard is needed, the macros below may be used
292 // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
293 // but this results in compiler warnings about unused variables and I
294 // didn't find a way to work around this other than by having different
295 // macros with different names
297 #define wxGuardName wxMAKE_UNIQUE_NAME(scopeGuard)
299 #define wxON_BLOCK_EXIT0_IMPL(n, f) \
300 wxScopeGuard n = wxMakeGuard(f); \
302 #define wxON_BLOCK_EXIT0(f) \
303 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
305 #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
306 wxScopeGuard n = wxMakeObjGuard(o, m); \
308 #define wxON_BLOCK_EXIT_OBJ0(o, m) \
309 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, m)
311 #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
312 wxScopeGuard n = wxMakeGuard(f, p1); \
314 #define wxON_BLOCK_EXIT1(f, p1) \
315 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
317 #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
318 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
320 #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
321 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, m, p1)
323 #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
324 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
326 #define wxON_BLOCK_EXIT2(f, p1, p2) \
327 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
329 #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
330 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
332 #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
333 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, m, p1, p2)
335 #endif // _WX_SCOPEGUARD_H_