39 using value_type = std::variant<variant_decay_t<await_resume_result_t<Ts>>...>;
52 when_any(Ts
const&...args) noexcept : _awaiters(args...) {}
58 when_any& operator=(when_any&&) =
delete;
59 when_any& operator=(when_any
const&) =
delete;
61 [[nodiscard]]
constexpr bool await_ready() noexcept
63 static_assert(
sizeof...(Ts) > 0);
64 return _await_ready<0>();
67 void await_suspend(std::coroutine_handle<>
const& handle)
noexcept
69 static_assert(
sizeof...(Ts) > 0);
70 return _await_suspend<0>(handle);
73 value_type await_resume() noexcept
75 hi_assert(_value.has_value());
83 std::optional<value_type> _value;
85 template<awaitable Awaiter>
86 static scoped_task<await_resume_result_t<Awaiter>> _await_suspend_task(Awaiter& awaiter)
88 co_return co_await awaiter;
91 template<std::
size_t I>
92 void _destroy_tasks() noexcept
94 std::get<I>(_task_cbts) = {};
95 std::get<I>(_tasks) = {};
96 if constexpr (I + 1 <
sizeof...(Ts)) {
97 _destroy_tasks<I + 1>();
101 template<std::
size_t I>
102 bool _await_ready() noexcept
104 auto& task = std::get<I>(_tasks) = _await_suspend_task(std::get<I>(_awaiters));
107 using arg_type = await_resume_result_t<decltype(std::get<I>(_awaiters))>;
109 if constexpr (std::is_same_v<arg_type, void>) {
110 _value = value_type{std::in_place_index<I>, std::monostate{}};
112 _value = value_type{std::in_place_index<I>, task.value()};
117 }
else if constexpr (I + 1 <
sizeof...(Ts)) {
118 return _await_ready<I + 1>();
125 template<std::
size_t I>
126 void _await_suspend(std::coroutine_handle<>
const& handle)
noexcept
128 using arg_type = await_resume_result_t<decltype(std::get<I>(_awaiters))>;
130 if constexpr (std::is_same_v<arg_type, void>) {
131 std::get<I>(_task_cbts) = std::get<I>(_tasks).subscribe(
133 this->_value = value_type{std::in_place_index<I>, std::monostate{}};
136 this->_destroy_tasks<0>();
139 callback_flags::main | callback_flags::once);
142 std::get<I>(_task_cbts) = std::get<I>(_tasks).subscribe(
143 [
this, handle](arg_type
const& arg) {
144 this->_value = value_type{std::in_place_index<I>, arg};
147 this->_destroy_tasks<0>();
150 callback_flags::main | callback_flags::once);
153 if constexpr (I + 1 <
sizeof...(Ts)) {
154 _await_suspend<I + 1>(handle);
auto when_any(Args const &...args)
await on a set of objects which can be converted to an awaitable.
Definition when_any.hpp:173