Common: ScopeGuard -> ScopedGuard

This commit is contained in:
Connor McLaughlin 2022-07-26 18:37:16 +10:00
parent 13e3f2a179
commit 8af4f4f01a
11 changed files with 55 additions and 61 deletions

View File

@ -47,7 +47,7 @@ add_library(common
progress_callback.cpp progress_callback.cpp
progress_callback.h progress_callback.h
rectangle.h rectangle.h
scope_guard.h scoped_guard.h
settings_interface.h settings_interface.h
string.cpp string.cpp
string.h string.h

View File

@ -56,7 +56,7 @@
<ClInclude Include="platform.h" /> <ClInclude Include="platform.h" />
<ClInclude Include="progress_callback.h" /> <ClInclude Include="progress_callback.h" />
<ClInclude Include="rectangle.h" /> <ClInclude Include="rectangle.h" />
<ClInclude Include="scope_guard.h" /> <ClInclude Include="scoped_guard.h" />
<ClInclude Include="settings_interface.h" /> <ClInclude Include="settings_interface.h" />
<ClInclude Include="string.h" /> <ClInclude Include="string.h" />
<ClInclude Include="heterogeneous_containers.h" /> <ClInclude Include="heterogeneous_containers.h" />

View File

@ -75,7 +75,6 @@
<Filter>vulkan</Filter> <Filter>vulkan</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="dimensional_array.h" /> <ClInclude Include="dimensional_array.h" />
<ClInclude Include="scope_guard.h" />
<ClInclude Include="vulkan\context.h"> <ClInclude Include="vulkan\context.h">
<Filter>vulkan</Filter> <Filter>vulkan</Filter>
</ClInclude> </ClInclude>
@ -138,6 +137,7 @@
<ClInclude Include="heterogeneous_containers.h" /> <ClInclude Include="heterogeneous_containers.h" />
<ClInclude Include="memory_settings_interface.h" /> <ClInclude Include="memory_settings_interface.h" />
<ClInclude Include="threading.h" /> <ClInclude Include="threading.h" />
<ClInclude Include="scoped_guard.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="gl\program.cpp"> <ClCompile Include="gl\program.cpp">

View File

@ -5,7 +5,7 @@
#include "context.h" #include "context.h"
#include "../assert.h" #include "../assert.h"
#include "../log.h" #include "../log.h"
#include "../scope_guard.h" #include "../scoped_guard.h"
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <dxgi1_2.h> #include <dxgi1_2.h>

View File

@ -1,7 +1,7 @@
#include "context_wgl.h" #include "context_wgl.h"
#include "../assert.h" #include "../assert.h"
#include "../log.h" #include "../log.h"
#include "../scope_guard.h" #include "../scoped_guard.h"
#include "loader.h" #include "loader.h"
Log_SetChannel(GL::ContextWGL); Log_SetChannel(GL::ContextWGL);
@ -295,13 +295,13 @@ bool ContextWGL::CreatePBuffer()
return false; return false;
} }
Common::ScopeGuard hwnd_guard([hwnd]() { DestroyWindow(hwnd); }); ScopedGuard hwnd_guard([hwnd]() { DestroyWindow(hwnd); });
HDC hdc = GetDCAndSetPixelFormat(hwnd); HDC hdc = GetDCAndSetPixelFormat(hwnd);
if (!hdc) if (!hdc)
return false; 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}; static constexpr const int pb_attribs[] = {0, 0};
@ -313,7 +313,7 @@ bool ContextWGL::CreatePBuffer()
return false; return false;
} }
Common::ScopeGuard pbuffer_guard([pbuffer]() { wglDestroyPbufferARB(pbuffer); }); ScopedGuard pbuffer_guard([pbuffer]() { wglDestroyPbufferARB(pbuffer); });
m_dc = wglGetPbufferDCARB(pbuffer); m_dc = wglGetPbufferDCARB(pbuffer);
if (!m_dc) if (!m_dc)
@ -326,9 +326,9 @@ bool ContextWGL::CreatePBuffer()
m_dummy_dc = hdc; m_dummy_dc = hdc;
m_pbuffer = pbuffer; m_pbuffer = pbuffer;
pbuffer_guard.Dismiss(); pbuffer_guard.Cancel();
hdc_guard.Dismiss(); hdc_guard.Cancel();
hwnd_guard.Dismiss(); hwnd_guard.Cancel();
return true; return true;
} }

View File

@ -3,7 +3,7 @@
#include "file_system.h" #include "file_system.h"
#include "log.h" #include "log.h"
#include "path.h" #include "path.h"
#include "scope_guard.h" #include "scoped_guard.h"
#include "stb_image.h" #include "stb_image.h"
#include "stb_image_write.h" #include "stb_image_write.h"
#include "string_util.h" #include "string_util.h"

View File

@ -1,41 +0,0 @@
// 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

35
src/common/scoped_guard.h Normal file
View File

@ -0,0 +1,35 @@
#pragma once
#include "types.h"
#include <optional>
#include <utility>
/// 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<typename T>
class ScopedGuard final
{
public:
ALWAYS_INLINE ScopedGuard(T&& func) : m_func(std::forward<T>(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<T> m_func;
};

View File

@ -7,7 +7,7 @@
#include "common/d3d12/shader_cache.h" #include "common/d3d12/shader_cache.h"
#include "common/d3d12/util.h" #include "common/d3d12/util.h"
#include "common/log.h" #include "common/log.h"
#include "common/scope_guard.h" #include "common/scoped_guard.h"
#include "common/timer.h" #include "common/timer.h"
#include "gpu_hw_shadergen.h" #include "gpu_hw_shadergen.h"
#include "host_display.h" #include "host_display.h"

View File

@ -1,7 +1,7 @@
#include "gpu_hw_vulkan.h" #include "gpu_hw_vulkan.h"
#include "common/assert.h" #include "common/assert.h"
#include "common/log.h" #include "common/log.h"
#include "common/scope_guard.h" #include "common/scoped_guard.h"
#include "common/timer.h" #include "common/timer.h"
#include "common/vulkan/builders.h" #include "common/vulkan/builders.h"
#include "common/vulkan/context.h" #include "common/vulkan/context.h"
@ -933,7 +933,7 @@ bool GPU_HW_Vulkan::CompilePipelines()
// fragment shaders - [render_mode][texture_mode][dithering][interlacing] // fragment shaders - [render_mode][texture_mode][dithering][interlacing]
DimensionalArray<VkShaderModule, 2> batch_vertex_shaders{}; DimensionalArray<VkShaderModule, 2> batch_vertex_shaders{};
DimensionalArray<VkShaderModule, 2, 2, 9, 4> batch_fragment_shaders{}; DimensionalArray<VkShaderModule, 2, 2, 9, 4> 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_vertex_shaders.enumerate(Vulkan::Util::SafeDestroyShaderModule);
batch_fragment_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 = VkShaderModule fullscreen_quad_vertex_shader =
g_vulkan_shader_cache->GetVertexShader(shadergen.GenerateScreenQuadVertexShader()); g_vulkan_shader_cache->GetVertexShader(shadergen.GenerateScreenQuadVertexShader());
@ -1081,7 +1081,7 @@ bool GPU_HW_Vulkan::CompilePipelines()
progress.Increment(); 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(), fullscreen_quad_vertex_shader, nullptr);
vkDestroyShaderModule(g_vulkan_context->GetDevice(), uv_quad_vertex_shader, nullptr); vkDestroyShaderModule(g_vulkan_context->GetDevice(), uv_quad_vertex_shader, nullptr);
}); });

View File

@ -1,7 +1,7 @@
#include "vulkan_host_display.h" #include "vulkan_host_display.h"
#include "common/assert.h" #include "common/assert.h"
#include "common/log.h" #include "common/log.h"
#include "common/scope_guard.h" #include "common/scoped_guard.h"
#include "common/vulkan/builders.h" #include "common/vulkan/builders.h"
#include "common/vulkan/context.h" #include "common/vulkan/context.h"
#include "common/vulkan/shader_cache.h" #include "common/vulkan/shader_cache.h"
@ -890,12 +890,12 @@ HostDisplay::AdapterAndModeList VulkanHostDisplay::StaticGetAdapterAndModeList(c
} }
else if (Vulkan::LoadVulkanLibrary()) else if (Vulkan::LoadVulkanLibrary())
{ {
Common::ScopeGuard lib_guard([]() { Vulkan::UnloadVulkanLibrary(); }); ScopedGuard lib_guard([]() { Vulkan::UnloadVulkanLibrary(); });
VkInstance instance = Vulkan::Context::CreateVulkanInstance(nullptr, false, false); VkInstance instance = Vulkan::Context::CreateVulkanInstance(nullptr, false, false);
if (instance != VK_NULL_HANDLE) if (instance != VK_NULL_HANDLE)
{ {
Common::ScopeGuard instance_guard([&instance]() { vkDestroyInstance(instance, nullptr); }); ScopedGuard instance_guard([&instance]() { vkDestroyInstance(instance, nullptr); });
if (Vulkan::LoadVulkanInstanceFunctions(instance)) if (Vulkan::LoadVulkanInstanceFunctions(instance))
ret.adapter_names = Vulkan::Context::EnumerateGPUNames(instance); ret.adapter_names = Vulkan::Context::EnumerateGPUNames(instance);