MeshLib
 
Loading...
Searching...
No Matches
MRFinally.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cpp_exceptions
4#include <exception>
5#endif
6#include <utility>
7
9#define MR_FINALLY DETAIL_MR_FINALLY( ScopeGuard )
10
11#ifdef __cpp_exceptions
12
14#define MR_FINALLY_ON_SUCCESS DETAIL_MR_FINALLY( ExceptionScopeGuard<true>::Type )
15
17#define MR_FINALLY_ON_THROW DETAIL_MR_FINALLY( ExceptionScopeGuard<false>::Type )
18
19#else // If no exceptions.
20
22#define MR_FINALLY_ON_SUCCESS MR_FINALLY
24#define MR_FINALLY_ON_THROW (void)[&]()
25
26#endif // If no exceptions.
27
28#define DETAIL_MR_FINALLY( type_ ) \
29 auto DETAIL_MR_FINALLY_CAT( _mrScopeGuard, __COUNTER__ ) = ::MR::detail::MakeScopeGuard<::MR::detail::type_>{} ->* [&]() -> void
30
31#define DETAIL_MR_FINALLY_CAT( x, y ) DETAIL_MR_FINALLY_CAT_( x, y )
32#define DETAIL_MR_FINALLY_CAT_( x, y ) x##y
33
34namespace MR::detail
35{
36
37template <typename F>
39{
40 F func;
41
42public:
43 ScopeGuard( F&& func ) : func( std::move( func ) ) {}
44 ScopeGuard( const ScopeGuard& ) = delete;
45 ScopeGuard& operator=( const ScopeGuard& ) = delete;
46 ~ScopeGuard() { func(); }
47};
48
49#ifdef __cpp_exceptions
50template <bool Success>
51struct ExceptionScopeGuard
52{
53 template <typename F>
54 class Type
55 {
56 F func;
57 int ex = std::uncaught_exceptions();
58
59 public:
60 Type( F&& func ) : func( std::move( func ) ) {}
61 Type( const Type& ) = delete;
62 Type& operator=( const Type& ) = delete;
63 ~Type() noexcept( !Success )
64 {
65 if ( ( ex == std::uncaught_exceptions() ) == Success )
66 func();
67 }
68 };
69};
70#endif
71
72template <template <typename> typename T>
74{
75 template <typename F>
76 T<F> operator->*( F&& func )
77 {
78 return T<F>( std::move( func ) );
79 }
80};
81
82} // namespace MR::detail
Definition MRFinally.h:39
~ScopeGuard()
Definition MRFinally.h:46
ScopeGuard(F &&func)
Definition MRFinally.h:43
ScopeGuard & operator=(const ScopeGuard &)=delete
ScopeGuard(const ScopeGuard &)=delete
Definition MRFinally.h:35
Definition MRFinally.h:74
T< F > operator->*(F &&func)
Definition MRFinally.h:76