IOSS  2.0
Ioss_ScopeGuard.h
Go to the documentation of this file.
1 /*
2  * Copyright(C) 1999-2020 National Technology & Engineering Solutions
3  * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4  * NTESS, the U.S. Government retains certain rights in this software.
5  *
6  * See packages/seacas/LICENSE for details
7  */
8 #ifndef SCOPEGUARD_H_
9 #define SCOPEGUARD_H_
10 
11 /*
12  Scopeguard, by Andrei Alexandrescu and Petru Marginean, December 2000.
13  Modified by Joshua Lehrer, FactSet Research Systems, November 2005.
14 */
15 
16 template <class T> class RefHolder
17 {
18  T &ref_;
19 
20 public:
21  explicit RefHolder(T &ref) : ref_(ref) {}
22  operator T &() const { return ref_; }
23  RefHolder &operator=(const RefHolder &) = delete;
24 };
25 
26 template <class T> inline RefHolder<T> ByRef(T &t) { return RefHolder<T>(t); }
27 
29 {
31 
32 protected:
33  ~ScopeGuardImplBase() = default;
35  {
36  other.Dismiss();
37  }
38  template <typename J> static void SafeExecute(J &j)
39  {
40  if (!j.dismissed_) {
41  try {
42  j.Execute();
43  }
44  catch (...) {
45  }
46  }
47  }
48 
49  mutable bool dismissed_;
50 
51 public:
53  void Dismiss() const { dismissed_ = true; }
54 };
55 
56 // typedef const ScopeGuardImplBase& ScopeGuard;
57 #ifndef _MSC_VER
58 __attribute__((unused))
59 #endif
60 typedef const ScopeGuardImplBase &ScopeGuard;
61 
62 template <typename F> class ScopeGuardImpl0 : public ScopeGuardImplBase
63 {
64 public:
65  static ScopeGuardImpl0<F> MakeGuard(F fun) { return ScopeGuardImpl0<F>(fun); }
67  void Execute() { fun_(); }
68 
69 protected:
70  explicit ScopeGuardImpl0(F fun) : fun_(fun) {}
71  F fun_;
72 };
73 
74 template <typename F> inline ScopeGuardImpl0<F> MakeGuard(F fun)
75 {
77 }
78 
79 template <typename F, typename P1> class ScopeGuardImpl1 : public ScopeGuardImplBase
80 {
81 public:
82  static ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1) { return ScopeGuardImpl1<F, P1>(fun, p1); }
84  void Execute() { fun_(p1_); }
85 
86 protected:
87  ScopeGuardImpl1(F fun, P1 p1) : fun_(fun), p1_(p1) {}
88  F fun_;
89  const P1 p1_;
90 };
91 
92 template <typename F, typename P1> inline ScopeGuardImpl1<F, P1> MakeGuard(F fun, P1 p1)
93 {
94  return ScopeGuardImpl1<F, P1>::MakeGuard(fun, p1);
95 }
96 
97 template <typename F, typename P1, typename P2> class ScopeGuardImpl2 : public ScopeGuardImplBase
98 {
99 public:
100  static ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
101  {
102  return ScopeGuardImpl2<F, P1, P2>(fun, p1, p2);
103  }
105  void Execute() { fun_(p1_, p2_); }
106 
107 protected:
108  ScopeGuardImpl2(F fun, P1 p1, P2 p2) : fun_(fun), p1_(p1), p2_(p2) {}
109  F fun_;
110  const P1 p1_;
111  const P2 p2_;
112 };
113 
114 template <typename F, typename P1, typename P2>
115 inline ScopeGuardImpl2<F, P1, P2> MakeGuard(F fun, P1 p1, P2 p2)
116 {
117  return ScopeGuardImpl2<F, P1, P2>::MakeGuard(fun, p1, p2);
118 }
119 
120 template <typename F, typename P1, typename P2, typename P3>
122 {
123 public:
124  static ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
125  {
126  return ScopeGuardImpl3<F, P1, P2, P3>(fun, p1, p2, p3);
127  }
129  void Execute() { fun_(p1_, p2_, p3_); }
130 
131 protected:
132  ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3) : fun_(fun), p1_(p1), p2_(p2), p3_(p3) {}
133  F fun_;
134  const P1 p1_;
135  const P2 p2_;
136  const P3 p3_;
137 };
138 
139 template <typename F, typename P1, typename P2, typename P3>
140 inline ScopeGuardImpl3<F, P1, P2, P3> MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
141 {
142  return ScopeGuardImpl3<F, P1, P2, P3>::MakeGuard(fun, p1, p2, p3);
143 }
144 
145 //************************************************************
146 
147 template <class Obj, typename MemFun> class ObjScopeGuardImpl0 : public ScopeGuardImplBase
148 {
149 public:
150  static ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj &obj, MemFun memFun)
151  {
152  return ObjScopeGuardImpl0<Obj, MemFun>(obj, memFun);
153  }
155  void Execute() { (obj_.*memFun_)(); }
156 
157 protected:
158  ObjScopeGuardImpl0(Obj &obj, MemFun memFun) : obj_(obj), memFun_(memFun) {}
159  Obj & obj_;
160  MemFun memFun_;
161 };
162 
163 template <class Obj, typename MemFun>
164 inline ObjScopeGuardImpl0<Obj, MemFun> MakeObjGuard(Obj &obj, MemFun memFun)
165 {
167 }
168 
169 template <typename Ret, class Obj1, class Obj2>
170 inline ObjScopeGuardImpl0<Obj1, Ret (Obj2::*)()> MakeGuard(Ret (Obj2::*memFun)(), Obj1 &obj)
171 {
172  return ObjScopeGuardImpl0<Obj1, Ret (Obj2::*)()>::MakeObjGuard(obj, memFun);
173 }
174 
175 template <typename Ret, class Obj1, class Obj2>
176 inline ObjScopeGuardImpl0<Obj1, Ret (Obj2::*)()> MakeGuard(Ret (Obj2::*memFun)(), Obj1 *obj)
177 {
178  return ObjScopeGuardImpl0<Obj1, Ret (Obj2::*)()>::MakeObjGuard(*obj, memFun);
179 }
180 
181 template <class Obj, typename MemFun, typename P1>
183 {
184 public:
185  static ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj &obj, MemFun memFun, P1 p1)
186  {
187  return ObjScopeGuardImpl1<Obj, MemFun, P1>(obj, memFun, p1);
188  }
190  void Execute() { (obj_.*memFun_)(p1_); }
191 
192 protected:
193  ObjScopeGuardImpl1(Obj &obj, MemFun memFun, P1 p1) : obj_(obj), memFun_(memFun), p1_(p1) {}
194  Obj & obj_;
195  MemFun memFun_;
196  const P1 p1_;
197 };
198 
199 template <class Obj, typename MemFun, typename P1>
200 inline ObjScopeGuardImpl1<Obj, MemFun, P1> MakeObjGuard(Obj &obj, MemFun memFun, P1 p1)
201 {
203 }
204 
205 template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b>
206 inline ObjScopeGuardImpl1<Obj1, Ret (Obj2::*)(P1a), P1b> MakeGuard(Ret (Obj2::*memFun)(P1a),
207  Obj1 &obj, P1b p1)
208 {
209  return ObjScopeGuardImpl1<Obj1, Ret (Obj2::*)(P1a), P1b>::MakeObjGuard(obj, memFun, p1);
210 }
211 
212 template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b>
213 inline ObjScopeGuardImpl1<Obj1, Ret (Obj2::*)(P1a), P1b> MakeGuard(Ret (Obj2::*memFun)(P1a),
214  Obj1 *obj, P1b p1)
215 {
216  return ObjScopeGuardImpl1<Obj1, Ret (Obj2::*)(P1a), P1b>::MakeObjGuard(*obj, memFun, p1);
217 }
218 
219 template <class Obj, typename MemFun, typename P1, typename P2>
221 {
222 public:
223  static ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj &obj, MemFun memFun, P1 p1, P2 p2)
224  {
225  return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>(obj, memFun, p1, p2);
226  }
228  void Execute() { (obj_.*memFun_)(p1_, p2_); }
229 
230 protected:
231  ObjScopeGuardImpl2(Obj &obj, MemFun memFun, P1 p1, P2 p2)
232  : obj_(obj), memFun_(memFun), p1_(p1), p2_(p2)
233  {
234  }
235  Obj & obj_;
236  MemFun memFun_;
237  const P1 p1_;
238  const P2 p2_;
239 };
240 
241 template <class Obj, typename MemFun, typename P1, typename P2>
242 inline ObjScopeGuardImpl2<Obj, MemFun, P1, P2> MakeObjGuard(Obj &obj, MemFun memFun, P1 p1, P2 p2)
243 {
244  return ObjScopeGuardImpl2<Obj, MemFun, P1, P2>::MakeObjGuard(obj, memFun, p1, p2);
245 }
246 
247 template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b, typename P2a,
248  typename P2b>
249 inline ObjScopeGuardImpl2<Obj1, Ret (Obj2::*)(P1a, P2a), P1b, P2b>
250 MakeGuard(Ret (Obj2::*memFun)(P1a, P2a), Obj1 &obj, P1b p1, P2b p2)
251 {
252  return ObjScopeGuardImpl2<Obj1, Ret (Obj2::*)(P1a, P2a), P1b, P2b>::MakeObjGuard(obj, memFun, p1,
253  p2);
254 }
255 
256 template <typename Ret, class Obj1, class Obj2, typename P1a, typename P1b, typename P2a,
257  typename P2b>
258 inline ObjScopeGuardImpl2<Obj1, Ret (Obj2::*)(P1a, P2a), P1b, P2b>
259 MakeGuard(Ret (Obj2::*memFun)(P1a, P2a), Obj1 *obj, P1b p1, P2b p2)
260 {
261  return ObjScopeGuardImpl2<Obj1, Ret (Obj2::*)(P1a, P2a), P1b, P2b>::MakeObjGuard(*obj, memFun, p1,
262  p2);
263 }
264 
265 #define CONCATENATE_DIRECT(s1, s2) s1##s2
266 #define CONCATENATE(s1, s2) CONCATENATE_DIRECT(s1, s2)
267 #define ANONYMOUS_VARIABLE(str) CONCATENATE(str, __LINE__)
268 
269 #define ON_BLOCK_EXIT ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = MakeGuard
270 #define ON_BLOCK_EXIT_OBJ ScopeGuard ANONYMOUS_VARIABLE(scopeGuard) = MakeObjGuard
271 
272 #endif // SCOPEGUARD_H_
ObjScopeGuardImpl0::obj_
Obj & obj_
Definition: Ioss_ScopeGuard.h:159
ScopeGuardImpl1::~ScopeGuardImpl1
~ScopeGuardImpl1()
Definition: Ioss_ScopeGuard.h:83
ObjScopeGuardImpl2::~ObjScopeGuardImpl2
~ObjScopeGuardImpl2()
Definition: Ioss_ScopeGuard.h:227
ObjScopeGuardImpl0::ObjScopeGuardImpl0
ObjScopeGuardImpl0(Obj &obj, MemFun memFun)
Definition: Ioss_ScopeGuard.h:158
ByRef
RefHolder< T > ByRef(T &t)
Definition: Ioss_ScopeGuard.h:26
ScopeGuardImpl1
Definition: Ioss_ScopeGuard.h:80
ScopeGuardImplBase
Definition: Ioss_ScopeGuard.h:29
RefHolder
Definition: Ioss_ScopeGuard.h:17
ObjScopeGuardImpl0::memFun_
MemFun memFun_
Definition: Ioss_ScopeGuard.h:160
ScopeGuardImpl3::fun_
F fun_
Definition: Ioss_ScopeGuard.h:133
RefHolder::RefHolder
RefHolder(T &ref)
Definition: Ioss_ScopeGuard.h:21
ScopeGuardImpl1::fun_
F fun_
Definition: Ioss_ScopeGuard.h:88
ScopeGuardImpl0::MakeGuard
static ScopeGuardImpl0< F > MakeGuard(F fun)
Definition: Ioss_ScopeGuard.h:65
ScopeGuardImplBase::ScopeGuardImplBase
ScopeGuardImplBase()
Definition: Ioss_ScopeGuard.h:52
ScopeGuardImplBase::dismissed_
bool dismissed_
Definition: Ioss_ScopeGuard.h:49
MakeGuard
ScopeGuardImpl0< F > MakeGuard(F fun)
Definition: Ioss_ScopeGuard.h:74
ObjScopeGuardImpl0::~ObjScopeGuardImpl0
~ObjScopeGuardImpl0()
Definition: Ioss_ScopeGuard.h:154
ObjScopeGuardImpl1::p1_
const P1 p1_
Definition: Ioss_ScopeGuard.h:196
ScopeGuardImpl0
Definition: Ioss_ScopeGuard.h:63
ScopeGuardImpl2::p2_
const P2 p2_
Definition: Ioss_ScopeGuard.h:111
ScopeGuardImpl2::Execute
void Execute()
Definition: Ioss_ScopeGuard.h:105
ObjScopeGuardImpl1::ObjScopeGuardImpl1
ObjScopeGuardImpl1(Obj &obj, MemFun memFun, P1 p1)
Definition: Ioss_ScopeGuard.h:193
ObjScopeGuardImpl2::MakeObjGuard
static ObjScopeGuardImpl2< Obj, MemFun, P1, P2 > MakeObjGuard(Obj &obj, MemFun memFun, P1 p1, P2 p2)
Definition: Ioss_ScopeGuard.h:223
ScopeGuardImpl3::p2_
const P2 p2_
Definition: Ioss_ScopeGuard.h:135
ScopeGuardImpl3
Definition: Ioss_ScopeGuard.h:122
ObjScopeGuardImpl2::obj_
Obj & obj_
Definition: Ioss_ScopeGuard.h:235
ScopeGuardImplBase::ScopeGuardImplBase
ScopeGuardImplBase(const ScopeGuardImplBase &other)
Definition: Ioss_ScopeGuard.h:34
ScopeGuardImpl3::Execute
void Execute()
Definition: Ioss_ScopeGuard.h:129
ScopeGuardImpl3::p1_
const P1 p1_
Definition: Ioss_ScopeGuard.h:134
ScopeGuardImpl1::ScopeGuardImpl1
ScopeGuardImpl1(F fun, P1 p1)
Definition: Ioss_ScopeGuard.h:87
ObjScopeGuardImpl0::MakeObjGuard
static ObjScopeGuardImpl0< Obj, MemFun > MakeObjGuard(Obj &obj, MemFun memFun)
Definition: Ioss_ScopeGuard.h:150
ObjScopeGuardImpl2
Definition: Ioss_ScopeGuard.h:221
ScopeGuardImpl2::~ScopeGuardImpl2
~ScopeGuardImpl2()
Definition: Ioss_ScopeGuard.h:104
ScopeGuardImpl2::p1_
const P1 p1_
Definition: Ioss_ScopeGuard.h:110
ObjScopeGuardImpl2::ObjScopeGuardImpl2
ObjScopeGuardImpl2(Obj &obj, MemFun memFun, P1 p1, P2 p2)
Definition: Ioss_ScopeGuard.h:231
ObjScopeGuardImpl2::Execute
void Execute()
Definition: Ioss_ScopeGuard.h:228
ObjScopeGuardImpl2::memFun_
MemFun memFun_
Definition: Ioss_ScopeGuard.h:236
MakeObjGuard
ObjScopeGuardImpl0< Obj, MemFun > MakeObjGuard(Obj &obj, MemFun memFun)
Definition: Ioss_ScopeGuard.h:164
ScopeGuardImpl1::Execute
void Execute()
Definition: Ioss_ScopeGuard.h:84
RefHolder::ref_
T & ref_
Definition: Ioss_ScopeGuard.h:18
ScopeGuardImpl2::MakeGuard
static ScopeGuardImpl2< F, P1, P2 > MakeGuard(F fun, P1 p1, P2 p2)
Definition: Ioss_ScopeGuard.h:100
ScopeGuardImpl1::MakeGuard
static ScopeGuardImpl1< F, P1 > MakeGuard(F fun, P1 p1)
Definition: Ioss_ScopeGuard.h:82
ScopeGuardImpl1::p1_
const P1 p1_
Definition: Ioss_ScopeGuard.h:89
ScopeGuardImplBase::~ScopeGuardImplBase
~ScopeGuardImplBase()=default
ScopeGuardImpl2::ScopeGuardImpl2
ScopeGuardImpl2(F fun, P1 p1, P2 p2)
Definition: Ioss_ScopeGuard.h:108
ScopeGuardImplBase::operator=
ScopeGuardImplBase & operator=(const ScopeGuardImplBase &)
ObjScopeGuardImpl1
Definition: Ioss_ScopeGuard.h:183
ObjScopeGuardImpl1::MakeObjGuard
static ObjScopeGuardImpl1< Obj, MemFun, P1 > MakeObjGuard(Obj &obj, MemFun memFun, P1 p1)
Definition: Ioss_ScopeGuard.h:185
RefHolder::operator=
RefHolder & operator=(const RefHolder &)=delete
__attribute__
__attribute__((unused)) typedef const ScopeGuardImplBase &ScopeGuard
ScopeGuardImpl0::fun_
F fun_
Definition: Ioss_ScopeGuard.h:71
ScopeGuardImpl3::MakeGuard
static ScopeGuardImpl3< F, P1, P2, P3 > MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
Definition: Ioss_ScopeGuard.h:124
ScopeGuardImpl0::Execute
void Execute()
Definition: Ioss_ScopeGuard.h:67
ObjScopeGuardImpl2::p1_
const P1 p1_
Definition: Ioss_ScopeGuard.h:237
ScopeGuardImpl3::p3_
const P3 p3_
Definition: Ioss_ScopeGuard.h:136
ObjScopeGuardImpl2::p2_
const P2 p2_
Definition: Ioss_ScopeGuard.h:238
ObjScopeGuardImpl0::Execute
void Execute()
Definition: Ioss_ScopeGuard.h:155
ScopeGuardImplBase::SafeExecute
static void SafeExecute(J &j)
Definition: Ioss_ScopeGuard.h:38
ObjScopeGuardImpl1::~ObjScopeGuardImpl1
~ObjScopeGuardImpl1()
Definition: Ioss_ScopeGuard.h:189
ScopeGuardImpl0::ScopeGuardImpl0
ScopeGuardImpl0(F fun)
Definition: Ioss_ScopeGuard.h:70
ObjScopeGuardImpl1::Execute
void Execute()
Definition: Ioss_ScopeGuard.h:190
ObjScopeGuardImpl1::obj_
Obj & obj_
Definition: Ioss_ScopeGuard.h:194
ScopeGuardImpl3::~ScopeGuardImpl3
~ScopeGuardImpl3()
Definition: Ioss_ScopeGuard.h:128
ObjScopeGuardImpl1::memFun_
MemFun memFun_
Definition: Ioss_ScopeGuard.h:195
ObjScopeGuardImpl0
Definition: Ioss_ScopeGuard.h:148
ScopeGuardImpl2
Definition: Ioss_ScopeGuard.h:98
ScopeGuardImplBase::Dismiss
void Dismiss() const
Definition: Ioss_ScopeGuard.h:53
ScopeGuardImpl0::~ScopeGuardImpl0
~ScopeGuardImpl0()
Definition: Ioss_ScopeGuard.h:66
ScopeGuardImpl2::fun_
F fun_
Definition: Ioss_ScopeGuard.h:109
ScopeGuardImpl3::ScopeGuardImpl3
ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3)
Definition: Ioss_ScopeGuard.h:132