2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-07-30 15:20:34 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Optional.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2021-04-27 11:46:44 +00:00
|
|
|
template<typename ValueT, typename ErrorT>
|
2020-12-30 21:44:54 +00:00
|
|
|
class [[nodiscard]] Result {
|
2019-07-30 15:20:34 +00:00
|
|
|
public:
|
2021-04-27 11:46:44 +00:00
|
|
|
using ValueType = ValueT;
|
|
|
|
using ErrorType = ErrorT;
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Result(ValueType const& res)
|
2019-07-30 15:20:34 +00:00
|
|
|
: m_result(res)
|
2020-04-21 11:27:03 +00:00
|
|
|
{
|
|
|
|
}
|
2021-01-01 10:39:04 +00:00
|
|
|
|
2020-12-30 21:44:54 +00:00
|
|
|
Result(ValueType&& res)
|
2020-10-03 17:11:11 +00:00
|
|
|
: m_result(move(res))
|
|
|
|
{
|
|
|
|
}
|
2021-01-01 10:39:04 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Result(ErrorType const& error)
|
2019-07-30 15:20:34 +00:00
|
|
|
: m_error(error)
|
|
|
|
{
|
|
|
|
}
|
2021-01-01 10:39:04 +00:00
|
|
|
|
2020-12-30 21:44:54 +00:00
|
|
|
Result(ErrorType&& error)
|
2020-10-03 17:11:11 +00:00
|
|
|
: m_error(move(error))
|
|
|
|
{
|
|
|
|
}
|
2019-07-30 15:20:34 +00:00
|
|
|
|
2021-01-01 10:39:04 +00:00
|
|
|
Result(Result&& other) = default;
|
2022-04-01 17:58:27 +00:00
|
|
|
Result(Result const& other) = default;
|
2021-01-01 10:39:04 +00:00
|
|
|
~Result() = default;
|
|
|
|
|
|
|
|
ValueType& value()
|
2019-07-30 15:20:34 +00:00
|
|
|
{
|
2021-01-01 10:39:04 +00:00
|
|
|
return m_result.value();
|
2019-07-30 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 10:39:04 +00:00
|
|
|
ErrorType& error()
|
2019-07-30 15:20:34 +00:00
|
|
|
{
|
2021-01-01 10:39:04 +00:00
|
|
|
return m_error.value();
|
2019-07-30 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 10:39:04 +00:00
|
|
|
bool is_error() const
|
2020-04-21 11:27:03 +00:00
|
|
|
{
|
2021-01-01 10:39:04 +00:00
|
|
|
return m_error.has_value();
|
2020-04-21 11:27:03 +00:00
|
|
|
}
|
2019-07-30 15:20:34 +00:00
|
|
|
|
2021-01-09 18:57:06 +00:00
|
|
|
ValueType release_value()
|
|
|
|
{
|
|
|
|
return m_result.release_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorType release_error()
|
|
|
|
{
|
|
|
|
return m_error.release_value();
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:39:04 +00:00
|
|
|
private:
|
|
|
|
Optional<ValueType> m_result;
|
|
|
|
Optional<ErrorType> m_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Partial specialization for void value type
|
2021-04-27 11:46:44 +00:00
|
|
|
template<typename ErrorT>
|
|
|
|
class [[nodiscard]] Result<void, ErrorT> {
|
2021-01-01 10:39:04 +00:00
|
|
|
public:
|
2021-04-27 11:46:44 +00:00
|
|
|
using ValueType = void;
|
|
|
|
using ErrorType = ErrorT;
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
Result(ErrorType const& error)
|
2021-01-01 10:39:04 +00:00
|
|
|
: m_error(error)
|
2020-04-21 11:27:03 +00:00
|
|
|
{
|
2019-07-30 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 10:39:04 +00:00
|
|
|
Result(ErrorType&& error)
|
|
|
|
: m_error(move(error))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Result() = default;
|
|
|
|
Result(Result&& other) = default;
|
2022-04-01 17:58:27 +00:00
|
|
|
Result(Result const& other) = default;
|
2021-01-01 10:39:04 +00:00
|
|
|
~Result() = default;
|
|
|
|
|
2021-10-05 20:05:42 +00:00
|
|
|
// For compatibility with TRY().
|
|
|
|
void value() {};
|
|
|
|
void release_value() {};
|
|
|
|
|
2020-04-21 11:27:03 +00:00
|
|
|
ErrorType& error()
|
|
|
|
{
|
2019-07-30 15:20:34 +00:00
|
|
|
return m_error.value();
|
|
|
|
}
|
|
|
|
|
2020-04-21 11:27:03 +00:00
|
|
|
bool is_error() const
|
|
|
|
{
|
2019-07-30 15:20:34 +00:00
|
|
|
return m_error.has_value();
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:03:57 +00:00
|
|
|
ErrorType release_error()
|
2021-01-09 18:57:06 +00:00
|
|
|
{
|
|
|
|
return m_error.release_value();
|
|
|
|
}
|
|
|
|
|
2019-07-30 15:20:34 +00:00
|
|
|
private:
|
2020-04-21 11:27:03 +00:00
|
|
|
Optional<ErrorType> m_error;
|
2019-07-30 15:20:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-07-30 15:20:34 +00:00
|
|
|
using AK::Result;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|