summaryrefslogtreecommitdiff
path: root/aisa/coroutine.h
blob: 1b553620fae654016cd6b33fe54b99942f20a161 (plain) (blame)
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
#pragma once

#include <coroutine>
#include <optional>

namespace aisa {

    template<typename result_t> struct task_promise;

    template<typename result_t> struct task : public std::coroutine_handle<task_promise<result_t>> {
        using handle = std::coroutine_handle<task_promise<result_t>>;
        using promise_type = task_promise<result_t>;
        bool await_ready() const noexcept { return handle::done(); }
        result_t await_resume() const noexcept;
        template<typename other_t> void await_suspend(std::coroutine_handle<task_promise<other_t>> h) const noexcept;
        std::optional<result_t> operator()() noexcept;
    };

    template<> struct task<void> : public std::coroutine_handle<task_promise<void>> {
        using handle = std::coroutine_handle<task_promise<void>>;
        using promise_type = task_promise<void>;
        bool await_ready() const noexcept { return handle::done(); }
        void await_resume() const noexcept;
        template<typename other_t> void await_suspend(std::coroutine_handle<task_promise<other_t>> h) const noexcept;
        bool operator()() noexcept;
    };

    template<typename result_t> struct task_promise {
        std::coroutine_handle<> precursor;
        std::optional<result_t> result;
        task_promise() = default;
        task_promise(const task_promise &) = delete;
        task<result_t> get_return_object() noexcept { return task<result_t>{std::coroutine_handle<task_promise<result_t>>::from_promise(*this)}; }
        std::suspend_never initial_suspend() const noexcept { return {}; }
        std::suspend_always final_suspend() const noexcept { return {}; }
        void unhandled_exception() { }
        void return_value(result_t x) noexcept { result = std::move(x); }
    };

    template<> struct task_promise<void> {
        std::coroutine_handle<> precursor;
        task_promise() = default;
        task_promise(const task_promise &) = delete;
        task<void> get_return_object() noexcept { return task<void>{std::coroutine_handle<task_promise<void>>::from_promise(*this)}; }
        std::suspend_never initial_suspend() const noexcept { return {}; }
        std::suspend_always final_suspend() const noexcept { return {}; }
        void unhandled_exception() { }
        void return_void() noexcept { }
    };

    template<typename result_t> result_t task<result_t>::await_resume() const noexcept
    {
        auto x = std::move(handle::promise().result.value());
        handle::destroy();
        return x;
    }

    template<typename result_t> template<typename other_t> void task<result_t>::await_suspend(std::coroutine_handle<task_promise<other_t>> h) const noexcept
    {
        h.promise().precursor = *this;
    }

    template<typename result_t> std::optional<result_t> task<result_t>::operator()() noexcept
    {
        if (!handle::operator bool())
            return {};
        if (!handle::done()) {
            auto &precursor = handle::promise().precursor;
            if (precursor) {
                if (!precursor.done())
                    precursor.resume();
                if (precursor.done())
                    precursor = nullptr;
            }
            if (!precursor)
                handle::resume();
        }
        if (handle::done()) {
            auto x = await_resume();
            handle::operator=(nullptr);
            return x;
        }
        return {};
    }

    inline void task<void>::await_resume() const noexcept
    {
        handle::destroy();
    }

    template<typename other_t> void task<void>::await_suspend(std::coroutine_handle<task_promise<other_t>> h) const noexcept
    {
        h.promise().precursor = *this;
    }

    inline bool task<void>::operator()() noexcept
    {
        if (!handle::operator bool())
            return true;
        if (!handle::done()) {
            auto &precursor = handle::promise().precursor;
            if (precursor) {
                if (!precursor.done())
                    precursor.resume();
                if (precursor.done())
                    precursor = nullptr;
            }
            if (!precursor)
                handle::resume();
        }
        if (handle::done()) {
            await_resume();
            handle::operator=(nullptr);
            return true;
        }
        return false;
    }

}