// // experimental/impl/promise.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2021-2022 Klemens D. Morgenstern // (klemens dot morgenstern at gmx dot net) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP #define ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/cancellation_signal.hpp" #include "asio/detail/utility.hpp" #include #include "asio/detail/push_options.hpp" namespace asio { namespace experimental { template> struct promise; namespace detail { template struct promise_impl; template struct promise_impl { using result_type = std::tuple; promise_impl(Allocator allocator, Executor executor) : allocator(std::move(allocator)), executor(std::move(executor)) { } promise_impl(const promise_impl&) = delete; ~promise_impl() { if (completion) this->cancel_(); if (done) reinterpret_cast(&result)->~result_type(); } typename aligned_storage::type result; std::atomic done{false}; cancellation_signal cancel; Allocator allocator; Executor executor; template void apply_impl(Func f, asio::detail::index_sequence) { auto& result_type = *reinterpret_cast(&result); f(std::get(std::move(result_type))...); } using allocator_type = Allocator; allocator_type get_allocator() {return allocator;} using executor_type = Executor; executor_type get_executor() {return executor;} template void apply(Func f) { apply_impl(std::forward(f), asio::detail::make_index_sequence{}); } struct completion_base { virtual void invoke(Ts&&...ts) = 0; }; template struct completion_impl final : completion_base { WaitHandler_ handler; Alloc allocator; void invoke(Ts&&... ts) { auto h = std::move(handler); using alloc_t = typename std::allocator_traits< typename asio::decay::type>::template rebind_alloc; alloc_t alloc_{allocator}; this->~completion_impl(); std::allocator_traits::deallocate(alloc_, this, 1u); std::move(h)(std::forward(ts)...); } template completion_impl(Alloc_&& alloc, Handler_&& wh) : handler(std::forward(wh)), allocator(std::forward(alloc)) { } }; completion_base* completion = nullptr; typename asio::aligned_storage::type completion_opt; template void set_completion(Alloc&& alloc, Handler&& handler) { if (completion) cancel_(); using impl_t = completion_impl< typename asio::decay::type, Handler>; using alloc_t = typename std::allocator_traits< typename asio::decay::type>::template rebind_alloc; alloc_t alloc_{alloc}; auto p = std::allocator_traits::allocate(alloc_, 1u); completion = new (p) impl_t(std::forward(alloc), std::forward(handler)); } template void complete(T_&&... ts) { assert(completion); std::exchange(completion, nullptr)->invoke(std::forward(ts)...); } template void cancel_impl_(std::exception_ptr*, T_*...) { complete( std::make_exception_ptr( asio::system_error( asio::error::operation_aborted)), T_{}...); } template void cancel_impl_(asio::error_code*, T_*...) { complete(asio::error::operation_aborted, T_{}...); } template void cancel_impl_(T_*...) { complete(T_{}...); } void cancel_() { cancel_impl_(static_cast(nullptr)...); } }; template struct promise_handler; template struct promise_handler { using promise_type = promise; promise_handler( Allocator allocator, Executor executor) // get_associated_allocator(exec) : impl_( std::allocate_shared>( allocator, allocator, executor)) { } std::shared_ptr> impl_; using cancellation_slot_type = cancellation_slot; cancellation_slot_type get_cancellation_slot() const noexcept { return impl_->cancel.slot(); } using allocator_type = Allocator; allocator_type get_allocator() const noexcept { return impl_->get_allocator(); } using executor_type = Executor; Executor get_executor() const noexcept { return impl_->get_executor(); } auto make_promise() -> promise { return promise{impl_}; } void operator()(std::remove_reference_t... ts) { assert(impl_); using result_type = typename promise_impl< void(Ts...), allocator_type, executor_type>::result_type ; new (&impl_->result) result_type(std::move(ts)...); impl_->done = true; if (impl_->completion) impl_->complete(std::move(ts)...); } }; } // namespace detail } // namespace experimental } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP