summaryrefslogtreecommitdiff
path: root/core/src/Utils/ScopeGuard.hpp
blob: 28ffd0b11c4ef9aec87beba8411bd52a2a374cb9 (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
36
37
38
39
#pragma once

#include "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) = [&]()