diff --git a/AK/Error.h b/AK/Error.h index decae334753..0ab143d6e7f 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include #include @@ -74,32 +73,36 @@ private: }; template -class [[nodiscard]] ErrorOr final : public Variant { +class [[nodiscard]] ErrorOr { public: - using Variant::Variant; + ErrorOr() requires(IsSame) + : m_value_or_error(Empty {}) + { + } template - ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame, ErrorOr>) - : Variant(forward(value)) + ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame, ErrorOr>) + : m_value_or_error(forward(value)) { } #ifdef __serenity__ ErrorOr(ErrnoCode code) - : Variant(Error::from_errno(code)) + : m_value_or_error(Error::from_errno(code)) { } #endif T& value() { - return this->template get(); + return m_value_or_error.template get(); } - T const& value() const { return this->template get(); } - ErrorType& error() { return this->template get(); } - ErrorType const& error() const { return this->template get(); } + T const& value() const { return m_value_or_error.template get(); } - bool is_error() const { return this->template has(); } + ErrorType& error() { return m_value_or_error.template get(); } + ErrorType const& error() const { return m_value_or_error.template get(); } + + bool is_error() const { return m_value_or_error.template has(); } T release_value() { return move(value()); } ErrorType release_error() { return move(error()); } @@ -111,41 +114,13 @@ public: } private: - // 'downcast' is fishy in this context. Let's hide it by making it private. - using Variant::downcast; + Variant m_value_or_error; }; -// Partial specialization for void value type template -class [[nodiscard]] ErrorOr { +class [[nodiscard]] ErrorOr : public ErrorOr { public: - ErrorOr(ErrorType error) - : m_error(move(error)) - { - } - -#ifdef __serenity__ - ErrorOr(ErrnoCode code) - : m_error(Error::from_errno(code)) - { - } -#endif - - ErrorOr() = default; - ErrorOr(ErrorOr&& other) = default; - ErrorOr(ErrorOr const& other) = default; - ~ErrorOr() = default; - - ErrorOr& operator=(ErrorOr&& other) = default; - ErrorOr& operator=(ErrorOr const& other) = default; - - ErrorType const& error() const { return m_error.value(); } - bool is_error() const { return m_error.has_value(); } - ErrorType release_error() { return m_error.release_value(); } - void release_value() { } - -private: - Optional m_error; + using ErrorOr::ErrorOr; }; }