From 22923f80412a67d82fd7ca8c19616df5b5579579 Mon Sep 17 00:00:00 2001 From: Klemens Morgenstern Date: Thu, 26 Oct 2023 11:36:51 +0800 Subject: [PATCH] adapted to recent asio changes. --- include/boost/cobalt/detail/spawn.hpp | 91 +++++++++++++++++---------- test/wrappers.cpp | 19 ++++-- 2 files changed, 71 insertions(+), 39 deletions(-) diff --git a/include/boost/cobalt/detail/spawn.hpp b/include/boost/cobalt/detail/spawn.hpp index 782bc419..a6d653f9 100644 --- a/include/boost/cobalt/detail/spawn.hpp +++ b/include/boost/cobalt/detail/spawn.hpp @@ -49,25 +49,36 @@ struct async_initiate_spawn p->promise->exec.emplace(exec); p->promise->exec_ = exec; + + struct completion_handler + { + using allocator_type = std::decay_t; + + allocator_type get_allocator() const { return alloc; } + allocator_type alloc; + + using executor_type = std::decay_t; + const executor_type &get_executor() const { return exec; } + executor_type exec; + + decltype(recs) r; + Handler h; + + void operator()() + { + auto ex = r->exception; + T rr{}; + if (r->result) + rr = std::move(*r->result); + r.reset(); + std::move(h)(ex, std::move(rr)); + } + }; + p->awaited_from.reset(detail::post_coroutine( - asio::bind_executor( - asio::get_associated_executor(h, exec), - asio::bind_allocator( - alloc, - [r = std::move(recs), - h = std::move(h)]() mutable - { - - auto ex = r->exception; - T rr{}; - if (r->result) - rr = std::move(*r->result); - r.reset(); - h(ex, std::move(rr)); - } - ) - ) - ).address()); + completion_handler{ + alloc, asio::get_associated_executor(h, exec), std::move(recs), std::move(h) + }).address()); asio::dispatch(exec, std::coroutine_handle>::from_promise(*p->promise)); } @@ -100,22 +111,34 @@ struct async_initiate_spawn p->promise->exec.emplace(exec); p->promise->exec_ = exec; - p->awaited_from.reset(detail::post_coroutine( - asio::bind_executor( - asio::get_associated_executor(h, exec), - asio::bind_allocator( - alloc, - [r = std::move(recs), - h = std::move(h)]() mutable - { - auto ex = r->exception; - r.reset(); - h(ex); - } - ) - ) - ).address()); - + + struct completion_handler + { + using allocator_type = std::decay_t; + + const allocator_type &get_allocator() const { return alloc; } + + allocator_type alloc; + + using executor_type = std::decay_t; + const executor_type &get_executor() const { return exec; } + + executor_type exec; + decltype(recs) r; + Handler h; + + void operator()() + { + auto ex = r->exception; + r.reset(); + std::move(h)(ex); + } + }; + + p->awaited_from.reset(detail::post_coroutine(completion_handler{ + alloc, asio::get_associated_executor(h, exec), std::move(recs), std::forward(h) + }).address()); + asio::dispatch(exec, std::coroutine_handle>::from_promise(*p->promise)); } }; diff --git a/test/wrappers.cpp b/test/wrappers.cpp index 39e8cc24..ce43f1c1 100644 --- a/test/wrappers.cpp +++ b/test/wrappers.cpp @@ -23,14 +23,23 @@ BOOST_AUTO_TEST_CASE(regular) #if !defined(BOOST_COBALT_NO_PMR) char buf[512]; boost::cobalt::pmr::monotonic_buffer_resource res{buf, 512}; + struct completion + { + bool & ran; + using allocator_type = boost::cobalt::pmr::polymorphic_allocator; + allocator_type get_allocator() const { return alloc; } + boost::cobalt::pmr::polymorphic_allocator alloc; + void operator()() + { + ran = true; + } + }; + auto p = boost::cobalt::detail::post_coroutine(ctx.get_executor(), - boost::asio::bind_allocator( - boost::cobalt::pmr::polymorphic_allocator(&res), - [&]{ran = true;} - ) + completion{ran, boost::cobalt::pmr::polymorphic_allocator(&res)} ); #else - auto p = boost::cobalt::detail::post_coroutine(ctx.get_executor(), [&]{ran = true;}); + auto p = boost::cobalt::detail::post_coroutine(ctx.get_executor(), [&]{ran = true;}); #endif BOOST_CHECK(p); BOOST_CHECK(!ran);