Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_ScopeGuard.h

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