completion.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#pragma once

#include <felspar/coro/task.hpp>
#include <felspar/io/exceptions.hpp>
#include <felspar/test/source.hpp>

#include <memory>
#include <system_error>


namespace felspar::io {


    template<typename R>
    class ec;
    class warden;

Describe the outcome of an API

20
21
22
23
24
25
    template<typename R>
    struct outcome;
    template<>
    struct [[nodiscard]] outcome<void> {
        std::error_code error = {};
        char const *message = "";

Returns true if no error occurred

28
29
30
        explicit operator bool() const noexcept {
            return not static_cast<bool>(error);
        }

Throw the exception associated with the error

32
33
34
35
36
37
38
39
40
        [[noreturn]] void throw_exception(
                felspar::source_location const &loc =
                        felspar::source_location::current()) {
            if (error == timeout::error) {
                throw timeout{message, loc};
            } else {
                throw felspar::stdexcept::system_error{error, message, loc};
            }
        }

Throw the exception if there was one

42
43
44
45
46
47
48
49
50
51
52
53
        void
                value(felspar::source_location const &loc =
                              felspar::source_location::current()) {
            if (error) { throw_exception(loc); }
        }
    };
    template<typename R>
    struct [[nodiscard]] outcome : private outcome<void> {
        using outcome<void>::error;
        using outcome<void>::message;
        using outcome<void>::throw_exception;
        using outcome<void>::operator bool;

Contains the result (if one was recorded)

56
        std::optional<R> result = {};

Assign a value to the outcome

59
60
61
62
63
64
65
66
67
68
69
70
        outcome &operator=(R r) {
            error = {};
            message = "";
            result = std::move(r);
            return *this;
        }
        outcome &operator=(outcome<void> r) {
            error = r.error;
            message = r.message;
            result = {};
            return *this;
        }

Consume the value, or throw the exception

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        R value(felspar::source_location const &loc =
                        felspar::source_location::current()) && {
            if (error) {
                throw_exception(loc);
            } else if (result.has_value()) {
                return std::move(*result);
            } else {
                throw felspar::stdexcept::logic_error{
                        "Optional in outcome was empty", loc};
            }
        }
        R value(felspar::source_location const &loc =
                        felspar::source_location::current()) & {
            if (error) {
                throw_exception(loc);
            } else {
                return result.value();
            }
        }
    };

A completion is always created in response to an IOP request and is used to track and manage the IOP as it executes

 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
    template<typename R>
    struct [[nodiscard]] completion {
        using result_type = R;

        felspar::coro::coroutine_handle<> handle = {};
        felspar::source_location loc;
        outcome<R> result = {};

        completion(felspar::source_location const &l) : loc{l} {}
        virtual ~completion() = default;

        virtual warden *ward() = 0;
        virtual felspar::coro::coroutine_handle<>
                await_suspend(felspar::coro::coroutine_handle<>) = 0;

Return true if the completion should be destroyed when the iop is

113
114
        virtual bool delete_due_to_iop_destructed() = 0;
    };

The awaitable type associated with all IOPs.

118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    template<typename R>
    class [[nodiscard]] iop {
        template<typename E>
        friend class ec;

      public:
        using result_type = R;
        using completion_type = completion<result_type>;

        iop(completion_type *c) : comp{c} {}
        ~iop();

        iop(iop const &) = delete;
        iop &operator=(iop const &) = delete;
        iop(iop &&i) : comp{std::exchange(i.comp, {})} {}
        iop &operator=(iop &&i) {
            std::swap(i.comp, comp);
            return *this;
        }

        bool await_ready() const noexcept { return false; }
        felspar::coro::coroutine_handle<>
                await_suspend(felspar::coro::coroutine_handle<> h) {
            return comp->await_suspend(h);
        }
        R await_resume() { return std::move(comp->result).value(comp->loc); }

      private:
        completion_type *comp;
    };


}