Debian Codegraph @

Source/Core/Common/ScopeGuard.h

raw symbols SCIP
1 // Copyright 2015 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <optional>
7
8 namespace Common
9 {
10 template <typename Callable>
11 class ScopeGuard final
12 {
13 public:
14 ScopeGuard(Callable&& finalizer) : m_finalizer(std::forward<Callable>(finalizer)) {}
15
16 ScopeGuard(ScopeGuard&& other) : m_finalizer(std::move(other.m_finalizer)) { other.Dismiss(); }
17
18 ~ScopeGuard() { Exit(); }
19 void Dismiss() { m_finalizer.reset(); }
20 void Exit()
21 {
22 if (m_finalizer)
23 {
24 (*m_finalizer)(); // must not throw
25 m_finalizer.reset();
26 }
27 }
28
29 ScopeGuard(const ScopeGuard&) = delete;
30
31 void operator=(const ScopeGuard&) = delete;
32
33 private:
34 std::optional<Callable> m_finalizer;
35 };
36
37 } // Namespace Common