mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-04-28 21:08:04 +03:00
Common: Add MoveOnlyFunction.
This commit is contained in:
parent
8ee64a84c7
commit
e403dee3da
3 changed files with 53 additions and 0 deletions
|
@ -70,6 +70,7 @@ add_library(common
|
||||||
Flag.h
|
Flag.h
|
||||||
FloatUtils.cpp
|
FloatUtils.cpp
|
||||||
FloatUtils.h
|
FloatUtils.h
|
||||||
|
Functional.h
|
||||||
FormatUtil.h
|
FormatUtil.h
|
||||||
FPURoundMode.h
|
FPURoundMode.h
|
||||||
GekkoDisassembler.cpp
|
GekkoDisassembler.cpp
|
||||||
|
|
51
Source/Core/Common/Functional.h
Normal file
51
Source/Core/Common/Functional.h
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
// Copyright 2025 Dolphin Emulator Project
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
// TODO C++23: Replace with std::move_only_function.
|
||||||
|
|
||||||
|
namespace Common
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class MoveOnlyFunction;
|
||||||
|
|
||||||
|
template <typename R, typename... Args>
|
||||||
|
class MoveOnlyFunction<R(Args...)>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using result_type = R;
|
||||||
|
|
||||||
|
MoveOnlyFunction() = default;
|
||||||
|
|
||||||
|
template <typename F>
|
||||||
|
MoveOnlyFunction(F&& f) : m_ptr{std::make_unique<Func<F>>(std::forward<F>(f))}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
result_type operator()(Args... args) const { return m_ptr->Invoke(std::forward<Args>(args)...); }
|
||||||
|
explicit operator bool() const { return m_ptr != nullptr; };
|
||||||
|
void swap(MoveOnlyFunction& other) { m_ptr.swap(other.m_ptr); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct FuncBase
|
||||||
|
{
|
||||||
|
virtual ~FuncBase() = default;
|
||||||
|
virtual result_type Invoke(Args...) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename F>
|
||||||
|
struct Func : FuncBase
|
||||||
|
{
|
||||||
|
Func(F&& f) : func{std::forward<F>(f)} {}
|
||||||
|
result_type Invoke(Args... args) override { return func(std::forward<Args>(args)...); }
|
||||||
|
F func;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<FuncBase> m_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Common
|
|
@ -62,6 +62,7 @@
|
||||||
<ClInclude Include="Common\FloatUtils.h" />
|
<ClInclude Include="Common\FloatUtils.h" />
|
||||||
<ClInclude Include="Common\FormatUtil.h" />
|
<ClInclude Include="Common\FormatUtil.h" />
|
||||||
<ClInclude Include="Common\FPURoundMode.h" />
|
<ClInclude Include="Common\FPURoundMode.h" />
|
||||||
|
<ClInclude Include="Common\Functional.h" />
|
||||||
<ClInclude Include="Common\GekkoDisassembler.h" />
|
<ClInclude Include="Common\GekkoDisassembler.h" />
|
||||||
<ClInclude Include="Common\GL\GLContext.h" />
|
<ClInclude Include="Common\GL\GLContext.h" />
|
||||||
<ClInclude Include="Common\GL\GLExtensions\AMD_pinned_memory.h" />
|
<ClInclude Include="Common\GL\GLExtensions\AMD_pinned_memory.h" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue