]> git.saurik.com Git - wxWidgets.git/blame - include/wx/scopeguard.h
Add support for wxLIST_AUTOSIZE_USEHEADER to InsertColumn().
[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
f7e81ed1
VZ
98 wxScopeGuardImplBase(const wxScopeGuardImplBase& other)
99 : m_wasDismissed(other.m_wasDismissed)
100 {
101 other.Dismiss();
102 }
103
c66cca2a
VZ
104 void Dismiss() const { m_wasDismissed = true; }
105
426a63a3 106 // for OnScopeExit() only (we can't make it friend, unfortunately)!
c66cca2a
VZ
107 bool WasDismissed() const { return m_wasDismissed; }
108
109protected:
110 ~wxScopeGuardImplBase() { }
111
c66cca2a
VZ
112 // must be mutable for copy ctor to work
113 mutable bool m_wasDismissed;
114
115private:
116 wxScopeGuardImplBase& operator=(const wxScopeGuardImplBase&);
117};
118
d2a48d5c
VZ
119// wxScopeGuard is just a reference, see the explanation in CUJ article
120typedef const wxScopeGuardImplBase& wxScopeGuard;
121
c66cca2a
VZ
122// ----------------------------------------------------------------------------
123// wxScopeGuardImpl0: scope guard for actions without parameters
124// ----------------------------------------------------------------------------
125
17e22c50 126template <class F>
c66cca2a
VZ
127class wxScopeGuardImpl0 : public wxScopeGuardImplBase
128{
129public:
130 static wxScopeGuardImpl0<F> MakeGuard(F fun)
131 {
132 return wxScopeGuardImpl0<F>(fun);
133 }
134
822fc19e 135 ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
136
137 void Execute() { m_fun(); }
138
139protected:
140 wxScopeGuardImpl0(F fun) : m_fun(fun) { }
141
142 F m_fun;
143
144 wxScopeGuardImpl0& operator=(const wxScopeGuardImpl0&);
145};
146
17e22c50 147template <class F>
c66cca2a
VZ
148inline wxScopeGuardImpl0<F> wxMakeGuard(F fun)
149{
150 return wxScopeGuardImpl0<F>::MakeGuard(fun);
151}
152
153// ----------------------------------------------------------------------------
154// wxScopeGuardImpl1: scope guard for actions with 1 parameter
155// ----------------------------------------------------------------------------
156
17e22c50 157template <class F, class P1>
c66cca2a
VZ
158class wxScopeGuardImpl1 : public wxScopeGuardImplBase
159{
160public:
161 static wxScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
162 {
163 return wxScopeGuardImpl1<F, P1>(fun, p1);
164 }
165
822fc19e 166 ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); }
c66cca2a
VZ
167
168 void Execute() { m_fun(m_p1); }
169
170protected:
171 wxScopeGuardImpl1(F fun, P1 p1) : m_fun(fun), m_p1(p1) { }
172
173 F m_fun;
174 const P1 m_p1;
175
176 wxScopeGuardImpl1& operator=(const wxScopeGuardImpl1&);
177};
178
17e22c50 179template <class F, class P1>
c66cca2a
VZ
180inline wxScopeGuardImpl1<F, P1> wxMakeGuard(F fun, P1 p1)
181{
182 return wxScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
183}
184
185// ----------------------------------------------------------------------------
186// wxScopeGuardImpl2: scope guard for actions with 2 parameters
187// ----------------------------------------------------------------------------
188
17e22c50 189template <class F, class P1, class P2>
c66cca2a
VZ
190class wxScopeGuardImpl2 : public wxScopeGuardImplBase
191{
192public:
193 static wxScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
194 {
195 return wxScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
196 }
197
822fc19e 198 ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
199
200 void Execute() { m_fun(m_p1, m_p2); }
201
202protected:
203 wxScopeGuardImpl2(F fun, P1 p1, P2 p2) : m_fun(fun), m_p1(p1), m_p2(p2) { }
204
205 F m_fun;
206 const P1 m_p1;
207 const P2 m_p2;
208
209 wxScopeGuardImpl2& operator=(const wxScopeGuardImpl2&);
210};
211
17e22c50 212template <class F, class P1, class P2>
c66cca2a
VZ
213inline wxScopeGuardImpl2<F, P1, P2> wxMakeGuard(F fun, P1 p1, P2 p2)
214{
215 return wxScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
216}
217
bcffb4d1
VZ
218// ----------------------------------------------------------------------------
219// wxScopeGuardImpl3: scope guard for actions with 3 parameters
220// ----------------------------------------------------------------------------
221
222template <class F, class P1, class P2, class P3>
223class wxScopeGuardImpl3 : public wxScopeGuardImplBase
224{
225public:
226 static wxScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
227 {
228 return wxScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3);
229 }
230
231 ~wxScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
232
233 void Execute() { m_fun(m_p1, m_p2, m_p3); }
234
235protected:
236 wxScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3)
237 : m_fun(fun), m_p1(p1), m_p2(p2), m_p3(p3) { }
238
239 F m_fun;
240 const P1 m_p1;
241 const P2 m_p2;
242 const P3 m_p3;
243
244 wxScopeGuardImpl3& operator=(const wxScopeGuardImpl3&);
245};
246
247template <class F, class P1, class P2, class P3>
248inline wxScopeGuardImpl3<F, P1, P2, P3> wxMakeGuard(F fun, P1 p1, P2 p2, P3 p3)
249{
250 return wxScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3);
251}
252
c66cca2a
VZ
253// ============================================================================
254// wxScopeGuards for object methods
255// ============================================================================
256
257// ----------------------------------------------------------------------------
258// wxObjScopeGuardImpl0
259// ----------------------------------------------------------------------------
260
17e22c50 261template <class Obj, class MemFun>
c66cca2a
VZ
262class wxObjScopeGuardImpl0 : public wxScopeGuardImplBase
263{
264public:
265 static wxObjScopeGuardImpl0<Obj, MemFun>
266 MakeObjGuard(Obj& obj, MemFun memFun)
267 {
268 return wxObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
269 }
270
822fc19e 271 ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
272
273 void Execute() { (m_obj.*m_memfun)(); }
274
275protected:
276 wxObjScopeGuardImpl0(Obj& obj, MemFun memFun)
277 : m_obj(obj), m_memfun(memFun) { }
278
279 Obj& m_obj;
280 MemFun m_memfun;
281};
282
17e22c50 283template <class Obj, class MemFun>
c66cca2a
VZ
284inline wxObjScopeGuardImpl0<Obj, MemFun> wxMakeObjGuard(Obj& obj, MemFun memFun)
285{
286 return wxObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun);
287}
288
17e22c50 289template <class Obj, class MemFun, class P1>
c66cca2a
VZ
290class wxObjScopeGuardImpl1 : public wxScopeGuardImplBase
291{
292public:
293 static wxObjScopeGuardImpl1<Obj, MemFun, P1>
294 MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
295 {
296 return wxObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
297 }
298
822fc19e 299 ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
300
301 void Execute() { (m_obj.*m_memfun)(m_p1); }
302
303protected:
d775fa82 304 wxObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1)
c66cca2a
VZ
305 : m_obj(obj), m_memfun(memFun), m_p1(p1) { }
306
307 Obj& m_obj;
308 MemFun m_memfun;
309 const P1 m_p1;
310};
311
17e22c50 312template <class Obj, class MemFun, class P1>
c66cca2a
VZ
313inline wxObjScopeGuardImpl1<Obj, MemFun, P1>
314wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
315{
316 return wxObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1);
317}
318
17e22c50 319template <class Obj, class MemFun, class P1, class P2>
c66cca2a
VZ
320class wxObjScopeGuardImpl2 : public wxScopeGuardImplBase
321{
322public:
323 static wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
324 MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
325 {
326 return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
327 }
328
822fc19e 329 ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); }
c66cca2a
VZ
330
331 void Execute() { (m_obj.*m_memfun)(m_p1, m_p2); }
332
333protected:
d775fa82 334 wxObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2)
c66cca2a
VZ
335 : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2) { }
336
337 Obj& m_obj;
338 MemFun m_memfun;
339 const P1 m_p1;
340 const P2 m_p2;
341};
342
17e22c50 343template <class Obj, class MemFun, class P1, class P2>
c66cca2a
VZ
344inline wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>
345wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
346{
347 return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>::
348 MakeObjGuard(obj, memFun, p1, p2);
349}
350
bcffb4d1
VZ
351template <class Obj, class MemFun, class P1, class P2, class P3>
352class wxObjScopeGuardImpl3 : public wxScopeGuardImplBase
353{
354public:
355 static wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>
356 MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3)
357 {
22c5cac6 358 return wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>(obj, memFun, p1, p2, p3);
bcffb4d1
VZ
359 }
360
361 ~wxObjScopeGuardImpl3() { wxPrivateOnScopeExit(*this); }
362
363 void Execute() { (m_obj.*m_memfun)(m_p1, m_p2, m_p3); }
364
365protected:
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) { }
368
369 Obj& m_obj;
370 MemFun m_memfun;
371 const P1 m_p1;
372 const P2 m_p2;
373 const P3 m_p3;
374};
375
376template <class Obj, class MemFun, class P1, class P2, class P3>
377inline wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>
378wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3)
379{
380 return wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>::
381 MakeObjGuard(obj, memFun, p1, p2, p3);
382}
383
d2a48d5c
VZ
384// ----------------------------------------------------------------------------
385// wxVariableSetter: use the same technique as for wxScopeGuard to allow
386// setting a variable to some value on block exit
387// ----------------------------------------------------------------------------
388
d2a48d5c
VZ
389namespace wxPrivate
390{
391
392// empty class just to be able to define a reference to it
aef081f4 393class VariableSetterBase : public wxScopeGuardImplBase { };
d2a48d5c
VZ
394
395typedef const VariableSetterBase& VariableSetter;
396
397template <typename T, typename U>
398class VariableSetterImpl : public VariableSetterBase
399{
400public:
e196e523 401 VariableSetterImpl(T& var, U value)
d2a48d5c
VZ
402 : m_var(var),
403 m_value(value)
404 {
405 }
406
aef081f4
VS
407 ~VariableSetterImpl() { wxPrivateOnScopeExit(*this); }
408
409 void Execute() { m_var = m_value; }
d2a48d5c
VZ
410
411private:
412 T& m_var;
e196e523 413 const U m_value;
d2a48d5c
VZ
414
415 // suppress the warning about assignment operator not being generated
416 VariableSetterImpl<T, U>& operator=(const VariableSetterImpl<T, U>&);
417};
418
419template <typename T>
420class VariableNullerImpl : public VariableSetterBase
421{
422public:
423 VariableNullerImpl(T& var)
424 : m_var(var)
425 {
426 }
427
aef081f4
VS
428 ~VariableNullerImpl() { wxPrivateOnScopeExit(*this); }
429
430 void Execute() { m_var = NULL; }
d2a48d5c
VZ
431
432private:
433 T& m_var;
434
435 VariableNullerImpl<T>& operator=(const VariableNullerImpl<T>&);
436};
437
438} // namespace wxPrivate
439
440template <typename T, typename U>
441inline
e196e523 442wxPrivate::VariableSetterImpl<T, U> wxMakeVarSetter(T& var, U value)
d2a48d5c
VZ
443{
444 return wxPrivate::VariableSetterImpl<T, U>(var, value);
445}
446
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
450template <typename T>
451inline
452wxPrivate::VariableNullerImpl<T> wxMakeVarNuller(T& var)
453{
454 return wxPrivate::VariableNullerImpl<T>(var);
455}
456
c66cca2a 457// ============================================================================
d2a48d5c 458// macros for declaring unnamed scoped guards (which can't be dismissed)
c66cca2a
VZ
459// ============================================================================
460
c66cca2a
VZ
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
d2a48d5c
VZ
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)).
426a63a3 468
d2a48d5c 469#define wxGuardName wxMAKE_UNIQUE_NAME(wxScopeGuard)
426a63a3
VZ
470
471#define wxON_BLOCK_EXIT0_IMPL(n, f) \
472 wxScopeGuard n = wxMakeGuard(f); \
822fc19e 473 wxPrivateUse(n)
5800b5be 474#define wxON_BLOCK_EXIT0(f) \
426a63a3 475 wxON_BLOCK_EXIT0_IMPL(wxGuardName, f)
c66cca2a 476
426a63a3
VZ
477#define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \
478 wxScopeGuard n = wxMakeObjGuard(o, m); \
822fc19e 479 wxPrivateUse(n)
5800b5be 480#define wxON_BLOCK_EXIT_OBJ0(o, m) \
2bb3c0c0 481 wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m)
c66cca2a 482
51c679d5
VZ
483#define wxON_BLOCK_EXIT_THIS0(m) \
484 wxON_BLOCK_EXIT_OBJ0(*this, m)
485
486
426a63a3
VZ
487#define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \
488 wxScopeGuard n = wxMakeGuard(f, p1); \
822fc19e 489 wxPrivateUse(n)
5800b5be 490#define wxON_BLOCK_EXIT1(f, p1) \
426a63a3 491 wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1)
c66cca2a 492
426a63a3
VZ
493#define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \
494 wxScopeGuard n = wxMakeObjGuard(o, m, p1); \
822fc19e 495 wxPrivateUse(n)
5800b5be 496#define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \
2bb3c0c0 497 wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1)
c66cca2a 498
51c679d5
VZ
499#define wxON_BLOCK_EXIT_THIS1(m, p1) \
500 wxON_BLOCK_EXIT_OBJ1(*this, m, p1)
501
502
426a63a3
VZ
503#define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \
504 wxScopeGuard n = wxMakeGuard(f, p1, p2); \
822fc19e 505 wxPrivateUse(n)
5800b5be 506#define wxON_BLOCK_EXIT2(f, p1, p2) \
426a63a3 507 wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2)
c66cca2a 508
426a63a3
VZ
509#define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \
510 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \
822fc19e 511 wxPrivateUse(n)
5800b5be 512#define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \
2bb3c0c0 513 wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2)
c66cca2a 514
51c679d5
VZ
515#define wxON_BLOCK_EXIT_THIS2(m, p1, p2) \
516 wxON_BLOCK_EXIT_OBJ2(*this, m, p1, p2)
517
d2a48d5c 518
bcffb4d1
VZ
519#define wxON_BLOCK_EXIT3_IMPL(n, f, p1, p2, p3) \
520 wxScopeGuard n = wxMakeGuard(f, p1, p2, p3); \
521 wxPrivateUse(n)
522#define wxON_BLOCK_EXIT3(f, p1, p2, p3) \
523 wxON_BLOCK_EXIT3_IMPL(wxGuardName, f, p1, p2, p3)
524
525#define wxON_BLOCK_EXIT_OBJ3_IMPL(n, o, m, p1, p2, p3) \
526 wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2, p3); \
527 wxPrivateUse(n)
528#define wxON_BLOCK_EXIT_OBJ3(o, m, p1, p2, p3) \
529 wxON_BLOCK_EXIT_OBJ3_IMPL(wxGuardName, o, &m, p1, p2, p3)
530
531#define wxON_BLOCK_EXIT_THIS3(m, p1, p2, p3) \
532 wxON_BLOCK_EXIT_OBJ3(*this, m, p1, p2, p3)
533
534
d2a48d5c
VZ
535#define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter)
536
537#define wxON_BLOCK_EXIT_SET_IMPL(n, var, value) \
538 wxPrivate::VariableSetter n = wxMakeVarSetter(var, value); \
539 wxPrivateUse(n)
540
541#define wxON_BLOCK_EXIT_SET(var, value) \
542 wxON_BLOCK_EXIT_SET_IMPL(wxSetterName, var, value)
543
544#define wxON_BLOCK_EXIT_NULL_IMPL(n, var) \
545 wxPrivate::VariableSetter n = wxMakeVarNuller(var); \
546 wxPrivateUse(n)
547
548#define wxON_BLOCK_EXIT_NULL(ptr) \
549 wxON_BLOCK_EXIT_NULL_IMPL(wxSetterName, ptr)
550
c66cca2a 551#endif // _WX_SCOPEGUARD_H_