CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
scope_guard.h
1
3#ifndef _aspose_system_scope_guard_h_
4#define _aspose_system_scope_guard_h_
5
6#include <exception>
7
8namespace System
9{
10
14template <typename F>
16{
19 ScopeGuard(F f) : f(f), disabled(false)
20 {}
21#if defined(_MSC_VER)
22#pragma warning(push)
23// Warning C4722: destructor never returns, potential memory leak.
24#pragma warning(disable : 4722)
25#endif
27 ~ScopeGuard() noexcept(false)
28 {
29 if (!disabled)
30 {
31#ifdef __cpp_lib_uncaught_exceptions
32 if (std::uncaught_exceptions() > 0)
33#else
34 if (std::uncaught_exception()) // Deprecated since C++ 17
35#endif
36 {
37 // "finally" during exception (unwinding stack)
38 // need prevent double exception and process termination
39 try
40 {
41 f();
42 }
43 catch (...)
44 {
45 }
46 }
47 else
48 {
49 f();
50 }
51 }
52 }
53#if defined(_MSC_VER)
54#pragma warning(pop)
55#endif
56
58 void Disable()
59 {
60 disabled = true;
61 }
62
63private:
65 F f;
66 bool disabled;
67};
68
69// auto guard = MakeScopeGuard([&](){
70// .....
71// });
76template <typename F>
78{
79 return ScopeGuard<F>(f);
80};
81
82} // namespace System
83#endif // _aspose_system_scope_guard_h_
Definition: db_command.h:9
ScopeGuard< F > MakeScopeGuard(F f)
A factory function that creates instances of ScopedGuard class.
Definition: scope_guard.h:77
The service class that provides services for running a particular function object when an instance of...
Definition: scope_guard.h:16
void Disable()
Disables guard invocation.
Definition: scope_guard.h:58
~ScopeGuard() noexcept(false)
Invokes the function object passed to the constructor.
Definition: scope_guard.h:27
ScopeGuard(F f)
Constructs an instance that is set up to invoke the specified function object.
Definition: scope_guard.h:19