forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathon_scope_exit.hpp
More file actions
147 lines (122 loc) · 3.47 KB
/
Copy pathon_scope_exit.hpp
File metadata and controls
147 lines (122 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCORO_ON_SCOPE_EXIT_HPP_INCLUDED
#define CPPCORO_ON_SCOPE_EXIT_HPP_INCLUDED
#include <type_traits>
#include <exception>
namespace cppcoro
{
template<typename FUNC>
class scoped_lambda
{
public:
scoped_lambda(FUNC&& func)
: m_func(std::forward<FUNC>(func))
, m_cancelled(false)
{}
scoped_lambda(const scoped_lambda& other) = delete;
scoped_lambda(scoped_lambda&& other)
: m_func(std::forward<FUNC>(other.m_func))
, m_cancelled(other.m_cancelled)
{
other.cancel();
}
~scoped_lambda()
{
if (!m_cancelled)
{
m_func();
}
}
void cancel()
{
m_cancelled = true;
}
void call_now()
{
m_cancelled = true;
m_func();
}
private:
FUNC m_func;
bool m_cancelled;
};
/// A scoped lambda that executes the lambda when the object destructs
/// but only if exiting due to an exception (CALL_ON_FAILURE = true) or
/// only if not exiting due to an exception (CALL_ON_FAILURE = false).
template<typename FUNC, bool CALL_ON_FAILURE>
class conditional_scoped_lambda
{
public:
conditional_scoped_lambda(FUNC&& func)
: m_func(std::forward<FUNC>(func))
, m_uncaughtExceptionCount(std::uncaught_exceptions())
, m_cancelled(false)
{}
conditional_scoped_lambda(const conditional_scoped_lambda& other) = delete;
conditional_scoped_lambda(conditional_scoped_lambda&& other)
noexcept(std::is_nothrow_move_constructible<FUNC>::value)
: m_func(std::forward<FUNC>(other.m_func))
, m_uncaughtExceptionCount(other.m_uncaughtExceptionCount)
, m_cancelled(other.m_cancelled)
{
other.cancel();
}
~conditional_scoped_lambda() noexcept(CALL_ON_FAILURE || noexcept(std::declval<FUNC>()()))
{
if (!m_cancelled && (is_unwinding_due_to_exception() == CALL_ON_FAILURE))
{
m_func();
}
}
void cancel() noexcept
{
m_cancelled = true;
}
private:
bool is_unwinding_due_to_exception() const noexcept
{
return std::uncaught_exceptions() > m_uncaughtExceptionCount;
}
FUNC m_func;
int m_uncaughtExceptionCount;
bool m_cancelled;
};
/// Returns an object that calls the provided function when it goes out
/// of scope either normally or due to an uncaught exception unwinding
/// the stack.
///
/// \param func
/// The function to call when the scope exits.
/// The function must be noexcept.
template<typename FUNC>
auto on_scope_exit(FUNC&& func)
{
return scoped_lambda<FUNC>{ std::forward<FUNC>(func) };
}
/// Returns an object that calls the provided function when it goes out
/// of scope due to an uncaught exception unwinding the stack.
///
/// \param func
/// The function to be called if unwinding due to an exception.
/// The function must be noexcept.
template<typename FUNC>
auto on_scope_failure(FUNC&& func)
{
return conditional_scoped_lambda<FUNC, true>{ std::forward<FUNC>(func) };
}
/// Returns an object that calls the provided function when it goes out
/// of scope via normal execution (ie. not unwinding due to an exception).
///
/// \param func
/// The function to call if the scope exits normally.
/// The function does not necessarily need to be noexcept.
template<typename FUNC>
auto on_scope_success(FUNC&& func)
{
return conditional_scoped_lambda<FUNC, false>{ std::forward<FUNC>(func) };
}
}
#endif