summaryrefslogtreecommitdiffstats
path: root/BrowserSource/Proxy/ScopeGuard.h
diff options
context:
space:
mode:
Diffstat (limited to 'BrowserSource/Proxy/ScopeGuard.h')
-rw-r--r--BrowserSource/Proxy/ScopeGuard.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/BrowserSource/Proxy/ScopeGuard.h b/BrowserSource/Proxy/ScopeGuard.h
new file mode 100644
index 0000000..61bb64d
--- /dev/null
+++ b/BrowserSource/Proxy/ScopeGuard.h
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <functional>
+#include <utility>
+
+class ScopeGuard {
+public:
+ ScopeGuard(std::function<void()>&& cb) : cb_(std::move(cb)), active_(true) {}
+ ~ScopeGuard() {
+ Invoke();
+ }
+
+ ScopeGuard() = delete;
+ ScopeGuard(ScopeGuard&) = delete;
+ ScopeGuard(const ScopeGuard&) = delete;
+ ScopeGuard(ScopeGuard&&) = delete;
+ ScopeGuard& operator=(ScopeGuard&) = delete;
+ ScopeGuard& operator=(const ScopeGuard&) = delete;
+
+ void Cancel() { active_ = false; }
+
+ void Invoke() {
+ if (active_) {
+ cb_();
+ active_ = false;
+ }
+ }
+
+private:
+ const std::function<void()> cb_;
+ bool active_;
+};