mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-19 11:43:56 +00:00
some refactoring with optional_function
- wrapped std::function into utils::optional_function -- cleaned the code accordingly in windows_emulator - using the 'emulator'/'windows_emulator' dependency implies the emulator_common as well.
This commit is contained in:
48
src/common/utils/function.hpp
Normal file
48
src/common/utils/function.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
template <typename Signature>
|
||||
class optional_function;
|
||||
|
||||
template <typename Ret, typename... Args>
|
||||
class optional_function<Ret(Args...)>
|
||||
{
|
||||
private:
|
||||
std::function<Ret(Args...)> func;
|
||||
|
||||
public:
|
||||
optional_function() = default;
|
||||
optional_function(std::function<Ret(Args...)> f)
|
||||
: func(std::move(f))
|
||||
{
|
||||
}
|
||||
|
||||
optional_function& operator=(std::function<Ret(Args...)> f)
|
||||
{
|
||||
func = std::move(f);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ret operator()(Args... args) const
|
||||
{
|
||||
if (func)
|
||||
{
|
||||
return func(std::forward<Args>(args)...);
|
||||
}
|
||||
else
|
||||
{
|
||||
if constexpr (!std::is_void_v<Ret>)
|
||||
{
|
||||
return Ret();
|
||||
}
|
||||
}
|
||||
}
|
||||
explicit operator bool() const
|
||||
{
|
||||
return static_cast<bool>(func);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user