/src/wxwidgets/include/wx/scopeguard.h
Line | Count | Source |
1 | | /////////////////////////////////////////////////////////////////////////////// |
2 | | // Name: wx/scopeguard.h |
3 | | // Purpose: declares wxScopeGuard and related macros |
4 | | // Author: Vadim Zeitlin |
5 | | // Created: 03.07.2003 |
6 | | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
7 | | // Licence: wxWindows licence |
8 | | /////////////////////////////////////////////////////////////////////////////// |
9 | | |
10 | | /* |
11 | | Acknowledgements: this header is heavily based on (well, almost the exact |
12 | | copy of) ScopeGuard.h by Andrei Alexandrescu and Petru Marginean published |
13 | | in December 2000 issue of C/C++ Users Journal. |
14 | | http://www.cuj.com/documents/cujcexp1812alexandr/ |
15 | | */ |
16 | | |
17 | | #ifndef _WX_SCOPEGUARD_H_ |
18 | | #define _WX_SCOPEGUARD_H_ |
19 | | |
20 | | #include "wx/defs.h" |
21 | | |
22 | | #include "wx/except.h" |
23 | | |
24 | | // ---------------------------------------------------------------------------- |
25 | | // helpers |
26 | | // ---------------------------------------------------------------------------- |
27 | | |
28 | | namespace wxPrivate |
29 | | { |
30 | | // in the original implementation this was a member template function of |
31 | | // ScopeGuardImplBase but gcc 2.8 which is still used for OS/2 doesn't |
32 | | // support member templates and so we must make it global |
33 | | template <class ScopeGuardImpl> |
34 | | void OnScopeExit(ScopeGuardImpl& guard) |
35 | 0 | { |
36 | 0 | if ( !guard.WasDismissed() ) |
37 | 0 | { |
38 | | // we're called from ScopeGuardImpl dtor and so we must not throw |
39 | 0 | wxTRY |
40 | 0 | { |
41 | 0 | guard.Execute(); |
42 | 0 | } |
43 | 0 | wxCATCH_ALL(;) // do nothing, just eat the exception |
44 | 0 | } |
45 | 0 | } Unexecuted instantiation: void wxPrivate::OnScopeExit<wxScopeGuardImpl1<char* (*)(char const*), char const*> >(wxScopeGuardImpl1<char* (*)(char const*), char const*>&) Unexecuted instantiation: void wxPrivate::OnScopeExit<wxPrivate::VariableSetterImpl<bool, bool> >(wxPrivate::VariableSetterImpl<bool, bool>&) Unexecuted instantiation: void wxPrivate::OnScopeExit<wxPrivate::VariableSetterImpl<int, int> >(wxPrivate::VariableSetterImpl<int, int>&) Unexecuted instantiation: void wxPrivate::OnScopeExit<wxPrivate::VariableSetterImpl<long, long> >(wxPrivate::VariableSetterImpl<long, long>&) Unexecuted instantiation: void wxPrivate::OnScopeExit<wxScopeGuardImpl0<void (*)()> >(wxScopeGuardImpl0<void (*)()>&) Unexecuted instantiation: void wxPrivate::OnScopeExit<wxObjScopeGuardImpl0<wxEventLoopManual, void (wxEventLoopBase::*)()> >(wxObjScopeGuardImpl0<wxEventLoopManual, void (wxEventLoopBase::*)()>&) Unexecuted instantiation: void wxPrivate::OnScopeExit<wxObjScopeGuardImpl0<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const> >(wxObjScopeGuardImpl0<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const>&) Unexecuted instantiation: void wxPrivate::OnScopeExit<wxObjScopeGuardImpl0<wxWrapperInputStream, void (wxWrapperInputStream::*)() const> >(wxObjScopeGuardImpl0<wxWrapperInputStream, void (wxWrapperInputStream::*)() const>&) |
46 | | |
47 | | // just to avoid the warning about unused variables |
48 | | template <class T> |
49 | | void Use(const T& WXUNUSED(t)) |
50 | 0 | { |
51 | 0 | } Unexecuted instantiation: void wxPrivate::Use<wxScopeGuardImplBase>(wxScopeGuardImplBase const&) Unexecuted instantiation: void wxPrivate::Use<wxPrivate::VariableSetterBase>(wxPrivate::VariableSetterBase const&) |
52 | | } // namespace wxPrivate |
53 | | |
54 | 0 | #define wxPrivateOnScopeExit(n) wxPrivate::OnScopeExit(n) |
55 | 0 | #define wxPrivateUse(n) wxPrivate::Use(n) |
56 | | |
57 | | // ============================================================================ |
58 | | // wxScopeGuard for functions and functors |
59 | | // ============================================================================ |
60 | | |
61 | | // ---------------------------------------------------------------------------- |
62 | | // wxScopeGuardImplBase: used by wxScopeGuardImpl[0..N] below |
63 | | // ---------------------------------------------------------------------------- |
64 | | |
65 | | class wxScopeGuardImplBase |
66 | | { |
67 | | public: |
68 | 0 | wxScopeGuardImplBase() : m_wasDismissed(false) { } |
69 | | |
70 | | wxScopeGuardImplBase(const wxScopeGuardImplBase& other) |
71 | | : m_wasDismissed(other.m_wasDismissed) |
72 | 0 | { |
73 | 0 | other.Dismiss(); |
74 | 0 | } |
75 | | |
76 | 0 | void Dismiss() const { m_wasDismissed = true; } |
77 | | |
78 | | // for OnScopeExit() only (we can't make it friend, unfortunately)! |
79 | 0 | bool WasDismissed() const { return m_wasDismissed; } |
80 | | |
81 | | protected: |
82 | | ~wxScopeGuardImplBase() = default; |
83 | | |
84 | | // must be mutable for copy ctor to work |
85 | | mutable bool m_wasDismissed; |
86 | | |
87 | | private: |
88 | | wxScopeGuardImplBase& operator=(const wxScopeGuardImplBase&); |
89 | | }; |
90 | | |
91 | | // wxScopeGuard is just a reference, see the explanation in CUJ article |
92 | | typedef const wxScopeGuardImplBase& wxScopeGuard; |
93 | | |
94 | | // ---------------------------------------------------------------------------- |
95 | | // wxScopeGuardImpl0: scope guard for actions without parameters |
96 | | // ---------------------------------------------------------------------------- |
97 | | |
98 | | template <class F> |
99 | | class wxScopeGuardImpl0 : public wxScopeGuardImplBase |
100 | | { |
101 | | public: |
102 | | static wxScopeGuardImpl0<F> MakeGuard(F fun) |
103 | 0 | { |
104 | 0 | return wxScopeGuardImpl0<F>(fun); |
105 | 0 | } |
106 | | |
107 | 0 | ~wxScopeGuardImpl0() { wxPrivateOnScopeExit(*this); } |
108 | | |
109 | 0 | void Execute() { m_fun(); } |
110 | | |
111 | | protected: |
112 | 0 | wxScopeGuardImpl0(F fun) : m_fun(fun) { } |
113 | | |
114 | | F m_fun; |
115 | | |
116 | | wxScopeGuardImpl0& operator=(const wxScopeGuardImpl0&); |
117 | | }; |
118 | | |
119 | | template <class F> |
120 | | inline wxScopeGuardImpl0<F> wxMakeGuard(F fun) |
121 | 0 | { |
122 | 0 | return wxScopeGuardImpl0<F>::MakeGuard(fun); |
123 | 0 | } |
124 | | |
125 | | // ---------------------------------------------------------------------------- |
126 | | // wxScopeGuardImpl1: scope guard for actions with 1 parameter |
127 | | // ---------------------------------------------------------------------------- |
128 | | |
129 | | template <class F, class P1> |
130 | | class wxScopeGuardImpl1 : public wxScopeGuardImplBase |
131 | | { |
132 | | public: |
133 | | static wxScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1) |
134 | 0 | { |
135 | 0 | return wxScopeGuardImpl1<F, P1>(fun, p1); |
136 | 0 | } |
137 | | |
138 | 0 | ~wxScopeGuardImpl1() { wxPrivateOnScopeExit(* this); } |
139 | | |
140 | 0 | void Execute() { m_fun(m_p1); } |
141 | | |
142 | | protected: |
143 | 0 | wxScopeGuardImpl1(F fun, P1 p1) : m_fun(fun), m_p1(p1) { } |
144 | | |
145 | | F m_fun; |
146 | | const P1 m_p1; |
147 | | |
148 | | wxScopeGuardImpl1& operator=(const wxScopeGuardImpl1&); |
149 | | }; |
150 | | |
151 | | template <class F, class P1> |
152 | | inline wxScopeGuardImpl1<F, P1> wxMakeGuard(F fun, P1 p1) |
153 | 0 | { |
154 | 0 | return wxScopeGuardImpl1<F, P1>::MakeGuard(fun, p1); |
155 | 0 | } |
156 | | |
157 | | // ---------------------------------------------------------------------------- |
158 | | // wxScopeGuardImpl2: scope guard for actions with 2 parameters |
159 | | // ---------------------------------------------------------------------------- |
160 | | |
161 | | template <class F, class P1, class P2> |
162 | | class wxScopeGuardImpl2 : public wxScopeGuardImplBase |
163 | | { |
164 | | public: |
165 | | static wxScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2) |
166 | | { |
167 | | return wxScopeGuardImpl2<F, P1, P2>(fun, p1, p2); |
168 | | } |
169 | | |
170 | | ~wxScopeGuardImpl2() { wxPrivateOnScopeExit(*this); } |
171 | | |
172 | | void Execute() { m_fun(m_p1, m_p2); } |
173 | | |
174 | | protected: |
175 | | wxScopeGuardImpl2(F fun, P1 p1, P2 p2) : m_fun(fun), m_p1(p1), m_p2(p2) { } |
176 | | |
177 | | F m_fun; |
178 | | const P1 m_p1; |
179 | | const P2 m_p2; |
180 | | |
181 | | wxScopeGuardImpl2& operator=(const wxScopeGuardImpl2&); |
182 | | }; |
183 | | |
184 | | template <class F, class P1, class P2> |
185 | | inline wxScopeGuardImpl2<F, P1, P2> wxMakeGuard(F fun, P1 p1, P2 p2) |
186 | | { |
187 | | return wxScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2); |
188 | | } |
189 | | |
190 | | // ---------------------------------------------------------------------------- |
191 | | // wxScopeGuardImpl3: scope guard for actions with 3 parameters |
192 | | // ---------------------------------------------------------------------------- |
193 | | |
194 | | template <class F, class P1, class P2, class P3> |
195 | | class wxScopeGuardImpl3 : public wxScopeGuardImplBase |
196 | | { |
197 | | public: |
198 | | static wxScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3) |
199 | | { |
200 | | return wxScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3); |
201 | | } |
202 | | |
203 | | ~wxScopeGuardImpl3() { wxPrivateOnScopeExit(*this); } |
204 | | |
205 | | void Execute() { m_fun(m_p1, m_p2, m_p3); } |
206 | | |
207 | | protected: |
208 | | wxScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3) |
209 | | : m_fun(fun), m_p1(p1), m_p2(p2), m_p3(p3) { } |
210 | | |
211 | | F m_fun; |
212 | | const P1 m_p1; |
213 | | const P2 m_p2; |
214 | | const P3 m_p3; |
215 | | |
216 | | wxScopeGuardImpl3& operator=(const wxScopeGuardImpl3&); |
217 | | }; |
218 | | |
219 | | template <class F, class P1, class P2, class P3> |
220 | | inline wxScopeGuardImpl3<F, P1, P2, P3> wxMakeGuard(F fun, P1 p1, P2 p2, P3 p3) |
221 | | { |
222 | | return wxScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3); |
223 | | } |
224 | | |
225 | | // ============================================================================ |
226 | | // wxScopeGuards for object methods |
227 | | // ============================================================================ |
228 | | |
229 | | // ---------------------------------------------------------------------------- |
230 | | // wxObjScopeGuardImpl0 |
231 | | // ---------------------------------------------------------------------------- |
232 | | |
233 | | template <class Obj, class MemFun> |
234 | | class wxObjScopeGuardImpl0 : public wxScopeGuardImplBase |
235 | | { |
236 | | public: |
237 | | static wxObjScopeGuardImpl0<Obj, MemFun> |
238 | | MakeObjGuard(Obj& obj, MemFun memFun) |
239 | 0 | { |
240 | 0 | return wxObjScopeGuardImpl0<Obj, MemFun>(obj, memFun); |
241 | 0 | } Unexecuted instantiation: wxObjScopeGuardImpl0<wxEventLoopManual, void (wxEventLoopBase::*)()>::MakeObjGuard(wxEventLoopManual&, void (wxEventLoopBase::*)()) Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const>::MakeObjGuard(wxWrapperInputStream const&, void (wxWrapperInputStream::*)() const) Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream, void (wxWrapperInputStream::*)() const>::MakeObjGuard(wxWrapperInputStream&, void (wxWrapperInputStream::*)() const) |
242 | | |
243 | 0 | ~wxObjScopeGuardImpl0() { wxPrivateOnScopeExit(*this); }Unexecuted instantiation: wxObjScopeGuardImpl0<wxEventLoopManual, void (wxEventLoopBase::*)()>::~wxObjScopeGuardImpl0() Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const>::~wxObjScopeGuardImpl0() Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream, void (wxWrapperInputStream::*)() const>::~wxObjScopeGuardImpl0() |
244 | | |
245 | 0 | void Execute() { (m_obj.*m_memfun)(); }Unexecuted instantiation: wxObjScopeGuardImpl0<wxEventLoopManual, void (wxEventLoopBase::*)()>::Execute() Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const>::Execute() Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream, void (wxWrapperInputStream::*)() const>::Execute() |
246 | | |
247 | | protected: |
248 | | wxObjScopeGuardImpl0(Obj& obj, MemFun memFun) |
249 | 0 | : m_obj(obj), m_memfun(memFun) { }Unexecuted instantiation: wxObjScopeGuardImpl0<wxEventLoopManual, void (wxEventLoopBase::*)()>::wxObjScopeGuardImpl0(wxEventLoopManual&, void (wxEventLoopBase::*)()) Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const>::wxObjScopeGuardImpl0(wxWrapperInputStream const&, void (wxWrapperInputStream::*)() const) Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream, void (wxWrapperInputStream::*)() const>::wxObjScopeGuardImpl0(wxWrapperInputStream&, void (wxWrapperInputStream::*)() const) |
250 | | |
251 | | Obj& m_obj; |
252 | | MemFun m_memfun; |
253 | | }; |
254 | | |
255 | | template <class Obj, class MemFun> |
256 | | inline wxObjScopeGuardImpl0<Obj, MemFun> wxMakeObjGuard(Obj& obj, MemFun memFun) |
257 | 0 | { |
258 | 0 | return wxObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun); |
259 | 0 | } Unexecuted instantiation: wxObjScopeGuardImpl0<wxEventLoopManual, void (wxEventLoopBase::*)()> wxMakeObjGuard<wxEventLoopManual, void (wxEventLoopBase::*)()>(wxEventLoopManual&, void (wxEventLoopBase::*)()) Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const> wxMakeObjGuard<wxWrapperInputStream const, void (wxWrapperInputStream::*)() const>(wxWrapperInputStream const&, void (wxWrapperInputStream::*)() const) Unexecuted instantiation: wxObjScopeGuardImpl0<wxWrapperInputStream, void (wxWrapperInputStream::*)() const> wxMakeObjGuard<wxWrapperInputStream, void (wxWrapperInputStream::*)() const>(wxWrapperInputStream&, void (wxWrapperInputStream::*)() const) |
260 | | |
261 | | template <class Obj, class MemFun, class P1> |
262 | | class wxObjScopeGuardImpl1 : public wxScopeGuardImplBase |
263 | | { |
264 | | public: |
265 | | static wxObjScopeGuardImpl1<Obj, MemFun, P1> |
266 | | MakeObjGuard(Obj& obj, MemFun memFun, P1 p1) |
267 | | { |
268 | | return wxObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1); |
269 | | } |
270 | | |
271 | | ~wxObjScopeGuardImpl1() { wxPrivateOnScopeExit(*this); } |
272 | | |
273 | | void Execute() { (m_obj.*m_memfun)(m_p1); } |
274 | | |
275 | | protected: |
276 | | wxObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1) |
277 | | : m_obj(obj), m_memfun(memFun), m_p1(p1) { } |
278 | | |
279 | | Obj& m_obj; |
280 | | MemFun m_memfun; |
281 | | const P1 m_p1; |
282 | | }; |
283 | | |
284 | | template <class Obj, class MemFun, class P1> |
285 | | inline wxObjScopeGuardImpl1<Obj, MemFun, P1> |
286 | | wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1) |
287 | | { |
288 | | return wxObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1); |
289 | | } |
290 | | |
291 | | template <class Obj, class MemFun, class P1, class P2> |
292 | | class wxObjScopeGuardImpl2 : public wxScopeGuardImplBase |
293 | | { |
294 | | public: |
295 | | static wxObjScopeGuardImpl2<Obj, MemFun, P1, P2> |
296 | | MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2) |
297 | | { |
298 | | return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2); |
299 | | } |
300 | | |
301 | | ~wxObjScopeGuardImpl2() { wxPrivateOnScopeExit(*this); } |
302 | | |
303 | | void Execute() { (m_obj.*m_memfun)(m_p1, m_p2); } |
304 | | |
305 | | protected: |
306 | | wxObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2) |
307 | | : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2) { } |
308 | | |
309 | | Obj& m_obj; |
310 | | MemFun m_memfun; |
311 | | const P1 m_p1; |
312 | | const P2 m_p2; |
313 | | }; |
314 | | |
315 | | template <class Obj, class MemFun, class P1, class P2> |
316 | | inline wxObjScopeGuardImpl2<Obj, MemFun, P1, P2> |
317 | | wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2) |
318 | | { |
319 | | return wxObjScopeGuardImpl2<Obj, MemFun, P1, P2>:: |
320 | | MakeObjGuard(obj, memFun, p1, p2); |
321 | | } |
322 | | |
323 | | template <class Obj, class MemFun, class P1, class P2, class P3> |
324 | | class wxObjScopeGuardImpl3 : public wxScopeGuardImplBase |
325 | | { |
326 | | public: |
327 | | static wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3> |
328 | | MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3) |
329 | | { |
330 | | return wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>(obj, memFun, p1, p2, p3); |
331 | | } |
332 | | |
333 | | ~wxObjScopeGuardImpl3() { wxPrivateOnScopeExit(*this); } |
334 | | |
335 | | void Execute() { (m_obj.*m_memfun)(m_p1, m_p2, m_p3); } |
336 | | |
337 | | protected: |
338 | | wxObjScopeGuardImpl3(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3) |
339 | | : m_obj(obj), m_memfun(memFun), m_p1(p1), m_p2(p2), m_p3(p3) { } |
340 | | |
341 | | Obj& m_obj; |
342 | | MemFun m_memfun; |
343 | | const P1 m_p1; |
344 | | const P2 m_p2; |
345 | | const P3 m_p3; |
346 | | }; |
347 | | |
348 | | template <class Obj, class MemFun, class P1, class P2, class P3> |
349 | | inline wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3> |
350 | | wxMakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2, P3 p3) |
351 | | { |
352 | | return wxObjScopeGuardImpl3<Obj, MemFun, P1, P2, P3>:: |
353 | | MakeObjGuard(obj, memFun, p1, p2, p3); |
354 | | } |
355 | | |
356 | | // ---------------------------------------------------------------------------- |
357 | | // wxVariableSetter: use the same technique as for wxScopeGuard to allow |
358 | | // setting a variable to some value on block exit |
359 | | // ---------------------------------------------------------------------------- |
360 | | |
361 | | namespace wxPrivate |
362 | | { |
363 | | |
364 | | // empty class just to be able to define a reference to it |
365 | | class VariableSetterBase : public wxScopeGuardImplBase { }; |
366 | | |
367 | | typedef const VariableSetterBase& VariableSetter; |
368 | | |
369 | | template <typename T, typename U> |
370 | | class VariableSetterImpl : public VariableSetterBase |
371 | | { |
372 | | public: |
373 | | VariableSetterImpl(T& var, U value) |
374 | 0 | : m_var(var), |
375 | 0 | m_value(value) |
376 | 0 | { |
377 | 0 | } Unexecuted instantiation: wxPrivate::VariableSetterImpl<bool, bool>::VariableSetterImpl(bool&, bool) Unexecuted instantiation: wxPrivate::VariableSetterImpl<int, int>::VariableSetterImpl(int&, int) Unexecuted instantiation: wxPrivate::VariableSetterImpl<long, long>::VariableSetterImpl(long&, long) |
378 | | |
379 | 0 | ~VariableSetterImpl() { wxPrivateOnScopeExit(*this); }Unexecuted instantiation: wxPrivate::VariableSetterImpl<bool, bool>::~VariableSetterImpl() Unexecuted instantiation: wxPrivate::VariableSetterImpl<int, int>::~VariableSetterImpl() Unexecuted instantiation: wxPrivate::VariableSetterImpl<long, long>::~VariableSetterImpl() |
380 | | |
381 | 0 | void Execute() { m_var = m_value; }Unexecuted instantiation: wxPrivate::VariableSetterImpl<bool, bool>::Execute() Unexecuted instantiation: wxPrivate::VariableSetterImpl<int, int>::Execute() Unexecuted instantiation: wxPrivate::VariableSetterImpl<long, long>::Execute() |
382 | | |
383 | | private: |
384 | | T& m_var; |
385 | | const U m_value; |
386 | | |
387 | | // suppress the warning about assignment operator not being generated |
388 | | VariableSetterImpl<T, U>& operator=(const VariableSetterImpl<T, U>&); |
389 | | }; |
390 | | |
391 | | template <typename T> |
392 | | class VariableNullerImpl : public VariableSetterBase |
393 | | { |
394 | | public: |
395 | | VariableNullerImpl(T& var) |
396 | | : m_var(var) |
397 | | { |
398 | | } |
399 | | |
400 | | ~VariableNullerImpl() { wxPrivateOnScopeExit(*this); } |
401 | | |
402 | | void Execute() { m_var = nullptr; } |
403 | | |
404 | | private: |
405 | | T& m_var; |
406 | | |
407 | | VariableNullerImpl<T>& operator=(const VariableNullerImpl<T>&); |
408 | | }; |
409 | | |
410 | | } // namespace wxPrivate |
411 | | |
412 | | template <typename T, typename U> |
413 | | inline |
414 | | wxPrivate::VariableSetterImpl<T, U> wxMakeVarSetter(T& var, U value) |
415 | 0 | { |
416 | 0 | return wxPrivate::VariableSetterImpl<T, U>(var, value); |
417 | 0 | } Unexecuted instantiation: wxPrivate::VariableSetterImpl<bool, bool> wxMakeVarSetter<bool, bool>(bool&, bool) Unexecuted instantiation: wxPrivate::VariableSetterImpl<int, int> wxMakeVarSetter<int, int>(int&, int) Unexecuted instantiation: wxPrivate::VariableSetterImpl<long, long> wxMakeVarSetter<long, long>(long&, long) |
418 | | |
419 | | // calling wxMakeVarSetter(ptr, NULL) doesn't work because U is deduced to be |
420 | | // "int" and subsequent assignment of "U" to "T *" fails, so provide a special |
421 | | // function for this special case |
422 | | template <typename T> |
423 | | inline |
424 | | wxPrivate::VariableNullerImpl<T> wxMakeVarNuller(T& var) |
425 | | { |
426 | | return wxPrivate::VariableNullerImpl<T>(var); |
427 | | } |
428 | | |
429 | | // ============================================================================ |
430 | | // macros for declaring unnamed scoped guards (which can't be dismissed) |
431 | | // ============================================================================ |
432 | | |
433 | | // NB: the original code has a single (and much nicer) ON_BLOCK_EXIT macro |
434 | | // but this results in compiler warnings about unused variables and I |
435 | | // didn't find a way to work around this other than by having different |
436 | | // macros with different names or using a less natural syntax for passing |
437 | | // the arguments (e.g. as Boost preprocessor sequences, which would mean |
438 | | // having to write wxON_BLOCK_EXIT(fwrite, (buf)(size)(n)(fp)) instead of |
439 | | // wxON_BLOCK_EXIT4(fwrite, buf, size, n, fp)). |
440 | | |
441 | | #define wxGuardName wxMAKE_UNIQUE_NAME(wxScopeGuard) |
442 | | |
443 | | #define wxON_BLOCK_EXIT0_IMPL(n, f) \ |
444 | 0 | wxScopeGuard n = wxMakeGuard(f); \ |
445 | 0 | wxPrivateUse(n) |
446 | | #define wxON_BLOCK_EXIT0(f) \ |
447 | 0 | wxON_BLOCK_EXIT0_IMPL(wxGuardName, f) |
448 | | |
449 | | #define wxON_BLOCK_EXIT_OBJ0_IMPL(n, o, m) \ |
450 | 0 | wxScopeGuard n = wxMakeObjGuard(o, m); \ |
451 | 0 | wxPrivateUse(n) |
452 | | #define wxON_BLOCK_EXIT_OBJ0(o, m) \ |
453 | 0 | wxON_BLOCK_EXIT_OBJ0_IMPL(wxGuardName, o, &m) |
454 | | |
455 | | #define wxON_BLOCK_EXIT_THIS0(m) \ |
456 | 0 | wxON_BLOCK_EXIT_OBJ0(*this, m) |
457 | | |
458 | | |
459 | | #define wxON_BLOCK_EXIT1_IMPL(n, f, p1) \ |
460 | 0 | wxScopeGuard n = wxMakeGuard(f, p1); \ |
461 | 0 | wxPrivateUse(n) |
462 | | #define wxON_BLOCK_EXIT1(f, p1) \ |
463 | 0 | wxON_BLOCK_EXIT1_IMPL(wxGuardName, f, p1) |
464 | | |
465 | | #define wxON_BLOCK_EXIT_OBJ1_IMPL(n, o, m, p1) \ |
466 | | wxScopeGuard n = wxMakeObjGuard(o, m, p1); \ |
467 | | wxPrivateUse(n) |
468 | | #define wxON_BLOCK_EXIT_OBJ1(o, m, p1) \ |
469 | | wxON_BLOCK_EXIT_OBJ1_IMPL(wxGuardName, o, &m, p1) |
470 | | |
471 | | #define wxON_BLOCK_EXIT_THIS1(m, p1) \ |
472 | | wxON_BLOCK_EXIT_OBJ1(*this, m, p1) |
473 | | |
474 | | |
475 | | #define wxON_BLOCK_EXIT2_IMPL(n, f, p1, p2) \ |
476 | | wxScopeGuard n = wxMakeGuard(f, p1, p2); \ |
477 | | wxPrivateUse(n) |
478 | | #define wxON_BLOCK_EXIT2(f, p1, p2) \ |
479 | | wxON_BLOCK_EXIT2_IMPL(wxGuardName, f, p1, p2) |
480 | | |
481 | | #define wxON_BLOCK_EXIT_OBJ2_IMPL(n, o, m, p1, p2) \ |
482 | | wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2); \ |
483 | | wxPrivateUse(n) |
484 | | #define wxON_BLOCK_EXIT_OBJ2(o, m, p1, p2) \ |
485 | | wxON_BLOCK_EXIT_OBJ2_IMPL(wxGuardName, o, &m, p1, p2) |
486 | | |
487 | | #define wxON_BLOCK_EXIT_THIS2(m, p1, p2) \ |
488 | | wxON_BLOCK_EXIT_OBJ2(*this, m, p1, p2) |
489 | | |
490 | | |
491 | | #define wxON_BLOCK_EXIT3_IMPL(n, f, p1, p2, p3) \ |
492 | | wxScopeGuard n = wxMakeGuard(f, p1, p2, p3); \ |
493 | | wxPrivateUse(n) |
494 | | #define wxON_BLOCK_EXIT3(f, p1, p2, p3) \ |
495 | | wxON_BLOCK_EXIT3_IMPL(wxGuardName, f, p1, p2, p3) |
496 | | |
497 | | #define wxON_BLOCK_EXIT_OBJ3_IMPL(n, o, m, p1, p2, p3) \ |
498 | | wxScopeGuard n = wxMakeObjGuard(o, m, p1, p2, p3); \ |
499 | | wxPrivateUse(n) |
500 | | #define wxON_BLOCK_EXIT_OBJ3(o, m, p1, p2, p3) \ |
501 | | wxON_BLOCK_EXIT_OBJ3_IMPL(wxGuardName, o, &m, p1, p2, p3) |
502 | | |
503 | | #define wxON_BLOCK_EXIT_THIS3(m, p1, p2, p3) \ |
504 | | wxON_BLOCK_EXIT_OBJ3(*this, m, p1, p2, p3) |
505 | | |
506 | | |
507 | | #define wxSetterName wxMAKE_UNIQUE_NAME(wxVarSetter) |
508 | | |
509 | | #define wxON_BLOCK_EXIT_SET_IMPL(n, var, value) \ |
510 | 0 | wxPrivate::VariableSetter n = wxMakeVarSetter(var, value); \ |
511 | 0 | wxPrivateUse(n) |
512 | | |
513 | | #define wxON_BLOCK_EXIT_SET(var, value) \ |
514 | 0 | wxON_BLOCK_EXIT_SET_IMPL(wxSetterName, var, value) |
515 | | |
516 | | #define wxON_BLOCK_EXIT_NULL_IMPL(n, var) \ |
517 | | wxPrivate::VariableSetter n = wxMakeVarNuller(var); \ |
518 | | wxPrivateUse(n) |
519 | | |
520 | | #define wxON_BLOCK_EXIT_NULL(ptr) \ |
521 | | wxON_BLOCK_EXIT_NULL_IMPL(wxSetterName, ptr) |
522 | | |
523 | | #endif // _WX_SCOPEGUARD_H_ |