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.h
rectangle.h
scope_guard.h
scoped_guard.h
settings_interface.h
string.cpp
string.h

View file

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

View file

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

View file

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

View file

@ -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;
}

View file

@ -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"

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/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"

View file

@ -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<VkShaderModule, 2> batch_vertex_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_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);
});

View file

@ -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);