// Copyright 2021 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. #pragma once #include #include namespace Common { template class UniqueFunction { class CallableBase { public: virtual ~CallableBase() = default; virtual ResultType operator()(Args...) = 0; }; template class Callable final : public CallableBase { public: Callable(Functor&& functor_) : functor{std::move(functor_)} {} ~Callable() override = default; ResultType operator()(Args... args) override { return functor(std::forward(args)...); } private: Functor functor; }; public: UniqueFunction() = default; template UniqueFunction(Functor&& functor) : callable{std::make_unique>(std::move(functor))} {} UniqueFunction& operator=(UniqueFunction&& rhs) noexcept { callable = std::move(rhs.callable); return *this; } UniqueFunction(UniqueFunction&& rhs) noexcept : callable{std::move(rhs.callable)} {} ResultType operator()(Args... args) const { return (*callable)(std::forward(args)...); } explicit operator bool() const noexcept { return callable != nullptr; } UniqueFunction& operator=(const UniqueFunction&) = delete; UniqueFunction(const UniqueFunction&) = delete; private: std::unique_ptr callable; }; } // namespace Common