Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/asio/use_future.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // use_future.hpp
0003 // ~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_USE_FUTURE_HPP
0012 #define BOOST_ASIO_USE_FUTURE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/future.hpp>
0020 
0021 #if defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) \
0022   || defined(GENERATING_DOCUMENTATION)
0023 
0024 #include <memory>
0025 #include <boost/asio/detail/type_traits.hpp>
0026 
0027 #include <boost/asio/detail/push_options.hpp>
0028 
0029 namespace boost {
0030 namespace asio {
0031 namespace detail {
0032 
0033 template <typename Function, typename Allocator>
0034 class packaged_token;
0035 
0036 template <typename Function, typename Allocator, typename Result>
0037 class packaged_handler;
0038 
0039 } // namespace detail
0040 
0041 /// A @ref completion_token type that causes an asynchronous operation to return
0042 /// a future.
0043 /**
0044  * The use_future_t class is a completion token type that is used to indicate
0045  * that an asynchronous operation should return a std::future object. A
0046  * use_future_t object may be passed as a completion token to an asynchronous
0047  * operation, typically using the special value @c boost::asio::use_future. For
0048  * example:
0049  *
0050  * @code std::future<std::size_t> my_future
0051  *   = my_socket.async_read_some(my_buffer, boost::asio::use_future); @endcode
0052  *
0053  * The initiating function (async_read_some in the above example) returns a
0054  * future that will receive the result of the operation. If the operation
0055  * completes with an error_code indicating failure, it is converted into a
0056  * system_error and passed back to the caller via the future.
0057  */
0058 template <typename Allocator = std::allocator<void>>
0059 class use_future_t
0060 {
0061 public:
0062   /// The allocator type. The allocator is used when constructing the
0063   /// @c std::promise object for a given asynchronous operation.
0064   typedef Allocator allocator_type;
0065 
0066   /// Construct using default-constructed allocator.
0067   constexpr use_future_t()
0068   {
0069   }
0070 
0071   /// Construct using specified allocator.
0072   explicit use_future_t(const Allocator& allocator)
0073     : allocator_(allocator)
0074   {
0075   }
0076 
0077   /// Specify an alternate allocator.
0078   template <typename OtherAllocator>
0079   use_future_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
0080   {
0081     return use_future_t<OtherAllocator>(allocator);
0082   }
0083 
0084   /// Obtain allocator.
0085   allocator_type get_allocator() const
0086   {
0087     return allocator_;
0088   }
0089 
0090   /// Wrap a function object in a packaged task.
0091   /**
0092    * The @c package function is used to adapt a function object as a packaged
0093    * task. When this adapter is passed as a completion token to an asynchronous
0094    * operation, the result of the function object is returned via a std::future.
0095    *
0096    * @par Example
0097    *
0098    * @code std::future<std::size_t> fut =
0099    *   my_socket.async_read_some(buffer,
0100    *     use_future([](boost::system::error_code ec, std::size_t n)
0101    *       {
0102    *         return ec ? 0 : n;
0103    *       }));
0104    * ...
0105    * std::size_t n = fut.get(); @endcode
0106    */
0107   template <typename Function>
0108 #if defined(GENERATING_DOCUMENTATION)
0109   unspecified
0110 #else // defined(GENERATING_DOCUMENTATION)
0111   detail::packaged_token<decay_t<Function>, Allocator>
0112 #endif // defined(GENERATING_DOCUMENTATION)
0113   operator()(Function&& f) const;
0114 
0115 private:
0116   // Helper type to ensure that use_future can be constexpr default-constructed
0117   // even when std::allocator<void> can't be.
0118   struct std_allocator_void
0119   {
0120     constexpr std_allocator_void()
0121     {
0122     }
0123 
0124     operator std::allocator<void>() const
0125     {
0126       return std::allocator<void>();
0127     }
0128   };
0129 
0130   conditional_t<
0131     is_same<std::allocator<void>, Allocator>::value,
0132     std_allocator_void, Allocator> allocator_;
0133 };
0134 
0135 /// A @ref completion_token object that causes an asynchronous operation to
0136 /// return a future.
0137 /**
0138  * See the documentation for boost::asio::use_future_t for a usage example.
0139  */
0140 BOOST_ASIO_INLINE_VARIABLE constexpr use_future_t<> use_future;
0141 
0142 } // namespace asio
0143 } // namespace boost
0144 
0145 #include <boost/asio/detail/pop_options.hpp>
0146 
0147 #include <boost/asio/impl/use_future.hpp>
0148 
0149 #endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS)
0150        //   || defined(GENERATING_DOCUMENTATION)
0151 
0152 #endif // BOOST_ASIO_USE_FUTURE_HPP