Explorar o código

common: scope_exit: Implement mechanism for canceling a scope exit.

bunnei %!s(int64=6) %!d(string=hai) anos
pai
achega
4df6ef04ac
Modificáronse 1 ficheiros con 8 adicións e 1 borrados
  1. 8 1
      src/common/scope_exit.h

+ 8 - 1
src/common/scope_exit.h

@@ -12,10 +12,17 @@ template <typename Func>
 struct ScopeExitHelper {
     explicit ScopeExitHelper(Func&& func) : func(std::move(func)) {}
     ~ScopeExitHelper() {
-        func();
+        if (active) {
+            func();
+        }
+    }
+
+    void Cancel() {
+        active = false;
     }
 
     Func func;
+    bool active{true};
 };
 
 template <typename Func>