blob: 2a232619a8b6550d1c7f228ceb1d06f2525efe6a (
plain)
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
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#ifndef DEFER
#define DEFER
#include <functional>
#include <utility>
namespace icinga
{
/**
* An action to be executed at end of scope.
*
* @ingroup base
*/
class Defer
{
public:
inline
Defer(std::function<void()> func) : m_Func(std::move(func))
{
}
Defer(const Defer&) = delete;
Defer(Defer&&) = delete;
Defer& operator=(const Defer&) = delete;
Defer& operator=(Defer&&) = delete;
inline
~Defer()
{
if (m_Func) {
try {
m_Func();
} catch (...) {
// https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor
}
}
}
inline
void Cancel()
{
m_Func = nullptr;
}
private:
std::function<void()> m_Func;
};
}
#endif /* DEFER */
|