Coder Social home page Coder Social logo

Comments (5)

breese avatar breese commented on July 25, 2024

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.

from trial.datagram.

breese avatar breese commented on July 25, 2024

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

from trial.datagram.

gegles avatar gegles commented on July 25, 2024

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.

from trial.datagram.

gegles avatar gegles commented on July 25, 2024

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]

from trial.datagram.

breese avatar breese commented on July 25, 2024

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)

from trial.datagram.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.