From 6ad955ae0aab8b79910cb4a12777419a78a42a90 Mon Sep 17 00:00:00 2001 From: Jack <66967891+ASpoonPlaysGames@users.noreply.github.com> Date: Tue, 30 Jan 2024 22:06:40 +0000 Subject: ScopeGuard improvements (#651) `std::function` introduced a layer of indirection that can be removed through templating the class. --- primedev/util/utils.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'primedev/util') diff --git a/primedev/util/utils.h b/primedev/util/utils.h index 1a419607..c8cbc7e8 100644 --- a/primedev/util/utils.h +++ b/primedev/util/utils.h @@ -2,22 +2,24 @@ void RemoveAsciiControlSequences(char* str, bool allow_color_codes); -class ScopeGuard +template class ScopeGuard { public: auto operator=(ScopeGuard&) = delete; ScopeGuard(ScopeGuard&) = delete; - ScopeGuard(std::function callback) : m_callback(callback) {} + ScopeGuard(T callback) : m_callback(callback) {} ~ScopeGuard() { - m_callback(); + if (!m_dismissed) + m_callback(); } void Dismiss() { - m_callback = [] {}; + m_dismissed = true; } private: - std::function m_callback; + bool m_dismissed = false; + T m_callback; }; -- cgit v1.2.3