A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <memory> |
| 7 | #include <utility> |
| 8 | |
| 9 | namespace Common { |
| 10 | |
| 11 | /// General purpose function wrapper similar to std::function. |
| 12 | /// Unlike std::function, the captured values don't have to be copyable. |
| 13 | /// This class can be moved but not copied. |
| 14 | template <typename ResultType, typename... Args> |
| 15 | class UniqueFunction { |
| 16 | class CallableBase { |
| 17 | public: |
| 18 | virtual ~CallableBase() = default; |
| 19 | virtual ResultType operator()(Args&&...) = 0; |
| 20 | }; |
| 21 | |
| 22 | template <typename Functor> |
| 23 | class Callable final : public CallableBase { |
| 24 | public: |
| 25 | Callable(Functor&& functor_) : functor{std::move(functor_)} {} |
| 26 | ~Callable() override = default; |
| 27 | |
| 28 | ResultType operator()(Args&&... args) override { |
| 29 | return functor(std::forward<Args>(args)...); |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | Functor functor; |
| 34 | }; |
| 35 | |
| 36 | public: |
| 37 | UniqueFunction() = default; |
| 38 | |
| 39 | template <typename Functor> |
| 40 | UniqueFunction(Functor&& functor) |
| 41 | : callable{std::make_unique<Callable<Functor>>(std::move(functor))} {} |
| 42 | |
| 43 | UniqueFunction& operator=(UniqueFunction&& rhs) noexcept = default; |
| 44 | UniqueFunction(UniqueFunction&& rhs) noexcept = default; |
| 45 | |
| 46 | UniqueFunction& operator=(const UniqueFunction&) = delete; |
| 47 | UniqueFunction(const UniqueFunction&) = delete; |
| 48 | |
| 49 | ResultType operator()(Args&&... args) const { |
| 50 | return (*callable)(std::forward<Args>(args)...); |
| 51 | } |
| 52 | |
| 53 | explicit operator bool() const noexcept { |
| 54 | return static_cast<bool>(callable); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | std::unique_ptr<CallableBase> callable; |
| 59 | }; |
| 60 | |
| 61 | } // namespace Common |
| 62 |