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/Common | |
| 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/Common')
| -rw-r--r-- | Source/Core/Common/ScopeGuard.h | 16 |
1 files changed, 7 insertions, 9 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 |
