Qore Programming Language  0.8.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ScopeGuard.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 #ifndef SCOPEGUARD_H_
3 #define SCOPEGUARD_H_
4 
6 template <class T>
7 class RefHolder
8 {
9  T& ref_;
10 public:
11  RefHolder(T& ref) : ref_(ref) {}
12  operator T& () const
13  {
14  return ref_;
15  }
16 private:
17  // Disable assignment - not implemented
18  RefHolder& operator=(const RefHolder&);
19 };
20 
21 template <class T>
22 inline RefHolder<T> ByRef(T& t)
23 {
24  return RefHolder<T>(t);
25 }
26 
29 {
30  ScopeGuardImplBase& operator =(const ScopeGuardImplBase&);
31 protected:
33  {
34  }
35 public:
36  ScopeGuardImplBase(const ScopeGuardImplBase& other) throw()
37  : dismissed_(other.dismissed_)
38  {
39  other.Dismiss();
40  }
41 protected:
42  template <typename J>
43  static void SafeExecute(J& j) throw()
44  {
45  if (!j.dismissed_)
46  try
47  {
48  j.Execute();
49  }
50  catch(...)
51  {
52  }
53  }
54 
55  mutable bool dismissed_;
56 public:
57  ScopeGuardImplBase() throw() : dismissed_(false)
58  {
59  }
60  void Dismiss() const throw()
61  {
62  dismissed_ = true;
63  }
64 };
65 
66 typedef const ScopeGuardImplBase& ScopeGuard;
67 
69 template <typename F>
71 {
72 public:
73  static ScopeGuardImpl0<F> MakeGuard(F fun)
74  {
75  return ScopeGuardImpl0<F>(fun);
76  }
77  ~ScopeGuardImpl0() throw()
78  {
79  SafeExecute(*this);
80  }
81  void Execute()
82  {
83  fun_();
84  }
85 protected:
86  ScopeGuardImpl0(F fun) : fun_(fun)
87  {
88  }
89  F fun_;
90 };
91 
93 template <typename F>
94 inline ScopeGuardImpl0<F> MakeGuard(F fun)
95 {
97 }
98 
100 template <typename F, typename P1>
102 {
103 public:
104  static ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
105  {
106  return ScopeGuardImpl1<F, P1>(fun, p1);
107  }
108  ~ScopeGuardImpl1() throw()
109  {
110  SafeExecute(*this);
111  }
112  void Execute()
113  {
114  fun_(p1_);
115  }
116 protected:
117  ScopeGuardImpl1(F fun, P1 p1) : fun_(fun), p1_(p1)
118  {
119  }
120  F fun_;
121  const P1 p1_;
122 };
123 
125 template <typename F, typename P1>
126 inline ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
127 {
128  return ScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
129 }
130 
132 template <typename F, typename P1, typename P2>
134 {
135 public:
136  static ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
137  {
138  return ScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
139  }
140  ~ScopeGuardImpl2() throw()
141  {
142  SafeExecute(*this);
143  }
144  void Execute()
145  {
146  fun_(p1_, p2_);
147  }
148 protected:
149  ScopeGuardImpl2(F fun, P1 p1, P2 p2) : fun_(fun), p1_(p1), p2_(p2)
150  {
151  }
152  F fun_;
153  const P1 p1_;
154  const P2 p2_;
155 };
156 
158 template <typename F, typename P1, typename P2>
159 inline ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
160 {
161  return ScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
162 }
163 
165 template <typename F, typename P1, typename P2, typename P3>
167 {
168 public:
169  static ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
170  {
171  return ScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3);
172  }
173  ~ScopeGuardImpl3() throw()
174  {
175  SafeExecute(*this);
176  }
177  void Execute()
178  {
179  fun_(p1_, p2_, p3_);
180  }
181 protected:
182  ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3) : fun_(fun), p1_(p1), p2_(p2), p3_(p3)
183  {
184  }
185  F fun_;
186  const P1 p1_;
187  const P2 p2_;
188  const P3 p3_;
189 };
190 
191 template <typename F, typename P1, typename P2, typename P3>
192 inline ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
193 {
194  return ScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3);
195 }
196 
197 //************************************************************
198 
200 template <class Obj, typename MemFun>
202 {
203 public:
204  static ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun)
205  {
206  return ObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
207  }
208  ~ObjScopeGuardImpl0() throw()
209  {
210  SafeExecute(*this);
211  }
212  void Execute()
213  {
214  (obj_.*memFun_)();
215  }
216 protected:
217  ObjScopeGuardImpl0(Obj& obj, MemFun memFun)
218  : obj_(obj), memFun_(memFun) {}
219  Obj& obj_;
220  MemFun memFun_;
221 };
222 
223 template <class Obj, typename MemFun>
224 inline ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj& obj, MemFun memFun)
225 {
227 }
228 
230 template <class Obj, typename MemFun, typename P1>
232 {
233 public:
234  static ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
235  {
236  return ObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
237  }
238  ~ObjScopeGuardImpl1() throw()
239  {
240  SafeExecute(*this);
241  }
242  void Execute()
243  {
244  (obj_.*memFun_)(p1_);
245  }
246 protected:
247  ObjScopeGuardImpl1(Obj& obj, MemFun memFun, P1 p1)
248  : obj_(obj), memFun_(memFun), p1_(p1) {}
249  Obj& obj_;
250  MemFun memFun_;
251  const P1 p1_;
252 };
253 
254 template <class Obj, typename MemFun, typename P1>
255 inline ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1)
256 {
258 }
259 
261 template <class Obj, typename MemFun, typename P1, typename P2>
263 {
264 public:
265  static ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
266  {
267  return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
268  }
269  ~ObjScopeGuardImpl2() throw()
270  {
271  SafeExecute(*this);
272  }
273  void Execute()
274  {
275  (obj_.*memFun_)(p1_, p2_);
276  }
277 protected:
278  ObjScopeGuardImpl2(Obj& obj, MemFun memFun, P1 p1, P2 p2)
279  : obj_(obj), memFun_(memFun), p1_(p1), p2_(p2) {}
280  Obj& obj_;
281  MemFun memFun_;
282  const P1 p1_;
283  const P2 p2_;
284 };
285 
286 template <class Obj, typename MemFun, typename P1, typename P2>
287 inline ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj& obj, MemFun memFun, P1 p1, P2 p2)
288 {
289  return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>::MakeObjGuard(obj, memFun, p1, p2);
290 }
291 
292 #define CONCATENATE_DIRECT(s1, s2) s1##s2
293 #define CONCATENATE(s1, s2) CONCATENATE_DIRECT(s1, s2)
294 #define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __LINE__)
295 
296 #ifdef __GNUC__
297 # define UNUSED_VARIABLE __attribute__((unused))
298 #else
299 # define UNUSED_VARIABLE
300 #endif
301 
302 #define ON_BLOCK_EXIT ScopeGuard UNUSED_VARIABLE ANONYMOUS_VARIABLE(scopeGuard) = MakeGuard
303 #define ON_BLOCK_EXIT_OBJ ScopeGuard UNUSED_VARIABLE ANONYMOUS_VARIABLE(scopeGuard) = MakeObjGuard
304 
305 #endif //SCOPEGUARD_H_
306 
307 
scope guard class
Definition: ScopeGuard.h:101
scope guard class
Definition: ScopeGuard.h:28
scope guard class
Definition: ScopeGuard.h:70
scope guard class
Definition: ScopeGuard.h:201
scope guard class
Definition: ScopeGuard.h:166
templated class for ScopeGuard to hold a c++ reference
Definition: ScopeGuard.h:7
scope guard class
Definition: ScopeGuard.h:231
scope guard class
Definition: ScopeGuard.h:133
scope guard class
Definition: ScopeGuard.h:262