summaryrefslogtreecommitdiffstats
path: root/src/common/unique_function.h
diff options
context:
space:
mode:
authorAmeer J <52414509+ameerj@users.noreply.github.com>2021-07-09 01:20:57 +0200
committerGitHub <noreply@github.com>2021-07-09 01:20:57 +0200
commit975a7b3a78af40f05b92a53a96b9dcd7e85969e4 (patch)
tree56196b303ab9dd7f138beb45c471169647e1144a /src/common/unique_function.h
parentMerge pull request #6539 from lat9nq/default-setting (diff)
parentcommon/thread_worker: Stop workers on stop_token when waiting (diff)
downloadyuzu-975a7b3a78af40f05b92a53a96b9dcd7e85969e4.tar
yuzu-975a7b3a78af40f05b92a53a96b9dcd7e85969e4.tar.gz
yuzu-975a7b3a78af40f05b92a53a96b9dcd7e85969e4.tar.bz2
yuzu-975a7b3a78af40f05b92a53a96b9dcd7e85969e4.tar.lz
yuzu-975a7b3a78af40f05b92a53a96b9dcd7e85969e4.tar.xz
yuzu-975a7b3a78af40f05b92a53a96b9dcd7e85969e4.tar.zst
yuzu-975a7b3a78af40f05b92a53a96b9dcd7e85969e4.zip
Diffstat (limited to '')
-rw-r--r--src/common/unique_function.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/common/unique_function.h b/src/common/unique_function.h
new file mode 100644
index 000000000..ca0559071
--- /dev/null
+++ b/src/common/unique_function.h
@@ -0,0 +1,62 @@
+// Copyright 2021 yuzu emulator team
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <memory>
+#include <utility>
+
+namespace Common {
+
+/// General purpose function wrapper similar to std::function.
+/// Unlike std::function, the captured values don't have to be copyable.
+/// This class can be moved but not copied.
+template <typename ResultType, typename... Args>
+class UniqueFunction {
+ class CallableBase {
+ public:
+ virtual ~CallableBase() = default;
+ virtual ResultType operator()(Args&&...) = 0;
+ };
+
+ template <typename Functor>
+ 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>(args)...);
+ }
+
+ private:
+ Functor functor;
+ };
+
+public:
+ UniqueFunction() = default;
+
+ template <typename Functor>
+ UniqueFunction(Functor&& functor)
+ : callable{std::make_unique<Callable<Functor>>(std::move(functor))} {}
+
+ UniqueFunction& operator=(UniqueFunction&& rhs) noexcept = default;
+ UniqueFunction(UniqueFunction&& rhs) noexcept = default;
+
+ UniqueFunction& operator=(const UniqueFunction&) = delete;
+ UniqueFunction(const UniqueFunction&) = delete;
+
+ ResultType operator()(Args&&... args) const {
+ return (*callable)(std::forward<Args>(args)...);
+ }
+
+ explicit operator bool() const noexcept {
+ return static_cast<bool>(callable);
+ }
+
+private:
+ std::unique_ptr<CallableBase> callable;
+};
+
+} // namespace Common