Open gegles opened 4 years ago
That would be a good addition.
With C++14 lambda move captures, this will be a simple thing to do. Adding C++11 support will be more tricky though.
In the initial version we could move it only for C++14 onward. This may be most cleanly done with a macro (warning: untested code)
#if defined(__cpp_init_captures)
# define TRIAL_DATAGRAM_CXX14_MOVE_CAPTURE(x) x = std::move(x)
#else
# define TRIAL_DATAGRAM_CXX14_MOVE_CAPTURE(x) x
#endif
Ok, so I've been banging my head on this for a few hours.. trying to leverage C++14 generalized lambda capture but also looking at https://github.com/boostorg/asio/blob/develop/example/cpp14/operations/composed_5.cpp for inspiration, I came up with the following....
template <typename CompletionToken>
auto acceptor::async_accept(CompletionToken &&token) -> trial::net::async_result_t<CompletionToken, void(boost::system::error_code, socket_type)> {
auto initiation = [this](auto&& completion_handler)
{
struct accept_handler
{
std::unique_ptr<socket_type> socket_ptr_;
typename std::decay<decltype(completion_handler)>::type handler_;
void operator()(boost::system::error_code error) const
{
handler_(error, std::move(*socket_ptr_));
}
using executor_type = boost::asio::associated_executor_t<typename std::decay<decltype(completion_handler)>::type, socket_type::executor_type>;
executor_type get_executor() noexcept
{
return boost::asio::get_associated_executor(
handler_, socket_ptr_->get_executor());
}
using allocator_type = boost::asio::associated_allocator_t<
typename std::decay<decltype(completion_handler)>::type,
std::allocator<void>>;
allocator_type get_allocator() const noexcept
{
return boost::asio::get_associated_allocator(
handler_, std::allocator<void>{});
}
};
auto socket_ptr = std::make_unique<socket_type>(get_executor());
this->async_accept(*socket_ptr, accept_handler{std::move(socket_ptr), std::forward<decltype(completion_handler)>(completion_handler)});
};
return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code, socket_type)>(initiation, token);
}
Sadly, I get some compilation issues and my brain can't compute what's happening:
/Users/gegles/Workspaces/ibm/fasp.io/3rdparty/trial.datagram/include/trial/datagram/detail/acceptor.ipp:68:26: error: call to implicitly-deleted copy constructor of 'accept_handler'
[this, &socket, handler]
Any hint/help appreciated...
Cheers. G.
An alternative version could also be:
template <typename CompletionToken>
auto acceptor::async_accept(CompletionToken &&token) -> trial::net::async_result_t<CompletionToken, void(boost::system::error_code, socket_type)> {
auto initiation = [this](auto&& completion_handler)
{
auto socket_ptr = std::make_unique<socket_type>(get_executor());
this->async_accept(*socket_ptr, [socket_ptr = std::move(socket_ptr)](boost::system::error_code ec) {
std::cout << "Accepted" << std::endl;
});
};
return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code, socket_type)>(initiation, token);
}
But that too fails to compile with the following:
/Users/gegles/Workspaces/ibm/fasp.io/3rdparty/trial.datagram/include/trial/datagram/detail/acceptor.ipp:68:26: error: call to implicitly-deleted copy constructor of '(lambda at /Users/gegles/Workspaces/ibm/fasp.io/3rdparty/trial.datagram/include/trial/datagram/detail/acceptor.ipp:129:41)'
[this, &socket, handler]
You are trying to do two things at the same time: move capture and async_result. I suggest that you attack those separately.
With regards to move capture, my thinking was simply to use the above-mentioned macro like this:
diff --git a/include/trial/datagram/detail/acceptor.ipp b/include/trial/datagram/detail/acceptor.ipp
index ddc2837..526bc16 100644
--- a/include/trial/datagram/detail/acceptor.ipp
+++ b/include/trial/datagram/detail/acceptor.ipp
@@ -35,7 +35,7 @@ void acceptor::async_accept(socket_type& socket,
multiplexer->async_accept
(socket,
- [this, &socket, handler]
+ [this, &socket, TRIAL_DATAGRAM_CXX14_MOVE_CAPTURE(handler)]
(const boost::system::error_code& error)
{
if (!error)
Any particular reason why it would be hard to do?
I will give it a try and submit a PR if I succeed, but my asio + move internals chops are not super great.... ;-)