Given a coroutine spawn into..
void my_coroutine(asio::yield_context yield) {
system::error_code ec;
my_wrapper(yield[ec]);
if (ec) {
// something went wrong
return;
}
...
}
void my_wrapper(asio::yield_context&& yield) {
asio::async_read(..., yield); // any asio async call
if (ec) {
// something went wrong
return;
}
...
}
In the wrapper function, it's not possible to get access to ec from the passed yield context. So how can this be resolved?
What do you want to /do/ with the ec
? You CAN get access is precisely the same way as your my_coroutine already shows:
void my_wrapper(asio::yield_context&& yield) {
system::error_code ec;
asio::async_read(..., yield[ec]); // any asio async call
if (ec) {
// something went wrong
return;
}
...
}
If you mean you wanted to make an async operation that is composable, see this pattern:
Basic example:
template <typename Token>
auto async_meaning_of_life(bool success, Token&& token)
{
#if BOOST_VERSION >= 106600
using result_type = typename asio::async_result<std::decay_t<Token>, void(error_code, int)>;
typename result_type::completion_handler_type handler(std::forward<Token>(token));
result_type result(handler);
#else
typename asio::handler_type<Token, void(error_code, int)>::type
handler(std::forward<Token>(token));
asio::async_result<decltype (handler)> result (handler);
#endif
if (success)
handler(error_code{}, 42);
else
handler(asio::error::operation_aborted, 0);
return result.get ();
}