// // 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 BOOST_ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP #define BOOST_ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include namespace boost { namespace asio { namespace experimental { template struct promise; namespace detail { template struct promise_impl; template struct promise_impl { using result_type = std::tuple; promise_impl(Executor executor = {}) : executor(std::move(executor)) { } std::optional result; bool done{false}; detail::completion_handler_erasure completion; cancellation_signal cancel; Executor executor; }; template struct promise_handler; template struct promise_handler; template struct promise_handler { using promise_type = promise; promise_handler(Executor executor) // get_associated_allocator(exec) : impl_{ std::allocate_shared>( get_associated_allocator(executor))} { impl_->executor = std::move(executor); } std::shared_ptr> impl_; using cancellation_slot_type = cancellation_slot; cancellation_slot_type get_cancellation_slot() const noexcept { return impl_->cancel.slot(); } auto make_promise() -> promise { return {impl_}; } void operator()(std::remove_reference_t... ts) { assert(impl_); impl_->result.emplace(std::move(ts)...); impl_->done = true; if (auto f = std::exchange(impl_->completion, nullptr); f != nullptr) std::apply(std::move(f), std::move(*impl_->result)); } }; } // namespace detail } // namespace experimental } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP