forked from qicosmos/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScopeGuard.hpp
More file actions
37 lines (31 loc) · 715 Bytes
/
ScopeGuard.hpp
File metadata and controls
37 lines (31 loc) · 715 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
#pragma once
template <typename F>
class ScopeGuard
{
public:
explicit ScopeGuard(F && f) : m_func(std::move(f)), m_dismiss(false){}
explicit ScopeGuard(const F& f) : m_func(f), m_dismiss(false){}
~ScopeGuard()
{
if (!m_dismiss&&m_func != nullptr)
m_func();
}
ScopeGuard(ScopeGuard && rhs) : m_func(std::move(rhs.m_func)), m_dismiss(rhs.m_dismiss)
{
rhs.Dismiss();
}
void Dismiss()
{
m_dismiss = true;
}
private:
F m_func;
bool m_dismiss;
ScopeGuard();
ScopeGuard(const ScopeGuard&);
ScopeGuard&operator=(const ScopeGuard&);
};
template <typename F>ScopeGuard<typename std::decay<F>::type> MakeGuard(F && f)
{
return ScopeGuard<typename std::decay<F>::type>(std::forward<F>(f));
}