mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-26 23:24:09 +02:00
Backport Defer class for 2.10
This commit is contained in:
parent
32dc61a788
commit
7e87a61a62
46
lib/base/defer.hpp
Normal file
46
lib/base/defer.hpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/* 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()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
m_Func();
|
||||||
|
} catch (...) {
|
||||||
|
// https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::function<void()> m_Func;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* DEFER */
|
Loading…
x
Reference in New Issue
Block a user