From f1bbf06cd83cb55b5285bbed6a5788766f55dc8d Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sat, 17 Jul 2021 14:01:39 +0100 Subject: [PATCH] Allow supplying a custom ResultValue result type and optimise span This allows PosixResultValue to be created easily. --- app/src/main/cpp/skyline/common.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/app/src/main/cpp/skyline/common.h b/app/src/main/cpp/skyline/common.h index 5e991af7..45a4ed41 100644 --- a/app/src/main/cpp/skyline/common.h +++ b/app/src/main/cpp/skyline/common.h @@ -83,26 +83,27 @@ namespace skyline { /** * @brief A wrapper around std::optional that also stores a HOS result code - * @tparam T The object type to hold + * @tparam ValueType The object type to hold + * @tparam ResultType The result type to hold */ - template + template class ResultValue { - static_assert(!std::is_same::value); + static_assert(!std::is_same::value); private: - std::optional value; + std::optional value; public: - Result result; + ResultType result{}; - constexpr ResultValue(T value) : value(value) {}; + constexpr ResultValue(ValueType value) : value(value) {}; - constexpr ResultValue(Result result) : result(result) {}; + constexpr ResultValue(ResultType result) : result(result) {}; template constexpr ResultValue(ResultValue result) : result(result) {}; - constexpr operator Result() const { + constexpr operator ResultType() const { return result; } @@ -110,11 +111,11 @@ namespace skyline { return value.has_value(); } - constexpr T& operator*() { + constexpr ValueType& operator*() { return *value; } - constexpr T* operator->() { + constexpr ValueType* operator->() { return &*value; } }; @@ -362,6 +363,9 @@ namespace skyline { template constexpr Out &as() { + if constexpr (Extent != std::dynamic_extent && sizeof(T) * Extent >= sizeof(Out)) + return *reinterpret_cast(span::data()); + if (span::size_bytes() >= sizeof(Out)) return *reinterpret_cast(span::data()); throw exception("Span size is less than Out type size (0x{:X}/0x{:X})", span::size_bytes(), sizeof(Out));