mirror of https://github.com/PCSX2/pcsx2.git
Common: Add ScopedGuard
This commit is contained in:
parent
52eab49359
commit
de8b4f17a1
|
@ -76,6 +76,7 @@ target_sources(common PRIVATE
|
||||||
RwMutex.h
|
RwMutex.h
|
||||||
SafeArray.h
|
SafeArray.h
|
||||||
ScopedAlloc.h
|
ScopedAlloc.h
|
||||||
|
ScopedGuard.h
|
||||||
ScopedPtrMT.h
|
ScopedPtrMT.h
|
||||||
SettingsInterface.h
|
SettingsInterface.h
|
||||||
SettingsWrapper.h
|
SettingsWrapper.h
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
/* PCSX2 - PS2 Emulator for PCs
|
||||||
|
* Copyright (C) 2002-2021 PCSX2 Dev Team
|
||||||
|
*
|
||||||
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
||||||
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
||||||
|
* ation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||||
|
* PURPOSE. See the GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
||||||
|
* If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "Pcsx2Defs.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:
|
||||||
|
__fi ScopedGuard(T&& func)
|
||||||
|
: m_func(std::forward<T>(func))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
__fi ScopedGuard(ScopedGuard&& other)
|
||||||
|
: m_func(std::move(other.m_func))
|
||||||
|
{
|
||||||
|
other.m_func = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
__fi ~ScopedGuard()
|
||||||
|
{
|
||||||
|
if (!m_func.has_value())
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_func.value()();
|
||||||
|
m_func.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedGuard(const ScopedGuard&) = delete;
|
||||||
|
void operator=(const ScopedGuard&) = delete;
|
||||||
|
|
||||||
|
/// Prevents the function from being invoked when we go out of scope.
|
||||||
|
__fi void Cancel()
|
||||||
|
{
|
||||||
|
m_func.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::optional<T> m_func;
|
||||||
|
};
|
|
@ -98,6 +98,7 @@
|
||||||
<ClInclude Include="boost_spsc_queue.hpp" />
|
<ClInclude Include="boost_spsc_queue.hpp" />
|
||||||
<ClInclude Include="FastJmp.h" />
|
<ClInclude Include="FastJmp.h" />
|
||||||
<ClInclude Include="ScopedAlloc.h" />
|
<ClInclude Include="ScopedAlloc.h" />
|
||||||
|
<ClInclude Include="ScopedGuard.h" />
|
||||||
<ClInclude Include="StringUtil.h" />
|
<ClInclude Include="StringUtil.h" />
|
||||||
<ClInclude Include="SettingsInterface.h" />
|
<ClInclude Include="SettingsInterface.h" />
|
||||||
<ClInclude Include="SettingsWrapper.h" />
|
<ClInclude Include="SettingsWrapper.h" />
|
||||||
|
|
|
@ -270,6 +270,9 @@
|
||||||
<ClInclude Include="SettingsWrapper.h">
|
<ClInclude Include="SettingsWrapper.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="ScopedGuard.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Filter Include="Source Files">
|
<Filter Include="Source Files">
|
||||||
|
|
Loading…
Reference in New Issue