-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScopeGuard.mpp
More file actions
46 lines (39 loc) · 927 Bytes
/
ScopeGuard.mpp
File metadata and controls
46 lines (39 loc) · 927 Bytes
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
export module CppUtils.Execution.ScopeGuard;
import std;
export namespace CppUtils::Execution
{
template<class Function>
class ScopeGuard final
{
public:
template<class F>
requires std::is_constructible_v<Function, F>
explicit constexpr ScopeGuard(F&& onExit) noexcept(std::is_nothrow_constructible_v<Function, F>):
m_onExit{std::forward<F>(onExit)}
{}
ScopeGuard(const ScopeGuard&) = delete;
auto operator=(const ScopeGuard&) -> ScopeGuard& = delete;
constexpr ~ScopeGuard() noexcept
{
if (m_active)
{
if constexpr (requires { static_cast<bool>(m_onExit); })
{
if (m_onExit)
std::invoke(m_onExit);
}
else
std::invoke(m_onExit);
}
}
constexpr void dismiss() noexcept
{
m_active = false;
}
private:
[[no_unique_address]] Function m_onExit;
bool m_active = true;
};
template<class Function>
ScopeGuard(Function) -> ScopeGuard<Function>;
}