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