From 75a67dcfa5c3b84cbbbbb7a03711ecb8fcb7a0e0 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 7b3dab13..dba89ec6 100644 --- a/app/src/main/cpp/skyline/common.h +++ b/app/src/main/cpp/skyline/common.h @@ -84,26 +84,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; } @@ -111,11 +112,11 @@ namespace skyline { return value.has_value(); } - constexpr T& operator*() { + constexpr ValueType& operator*() { return *value; } - constexpr T* operator->() { + constexpr ValueType* operator->() { return &*value; } }; @@ -398,6 +399,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));