summaryrefslogtreecommitdiff
path: root/AK/Optional.h
blob: ee6320925e4fa480b69a8906fc6813428a02ee6b (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once

#include <AK/Assertions.h>
#include <AK/Platform.h>

template<typename T>
class CONSUMABLE(unknown) alignas(T) Optional {
public:
    RETURN_TYPESTATE(unknown)
    Optional() {}

    RETURN_TYPESTATE(unknown)
    Optional(T&& value)
        : m_has_value(true)
    {
        new (&m_storage) T(move(value));
    }

    template<typename U>
    RETURN_TYPESTATE(unknown)
    Optional(U&& value)
        : m_has_value(true)
    {
        new (&m_storage) T(move(value));
    }

    RETURN_TYPESTATE(unknown)
    Optional(Optional&& other)
        : m_has_value(other.m_has_value)
    {
        if (m_has_value) {
            new (&m_storage) T(move(other.value_without_consume_state()));
            other.m_has_value = false;
        }
    }

    RETURN_TYPESTATE(unknown)
    Optional(const Optional& other)
        : m_has_value(other.m_has_value)
    {
        if (m_has_value) {
            new (&m_storage) T(other.value_without_consume_state());
        }
    }

    RETURN_TYPESTATE(unknown)
    Optional& operator=(const Optional& other)
    {
        if (this != &other) {
            clear();
            m_has_value = other.m_has_value;
            if (m_has_value) {
                new (&m_storage) T(other.value());
            }
        }
        return *this;
    }

    RETURN_TYPESTATE(unknown)
    Optional& operator=(Optional&& other)
    {
        if (this != &other) {
            clear();
            m_has_value = other.m_has_value;
            if (m_has_value) {
                new (&m_storage) T(other.release_value());
            }
        }
        return *this;
    }

    ~Optional()
    {
        clear();
    }

    void clear()
    {
        if (m_has_value) {
            value().~T();
            m_has_value = false;
        }
    }

    SET_TYPESTATE(consumed)
    bool has_value() const { return m_has_value; }

    CALLABLE_WHEN(consumed)
    T& value()
    {
        ASSERT(m_has_value);
        return *reinterpret_cast<T*>(&m_storage);
    }

    CALLABLE_WHEN(consumed)
    const T& value() const
    {
        return value_without_consume_state();
    }

    CALLABLE_WHEN(consumed)
    T release_value()
    {
        ASSERT(m_has_value);
        T released_value = move(value());
        value().~T();
        return released_value;
    }

    T value_or(const T& fallback) const
    {
        if (m_has_value)
            return value();
        return fallback;
    }

    operator bool() const { return m_has_value; }

private:
    // Call when we don't want to alter the consume state
    const T& value_without_consume_state() const
    {
        ASSERT(m_has_value);
        return *reinterpret_cast<const T*>(&m_storage);
    }
    char m_storage[sizeof(T)];
    bool m_has_value { false };
};