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 aaccd0448f
commit 236a888c1b

View File

@ -30,12 +30,20 @@ public:
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;