IOSS 2.0
Loading...
Searching...
No Matches
Ioss_ScopeGuard.h
Go to the documentation of this file.
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
17namespace 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:
34
35 protected:
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); }
71 void Execute() { fun_(); }
72
73 protected:
74 explicit ScopeGuardImpl0(F fun) : fun_(fun) {}
76 };
77
78 template <typename F> inline ScopeGuardImpl0<F> MakeGuard(F fun)
79 {
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 }
91 void Execute() { fun_(p1_); }
92
93 protected:
94 ScopeGuardImpl1(F fun, P1 p1) : fun_(fun), p1_(p1) {}
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 }
112 void Execute() { fun_(p1_, p2_); }
113
114 protected:
115 ScopeGuardImpl2(F fun, P1 p1, P2 p2) : fun_(fun), p1_(p1), p2_(p2) {}
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>
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 }
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) {}
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 }
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 {
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>
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 }
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 {
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>
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 }
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 {
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
Definition Ioss_ScopeGuard.h:155
Obj & obj_
Definition Ioss_ScopeGuard.h:166
void Execute()
Definition Ioss_ScopeGuard.h:162
static ObjScopeGuardImpl0< Obj, MemFun > MakeObjGuard(Obj &obj, MemFun memFun)
Definition Ioss_ScopeGuard.h:157
MemFun memFun_
Definition Ioss_ScopeGuard.h:167
~ObjScopeGuardImpl0()
Definition Ioss_ScopeGuard.h:161
ObjScopeGuardImpl0(Obj &obj, MemFun memFun)
Definition Ioss_ScopeGuard.h:165
Definition Ioss_ScopeGuard.h:190
ObjScopeGuardImpl1(Obj &obj, MemFun memFun, P1 p1)
Definition Ioss_ScopeGuard.h:200
void Execute()
Definition Ioss_ScopeGuard.h:197
static ObjScopeGuardImpl1< Obj, MemFun, P1 > MakeObjGuard(Obj &obj, MemFun memFun, P1 p1)
Definition Ioss_ScopeGuard.h:192
~ObjScopeGuardImpl1()
Definition Ioss_ScopeGuard.h:196
Obj & obj_
Definition Ioss_ScopeGuard.h:201
MemFun memFun_
Definition Ioss_ScopeGuard.h:202
const P1 p1_
Definition Ioss_ScopeGuard.h:203
Definition Ioss_ScopeGuard.h:228
void Execute()
Definition Ioss_ScopeGuard.h:236
ObjScopeGuardImpl2(Obj &obj, MemFun memFun, P1 p1, P2 p2)
Definition Ioss_ScopeGuard.h:239
Obj & obj_
Definition Ioss_ScopeGuard.h:243
static ObjScopeGuardImpl2< Obj, MemFun, P1, P2 > MakeObjGuard(Obj &obj, MemFun memFun, P1 p1, P2 p2)
Definition Ioss_ScopeGuard.h:230
const P2 p2_
Definition Ioss_ScopeGuard.h:246
const P1 p1_
Definition Ioss_ScopeGuard.h:245
~ObjScopeGuardImpl2()
Definition Ioss_ScopeGuard.h:235
MemFun memFun_
Definition Ioss_ScopeGuard.h:244
Definition Ioss_ScopeGuard.h:19
RefHolder & operator=(const RefHolder &)=delete
T & ref_
Definition Ioss_ScopeGuard.h:20
RefHolder(T &ref)
Definition Ioss_ScopeGuard.h:23
Definition Ioss_ScopeGuard.h:67
static ScopeGuardImpl0< F > MakeGuard(F fun)
Definition Ioss_ScopeGuard.h:69
F fun_
Definition Ioss_ScopeGuard.h:75
ScopeGuardImpl0(F fun)
Definition Ioss_ScopeGuard.h:74
~ScopeGuardImpl0()
Definition Ioss_ScopeGuard.h:70
void Execute()
Definition Ioss_ScopeGuard.h:71
Definition Ioss_ScopeGuard.h:84
const P1 p1_
Definition Ioss_ScopeGuard.h:96
static ScopeGuardImpl1< F, P1 > MakeGuard(F fun, P1 p1)
Definition Ioss_ScopeGuard.h:86
ScopeGuardImpl1(F fun, P1 p1)
Definition Ioss_ScopeGuard.h:94
F fun_
Definition Ioss_ScopeGuard.h:95
void Execute()
Definition Ioss_ScopeGuard.h:91
~ScopeGuardImpl1()
Definition Ioss_ScopeGuard.h:90
Definition Ioss_ScopeGuard.h:105
ScopeGuardImpl2(F fun, P1 p1, P2 p2)
Definition Ioss_ScopeGuard.h:115
void Execute()
Definition Ioss_ScopeGuard.h:112
static ScopeGuardImpl2< F, P1, P2 > MakeGuard(F fun, P1 p1, P2 p2)
Definition Ioss_ScopeGuard.h:107
F fun_
Definition Ioss_ScopeGuard.h:116
const P2 p2_
Definition Ioss_ScopeGuard.h:118
const P1 p1_
Definition Ioss_ScopeGuard.h:117
~ScopeGuardImpl2()
Definition Ioss_ScopeGuard.h:111
Definition Ioss_ScopeGuard.h:129
const P1 p1_
Definition Ioss_ScopeGuard.h:141
~ScopeGuardImpl3()
Definition Ioss_ScopeGuard.h:135
static ScopeGuardImpl3< F, P1, P2, P3 > MakeGuard(F fun, P1 p1, P2 p2, P3 p3)
Definition Ioss_ScopeGuard.h:131
const P3 p3_
Definition Ioss_ScopeGuard.h:143
F fun_
Definition Ioss_ScopeGuard.h:140
ScopeGuardImpl3(F fun, P1 p1, P2 p2, P3 p3)
Definition Ioss_ScopeGuard.h:139
const P2 p2_
Definition Ioss_ScopeGuard.h:142
void Execute()
Definition Ioss_ScopeGuard.h:136
Definition Ioss_ScopeGuard.h:31
ScopeGuardImplBase(const ScopeGuardImplBase &other)
Definition Ioss_ScopeGuard.h:37
static void SafeExecute(J &j)
Definition Ioss_ScopeGuard.h:42
void Dismiss() const
Definition Ioss_ScopeGuard.h:57
ScopeGuardImplBase & operator=(const ScopeGuardImplBase &)=delete
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40
ObjScopeGuardImpl0< Obj, MemFun > MakeObjGuard(Obj &obj, MemFun memFun)
Definition Ioss_ScopeGuard.h:171
RefHolder< T > ByRef(T &t)
Definition Ioss_ScopeGuard.h:28
__attribute__((unused)) typedef const ScopeGuardImplBase &ScopeGuard
ScopeGuardImpl0< F > MakeGuard(F fun)
Definition Ioss_ScopeGuard.h:78