1 | //////////////////////////////////////////////////////////////////////////////// |
---|
2 | // The Loki Library |
---|
3 | // Copyright (c) 2000 Andrei Alexandrescu |
---|
4 | // Copyright (c) 2000 Petru Marginean |
---|
5 | // Copyright (c) 2005 Joshua Lehrer |
---|
6 | // |
---|
7 | // Permission to use, copy, modify, distribute and sell this software for any |
---|
8 | // purpose is hereby granted without fee, provided that the above copyright |
---|
9 | // notice appear in all copies and that both that copyright notice and this |
---|
10 | // permission notice appear in supporting documentation. |
---|
11 | // The author makes no representations about the |
---|
12 | // suitability of this software for any purpose. It is provided "as is" |
---|
13 | // without express or implied warranty. |
---|
14 | // |
---|
15 | // Changes by Orxonox (Reto Grieder) |
---|
16 | // Moved one protected variable to the public section to allow the use of |
---|
17 | // the ScopeGuard in a class definition. |
---|
18 | //////////////////////////////////////////////////////////////////////////////// |
---|
19 | #ifndef LOKI_SCOPEGUARD_INC_ |
---|
20 | #define LOKI_SCOPEGUARD_INC_ |
---|
21 | |
---|
22 | // $Id: ScopeGuard.h 799 2006-12-20 00:37:13Z rich_sposato $ |
---|
23 | |
---|
24 | |
---|
25 | #include <loki/RefToValue.h> |
---|
26 | |
---|
27 | /// \defgroup ExceptionGroup Exception-safe code |
---|
28 | |
---|
29 | namespace Loki |
---|
30 | { |
---|
31 | |
---|
32 | //////////////////////////////////////////////////////////////// |
---|
33 | /// |
---|
34 | /// \class ScopeGuardImplBase |
---|
35 | /// \ingroup ExceptionGroup |
---|
36 | /// |
---|
37 | /// Base class used by all ScopeGuard implementations. All commonly used |
---|
38 | /// functions are in this class (e.g. - Dismiss and SafeExecute). |
---|
39 | /// |
---|
40 | /// See Andrei's and Petru Marginean's CUJ article |
---|
41 | /// http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm |
---|
42 | /// |
---|
43 | /// Changes to the original code by Joshua Lehrer: |
---|
44 | /// http://www.lehrerfamily.com/scopeguard.html |
---|
45 | //////////////////////////////////////////////////////////////// |
---|
46 | |
---|
47 | class ScopeGuardImplBase |
---|
48 | { |
---|
49 | /// Copy-assignment operator is not implemented and private. |
---|
50 | ScopeGuardImplBase& operator =(const ScopeGuardImplBase&); |
---|
51 | |
---|
52 | protected: |
---|
53 | |
---|
54 | ~ScopeGuardImplBase() |
---|
55 | {} |
---|
56 | |
---|
57 | /// Copy-constructor takes over responsibility from other ScopeGuard. |
---|
58 | ScopeGuardImplBase(const ScopeGuardImplBase& other) throw() |
---|
59 | : dismissed_(other.dismissed_) |
---|
60 | { |
---|
61 | other.Dismiss(); |
---|
62 | } |
---|
63 | |
---|
64 | template <typename J> |
---|
65 | static void SafeExecute(J& j) throw() |
---|
66 | { |
---|
67 | if (!j.dismissed_) |
---|
68 | try |
---|
69 | { |
---|
70 | j.Execute(); |
---|
71 | } |
---|
72 | catch(...) |
---|
73 | {} |
---|
74 | } |
---|
75 | |
---|
76 | mutable bool dismissed_; |
---|
77 | |
---|
78 | public: |
---|
79 | ScopeGuardImplBase() throw() : dismissed_(false) |
---|
80 | {} |
---|
81 | |
---|
82 | void Dismiss() const throw() |
---|
83 | { |
---|
84 | dismissed_ = true; |
---|
85 | } |
---|
86 | }; |
---|
87 | |
---|
88 | //////////////////////////////////////////////////////////////// |
---|
89 | /// |
---|
90 | /// \typedef typedef const ScopeGuardImplBase& ScopeGuard |
---|
91 | /// \ingroup ExceptionGroup |
---|
92 | /// |
---|
93 | //////////////////////////////////////////////////////////////// |
---|
94 | |
---|
95 | typedef const ScopeGuardImplBase& ScopeGuard; |
---|
96 | |
---|
97 | //////////////////////////////////////////////////////////////// |
---|
98 | /// |
---|
99 | /// \class ScopeGuardImpl0 |
---|
100 | /// \ingroup ExceptionGroup |
---|
101 | /// |
---|
102 | /// Implementation class for a standalone function or class static function |
---|
103 | /// with no parameters. ScopeGuard ignores any value returned from the |
---|
104 | /// call within the Execute function. |
---|
105 | /// |
---|
106 | /// This class has a single standalone helper function, MakeGuard which |
---|
107 | /// creates and returns a ScopeGuard. |
---|
108 | /// |
---|
109 | //////////////////////////////////////////////////////////////// |
---|
110 | |
---|
111 | template <typename F> |
---|
112 | class ScopeGuardImpl0 : public ScopeGuardImplBase |
---|
113 | { |
---|
114 | public: |
---|
115 | static ScopeGuardImpl0<F> MakeGuard(F fun) |
---|
116 | { |
---|
117 | return ScopeGuardImpl0<F>(fun); |
---|
118 | } |
---|
119 | |
---|
120 | ~ScopeGuardImpl0() throw() |
---|
121 | { |
---|
122 | SafeExecute(*this); |
---|
123 | } |
---|
124 | |
---|
125 | void Execute() |
---|
126 | { |
---|
127 | fun_(); |
---|
128 | } |
---|
129 | |
---|
130 | ScopeGuardImpl0(F fun) : fun_(fun) |
---|
131 | {} |
---|
132 | |
---|
133 | protected: |
---|
134 | F fun_; |
---|
135 | }; |
---|
136 | |
---|
137 | template <typename F> |
---|
138 | inline ScopeGuardImpl0<F> MakeGuard(F fun) |
---|
139 | { |
---|
140 | return ScopeGuardImpl0<F>::MakeGuard(fun); |
---|
141 | } |
---|
142 | |
---|
143 | //////////////////////////////////////////////////////////////// |
---|
144 | /// |
---|
145 | /// \class ScopeGuardImpl1 |
---|
146 | /// \ingroup ExceptionGroup |
---|
147 | /// |
---|
148 | /// Implementation class for a standalone function or class static function |
---|
149 | /// with one parameter. Each parameter is copied by value - use |
---|
150 | /// ::Loki::ByRef if you must use a reference instead. ScopeGuard ignores |
---|
151 | /// any value returned from the call within the Execute function. |
---|
152 | /// |
---|
153 | /// This class has a single standalone helper function, MakeGuard which |
---|
154 | /// creates and returns a ScopeGuard. |
---|
155 | /// |
---|
156 | //////////////////////////////////////////////////////////////// |
---|
157 | |
---|
158 | template <typename F, typename P1> |
---|
159 | class ScopeGuardImpl1 : public ScopeGuardImplBase |
---|
160 | { |
---|
161 | public: |
---|
162 | static ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1) |
---|
163 | { |
---|
164 | return ScopeGuardImpl1<F, P1>(fun, p1); |
---|
165 | } |
---|
166 | |
---|
167 | ~ScopeGuardImpl1() throw() |
---|
168 | { |
---|
169 | SafeExecute(*this); |
---|
170 | } |
---|
171 | |
---|
172 | void Execute() |
---|
173 | { |
---|
174 | fun_(p1_); |
---|
175 | } |
---|
176 | |
---|
177 | ScopeGuardImpl1(F fun, P1 p1) : fun_(fun), p1_(p1) |
---|
178 | {} |
---|
179 | |
---|
180 | protected: |
---|
181 | F fun_; |
---|
182 | const P1 p1_; |
---|
183 | }; |
---|
184 | |
---|
185 | template <typename F, typename P1> |
---|
186 | inline ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1) |
---|
187 | { |
---|
188 | return ScopeGuardImpl1<F, P1>::MakeGuard(fun, p1); |
---|
189 | } |
---|
190 | |
---|
191 | //////////////////////////////////////////////////////////////// |
---|
192 | /// |
---|
193 | /// \class ScopeGuardImpl2 |
---|
194 | /// \ingroup ExceptionGroup |
---|
195 | /// |
---|
196 | /// Implementation class for a standalone function or class static function |
---|
197 | /// with two parameters. Each parameter is copied by value - use |
---|
198 | /// ::Loki::ByRef if you must use a reference instead. ScopeGuard ignores |
---|
199 | /// any value returned from the call within the Execute function. |
---|
200 | /// |
---|
201 | /// This class has a single standalone helper function, MakeGuard which |
---|
202 | /// creates and returns a ScopeGuard. |
---|
203 | /// |
---|
204 | //////////////////////////////////////////////////////////////// |
---|
205 | |
---|
206 | template <typename F, typename P1, typename P2> |
---|
207 | class ScopeGuardImpl2: public ScopeGuardImplBase |
---|
208 | { |
---|
209 | public: |
---|
210 | static ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2) |
---|
211 | { |
---|
212 | return ScopeGuardImpl2<F, P1, P2>(fun, p1, p2); |
---|
213 | } |
---|
214 | |
---|
215 | ~ScopeGuardImpl2() throw() |
---|
216 | { |
---|
217 | SafeExecute(*this); |
---|
218 | } |
---|
219 | |
---|
220 | void Execute() |
---|
221 | { |
---|
222 | fun_(p1_, p2_); |
---|
223 | } |
---|
224 | |
---|
225 | ScopeGuardImpl2(F fun, P1 p1, P2 p2) : fun_(fun), p1_(p1), p2_(p2) |
---|
226 | {} |
---|
227 | |
---|
228 | protected: |
---|
229 | F fun_; |
---|
230 | const P1 p1_; |
---|
231 | const P2 p2_; |
---|
232 | }; |
---|
233 | |
---|
234 | template <typename F, typename P1, typename P2> |
---|
235 | inline ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2) |
---|
236 | { |
---|
237 | return ScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2); |
---|
238 | } |
---|
239 | |
---|
240 | //////////////////////////////////////////////////////////////// |
---|
241 | /// |
---|
242 | /// \class ScopeGuardImpl3 |
---|
243 | /// \ingroup ExceptionGroup |
---|
244 | /// |
---|
245 | /// Implementation class for a standalone function or class static function |
---|
246 | /// with three parameters. Each parameter is copied by value - use |
---|
247 | /// ::Loki::ByRef if you must use a reference instead. ScopeGuard ignores |
---|
248 | /// any value returned from the call within the Execute function. |
---|
249 | /// |
---|
250 | /// This class has a single standalone helper function, MakeGuard which |
---|
251 | /// creates and returns a ScopeGuard. |
---|
252 | /// |
---|
253 | //////////////////////////////////////////////////////////////// |
---|
254 | |
---|
255 | template <typename F, typename P1, typename P2, typename P3> |
---|
256 | class ScopeGuardImpl3 : public ScopeGuardImplBase |
---|
257 | { |
---|
258 | public: |
---|
259 | static ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3) |
---|
260 | { |
---|
261 | return ScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3); |
---|
262 | } |
---|
263 | |
---|
264 | ~ScopeGuardImpl3() throw() |
---|
265 | { |
---|
266 | SafeExecute(*this); |
---|
267 | } |
---|
268 | |
---|
269 | void Execute() |
---|
270 | { |
---|
271 | fun_(p1_, p2_, p3_); |
---|
272 | } |
---|
273 | |
---|
274 | ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3) : fun_(fun), p1_(p1), p2_(p2), p3_(p3) |
---|
275 | {} |
---|
276 | |
---|
277 | protected: |
---|
278 | F fun_; |
---|
279 | const P1 p1_; |
---|
280 | const P2 p2_; |
---|
281 | const P3 p3_; |
---|
282 | }; |
---|
283 | |
---|
284 | template <typename F, typename P1, typename P2, typename P3> |
---|
285 | inline ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3) |
---|
286 | { |
---|
287 | return ScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3); |
---|
288 | } |
---|
289 | |
---|
290 | //////////////////////////////////////////////////////////////// |
---|
291 | /// |
---|
292 | /// \class ScopeGuardImpl4 |
---|
293 | /// \ingroup ExceptionGroup |
---|
294 | /// |
---|
295 | /// Implementation class for a standalone function or class static function |
---|
296 | /// with four parameters. Each parameter is copied by value - use |
---|
297 | /// ::Loki::ByRef if you must use a reference instead. ScopeGuard ignores |
---|
298 | /// any value returned from the call within the Execute function. |
---|
299 | /// |
---|
300 | /// This class has a single standalone helper function, MakeGuard which |
---|
301 | /// creates and returns a ScopeGuard. |
---|
302 | /// |
---|
303 | //////////////////////////////////////////////////////////////// |
---|
304 | |
---|
305 | template < typename F, typename P1, typename P2, typename P3, typename P4 > |
---|
306 | class ScopeGuardImpl4 : public ScopeGuardImplBase |
---|
307 | { |
---|
308 | public: |
---|
309 | static ScopeGuardImpl4< F, P1, P2, P3, P4 > MakeGuard( |
---|
310 | F fun, P1 p1, P2 p2, P3 p3, P4 p4 ) |
---|
311 | { |
---|
312 | return ScopeGuardImpl4< F, P1, P2, P3, P4 >( fun, p1, p2, p3, p4 ); |
---|
313 | } |
---|
314 | |
---|
315 | ~ScopeGuardImpl4() throw() |
---|
316 | { |
---|
317 | SafeExecute( *this ); |
---|
318 | } |
---|
319 | |
---|
320 | void Execute() |
---|
321 | { |
---|
322 | fun_( p1_, p2_, p3_, p4_ ); |
---|
323 | } |
---|
324 | |
---|
325 | ScopeGuardImpl4( F fun, P1 p1, P2 p2, P3 p3, P4 p4 ) : |
---|
326 | fun_( fun ), p1_( p1 ), p2_( p2 ), p3_( p3 ), p4_( p4 ) |
---|
327 | {} |
---|
328 | |
---|
329 | protected: |
---|
330 | F fun_; |
---|
331 | const P1 p1_; |
---|
332 | const P2 p2_; |
---|
333 | const P3 p3_; |
---|
334 | const P4 p4_; |
---|
335 | }; |
---|
336 | |
---|
337 | template < typename F, typename P1, typename P2, typename P3, typename P4 > |
---|
338 | inline ScopeGuardImpl4< F, P1, P2, P3, P4 > MakeGuard( F fun, P1 p1, P2 p2, P3 p3, P4 p4 ) |
---|
339 | { |
---|
340 | return ScopeGuardImpl4< F, P1, P2, P3, P4 >::MakeGuard( fun, p1, p2, p3, p4 ); |
---|
341 | } |
---|
342 | |
---|
343 | //////////////////////////////////////////////////////////////// |
---|
344 | /// |
---|
345 | /// \class ScopeGuardImpl5 |
---|
346 | /// \ingroup ExceptionGroup |
---|
347 | /// |
---|
348 | /// Implementation class for a standalone function or class static function |
---|
349 | /// with five parameters. Each parameter is copied by value - use |
---|
350 | /// ::Loki::ByRef if you must use a reference instead. ScopeGuard ignores |
---|
351 | /// any value returned from the call within the Execute function. |
---|
352 | /// |
---|
353 | /// This class has a single standalone helper function, MakeGuard which |
---|
354 | /// creates and returns a ScopeGuard. |
---|
355 | /// |
---|
356 | //////////////////////////////////////////////////////////////// |
---|
357 | |
---|
358 | template < typename F, typename P1, typename P2, typename P3, typename P4, typename P5 > |
---|
359 | class ScopeGuardImpl5 : public ScopeGuardImplBase |
---|
360 | { |
---|
361 | public: |
---|
362 | static ScopeGuardImpl5< F, P1, P2, P3, P4, P5 > MakeGuard( |
---|
363 | F fun, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5 ) |
---|
364 | { |
---|
365 | return ScopeGuardImpl5< F, P1, P2, P3, P4, P5 >( fun, p1, p2, p3, p4, p5 ); |
---|
366 | } |
---|
367 | |
---|
368 | ~ScopeGuardImpl5() throw() |
---|
369 | { |
---|
370 | SafeExecute( *this ); |
---|
371 | } |
---|
372 | |
---|
373 | void Execute() |
---|
374 | { |
---|
375 | fun_( p1_, p2_, p3_, p4_, p5_ ); |
---|
376 | } |
---|
377 | |
---|
378 | ScopeGuardImpl5( F fun, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5 ) : |
---|
379 | fun_( fun ), p1_( p1 ), p2_( p2 ), p3_( p3 ), p4_( p4 ), p5_( p5 ) |
---|
380 | {} |
---|
381 | |
---|
382 | protected: |
---|
383 | F fun_; |
---|
384 | const P1 p1_; |
---|
385 | const P2 p2_; |
---|
386 | const P3 p3_; |
---|
387 | const P4 p4_; |
---|
388 | const P5 p5_; |
---|
389 | }; |
---|
390 | |
---|
391 | template < typename F, typename P1, typename P2, typename P3, typename P4, typename P5 > |
---|
392 | inline ScopeGuardImpl5< F, P1, P2, P3, P4, P5 > MakeGuard( F fun, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5 ) |
---|
393 | { |
---|
394 | return ScopeGuardImpl5< F, P1, P2, P3, P4, P5 >::MakeGuard( fun, p1, p2, p3, p4, p5 ); |
---|
395 | } |
---|
396 | |
---|
397 | //////////////////////////////////////////////////////////////// |
---|
398 | /// |
---|
399 | /// \class ObjScopeGuardImpl0 |
---|
400 | /// \ingroup ExceptionGroup |
---|
401 | /// |
---|
402 | /// Implementation class for a class per-instance member function with no |
---|
403 | /// parameters. ScopeGuard ignores any value returned from the call within |
---|
404 | /// the Execute function. |
---|
405 | /// |
---|
406 | /// This class has 3 standalone helper functions which create a ScopeGuard. |
---|
407 | /// One is MakeObjGuard, which is deprecated but provided for older code. |
---|
408 | /// The other two are MakeGuard overloads, one which takes a pointer to an |
---|
409 | /// object, and the other which takes a reference. |
---|
410 | /// |
---|
411 | //////////////////////////////////////////////////////////////// |
---|
412 | |
---|
413 | template <class Obj, typename MemFun> |
---|
414 | class ObjScopeGuardImpl0 : public ScopeGuardImplBase |
---|
415 | { |
---|
416 | public: |
---|
417 | static ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun) |
---|
418 | { |
---|
419 | return ObjScopeGuardImpl0<Obj, MemFun>(obj, memFun); |
---|
420 | } |
---|
421 | |
---|
422 | ~ObjScopeGuardImpl0() throw() |
---|
423 | { |
---|
424 | SafeExecute(*this); |
---|
425 | } |
---|
426 | |
---|
427 | void Execute() |
---|
428 | { |
---|
429 | (obj_.*memFun_)(); |
---|
430 | } |
---|
431 | |
---|
432 | ObjScopeGuardImpl0(Obj& obj, MemFun memFun) : obj_(obj), memFun_(memFun) |
---|
433 | {} |
---|
434 | |
---|
435 | protected: |
---|
436 | Obj& obj_; |
---|
437 | MemFun memFun_; |
---|
438 | }; |
---|
439 | |
---|
440 | template <class Obj, typename MemFun> |
---|
441 | inline ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun) |
---|
442 | { |
---|
443 | return ObjScopeGuardImpl0<Obj, MemFun>::MakeObjGuard(obj, memFun); |
---|
444 | } |
---|
445 | |
---|
446 | template <typename Ret, class Obj1, class Obj2> |
---|
447 | inline ObjScopeGuardImpl0<Obj1,Ret(Obj2::*)()> MakeGuard(Ret(Obj2::*memFun)(), Obj1 &obj) |
---|
448 | { |
---|
449 | return ObjScopeGuardImpl0<Obj1,Ret(Obj2::*)()>::MakeObjGuard(obj,memFun); |
---|
450 | } |
---|
451 | |
---|
452 | template <typename Ret, class Obj1, class Obj2> |
---|
453 | inline ObjScopeGuardImpl0<Obj1,Ret(Obj2::*)()> MakeGuard(Ret(Obj2::*memFun)(), Obj1 *obj) |
---|
454 | { |
---|
455 | return ObjScopeGuardImpl0<Obj1,Ret(Obj2::*)()>::MakeObjGuard(*obj,memFun); |
---|
456 | } |
---|
457 | |
---|
458 | //////////////////////////////////////////////////////////////// |
---|
459 | /// |
---|
460 | /// \class ObjScopeGuardImpl1 |
---|
461 | /// \ingroup ExceptionGroup |
---|
462 | /// |
---|
463 | /// Implementation class for a class per-instance member function with one |
---|
464 | /// parameter. The parameter is copied by value - use ::Loki::ByRef if you |
---|
465 | /// must use a reference instead. ScopeGuard ignores any value returned |
---|
466 | /// from the call within the Execute function. |
---|
467 | /// |
---|
468 | /// This class has 3 standalone helper functions which create a ScopeGuard. |
---|
469 | /// One is MakeObjGuard, which is deprecated but provided for older code. |
---|
470 | /// The other two are MakeGuard overloads, one which takes a pointer to an |
---|
471 | /// object, and the other which takes a reference. |
---|
472 | /// |
---|
473 | //////////////////////////////////////////////////////////////// |
---|
474 | |
---|
475 | template <class Obj, typename MemFun, typename P1> |
---|
476 | class ObjScopeGuardImpl1 : public ScopeGuardImplBase |
---|
477 | { |
---|
478 | public: |
---|
479 | static ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1) |
---|
480 | { |
---|
481 | return ObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1); |
---|
482 | } |
---|
483 | |
---|
484 | ~ObjScopeGuardImpl1() throw() |
---|
485 | { |
---|
486 | SafeExecute(*this); |
---|
487 | } |
---|
488 | |
---|
489 | void Execute() |
---|
490 | { |
---|
491 | (obj_.*memFun_)(p1_); |
---|
492 | } |
---|
493 | |
---|
494 | ObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1) : obj_(obj), memFun_(memFun), p1_(p1) |
---|
495 | {} |
---|
496 | |
---|
497 | protected: |
---|
498 | Obj& obj_; |
---|
499 | MemFun memFun_; |
---|
500 | const P1 p1_; |
---|
501 | }; |
---|
502 | |
---|
503 | template <class Obj, typename MemFun, typename P1> |
---|
504 | inline ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1) |
---|
505 | { |
---|
506 | return ObjScopeGuardImpl1<Obj, MemFun, P1>::MakeObjGuard(obj, memFun, p1); |
---|
507 | } |
---|
508 | |
---|
509 | template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b> |
---|
510 | inline ObjScopeGuardImpl1<Obj1,Ret(Obj2::*)(P1a),P1b> MakeGuard(Ret(Obj2::*memFun)(P1a), Obj1 &obj, P1b p1) |
---|
511 | { |
---|
512 | return ObjScopeGuardImpl1<Obj1,Ret(Obj2::*)(P1a),P1b>::MakeObjGuard(obj,memFun,p1); |
---|
513 | } |
---|
514 | |
---|
515 | template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b> |
---|
516 | inline ObjScopeGuardImpl1<Obj1,Ret(Obj2::*)(P1a),P1b> MakeGuard(Ret(Obj2::*memFun)(P1a), Obj1 *obj, P1b p1) |
---|
517 | { |
---|
518 | return ObjScopeGuardImpl1<Obj1,Ret(Obj2::*)(P1a),P1b>::MakeObjGuard(*obj,memFun,p1); |
---|
519 | } |
---|
520 | |
---|
521 | //////////////////////////////////////////////////////////////// |
---|
522 | /// |
---|
523 | /// \class ObjScopeGuardImpl2 |
---|
524 | /// \ingroup ExceptionGroup |
---|
525 | /// |
---|
526 | /// Implementation class for a class per-instance member function with two |
---|
527 | /// parameters. Each parameter is copied by value - use ::Loki::ByRef if you |
---|
528 | /// must use a reference instead. ScopeGuard ignores any value returned |
---|
529 | /// from the call within the Execute function. |
---|
530 | /// |
---|
531 | /// This class has 3 standalone helper functions which create a ScopeGuard. |
---|
532 | /// One is MakeObjGuard, which is deprecated but provided for older code. |
---|
533 | /// The other two are MakeGuard overloads, one which takes a pointer to an |
---|
534 | /// object, and the other which takes a reference. |
---|
535 | /// |
---|
536 | //////////////////////////////////////////////////////////////// |
---|
537 | |
---|
538 | template <class Obj, typename MemFun, typename P1, typename P2> |
---|
539 | class ObjScopeGuardImpl2 : public ScopeGuardImplBase |
---|
540 | { |
---|
541 | public: |
---|
542 | static ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2) |
---|
543 | { |
---|
544 | return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2); |
---|
545 | } |
---|
546 | |
---|
547 | ~ObjScopeGuardImpl2() throw() |
---|
548 | { |
---|
549 | SafeExecute(*this); |
---|
550 | } |
---|
551 | |
---|
552 | void Execute() |
---|
553 | { |
---|
554 | (obj_.*memFun_)(p1_, p2_); |
---|
555 | } |
---|
556 | |
---|
557 | ObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2) : obj_(obj), memFun_(memFun), p1_(p1), p2_(p2) |
---|
558 | {} |
---|
559 | |
---|
560 | protected: |
---|
561 | Obj& obj_; |
---|
562 | MemFun memFun_; |
---|
563 | const P1 p1_; |
---|
564 | const P2 p2_; |
---|
565 | }; |
---|
566 | |
---|
567 | template <class Obj, typename MemFun, typename P1, typename P2> |
---|
568 | inline ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2) |
---|
569 | { |
---|
570 | return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>::MakeObjGuard(obj, memFun, p1, p2); |
---|
571 | } |
---|
572 | |
---|
573 | template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b, typename P2a, typename P2b> |
---|
574 | inline ObjScopeGuardImpl2<Obj1,Ret(Obj2::*)(P1a,P2a),P1b,P2b> MakeGuard(Ret(Obj2::*memFun)(P1a,P2a), Obj1 &obj, P1b p1, P2b p2) |
---|
575 | { |
---|
576 | return ObjScopeGuardImpl2<Obj1,Ret(Obj2::*)(P1a,P2a),P1b,P2b>::MakeObjGuard(obj,memFun,p1,p2); |
---|
577 | } |
---|
578 | |
---|
579 | template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b, typename P2a, typename P2b> |
---|
580 | inline ObjScopeGuardImpl2<Obj1,Ret(Obj2::*)(P1a,P2a),P1b,P2b> MakeGuard(Ret(Obj2::*memFun)(P1a,P2a), Obj1 *obj, P1b p1, P2b p2) |
---|
581 | { |
---|
582 | return ObjScopeGuardImpl2<Obj1,Ret(Obj2::*)(P1a,P2a),P1b,P2b>::MakeObjGuard(*obj,memFun,p1,p2); |
---|
583 | } |
---|
584 | |
---|
585 | //////////////////////////////////////////////////////////////// |
---|
586 | /// |
---|
587 | /// \class ObjScopeGuardImpl3 |
---|
588 | /// \ingroup ExceptionGroup |
---|
589 | /// |
---|
590 | /// Implementation class for a class per-instance member function with three |
---|
591 | /// parameters. Each parameter is copied by value - use ::Loki::ByRef if you |
---|
592 | /// must use a reference instead. ScopeGuard ignores any value returned |
---|
593 | /// from the call within the Execute function. |
---|
594 | /// |
---|
595 | /// This class has 3 standalone helper functions which create a ScopeGuard. |
---|
596 | /// One is MakeObjGuard, which is deprecated but provided for older code. |
---|
597 | /// The other two are MakeGuard overloads, one which takes a pointer to an |
---|
598 | /// object, and the other which takes a reference. |
---|
599 | /// |
---|
600 | //////////////////////////////////////////////////////////////// |
---|
601 | |
---|
602 | template < class Obj, typename MemFun, typename P1, typename P2, typename P3 > |
---|
603 | class ObjScopeGuardImpl3 : public ScopeGuardImplBase |
---|
604 | { |
---|
605 | public: |
---|
606 | static ObjScopeGuardImpl3< Obj, MemFun, P1, P2, P3 > MakeObjGuard( |
---|
607 | Obj & obj, MemFun memFun, P1 p1, P2 p2, P3 p3 ) |
---|
608 | { |
---|
609 | return ObjScopeGuardImpl3< Obj, MemFun, P1, P2, P3 >( obj, memFun, p1, p2, p3 ); |
---|
610 | } |
---|
611 | |
---|
612 | ~ObjScopeGuardImpl3() throw() |
---|
613 | { |
---|
614 | SafeExecute( *this ); |
---|
615 | } |
---|
616 | |
---|
617 | void Execute() |
---|
618 | { |
---|
619 | ( obj_.*memFun_ )( p1_, p2_, p3_ ); |
---|
620 | } |
---|
621 | |
---|
622 | ObjScopeGuardImpl3( Obj & obj, MemFun memFun, P1 p1, P2 p2, P3 p3 ) : |
---|
623 | obj_( obj ), memFun_( memFun ), p1_( p1 ), p2_( p2 ), p3_( p3 ) |
---|
624 | {} |
---|
625 | |
---|
626 | protected: |
---|
627 | Obj& obj_; |
---|
628 | MemFun memFun_; |
---|
629 | const P1 p1_; |
---|
630 | const P2 p2_; |
---|
631 | const P3 p3_; |
---|
632 | }; |
---|
633 | |
---|
634 | template < class Obj, typename MemFun, typename P1, typename P2, typename P3 > |
---|
635 | inline ObjScopeGuardImpl3< Obj, MemFun, P1, P2, P3 > MakeObjGuard( |
---|
636 | Obj & obj, MemFun memFun, P1 p1, P2 p2, P3 p3 ) |
---|
637 | { |
---|
638 | return ObjScopeGuardImpl3< Obj, MemFun, P1, P2, P3 >::MakeObjGuard( |
---|
639 | obj, memFun, p1, p2, p3 ); |
---|
640 | } |
---|
641 | |
---|
642 | template < typename Ret, class Obj1, class Obj2, typename P1a, typename P1b, |
---|
643 | typename P2a, typename P2b, typename P3a, typename P3b > |
---|
644 | inline ObjScopeGuardImpl3< Obj1, Ret( Obj2::* )( P1a, P2a, P3a ), P1b, P2b, P3b > |
---|
645 | MakeGuard( Ret( Obj2::*memFun )( P1a, P2a, P3a ), Obj1 & obj, P1b p1, P2b p2, P3b p3 ) |
---|
646 | { |
---|
647 | return ObjScopeGuardImpl3< Obj1, Ret( Obj2::* )( P1a, P2a, P3a ), P1b, P2b, P3b > |
---|
648 | ::MakeObjGuard( obj, memFun, p1, p2, p3 ); |
---|
649 | } |
---|
650 | |
---|
651 | template < typename Ret, class Obj1, class Obj2, typename P1a, typename P1b, |
---|
652 | typename P2a, typename P2b, typename P3a, typename P3b > |
---|
653 | inline ObjScopeGuardImpl3< Obj1, Ret( Obj2::* )( P1a, P2a, P3a ), P1b, P2b, P3b > |
---|
654 | MakeGuard( Ret( Obj2::*memFun )( P1a, P2a, P3a ), Obj1 * obj, P1b p1, P2b p2, P3b p3 ) |
---|
655 | { |
---|
656 | return ObjScopeGuardImpl3< Obj1, Ret( Obj2::* )( P1a, P2a, P3a ), P1b, P2b, P3b > |
---|
657 | ::MakeObjGuard( *obj, memFun, p1, p2, p3 ); |
---|
658 | } |
---|
659 | |
---|
660 | } // namespace Loki |
---|
661 | |
---|
662 | #define LOKI_CONCATENATE_DIRECT(s1, s2) s1##s2 |
---|
663 | #define LOKI_CONCATENATE(s1, s2) LOKI_CONCATENATE_DIRECT(s1, s2) |
---|
664 | #define LOKI_ANONYMOUS_VARIABLE(str) LOKI_CONCATENATE(str, __LINE__) |
---|
665 | |
---|
666 | #define LOKI_ON_BLOCK_EXIT ::Loki::ScopeGuard LOKI_ANONYMOUS_VARIABLE(scopeGuard) = ::Loki::MakeGuard |
---|
667 | #define LOKI_ON_BLOCK_EXIT_OBJ ::Loki::ScopeGuard LOKI_ANONYMOUS_VARIABLE(scopeGuard) = ::Loki::MakeObjGuard |
---|
668 | |
---|
669 | #endif // end file guardian |
---|
670 | |
---|