diff options
| author | JosJuice <josjuice@gmail.com> | 2020-02-17 12:40:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-17 12:40:10 +0100 |
| commit | 9cfe7f47f1a19eaa5fcd72774412e02e891cf985 (patch) | |
| tree | 02b2ccf4bc1edbf2f2e44c63a76d6e8d5285fb96 /Source/Core | |
| parent | 62046d93ce089659b49a1a5715c08fedf2af09a9 (diff) | |
| parent | 44b4c2db49f9d3283e04d4c34b5f4d22e66d0af4 (diff) | |
Merge pull request #8630 from leoetlino/scopeguard
Common: Avoid std::function overhead in ScopeGuard
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Common/ScopeGuard.h | 16 | ||||
| -rw-r--r-- | Source/Core/Core/Core.cpp | 6 |
2 files changed, 10 insertions, 12 deletions
diff --git a/Source/Core/Common/ScopeGuard.h b/Source/Core/Common/ScopeGuard.h index d95ef8e4ae..c1cad06dd7 100644 --- a/Source/Core/Common/ScopeGuard.h +++ b/Source/Core/Common/ScopeGuard.h @@ -4,17 +4,15 @@ #pragma once -#include <functional> +#include <optional> namespace Common { +template <typename Callable> class ScopeGuard final { public: - template <class Callable> - ScopeGuard(Callable&& finalizer) : m_finalizer(std::forward<Callable>(finalizer)) - { - } + ScopeGuard(Callable&& finalizer) : m_finalizer(std::forward<Callable>(finalizer)) {} ScopeGuard(ScopeGuard&& other) : m_finalizer(std::move(other.m_finalizer)) { @@ -22,13 +20,13 @@ public: } ~ScopeGuard() { Exit(); } - void Dismiss() { m_finalizer = nullptr; } + void Dismiss() { m_finalizer.reset(); } void Exit() { if (m_finalizer) { - m_finalizer(); // must not throw - m_finalizer = nullptr; + (*m_finalizer)(); // must not throw + m_finalizer.reset(); } } @@ -37,7 +35,7 @@ public: void operator=(const ScopeGuard&) = delete; private: - std::function<void()> m_finalizer; + std::optional<Callable> m_finalizer; }; } // Namespace Common diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp index ea5954e90a..dfad2b100e 100644 --- a/Source/Core/Core/Core.cpp +++ b/Source/Core/Core/Core.cpp @@ -434,7 +434,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi s_frame_step = false; Movie::Init(*boot); - Common::ScopeGuard movie_guard{Movie::Shutdown}; + Common::ScopeGuard movie_guard{&Movie::Shutdown}; HW::Init(); @@ -539,7 +539,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi }}; AudioCommon::InitSoundStream(); - Common::ScopeGuard audio_guard{AudioCommon::ShutdownSoundStream}; + Common::ScopeGuard audio_guard{&AudioCommon::ShutdownSoundStream}; // The hardware is initialized. s_hardware_initialized = true; @@ -565,7 +565,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi // Initialise Wii filesystem contents. // This is done here after Boot and not in HW to ensure that we operate // with the correct title context since save copying requires title directories to exist. - Common::ScopeGuard wiifs_guard{Core::CleanUpWiiFileSystemContents}; + Common::ScopeGuard wiifs_guard{&Core::CleanUpWiiFileSystemContents}; if (SConfig::GetInstance().bWii) Core::InitializeWiiFileSystemContents(); else |
