mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2024-12-29 05:50:51 +01:00
44b4c2db49
So far in all our uses of ScopeGuard, the type of the callable is usually just a lambda or a function pointer, so there is no need to rely on std::function's type erasure. While the cost of using std::function is probably negligible, it still causes some unnecessary overhead that can be avoided by making ScopeGuard a templated class. Thanks to class template argument deduction in C++17 most existing usages do not even need to be changed. See https://godbolt.org/z/KcoPni for a comparison between a ScopeGuard that uses std::function and one that doesn't
42 lines
796 B
C++
42 lines
796 B
C++
// Copyright 2015 Dolphin Emulator Project
|
|
// Licensed under GPLv2+
|
|
// Refer to the license.txt file included.
|
|
|
|
#pragma once
|
|
|
|
#include <optional>
|
|
|
|
namespace Common
|
|
{
|
|
template <typename Callable>
|
|
class ScopeGuard final
|
|
{
|
|
public:
|
|
ScopeGuard(Callable&& finalizer) : m_finalizer(std::forward<Callable>(finalizer)) {}
|
|
|
|
ScopeGuard(ScopeGuard&& other) : m_finalizer(std::move(other.m_finalizer))
|
|
{
|
|
other.m_finalizer = nullptr;
|
|
}
|
|
|
|
~ScopeGuard() { Exit(); }
|
|
void Dismiss() { m_finalizer.reset(); }
|
|
void Exit()
|
|
{
|
|
if (m_finalizer)
|
|
{
|
|
(*m_finalizer)(); // must not throw
|
|
m_finalizer.reset();
|
|
}
|
|
}
|
|
|
|
ScopeGuard(const ScopeGuard&) = delete;
|
|
|
|
void operator=(const ScopeGuard&) = delete;
|
|
|
|
private:
|
|
std::optional<Callable> m_finalizer;
|
|
};
|
|
|
|
} // Namespace Common
|