Defer: Allow to cancel the callback before going out of scope

This commit is contained in:
Yonas Habteab 2022-03-08 10:24:47 +01:00
parent d171301b9d
commit 575af4c980
1 changed files with 12 additions and 4 deletions

View File

@ -30,13 +30,21 @@ public:
inline
~Defer()
{
try {
m_Func();
} catch (...) {
// https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor
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;
};