Orxonox  0.0.5 Codename: Arcturus
ScopeGuard.h
Go to the documentation of this file.
1 // 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.
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 
28 
29 namespace Loki
30 {
31 
46 
48  {
51 
52  protected:
53 
55  {}
56 
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 
94 
96 
110 
111  template <typename F>
113  {
114  public:
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:
135  };
136 
137  template <typename F>
139  {
140  return ScopeGuardImpl0<F>::MakeGuard(fun);
141  }
142 
157 
158  template <typename F, typename P1>
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:
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 
205 
206  template <typename F, typename P1, typename P2>
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:
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 
254 
255  template <typename F, typename P1, typename P2, typename P3>
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:
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 
304 
305  template < typename F, typename P1, typename P2, typename P3, typename P4 >
307  {
308  public:
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:
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 
357 
358  template < typename F, typename P1, typename P2, typename P3, typename P4, typename P5 >
360  {
361  public:
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:
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 
412 
413  template <class Obj, typename MemFun>
415  {
416  public:
417  static ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun)
418  {
419  return ObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
420  }
421 
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  {
444  }
445 
446  template <typename Ret, class Obj1, class Obj2>
447  inline ObjScopeGuardImpl0<Obj1,Ret(Obj2::*)()> MakeGuard(Ret(Obj2::*memFun)(), Obj1 &obj)
448  {
450  }
451 
452  template <typename Ret, class Obj1, class Obj2>
453  inline ObjScopeGuardImpl0<Obj1,Ret(Obj2::*)()> MakeGuard(Ret(Obj2::*memFun)(), Obj1 *obj)
454  {
456  }
457 
474 
475  template <class Obj, typename MemFun, typename P1>
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 
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  {
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  {
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  {
519  }
520 
537 
538  template <class Obj, typename MemFun, typename P1, typename P2>
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 
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  {
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  {
583  }
584 
601 
602  template < class Obj, typename MemFun, typename P1, typename P2, typename P3 >
604  {
605  public:
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 
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 >
636  Obj & obj, MemFun memFun, P1 p1, P2 p2, P3 p3 )
637  {
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 >
645  MakeGuard( Ret( Obj2::*memFun )( P1a, P2a, P3a ), Obj1 & obj, P1b p1, P2b p2, P3b p3 )
646  {
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 >
654  MakeGuard( Ret( Obj2::*memFun )( P1a, P2a, P3a ), Obj1 * obj, P1b p1, P2b p2, P3b p3 )
655  {
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 
void Execute()
Definition: ScopeGuard.h:373
Implementation class for a class per-instance member function with three parameters.
Definition: ScopeGuard.h:603
~ScopeGuardImpl2()
Definition: ScopeGuard.h:215
Definition: NullType.h:21
ObjScopeGuardImpl0< Obj, MemFun > MakeObjGuard(Obj &obj, MemFun memFun)
Definition: ScopeGuard.h:441
Implementation class for a standalone function or class static function with three parameters...
Definition: ScopeGuard.h:256
MemFun memFun_
Definition: ScopeGuard.h:562
void Execute()
Definition: ScopeGuard.h:220
ScopeGuardImpl1(F fun, P1 p1)
Definition: ScopeGuard.h:177
F fun_
Definition: ScopeGuard.h:181
Implementation class for a class per-instance member function with two parameters.
Definition: ScopeGuard.h:539
const P2 p2_
Definition: ScopeGuard.h:231
ObjScopeGuardImpl3(Obj &obj, MemFun memFun, P1 p1, P2 p2, P3 p3)
Definition: ScopeGuard.h:622
void Execute()
Definition: ScopeGuard.h:427
const P1 p1_
Definition: ScopeGuard.h:182
const P1 p1_
Definition: ScopeGuard.h:384
Obj & obj_
Definition: ScopeGuard.h:561
ScopeGuardImplBase()
Definition: ScopeGuard.h:79
const P4 p4_
Definition: ScopeGuard.h:334
const P1 p1_
Definition: ScopeGuard.h:500
const P3 p3_
Definition: ScopeGuard.h:386
void Dismiss() const
Definition: ScopeGuard.h:82
Implementation class for a standalone function or class static function with two parameters.
Definition: ScopeGuard.h:207
void Execute()
Definition: ScopeGuard.h:489
const P5 p5_
Definition: ScopeGuard.h:388
MemFun memFun_
Definition: ScopeGuard.h:437
F fun_
Definition: ScopeGuard.h:229
static ObjScopeGuardImpl1< Obj, MemFun, P1 > MakeObjGuard(Obj &obj, MemFun memFun, P1 p1)
Definition: ScopeGuard.h:479
Implementation class for a class per-instance member function with one parameter. ...
Definition: ScopeGuard.h:476
Definition: InputPrereqs.h:91
F fun_
Definition: ScopeGuard.h:330
ObjScopeGuardImpl2(Obj &obj, MemFun memFun, P1 p1, P2 p2)
Definition: ScopeGuard.h:557
static ScopeGuardImpl0< F > MakeGuard(F fun)
Definition: ScopeGuard.h:115
~ScopeGuardImpl5()
Definition: ScopeGuard.h:368
const P3 p3_
Definition: ScopeGuard.h:631
void Execute()
Definition: ScopeGuard.h:552
ScopeGuardImpl5(F fun, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
Definition: ScopeGuard.h:378
Obj & obj_
Definition: ScopeGuard.h:436
void Execute()
Definition: ScopeGuard.h:617
ScopeGuardImpl2(F fun, P1 p1, P2 p2)
Definition: ScopeGuard.h:225
ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3)
Definition: ScopeGuard.h:274
void Execute()
Definition: ScopeGuard.h:269
const P2 p2_
Definition: ScopeGuard.h:280
static ScopeGuardImpl5< F, P1, P2, P3, P4, P5 > MakeGuard(F fun, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
Definition: ScopeGuard.h:362
const P2 p2_
Definition: ScopeGuard.h:332
static ScopeGuardImpl3< F, P1, P2, P3 > MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
Definition: ScopeGuard.h:259
void Execute()
Definition: ScopeGuard.h:172
const P4 p4_
Definition: ScopeGuard.h:387
Implementation class for a standalone function or class static function with one parameter.
Definition: ScopeGuard.h:159
const P3 p3_
Definition: ScopeGuard.h:281
MemFun memFun_
Definition: ScopeGuard.h:628
const P2 p2_
Definition: ScopeGuard.h:385
~ScopeGuardImpl3()
Definition: ScopeGuard.h:264
const P2 p2_
Definition: ScopeGuard.h:630
const P1 p1_
Definition: ScopeGuard.h:629
F fun_
Definition: ScopeGuard.h:278
const P2 p2_
Definition: ScopeGuard.h:564
ScopeGuardImpl4(F fun, P1 p1, P2 p2, P3 p3, P4 p4)
Definition: ScopeGuard.h:325
const P1 p1_
Definition: ScopeGuard.h:230
ObjScopeGuardImpl0(Obj &obj, MemFun memFun)
Definition: ScopeGuard.h:432
~ObjScopeGuardImpl2()
Definition: ScopeGuard.h:547
static ScopeGuardImpl4< F, P1, P2, P3, P4 > MakeGuard(F fun, P1 p1, P2 p2, P3 p3, P4 p4)
Definition: ScopeGuard.h:309
~ScopeGuardImpl4()
Definition: ScopeGuard.h:315
static ObjScopeGuardImpl2< Obj, MemFun, P1, P2 > MakeObjGuard(Obj &obj, MemFun memFun, P1 p1, P2 p2)
Definition: ScopeGuard.h:542
Implementation class for a standalone function or class static function with five parameters...
Definition: ScopeGuard.h:359
static ScopeGuardImpl2< F, P1, P2 > MakeGuard(F fun, P1 p1, P2 p2)
Definition: ScopeGuard.h:210
~ScopeGuardImpl0()
Definition: ScopeGuard.h:120
Implementation class for a class per-instance member function with no parameters. ...
Definition: ScopeGuard.h:414
static ObjScopeGuardImpl0< Obj, MemFun > MakeObjGuard(Obj &obj, MemFun memFun)
Definition: ScopeGuard.h:417
~ObjScopeGuardImpl0()
Definition: ScopeGuard.h:422
Obj & obj_
Definition: ScopeGuard.h:627
void Execute()
Definition: ScopeGuard.h:320
bool dismissed_
Definition: ScopeGuard.h:76
Obj & obj_
Definition: ScopeGuard.h:498
ObjScopeGuardImpl1(Obj &obj, MemFun memFun, P1 p1)
Definition: ScopeGuard.h:494
Implementation class for a standalone function or class static function with no parameters.
Definition: ScopeGuard.h:112
static ScopeGuardImpl1< F, P1 > MakeGuard(F fun, P1 p1)
Definition: ScopeGuard.h:162
ScopeGuardImplBase & operator=(const ScopeGuardImplBase &)
Copy-assignment operator is not implemented and private.
~ScopeGuardImplBase()
Definition: ScopeGuard.h:54
Base class used by all ScopeGuard implementations.
Definition: ScopeGuard.h:47
F fun_
Definition: ScopeGuard.h:134
~ScopeGuardImpl1()
Definition: ScopeGuard.h:167
F fun_
Definition: ScopeGuard.h:383
Implementation class for a standalone function or class static function with four parameters...
Definition: ScopeGuard.h:306
const P1 p1_
Definition: ScopeGuard.h:563
Definition: InputPrereqs.h:94
ScopeGuardImpl0(F fun)
Definition: ScopeGuard.h:130
void Execute()
Definition: ScopeGuard.h:125
const P1 p1_
Definition: ScopeGuard.h:331
~ObjScopeGuardImpl3()
Definition: ScopeGuard.h:612
static ObjScopeGuardImpl3< Obj, MemFun, P1, P2, P3 > MakeObjGuard(Obj &obj, MemFun memFun, P1 p1, P2 p2, P3 p3)
Definition: ScopeGuard.h:606
ScopeGuardImplBase(const ScopeGuardImplBase &other)
Copy-constructor takes over responsibility from other ScopeGuard.
Definition: ScopeGuard.h:58
~ObjScopeGuardImpl1()
Definition: ScopeGuard.h:484
const P3 p3_
Definition: ScopeGuard.h:333
ScopeGuardImpl0< F > MakeGuard(F fun)
Definition: ScopeGuard.h:138
MemFun memFun_
Definition: ScopeGuard.h:499
const P1 p1_
Definition: ScopeGuard.h:279
static void SafeExecute(J &j)
Definition: ScopeGuard.h:65
const ScopeGuardImplBase & ScopeGuard
Definition: ScopeGuard.h:95