From 8af4f4f01a852f60265436c7f50c95997d909d2b Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 26 Jul 2022 18:37:16 +1000 Subject: [PATCH] Common: ScopeGuard -> ScopedGuard --- src/common/CMakeLists.txt | 2 +- src/common/common.vcxproj | 2 +- src/common/common.vcxproj.filters | 2 +- src/common/d3d12/context.cpp | 2 +- src/common/gl/context_wgl.cpp | 14 +++---- src/common/image.cpp | 2 +- src/common/scope_guard.h | 41 --------------------- src/common/scoped_guard.h | 35 ++++++++++++++++++ src/core/gpu_hw_d3d12.cpp | 2 +- src/core/gpu_hw_vulkan.cpp | 8 ++-- src/frontend-common/vulkan_host_display.cpp | 6 +-- 11 files changed, 55 insertions(+), 61 deletions(-) delete mode 100644 src/common/scope_guard.h create mode 100644 src/common/scoped_guard.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index f031b5cde..fec06d1df 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -47,7 +47,7 @@ add_library(common progress_callback.cpp progress_callback.h rectangle.h - scope_guard.h + scoped_guard.h settings_interface.h string.cpp string.h diff --git a/src/common/common.vcxproj b/src/common/common.vcxproj index 5a905689e..7fb3bf774 100644 --- a/src/common/common.vcxproj +++ b/src/common/common.vcxproj @@ -56,7 +56,7 @@ - + diff --git a/src/common/common.vcxproj.filters b/src/common/common.vcxproj.filters index cec6b11db..fec7d08ea 100644 --- a/src/common/common.vcxproj.filters +++ b/src/common/common.vcxproj.filters @@ -75,7 +75,6 @@ vulkan - vulkan @@ -138,6 +137,7 @@ + diff --git a/src/common/d3d12/context.cpp b/src/common/d3d12/context.cpp index 6e29ee405..5e3b4dc6d 100644 --- a/src/common/d3d12/context.cpp +++ b/src/common/d3d12/context.cpp @@ -5,7 +5,7 @@ #include "context.h" #include "../assert.h" #include "../log.h" -#include "../scope_guard.h" +#include "../scoped_guard.h" #include #include #include diff --git a/src/common/gl/context_wgl.cpp b/src/common/gl/context_wgl.cpp index c640b2930..ebf6ed8f5 100644 --- a/src/common/gl/context_wgl.cpp +++ b/src/common/gl/context_wgl.cpp @@ -1,7 +1,7 @@ #include "context_wgl.h" #include "../assert.h" #include "../log.h" -#include "../scope_guard.h" +#include "../scoped_guard.h" #include "loader.h" Log_SetChannel(GL::ContextWGL); @@ -295,13 +295,13 @@ bool ContextWGL::CreatePBuffer() return false; } - Common::ScopeGuard hwnd_guard([hwnd]() { DestroyWindow(hwnd); }); + ScopedGuard hwnd_guard([hwnd]() { DestroyWindow(hwnd); }); HDC hdc = GetDCAndSetPixelFormat(hwnd); if (!hdc) return false; - Common::ScopeGuard hdc_guard([hdc, hwnd]() { ::ReleaseDC(hwnd, hdc); }); + ScopedGuard hdc_guard([hdc, hwnd]() { ::ReleaseDC(hwnd, hdc); }); static constexpr const int pb_attribs[] = {0, 0}; @@ -313,7 +313,7 @@ bool ContextWGL::CreatePBuffer() return false; } - Common::ScopeGuard pbuffer_guard([pbuffer]() { wglDestroyPbufferARB(pbuffer); }); + ScopedGuard pbuffer_guard([pbuffer]() { wglDestroyPbufferARB(pbuffer); }); m_dc = wglGetPbufferDCARB(pbuffer); if (!m_dc) @@ -326,9 +326,9 @@ bool ContextWGL::CreatePBuffer() m_dummy_dc = hdc; m_pbuffer = pbuffer; - pbuffer_guard.Dismiss(); - hdc_guard.Dismiss(); - hwnd_guard.Dismiss(); + pbuffer_guard.Cancel(); + hdc_guard.Cancel(); + hwnd_guard.Cancel(); return true; } diff --git a/src/common/image.cpp b/src/common/image.cpp index 6e743f302..82f84e48e 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -3,7 +3,7 @@ #include "file_system.h" #include "log.h" #include "path.h" -#include "scope_guard.h" +#include "scoped_guard.h" #include "stb_image.h" #include "stb_image_write.h" #include "string_util.h" diff --git a/src/common/scope_guard.h b/src/common/scope_guard.h deleted file mode 100644 index c1cad06dd..000000000 --- a/src/common/scope_guard.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2015 Dolphin Emulator Project -// Licensed under GPLv2+ -// Refer to the license.txt file included. - -#pragma once - -#include - -namespace Common -{ -template -class ScopeGuard final -{ -public: - ScopeGuard(Callable&& finalizer) : m_finalizer(std::forward(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 m_finalizer; -}; - -} // Namespace Common diff --git a/src/common/scoped_guard.h b/src/common/scoped_guard.h new file mode 100644 index 000000000..25ab6fd49 --- /dev/null +++ b/src/common/scoped_guard.h @@ -0,0 +1,35 @@ +#pragma once +#include "types.h" +#include +#include + +/// ScopedGuard provides an object which runs a function (usually a lambda) when +/// it goes out of scope. This can be useful for releasing resources or handles +/// which do not normally have C++ types to automatically release. +template +class ScopedGuard final +{ +public: + ALWAYS_INLINE ScopedGuard(T&& func) : m_func(std::forward(func)) {} + ALWAYS_INLINE ScopedGuard(ScopedGuard&& other) : m_func(std::move(other.m_func)) { other.m_func = nullptr; } + ALWAYS_INLINE ~ScopedGuard() { Invoke(); } + + ScopedGuard(const ScopedGuard&) = delete; + void operator=(const ScopedGuard&) = delete; + + /// Prevents the function from being invoked when we go out of scope. + ALWAYS_INLINE void Cancel() { m_func.reset(); } + + /// Explicitly fires the function. + ALWAYS_INLINE void Invoke() + { + if (!m_func.has_value()) + return; + + m_func.value()(); + m_func.reset(); + } + +private: + std::optional m_func; +}; diff --git a/src/core/gpu_hw_d3d12.cpp b/src/core/gpu_hw_d3d12.cpp index 357fb4c69..9af86083d 100644 --- a/src/core/gpu_hw_d3d12.cpp +++ b/src/core/gpu_hw_d3d12.cpp @@ -7,7 +7,7 @@ #include "common/d3d12/shader_cache.h" #include "common/d3d12/util.h" #include "common/log.h" -#include "common/scope_guard.h" +#include "common/scoped_guard.h" #include "common/timer.h" #include "gpu_hw_shadergen.h" #include "host_display.h" diff --git a/src/core/gpu_hw_vulkan.cpp b/src/core/gpu_hw_vulkan.cpp index 057893c4b..ab7fc5043 100644 --- a/src/core/gpu_hw_vulkan.cpp +++ b/src/core/gpu_hw_vulkan.cpp @@ -1,7 +1,7 @@ #include "gpu_hw_vulkan.h" #include "common/assert.h" #include "common/log.h" -#include "common/scope_guard.h" +#include "common/scoped_guard.h" #include "common/timer.h" #include "common/vulkan/builders.h" #include "common/vulkan/context.h" @@ -933,7 +933,7 @@ bool GPU_HW_Vulkan::CompilePipelines() // fragment shaders - [render_mode][texture_mode][dithering][interlacing] DimensionalArray batch_vertex_shaders{}; DimensionalArray batch_fragment_shaders{}; - Common::ScopeGuard batch_shader_guard([&batch_vertex_shaders, &batch_fragment_shaders]() { + ScopedGuard batch_shader_guard([&batch_vertex_shaders, &batch_fragment_shaders]() { batch_vertex_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule); batch_fragment_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule); }); @@ -1066,7 +1066,7 @@ bool GPU_HW_Vulkan::CompilePipelines() } } - batch_shader_guard.Exit(); + batch_shader_guard.Invoke(); VkShaderModule fullscreen_quad_vertex_shader = g_vulkan_shader_cache->GetVertexShader(shadergen.GenerateScreenQuadVertexShader()); @@ -1081,7 +1081,7 @@ bool GPU_HW_Vulkan::CompilePipelines() progress.Increment(); - Common::ScopeGuard fullscreen_quad_vertex_shader_guard([&fullscreen_quad_vertex_shader, &uv_quad_vertex_shader]() { + ScopedGuard fullscreen_quad_vertex_shader_guard([&fullscreen_quad_vertex_shader, &uv_quad_vertex_shader]() { vkDestroyShaderModule(g_vulkan_context->GetDevice(), fullscreen_quad_vertex_shader, nullptr); vkDestroyShaderModule(g_vulkan_context->GetDevice(), uv_quad_vertex_shader, nullptr); }); diff --git a/src/frontend-common/vulkan_host_display.cpp b/src/frontend-common/vulkan_host_display.cpp index 78b1c4b03..66075ef6a 100644 --- a/src/frontend-common/vulkan_host_display.cpp +++ b/src/frontend-common/vulkan_host_display.cpp @@ -1,7 +1,7 @@ #include "vulkan_host_display.h" #include "common/assert.h" #include "common/log.h" -#include "common/scope_guard.h" +#include "common/scoped_guard.h" #include "common/vulkan/builders.h" #include "common/vulkan/context.h" #include "common/vulkan/shader_cache.h" @@ -890,12 +890,12 @@ HostDisplay::AdapterAndModeList VulkanHostDisplay::StaticGetAdapterAndModeList(c } else if (Vulkan::LoadVulkanLibrary()) { - Common::ScopeGuard lib_guard([]() { Vulkan::UnloadVulkanLibrary(); }); + ScopedGuard lib_guard([]() { Vulkan::UnloadVulkanLibrary(); }); VkInstance instance = Vulkan::Context::CreateVulkanInstance(nullptr, false, false); if (instance != VK_NULL_HANDLE) { - Common::ScopeGuard instance_guard([&instance]() { vkDestroyInstance(instance, nullptr); }); + ScopedGuard instance_guard([&instance]() { vkDestroyInstance(instance, nullptr); }); if (Vulkan::LoadVulkanInstanceFunctions(instance)) ret.adapter_names = Vulkan::Context::EnumerateGPUNames(instance);