mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Simplify Result class so we can start using it
There were some ideas about how to use this class but we never actually started using it, so let's just simplify it and get it ready for use. The basic idea is: a function returns a Result<ValueType, ErrorType>. Callers check if the result object is_error(). If so, an ErrorType can be fetched with the error() getter. Otherwise, a ValueType is fetched with the value() getter. That's it. :^)
This commit is contained in:
parent
59cd181ed9
commit
ba3b561a40
Notes:
sideshowbarker
2024-07-19 07:25:08 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/ba3b561a40a
1 changed files with 17 additions and 30 deletions
47
AK/Result.h
47
AK/Result.h
|
@ -27,78 +27,65 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/Platform.h>
|
||||
#include <AK/Optional.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T, typename E>
|
||||
class CONSUMABLE(unknown) Result {
|
||||
template<typename ValueType, typename ErrorType>
|
||||
class Result {
|
||||
public:
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Result(const T& res)
|
||||
Result(const ValueType& res)
|
||||
: m_result(res)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Result(const E& error)
|
||||
Result(const ErrorType& error)
|
||||
: m_error(error)
|
||||
{
|
||||
}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Result(const T& res, const E& error)
|
||||
Result(const ValueType& res, const ErrorType& error)
|
||||
: m_result(res)
|
||||
, m_error(error)
|
||||
{
|
||||
}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Result(Result&& other)
|
||||
: m_result(move(other.m_result))
|
||||
, m_error(move(other.m_error))
|
||||
{
|
||||
}
|
||||
|
||||
RETURN_TYPESTATE(unknown)
|
||||
Result(Result& other)
|
||||
: m_result(other.m_result)
|
||||
, m_error(other.m_error)
|
||||
{
|
||||
}
|
||||
|
||||
CALLABLE_WHEN("unknown", "consumed")
|
||||
~Result()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(consumed)
|
||||
T& unwrap() {
|
||||
ValueType& value()
|
||||
{
|
||||
return m_result.value();
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(consumed)
|
||||
E& error() {
|
||||
ErrorType& error()
|
||||
{
|
||||
return m_error.value();
|
||||
}
|
||||
|
||||
bool has_error() const {
|
||||
bool is_error() const
|
||||
{
|
||||
return m_error.has_value();
|
||||
}
|
||||
bool has_value() const {
|
||||
return m_result.has_value();
|
||||
}
|
||||
|
||||
SET_TYPESTATE(consumed)
|
||||
bool failed() const {
|
||||
return m_error.value().failed();
|
||||
}
|
||||
|
||||
private:
|
||||
Optional<T> m_result;
|
||||
Optional<E> m_error;
|
||||
Optional<ValueType> m_result;
|
||||
Optional<ErrorType> m_error;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using AK::Result;
|
||||
|
||||
|
|
Loading…
Reference in a new issue