Add cancel() to ScopeGuard. v1.8.94
authorCarl Hetherington <cth@carlh.net>
Sat, 13 Jan 2024 12:34:46 +0000 (13:34 +0100)
committerCarl Hetherington <cth@carlh.net>
Sat, 13 Jan 2024 12:34:46 +0000 (13:34 +0100)
src/scope_guard.h

index 37b963adaf5db166e63ffd0d515a491ec64ea2cf..d422528c801d150e3198bb9b21e889f91ce399e6 100644 (file)
@@ -61,11 +61,19 @@ public:
 
        ~ScopeGuard()
        {
-               _function();
+               if (!_cancelled) {
+                       _function();
+               }
+       }
+
+       void cancel()
+       {
+               _cancelled = true;
        }
 
 private:
        std::function<void()> _function;
+       bool _cancelled = false;
 };