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 inline
~Defer() ~Defer()
{ {
try { if (m_Func) {
m_Func(); try {
} catch (...) { m_Func();
// https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor } catch (...) {
// https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor
}
} }
} }
inline
void Cancel()
{
m_Func = nullptr;
}
private: private:
std::function<void()> m_Func; std::function<void()> m_Func;
}; };