Skip to content

Commit 7e89a32

Browse files
Rename to scope_exit_function
1 parent b05238e commit 7e89a32

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

include/beman/scope/scope.hpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,23 @@ struct ExecuteAlways;
8282

8383
//=========================================================
8484

85-
template <scope_exit_function ExitFunc, scope_invoke_checker InvokeChecker = ExecuteAlways>
85+
86+
template <scope_exit_function ScopeExitFunc, scope_invoke_checker InvokeChecker = ExecuteAlways>
8687
class scope_guard {
8788
public:
88-
explicit constexpr scope_guard(ExitFunc&& exit_func) noexcept(std::is_nothrow_constructible_v<ExitFunc>) //
89-
try
90-
: m_exit_func(std::forward<ExitFunc>(exit_func)) //
89+
explicit constexpr scope_guard(ScopeExitFunc&& exit_func) noexcept(
90+
std::is_nothrow_constructible_v<ScopeExitFunc>) try
91+
: m_exit_func(std::forward<ScopeExitFunc>(exit_func)) //
9192
{
9293
} catch (...) {
93-
m_exit_func();
94+
if constexpr (InvokeChecker::invoke_on_constructor_exception) {
95+
exit_func();
96+
}
9497
}
9598

96-
explicit constexpr scope_guard(ExitFunc&& exit_func, InvokeChecker&& invoke_checker) noexcept(
97-
std::is_nothrow_constructible_v<ExitFunc> && std::is_nothrow_constructible_v<InvokeChecker>)
98-
: m_exit_func(std::forward<ExitFunc>(exit_func)),
99+
explicit constexpr scope_guard(ScopeExitFunc&& exit_func, InvokeChecker&& invoke_checker) noexcept(
100+
std::is_nothrow_constructible_v<ScopeExitFunc> && std::is_nothrow_constructible_v<InvokeChecker>)
101+
: m_exit_func(std::forward<ScopeExitFunc>(exit_func)),
99102
m_invoke_checker{std::forward<InvokeChecker>(invoke_checker)} //
100103
{}
101104

@@ -125,7 +128,7 @@ class scope_guard {
125128
}
126129

127130
private:
128-
[[no_unique_address]] ExitFunc m_exit_func;
131+
[[no_unique_address]] ScopeExitFunc m_exit_func;
129132
[[no_unique_address]] InvokeChecker m_invoke_checker;
130133

131134
template <typename T>
@@ -153,11 +156,15 @@ scope_guard(ExitFunc&&, InvokeChecker&&) -> scope_guard<ExitFunc, InvokeChecker>
153156
//=========================================================
154157

155158
struct ExecuteAlways {
159+
static constexpr bool invoke_on_constructor_exception = true;
160+
156161
[[nodiscard]] static constexpr bool can_invoke() { return true; }
157162
};
158163

159164
class ExecuteWhenNoException {
160165
public:
166+
static constexpr bool invoke_on_constructor_exception = false;
167+
161168
[[nodiscard]] bool can_invoke() const { return m_uncaught_on_creation >= std::uncaught_exceptions(); }
162169

163170
private:
@@ -166,6 +173,8 @@ class ExecuteWhenNoException {
166173

167174
class ExecuteOnlyWhenException {
168175
public:
176+
static constexpr bool invoke_on_constructor_exception = true;
177+
169178
[[nodiscard]] bool can_invoke() const { return m_uncaught_on_creation < std::uncaught_exceptions(); }
170179

171180
private:

0 commit comments

Comments
 (0)