summaryrefslogtreecommitdiffstats
path: root/BrowserSource/Proxy/ScopeGuard.h
blob: 61bb64ddfb052b05981e872f97b2b24c048e4eb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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_;
};