starter.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#pragma once


#include <felspar/coro/task.hpp>
#include <felspar/exceptions.hpp>

#include <vector>


namespace felspar::coro {

Start and manage multiple coroutines

14
15
16
17
18
19
    template<typename Task = task<void>>
    class starter {
      public:
        using task_type = Task;
        using promise_type = typename task_type::promise_type;
        using handle_type = typename promise_type::handle_type;

Start a new coroutine

22
23
24
25
26
27
28
29
30
31
32
33
34
35
        template<typename... PArgs, typename... MArgs>
        void post(task_type (*f)(PArgs...), MArgs &&...margs) {
            static_assert(sizeof...(PArgs) == sizeof...(MArgs));
            post(f(std::forward<MArgs>(margs)...));
        }
        template<typename N, typename... PArgs, typename... MArgs>
        void post(N &o, task_type (N::*f)(PArgs...), MArgs &&...margs) {
            static_assert(sizeof...(PArgs) == sizeof...(MArgs));
            post((o.*f)(std::forward<MArgs>(margs)...));
        }
        void post(task_type t) {
            t.start();
            live.push_back(t.release());
        }

The number of coroutines currently held by the starter

38
39
        [[nodiscard]] std::size_t size() const noexcept { return live.size(); }
        [[nodiscard]] bool empty() const noexcept { return live.empty(); }

Garbage collect old coroutines

Ignores any errors and return values.

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        void garbage_collect_completed() {
            live.erase(
                    std::remove_if(
                            live.begin(), live.end(),
                            [](auto const &h) { return h.done(); }),
                    live.end());
        }
        [[deprecated(
                "Use the new garbage_collect_completed or wait_for_all "
                "APIs")]] void
                gc() {
            live.erase(
                    std::remove_if(
                            live.begin(), live.end(),
                            [](auto const &h) {
                                if (h.done()) {
                                    h.promise().consume_value();
                                    return true;
                                } else {
                                    return false;
                                }
                            }),
                    live.end());
        }

Wait for all coroutines to complete

Or for the first to throw an exception. If no exception has happened then returns the number of coroutines awaited.

73
74
75
76
77
78
79
80
81
82
        task<std::size_t> wait_for_all() {
            std::size_t count{};
            while (live.size()) {
                task_type t{std::move(live.back())};
                live.pop_back();
                co_await std::move(t);
                ++count;
            }
            co_return count;
        }

The next item in line in the starter

85
86
87
88
89
90
91
92
93
94
95
        task_type next(source_location const &loc = source_location::current()) {
            if (live.empty()) {
                throw stdexcept::logic_error{
                        "Cannot call starter::next() if there are no items",
                        loc};
            } else {
                task_type t{std::move(live.back())};
                live.pop_back();
                return t;
            }
        }

Delete all coroutines

Calling this from any of the running coroutines will invoke undefined behaviour.

102
103
104
105
106
107
108
109
        void reset() { live.clear(); }

      private:
        std::vector<handle_type> live;
    };


}