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