aboutsummaryrefslogtreecommitdiff
path: root/app/source/Cplt/Utils/ScopeGuard.hpp
blob: e7db2a46fe3655d540f71fbfacaad88da0cd9114 (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
33
34
35
#pragma once

#include <Cplt/Utils/Macros.hpp>

#include <utility>

template <class TCleanupFunc>
class ScopeGuard {
private:
	TCleanupFunc mFunc;
	bool mDismissed = false;

public:
	/// Specifically left this implicit so that constructs like
	/// \code
	/// ScopeGuard sg = [&]() { res.Cleanup(); };
	/// \endcode
	/// would work. It is highly discourage and unlikely that one would want to use ScopeGuard as a function
	/// parameter, so the normal argument that implicit conversion are harmful doesn't really apply here.
	ScopeGuard(TCleanupFunc func)
		: mFunc{ std::move(func) } {
	}

	~ScopeGuard() {
		if (!mDismissed) {
			mFunc();
		}
	}

	void Dismiss() noexcept {
		mDismissed = true;
	}
};

#define DEFER ScopeGuard UNIQUE_NAME(scopeGuard) = [&]()