diff options
| author | 2021-04-01 01:03:25 -0300 | |
|---|---|---|
| committer | 2021-07-08 19:03:19 -0300 | |
| commit | 2c8d33741889ddffc6dfaf4b2f62e61f496c6b0a (patch) | |
| tree | 9f28e751655d1a46c9cff2a0fe104c0299dcd58e /src/common/unique_function.h | |
| parent | common/thread_worker: Add wait for requests method (diff) | |
| download | yuzu-2c8d33741889ddffc6dfaf4b2f62e61f496c6b0a.tar.gz yuzu-2c8d33741889ddffc6dfaf4b2f62e61f496c6b0a.tar.xz yuzu-2c8d33741889ddffc6dfaf4b2f62e61f496c6b0a.zip | |
common: Add unique function
Diffstat (limited to 'src/common/unique_function.h')
| -rw-r--r-- | src/common/unique_function.h | 62 |
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 @@ | |||
| 1 | // Copyright 2021 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | #include <utility> | ||
| 9 | |||
| 10 | namespace Common { | ||
| 11 | |||
| 12 | /// General purpose function wrapper similar to std::function. | ||
| 13 | /// Unlike std::function, the captured values don't have to be copyable. | ||
| 14 | /// This class can be moved but not copied. | ||
| 15 | template <typename ResultType, typename... Args> | ||
| 16 | class UniqueFunction { | ||
| 17 | class CallableBase { | ||
| 18 | public: | ||
| 19 | virtual ~CallableBase() = default; | ||
| 20 | virtual ResultType operator()(Args&&...) = 0; | ||
| 21 | }; | ||
| 22 | |||
| 23 | template <typename Functor> | ||
| 24 | class Callable final : public CallableBase { | ||
| 25 | public: | ||
| 26 | Callable(Functor&& functor_) : functor{std::move(functor_)} {} | ||
| 27 | ~Callable() override = default; | ||
| 28 | |||
| 29 | ResultType operator()(Args&&... args) override { | ||
| 30 | return functor(std::forward<Args>(args)...); | ||
| 31 | } | ||
| 32 | |||
| 33 | private: | ||
| 34 | Functor functor; | ||
| 35 | }; | ||
| 36 | |||
| 37 | public: | ||
| 38 | UniqueFunction() = default; | ||
| 39 | |||
| 40 | template <typename Functor> | ||
| 41 | UniqueFunction(Functor&& functor) | ||
| 42 | : callable{std::make_unique<Callable<Functor>>(std::move(functor))} {} | ||
| 43 | |||
| 44 | UniqueFunction& operator=(UniqueFunction&& rhs) noexcept = default; | ||
| 45 | UniqueFunction(UniqueFunction&& rhs) noexcept = default; | ||
| 46 | |||
| 47 | UniqueFunction& operator=(const UniqueFunction&) = delete; | ||
| 48 | UniqueFunction(const UniqueFunction&) = delete; | ||
| 49 | |||
| 50 | ResultType operator()(Args&&... args) const { | ||
| 51 | return (*callable)(std::forward<Args>(args)...); | ||
| 52 | } | ||
| 53 | |||
| 54 | explicit operator bool() const noexcept { | ||
| 55 | return static_cast<bool>(callable); | ||
| 56 | } | ||
| 57 | |||
| 58 | private: | ||
| 59 | std::unique_ptr<CallableBase> callable; | ||
| 60 | }; | ||
| 61 | |||
| 62 | } // namespace Common | ||