From 575af4c98031c9d0833394e118e7de7faf5573a3 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Tue, 8 Mar 2022 10:24:47 +0100 Subject: [PATCH] Defer: Allow to cancel the callback before going out of scope --- lib/base/defer.hpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/base/defer.hpp b/lib/base/defer.hpp index 9290c92b9..2a232619a 100644 --- a/lib/base/defer.hpp +++ b/lib/base/defer.hpp @@ -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 m_Func; };