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