-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalAction.h
More file actions
38 lines (33 loc) · 832 Bytes
/
FinalAction.h
File metadata and controls
38 lines (33 loc) · 832 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
#ifndef FINAL_ACTION_H
#define FINAL_ACTION_H
#include <utility> //std::move
template <class F>
class FinalAction
{
public:
explicit FinalAction(F f) noexcept : m_function(std::move(f)) {}
FinalAction(FinalAction&& other) noexcept : m_function(std::move(other.m_function)), m_active(other.m_active)
{
other.m_active = false;
}
FinalAction(const FinalAction&) = delete;
FinalAction& operator=(const FinalAction&) = delete;
FinalAction& operator=(FinalAction&&) = delete;
void disable()
{
m_active = false;
}
~FinalAction() noexcept
{
if (m_active) m_function();
}
private:
F m_function;
bool m_active{true};
};
template<class F>
FinalAction<F> finally(F&& f) noexcept
{
return FinalAction<F>(std::forward<F>(f));
}
#endif // FINAL_ACTION_H