How can I replace deprecated handler_type_t or boost::asio::handler_type in this example code snippet?
Asked Answered
J

1

2

I found this interesting link boost::asio::spawn yield as callback

and since that might be what I need I wanted to try out the following part:

template <class CompletionToken>
auto async_foo(uint64_t item_id, CompletionToken&& token)
{

    typename boost::asio::handler_type< CompletionToken, void(error_code, size_t) >::type handler(std::forward<CompletionToken>(token));
    //handler_type_t<CompletionToken, void(error_code, size_t)>                        handler(std::forward<CompletionToken>(token));

    async_result<decltype(handler)> result(handler);

    //async_request_data(item_id, handler);

    return result.get();
}  

But obviously neither handler_type_t nor boost::asio::handler_type is existing anymore in a newer boost version.

How can I adapt the example?

EDIT:

Is that he right way? Instead of

boost::asio::handler_type< CompletionToken, void(error_code, size_t) >::type

I used

typename boost::asio::async_result< CompletionToken, void(error_code, size_t) >::completion_handler_type
Judon answered 1/2, 2020 at 10:3 Comment(1)
See my answer here: https://mcmap.net/q/858357/-boost-asio-spawn-yield-as-callbackWriting
R
2

Its almost right. from the docs of boost.asio

An initiating function determines the type CompletionHandler of its completion handler function object by performing typename async_result<decay_t<CompletionToken>, Signature>::completion_handler_type

and

An initiating function produces its return type as follows:

— constructing an object result of type async_result<decay_t<CompletionToken>, Signature>, initialized as result(completion_handler); and

— using result.get() as the operand of the return statement.

So correct way to adapt example would be

template <class CompletionToken>
auto async_foo(uint64_t item_id, CompletionToken&& token)
{

    typename boost::asio::async_result<std::decay_t<CompletionToken>, void(std::error_code, std::size_t)>::completion_handler_type
                    handler(std::forward<CompletionToken>(token));
    boost::asio::async_result<std::decay_t<CompletionToken>, void(std::error_code, std::size_t)> result(handler);

    async_request_data(item_id, handler);

    return result.get();
}
Religiosity answered 1/2, 2020 at 11:11 Comment(1)
This does not work with the use_awaitable completion token. One must use the new boost::asio::async_initiate. See my answer here: https://mcmap.net/q/858357/-boost-asio-spawn-yield-as-callbackWriting

© 2022 - 2024 — McMap. All rights reserved.