]> git.saurik.com Git - wxWidgets.git/blame - include/wx/scopeguard.h
Applied patch [ 1381895 ] remove circular include dependendcy in wxchar.h
[wxWidgets.git] / include / wx / scopeguard.h
CommitLineData
c66cca2a
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: wx/scopeguard.h
3// Purpose: declares wxwxScopeGuard and related macros
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 03.07.2003
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
65571936 9// Licence: wxWindows licence
c66cca2a
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12/*
13 Acknowledgements: this header is heavily based on (well, almost the exact
822fc19e 14 copy of) ScopeGuard.h by Andrei Alexandrescu and Petru Marginean published
c66cca2a 15 in December 2000 issue of C/C++ Users Journal.
822fc19e 16 http://www.cuj.com/documents/cujcexp1812alexandr/
c66cca2a
VZ
17 */
18
19#ifndef _WX_SCOPEGUARD_H_
20#define _WX_SCOPEGUARD_H_
21
22#include "wx/defs.h"
23
5800b5be
VZ
24#include "wx/except.h"
25
c66cca2a
VZ
26// ----------------------------------------------------------------------------
27// helpers
28// ----------------------------------------------------------------------------
29
822fc19e
WS
30#ifdef __WATCOMC__
31
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.
37
38#define wxPrivateOnScopeExit(guard) \
39 { \
40 if ( !(guard).WasDismissed() ) \
41 { \
42 wxTRY \
43 { \
44 (guard).Execute(); \
45 } \
46 wxCATCH_ALL(;) \
47 } \
48 }
49
50#define wxPrivateUse(n) wxUnusedVar(n)
51
52#else
53
c66cca2a
VZ
54namespace wxPrivate
55{
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
17e22c50 59 template <class ScopeGuardImpl>
d775fa82 60 void OnScopeExit(ScopeGuardImpl& guard)
c66cca2a
VZ
61 {
62 if ( !guard.WasDismissed() )
63 {
64 // we're called from ScopeGuardImpl dtor and so we must not throw
5800b5be 65 wxTRY
c66cca2a
VZ
66 {
67 guard.Execute();
68 }
f0658b1e 69 wxCATCH_ALL(;) // do nothing, just eat the exception
c66cca2a
VZ
70 }
71 }
72
73 // just to avoid the warning about unused variables
17e22c50 74 template <class T>
c66cca2a
VZ
75 void Use(const T& WXUNUSED(t))
76 {
77 }
78} // namespace wxPrivate
d775fa82 79
822fc19e
WS
80#define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n)
81#define wxPrivateUse(n) wxPrivate::Use(n)
82
83#endif
84
c66cca2a
VZ
85// ============================================================================
86// wxScopeGuard for functions and functors
87// ============================================================================
88
89// ----------------------------------------------------------------------------
90// wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below
91// ----------------------------------------------------------------------------
92
93class wxScopeGuardImplBase
94{
95public:
96 wxScopeGuardImplBase() : m_wasDismissed(false) { }
97
98 void Dismiss() const { m_wasDismissed = true; }
99
426a63a3 100 // for OnScopeExit() only (we can't make it friend, unfortunately)!
c66cca2a
VZ
101 bool WasDismissed() const { return m_wasDismissed; }
102
103protected:
104 ~wxScopeGuardImplBase() { }
105
d775fa82 106 wxScopeGuardImplBase(const wxScopeGuardImplBase& other)
c66cca2a
VZ
107 : m_wasDismissed(other.m_wasDismissed)
108 {
109 other.Dismiss();
110 }
111
112 // must be mutable for copy ctor to work
113 mutable bool m_wasDismissed;
114
115private:
116 wxScopeGuardImplBase& operator=(const wxScopeGuardImplBase&);
117};
118
119// ----------------------------------------------------------------------------
120// wxScopeGuardImpl0: scope guard for actions without parameters
121// ----------------------------------------------------------------------------
122
17e22c50 123template <class F>
c66cca2a
VZ
124class wxScopeGuardImpl0 : public wxScopeGuardImplBase
125{
126public:
127 static wxScopeGuardImpl0<F> MakeGuard(F fun)
128 {
129 return wxScopeGuardImpl0<F>(fun);
130 }
131
822fc19e 132 ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
133
134 void Execute() { m_fun(); }
135
136protected:
137 wxScopeGuardImpl0(F fun) : m_fun(fun) { }
138
139 F m_fun;
140
141 wxScopeGuardImpl0& operator=(const wxScopeGuardImpl0&);
142};
143
17e22c50 144template <class F>
c66cca2a
VZ
145inline wxScopeGuardImpl0<F> wxMakeGuard(F fun)
146{
147 return wxScopeGuardImpl0<F>::MakeGuard(fun);
148}
149
150// ----------------------------------------------------------------------------
151// wxScopeGuardImpl1: scope guard for actions with 1 parameter
152// ----------------------------------------------------------------------------
153
17e22c50 154template <class F, class P1>
c66cca2a
VZ
155class wxScopeGuardImpl1 : public wxScopeGuardImplBase
156{
157public:
158 static wxScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
159 {
160 return wxScopeGuardImpl1<F, P1>(fun, p1);
161 }
162
822fc19e 163 ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
c66cca2a
VZ
164
165 void Execute() { m_fun(m_p1); }
166
167protected:
168 wxScopeGuardImpl1(F fun, P1 p1) : m_fun(fun), m_p1(p1) { }
169
170 F m_fun;
171 const P1 m_p1;
172
173 wxScopeGuardImpl1& operator=(const wxScopeGuardImpl1&);
174};
175
17e22c50 176template <class F, class P1>
c66cca2a
VZ
177inline wxScopeGuardImpl1<F, P1> wxMakeGuard(F fun, P1 p1)
178{
179 return wxScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
180}
181
182// ----------------------------------------------------------------------------
183// wxScopeGuardImpl2: scope guard for actions with 2 parameters
184// ----------------------------------------------------------------------------
185
17e22c50 186template <class F, class P1, class P2>
c66cca2a
VZ
187class wxScopeGuardImpl2 : public wxScopeGuardImplBase
188{
189public:
190 static wxScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
191 {
192 return wxScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
193 }
194
822fc19e 195 ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
196
197 void Execute() { m_fun(m_p1, m_p2); }
198
199protected:
200 wxScopeGuardImpl2(F fun, P1 p1, P2 p2) : m_fun(fun), m_p1(p1), m_p2(p2) { }
201
202 F m_fun;
203 const P1 m_p1;
204 const P2 m_p2;
205
206 wxScopeGuardImpl2& operator=(const wxScopeGuardImpl2&);
207};
208
17e22c50 209template <class F, class P1, class P2>
c66cca2a
VZ
210inline wxScopeGuardImpl2<F, P1, P2> wxMakeGuard(F fun, P1 p1, P2 p2)
211{
212 return wxScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
213}
214
215// ============================================================================
216// wxScopeGuards for object methods
217// ============================================================================
218
219// ----------------------------------------------------------------------------
220// wxObjScopeGuardImpl0
221// ----------------------------------------------------------------------------
222
17e22c50 223template <class Obj, class MemFun>
c66cca2a
VZ
224class wxObjScopeGuardImpl0 : public wxScopeGuardImplBase
225{
226public:
227 static wxObjScopeGuardImpl0<Obj, MemFun>
228 MakeObjGuard(Obj& obj, MemFun memFun)
229 {
230 return wxObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
231 }
232
822fc19e 233 ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
234
235 void Execute() { (m_obj.*m_memfun)(); }
236
237protected:
238 wxObjScopeGuardImpl0(Obj& obj, MemFun memFun)
239 : m_obj(obj), m_memfun(memFun) { }
240
241 Obj& m_obj;
242 MemFun m_memfun;
243};
244
17e22c50 245template <class Obj, class MemFun>
c66cca2a
VZ
246inline wxObjScopeGuardImpl0<Obj, MemFun> wxMakeObjGuard(Obj& obj, MemFun memFun)
247{
248 return wxObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun);
249}
250
17e22c50 251template <class Obj, class MemFun, class P1>
c66cca2a
VZ
252class wxObjScopeGuardImpl1 : public wxScopeGuardImplBase
253{
254public:
255 static wxObjScopeGuardImpl1<Obj, MemFun, P1>
256 MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
257 {
258 return wxObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
259 }
260
822fc19e 261 ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
262
263 void Execute() { (m_obj.*m_memfun)(m_p1); }
264
265protected:
d775fa82 266 wxObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1)
c66cca2a
VZ
267 : m_obj(obj), m_memfun(memFun), m_p1(p1) { }
268
269 Obj& m_obj;
270 MemFun m_memfun;
271 const P1 m_p1;
272};
273
17e22c50 274template <class Obj, class MemFun, class P1>
c66cca2a
VZ
275inline wxObjScopeGuardImpl1<Obj, MemFun, P1>
276wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
277{
278 return wxObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1);
279}
280
17e22c50 281template <class Obj, class MemFun, class P1, class P2>
c66cca2a
VZ
282class wxObjScopeGuardImpl2 : public wxScopeGuardImplBase
283{
284public:
285 static wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
286 MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
287 {
288 return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
289 }
290
822fc19e 291 ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
292
293 void Execute() { (m_obj.*m_memfun)(m_p1, m_p2); }
294
295protected:
d775fa82 296 wxObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2)
c66cca2a
VZ
297 : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2) { }
298
299 Obj& m_obj;
300 MemFun m_memfun;
301 const P1 m_p1;
302 const P2 m_p2;
303};
304
17e22c50 305template <class Obj, class MemFun, class P1, class P2>
c66cca2a
VZ
306inline wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
307wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
308{
309 return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>::
310 MakeObjGuard(obj, memFun, p1, p2);
311}
312
313// ============================================================================
314// public stuff
315// ============================================================================
316
317// wxScopeGuard is just a reference, see the explanation in CUJ article
318typedef const wxScopeGuardImplBase& wxScopeGuard;
319
320// when an unnamed scope guard is needed, the macros below may be used
321//
322// NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro
323// but this results in compiler warnings about unused variables and I
324// didn't find a way to work around this other than by having different
325// macros with different names
426a63a3
VZ
326
327#define wxGuardName wxMAKE_UNIQUE_NAME(scopeGuard)
328
329#define wxON_BLOCK_EXIT0_IMPL(n, f) \
330 wxScopeGuard n = wxMakeGuard(f); \
822fc19e 331 wxPrivateUse(n)
5800b5be 332#define wxON_BLOCK_EXIT0(f) \
426a63a3 333 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
c66cca2a 334
426a63a3
VZ
335#define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
336 wxScopeGuard n = wxMakeObjGuard(o, m); \
822fc19e 337 wxPrivateUse(n)
5800b5be 338#define wxON_BLOCK_EXIT_OBJ0(o, m) \
2bb3c0c0 339 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
c66cca2a 340
426a63a3
VZ
341#define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
342 wxScopeGuard n = wxMakeGuard(f, p1); \
822fc19e 343 wxPrivateUse(n)
5800b5be 344#define wxON_BLOCK_EXIT1(f, p1) \
426a63a3 345 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
c66cca2a 346
426a63a3
VZ
347#define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
348 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
822fc19e 349 wxPrivateUse(n)
5800b5be 350#define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
2bb3c0c0 351 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
c66cca2a 352
426a63a3
VZ
353#define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
354 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
822fc19e 355 wxPrivateUse(n)
5800b5be 356#define wxON_BLOCK_EXIT2(f, p1, p2) \
426a63a3 357 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
c66cca2a 358
426a63a3
VZ
359#define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
360 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
822fc19e 361 wxPrivateUse(n)
5800b5be 362#define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
2bb3c0c0 363 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
c66cca2a
VZ
364
365#endif // _WX_SCOPEGUARD_H_