Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:03:04

0001 //
0002 // experimental/promise.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2021-2023 Klemens D. Morgenstern
0006 //                         (klemens dot morgenstern at gmx dot net)
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 
0012 #ifndef BOOST_ASIO_EXPERIMENTAL_PROMISE_HPP
0013 #define BOOST_ASIO_EXPERIMENTAL_PROMISE_HPP
0014 
0015 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0016 # pragma once
0017 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0018 
0019 #include <boost/asio/detail/config.hpp>
0020 #include <boost/asio/detail/type_traits.hpp>
0021 #include <boost/asio/any_io_executor.hpp>
0022 #include <boost/asio/associated_cancellation_slot.hpp>
0023 #include <boost/asio/associated_executor.hpp>
0024 #include <boost/asio/bind_executor.hpp>
0025 #include <boost/asio/cancellation_signal.hpp>
0026 #include <boost/asio/dispatch.hpp>
0027 #include <boost/asio/experimental/impl/promise.hpp>
0028 #include <boost/asio/post.hpp>
0029 
0030 #include <algorithm>
0031 
0032 #include <boost/asio/detail/push_options.hpp>
0033 
0034 namespace boost {
0035 namespace asio {
0036 namespace experimental {
0037 
0038 template <typename T>
0039 struct is_promise : std::false_type {};
0040 
0041 template <typename ... Ts>
0042 struct is_promise<promise<Ts...>> : std::true_type {};
0043 
0044 template <typename T>
0045 constexpr bool is_promise_v = is_promise<T>::value;
0046 
0047 template <typename ... Ts>
0048 struct promise_value_type
0049 {
0050   using type = std::tuple<Ts...>;
0051 };
0052 
0053 template <typename T>
0054 struct promise_value_type<T>
0055 {
0056   using type = T;
0057 };
0058 
0059 template <>
0060 struct promise_value_type<>
0061 {
0062   using type = std::tuple<>;
0063 };
0064 
0065 #if defined(GENERATING_DOCUMENTATION)
0066 /// A disposable handle for an eager operation.
0067 /**
0068  * @tparam Signature The signature of the operation.
0069  *
0070  * @tparam Executor The executor to be used by the promise (taken from the
0071  * operation).
0072  *
0073  * @tparam Allocator The allocator used for the promise. Can be set through
0074  * use_allocator.
0075  *
0076  * A promise can be used to initiate an asynchronous option that can be
0077  * completed later. If the promise gets destroyed before completion, the
0078  * operation gets a cancel signal and the result is ignored.
0079  *
0080  * A promise fulfills the requirements of async_operation.
0081  *
0082  * @par Examples
0083  * Reading and writing from one coroutine.
0084  * @code
0085  * awaitable<void> read_write_some(boost::asio::ip::tcp::socket & sock,
0086  *     boost::asio::mutable_buffer read_buf, boost::asio::const_buffer to_write)
0087  * {
0088  *   auto p = boost::asio::async_read(read_buf, boost::asio::use_awaitable);
0089  *   co_await boost::asio::async_write_some(to_write, boost::asio::deferred);
0090  *   co_await p;
0091  * }
0092  * @endcode
0093  */
0094 template<typename Signature = void(),
0095     typename Executor = boost::asio::any_io_executor,
0096     typename Allocator = std::allocator<void>>
0097 struct promise
0098 #else
0099 template <typename ... Ts, typename Executor, typename Allocator>
0100 struct promise<void(Ts...), Executor,  Allocator>
0101 #endif // defined(GENERATING_DOCUMENTATION)
0102 {
0103   /// The value that's returned by the promise.
0104   using value_type = typename promise_value_type<Ts...>::type;
0105 
0106   /// Cancel the promise. Usually done through the destructor.
0107   void cancel(cancellation_type level = cancellation_type::all)
0108   {
0109     if (impl_ && !impl_->done)
0110     {
0111       boost::asio::dispatch(impl_->executor,
0112           [level, impl = impl_]{ impl->cancel.emit(level); });
0113     }
0114   }
0115 
0116   /// Check if the promise is completed already.
0117   bool completed() const noexcept
0118   {
0119     return impl_ && impl_->done;
0120   }
0121 
0122   /// Wait for the promise to become ready.
0123   template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void(Ts...)) CompletionToken>
0124   inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(Ts...))
0125   operator()(CompletionToken&& token)
0126   {
0127     assert(impl_);
0128 
0129     return async_initiate<CompletionToken, void(Ts...)>(
0130         initiate_async_wait{impl_}, token);
0131   }
0132 
0133   promise() = delete;
0134   promise(const promise& ) = delete;
0135   promise(promise&& ) noexcept = default;
0136 
0137   /// Destruct the promise and cancel the operation.
0138   /**
0139    * It is safe to destruct a promise of a promise that didn't complete.
0140    */
0141   ~promise() { cancel(); }
0142 
0143 
0144 private:
0145 #if !defined(GENERATING_DOCUMENTATION)
0146   template <typename, typename, typename> friend struct promise;
0147   friend struct detail::promise_handler<void(Ts...), Executor, Allocator>;
0148 #endif // !defined(GENERATING_DOCUMENTATION)
0149 
0150   std::shared_ptr<detail::promise_impl<
0151     void(Ts...), Executor, Allocator>> impl_;
0152 
0153   promise(
0154       std::shared_ptr<detail::promise_impl<
0155         void(Ts...), Executor, Allocator>> impl)
0156     : impl_(impl)
0157   {
0158   }
0159 
0160   struct initiate_async_wait
0161   {
0162     std::shared_ptr<detail::promise_impl<
0163       void(Ts...), Executor, Allocator>> self_;
0164 
0165     template <typename WaitHandler>
0166     void operator()(WaitHandler&& handler) const
0167     {
0168       const auto alloc = get_associated_allocator(
0169           handler, self_->get_allocator());
0170 
0171       auto cancel = get_associated_cancellation_slot(handler);
0172 
0173       if (self_->done)
0174       {
0175         auto exec = boost::asio::get_associated_executor(
0176             handler, self_->get_executor());
0177 
0178         boost::asio::post(exec,
0179             [self = std::move(self_),
0180               handler = std::forward<WaitHandler>(handler)]() mutable
0181             {
0182               self->apply(std::move(handler));
0183             });
0184       }
0185       else
0186       {
0187         if (cancel.is_connected())
0188         {
0189           struct cancel_handler
0190           {
0191             std::weak_ptr<detail::promise_impl<
0192               void(Ts...), Executor, Allocator>> self;
0193 
0194             cancel_handler(
0195                 std::weak_ptr<detail::promise_impl<
0196                   void(Ts...), Executor, Allocator>> self)
0197               : self(std::move(self))
0198             {
0199             }
0200 
0201             void operator()(cancellation_type level) const
0202             {
0203               if (auto p = self.lock())
0204               {
0205                 p->cancel.emit(level);
0206                 p->cancel_();
0207               }
0208             }
0209           };
0210           cancel.template emplace<cancel_handler>(self_);
0211         }
0212 
0213         self_->set_completion(alloc, std::forward<WaitHandler>(handler));
0214       }
0215     }
0216   };
0217 };
0218 
0219 } // namespace experimental
0220 
0221 } // namespace asio
0222 } // namespace boost
0223 
0224 #include <boost/asio/detail/pop_options.hpp>
0225 
0226 #endif // BOOST_ASIO_EXPERIMENTAL_PROMISE_HPP