Skip to content

Commit

Permalink
AK: Add some more ways to construct Error and ErrorOr<T>
Browse files Browse the repository at this point in the history
This is preparation for using Error in the kernel instead of KResult.
  • Loading branch information
awesomekling committed Nov 7, 2021
1 parent 5e473a6 commit 7ee10c6
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion AK/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class [[nodiscard]] ErrorOr {
{
}

template<typename U>
ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame<RemoveCVReference<U>, ErrorOr<T>>)
: m_value(forward<U>(value))
{
}

#ifdef __serenity__
ErrorOr(ErrnoCode code)
: m_error(Error::from_errno(code))
Expand All @@ -74,6 +80,9 @@ class [[nodiscard]] ErrorOr {
ErrorOr(ErrorOr const& other) = default;
~ErrorOr() = default;

ErrorOr& operator=(ErrorOr&& other) = default;
ErrorOr& operator=(ErrorOr const& other) = default;

T& value() { return m_value.value(); }
Error& error() { return m_error.value(); }

Expand All @@ -98,11 +107,21 @@ class [[nodiscard]] ErrorOr<void, ErrorType> {
{
}

#ifdef __serenity__
ErrorOr(ErrnoCode code)
: m_error(Error::from_errno(code))
{
}
#endif

ErrorOr() = default;
ErrorOr(ErrorOr&& other) = default;
ErrorOr(const ErrorOr& other) = default;
ErrorOr(ErrorOr const& other) = default;
~ErrorOr() = default;

ErrorOr& operator=(ErrorOr&& other) = default;
ErrorOr& operator=(ErrorOr const& other) = default;

ErrorType& error() { return m_error.value(); }
bool is_error() const { return m_error.has_value(); }
ErrorType release_error() { return m_error.release_value(); }
Expand Down

0 comments on commit 7ee10c6

Please sign in to comment.