diff options
Diffstat (limited to 'src/core/hle/result.h')
-rw-r--r-- | src/core/hle/result.h | 153 |
1 files changed, 0 insertions, 153 deletions
diff --git a/src/core/hle/result.h b/src/core/hle/result.h index 240f06689..92a1439eb 100644 --- a/src/core/hle/result.h +++ b/src/core/hle/result.h @@ -283,159 +283,6 @@ private: u32 description_end; }; -/** - * This is an optional value type. It holds a `Result` and, if that code is ResultSuccess, it - * also holds a result of type `T`. If the code is an error code (not ResultSuccess), then trying - * to access the inner value with operator* is undefined behavior and will assert with Unwrap(). - * Users of this class must be cognizant to check the status of the ResultVal with operator bool(), - * Code(), Succeeded() or Failed() prior to accessing the inner value. - * - * An example of how it could be used: - * \code - * ResultVal<int> Frobnicate(float strength) { - * if (strength < 0.f || strength > 1.0f) { - * // Can't frobnicate too weakly or too strongly - * return Result{ErrorModule::Common, 1}; - * } else { - * // Frobnicated! Give caller a cookie - * return 42; - * } - * } - * \endcode - * - * \code - * auto frob_result = Frobnicate(0.75f); - * if (frob_result) { - * // Frobbed ok - * printf("My cookie is %d\n", *frob_result); - * } else { - * printf("Guess I overdid it. :( Error code: %ux\n", frob_result.Code().raw); - * } - * \endcode - */ -template <typename T> -class ResultVal { -public: - constexpr ResultVal() : expected{} {} - - constexpr ResultVal(Result code) : expected{Common::Unexpected(code)} {} - - constexpr ResultVal(ResultRange range) : expected{Common::Unexpected(range)} {} - - template <typename U> - constexpr ResultVal(U&& val) : expected{std::forward<U>(val)} {} - - template <typename... Args> - constexpr ResultVal(Args&&... args) : expected{std::in_place, std::forward<Args>(args)...} {} - - ~ResultVal() = default; - - constexpr ResultVal(const ResultVal&) = default; - constexpr ResultVal(ResultVal&&) = default; - - ResultVal& operator=(const ResultVal&) = default; - ResultVal& operator=(ResultVal&&) = default; - - [[nodiscard]] constexpr explicit operator bool() const noexcept { - return expected.has_value(); - } - - [[nodiscard]] constexpr Result Code() const { - return expected.has_value() ? ResultSuccess : expected.error(); - } - - [[nodiscard]] constexpr bool Succeeded() const { - return expected.has_value(); - } - - [[nodiscard]] constexpr bool Failed() const { - return !expected.has_value(); - } - - [[nodiscard]] constexpr T* operator->() { - return std::addressof(expected.value()); - } - - [[nodiscard]] constexpr const T* operator->() const { - return std::addressof(expected.value()); - } - - [[nodiscard]] constexpr T& operator*() & { - return *expected; - } - - [[nodiscard]] constexpr const T& operator*() const& { - return *expected; - } - - [[nodiscard]] constexpr T&& operator*() && { - return *expected; - } - - [[nodiscard]] constexpr const T&& operator*() const&& { - return *expected; - } - - [[nodiscard]] constexpr T& Unwrap() & { - ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal"); - return expected.value(); - } - - [[nodiscard]] constexpr const T& Unwrap() const& { - ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal"); - return expected.value(); - } - - [[nodiscard]] constexpr T&& Unwrap() && { - ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal"); - return std::move(expected.value()); - } - - [[nodiscard]] constexpr const T&& Unwrap() const&& { - ASSERT_MSG(Succeeded(), "Tried to Unwrap empty ResultVal"); - return std::move(expected.value()); - } - - template <typename U> - [[nodiscard]] constexpr T ValueOr(U&& v) const& { - return expected.value_or(v); - } - - template <typename U> - [[nodiscard]] constexpr T ValueOr(U&& v) && { - return expected.value_or(v); - } - -private: - // TODO (Morph): Replace this with C++23 std::expected. - Common::Expected<T, Result> expected; -}; - -/** - * Check for the success of `source` (which must evaluate to a ResultVal). If it succeeds, unwraps - * the contained value and assigns it to `target`, which can be either an l-value expression or a - * variable declaration. If it fails the return code is returned from the current function. Thus it - * can be used to cascade errors out, achieving something akin to exception handling. - */ -#define CASCADE_RESULT(target, source) \ - auto CONCAT2(check_result_L, __LINE__) = source; \ - if (CONCAT2(check_result_L, __LINE__).Failed()) { \ - return CONCAT2(check_result_L, __LINE__).Code(); \ - } \ - target = std::move(*CONCAT2(check_result_L, __LINE__)) - -/** - * Analogous to CASCADE_RESULT, but for a bare Result. The code will be propagated if - * non-success, or discarded otherwise. - */ -#define CASCADE_CODE(source) \ - do { \ - auto CONCAT2(check_result_L, __LINE__) = source; \ - if (CONCAT2(check_result_L, __LINE__).IsError()) { \ - return CONCAT2(check_result_L, __LINE__); \ - } \ - } while (false) - #define R_SUCCEEDED(res) (static_cast<Result>(res).IsSuccess()) #define R_FAILED(res) (static_cast<Result>(res).IsFailure()) |