summaryrefslogtreecommitdiff
path: root/AK/Result.h
blob: 64997f6d20c26485df48606e856f18ee02956379 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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 {
public:
    RETURN_TYPESTATE(unknown)
    Result(const T& res)
        : m_result(res)
    {}

    RETURN_TYPESTATE(unknown)
    Result(const E& error)
        : m_error(error)
    {
    }

    RETURN_TYPESTATE(unknown)
    Result(const T& res, const E& 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() {
        return m_result.value();
    }

    CALLABLE_WHEN(consumed)
    E& error() {
        return m_error.value();
    }

    bool has_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;
};

}

using AK::Result;