Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // experimental/use_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_USE_PROMISE_HPP
0013 #define BOOST_ASIO_EXPERIMENTAL_USE_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 <memory>
0021 #include <boost/asio/detail/type_traits.hpp>
0022 
0023 #include <boost/asio/detail/push_options.hpp>
0024 
0025 namespace boost {
0026 namespace asio {
0027 namespace experimental {
0028 
0029 template <typename Allocator = std::allocator<void>>
0030 struct use_promise_t
0031 {
0032   /// The allocator type. The allocator is used when constructing the
0033   /// @c promise object for a given asynchronous operation.
0034   typedef Allocator allocator_type;
0035 
0036   /// Construct using default-constructed allocator.
0037   constexpr use_promise_t()
0038   {
0039   }
0040 
0041   /// Construct using specified allocator.
0042   explicit use_promise_t(const Allocator& allocator)
0043     : allocator_(allocator)
0044   {
0045   }
0046 
0047   /// Obtain allocator.
0048   allocator_type get_allocator() const noexcept
0049   {
0050     return allocator_;
0051   }
0052 
0053   /// Adapts an executor to add the @c use_promise_t completion token as the
0054   /// default.
0055   template <typename InnerExecutor>
0056   struct executor_with_default : InnerExecutor
0057   {
0058     /// Specify @c use_promise_t as the default completion token type.
0059     typedef use_promise_t<Allocator> default_completion_token_type;
0060 
0061     /// Construct the adapted executor from the inner executor type.
0062     executor_with_default(const InnerExecutor& ex) noexcept
0063       : InnerExecutor(ex)
0064     {
0065     }
0066 
0067     /// Convert the specified executor to the inner executor type, then use
0068     /// that to construct the adapted executor.
0069     template <typename OtherExecutor>
0070     executor_with_default(const OtherExecutor& ex,
0071         constraint_t<
0072           is_convertible<OtherExecutor, InnerExecutor>::value
0073         > = 0) noexcept
0074       : InnerExecutor(ex)
0075     {
0076     }
0077   };
0078 
0079   /// Function helper to adapt an I/O object to use @c use_promise_t as its
0080   /// default completion token type.
0081   template <typename T>
0082   static typename decay_t<T>::template rebind_executor<
0083       executor_with_default<typename decay_t<T>::executor_type>
0084     >::other
0085   as_default_on(T&& object)
0086   {
0087     return typename decay_t<T>::template rebind_executor<
0088         executor_with_default<typename decay_t<T>::executor_type>
0089       >::other(static_cast<T&&>(object));
0090   }
0091 
0092   /// Specify an alternate allocator.
0093   template <typename OtherAllocator>
0094   use_promise_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
0095   {
0096     return use_promise_t<OtherAllocator>(allocator);
0097   }
0098 
0099 private:
0100   Allocator allocator_;
0101 };
0102 
0103 constexpr use_promise_t<> use_promise;
0104 
0105 } // namespace experimental
0106 } // namespace asio
0107 } // namespace boost
0108 
0109 #include <boost/asio/detail/pop_options.hpp>
0110 
0111 #include <boost/asio/experimental/impl/use_promise.hpp>
0112 
0113 #endif // BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP