]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/experimental/impl/promise.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / asio / experimental / impl / promise.hpp
1 //
2 // experimental/impl/promise.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2021-2022 Klemens D. Morgenstern
6 // (klemens dot morgenstern at gmx dot net)
7 //
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 //
11 #ifndef BOOST_ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP
12 #define BOOST_ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/cancellation_signal.hpp>
19 #include <boost/asio/experimental/detail/completion_handler_erasure.hpp>
20 #include <tuple>
21 #include <optional>
22
23 #include <boost/asio/detail/push_options.hpp>
24
25 namespace boost {
26 namespace asio {
27 namespace experimental {
28
29 template<typename Signature = void(), typename Executor = any_io_executor>
30 struct promise;
31
32 namespace detail {
33
34 template<typename Signature, typename Executor>
35 struct promise_impl;
36
37 template<typename ... Ts, typename Executor>
38 struct promise_impl<void(Ts...), Executor>
39 {
40 using result_type = std::tuple<Ts...>;
41
42 promise_impl(Executor executor = {})
43 : executor(std::move(executor))
44 {
45 }
46
47 std::optional<result_type> result;
48 bool done{false};
49 detail::completion_handler_erasure<void(Ts...), Executor> completion;
50 cancellation_signal cancel;
51 Executor executor;
52 };
53
54 template<typename Signature = void(), typename Executor = any_io_executor>
55 struct promise_handler;
56
57 template<typename Signature, typename Executor>
58 struct promise_handler;
59
60 template<typename ... Ts, typename Executor>
61 struct promise_handler<void(Ts...), Executor>
62 {
63 using promise_type = promise<void(Ts...), Executor>;
64
65 promise_handler(Executor executor) // get_associated_allocator(exec)
66 : impl_{
67 std::allocate_shared<promise_impl<void(Ts...), Executor>>(
68 get_associated_allocator(executor))}
69 {
70 impl_->executor = std::move(executor);
71 }
72
73 std::shared_ptr<promise_impl<void(Ts...), Executor>> impl_;
74
75 using cancellation_slot_type = cancellation_slot;
76
77 cancellation_slot_type get_cancellation_slot() const noexcept
78 {
79 return impl_->cancel.slot();
80 }
81
82 auto make_promise() -> promise<void(Ts...), Executor>
83 {
84 return {impl_};
85 }
86
87 void operator()(std::remove_reference_t<Ts>... ts)
88 {
89 assert(impl_);
90 impl_->result.emplace(std::move(ts)...);
91 impl_->done = true;
92 if (auto f = std::exchange(impl_->completion, nullptr); f != nullptr)
93 std::apply(std::move(f), std::move(*impl_->result));
94 }
95 };
96
97 } // namespace detail
98 } // namespace experimental
99 } // namespace asio
100 } // namespace boost
101
102 #include <boost/asio/detail/pop_options.hpp>
103
104 #endif // BOOST_ASIO_EXPERIMENTAL_IMPL_PROMISE_HPP