use_future.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // use_future.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_USE_FUTURE_HPP
  11. #define BOOST_ASIO_USE_FUTURE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/future.hpp>
  17. #if defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <memory>
  20. #include <boost/asio/detail/type_traits.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. template <typename Function, typename Allocator>
  26. class packaged_token;
  27. template <typename Function, typename Allocator, typename Result>
  28. class packaged_handler;
  29. } // namespace detail
  30. /// A @ref completion_token type that causes an asynchronous operation to return
  31. /// a future.
  32. /**
  33. * The use_future_t class is a completion token type that is used to indicate
  34. * that an asynchronous operation should return a std::future object. A
  35. * use_future_t object may be passed as a completion token to an asynchronous
  36. * operation, typically using the special value @c boost::asio::use_future. For
  37. * example:
  38. *
  39. * @code std::future<std::size_t> my_future
  40. * = my_socket.async_read_some(my_buffer, boost::asio::use_future); @endcode
  41. *
  42. * The initiating function (async_read_some in the above example) returns a
  43. * future that will receive the result of the operation. If the operation
  44. * completes with an error_code indicating failure, it is converted into a
  45. * system_error and passed back to the caller via the future.
  46. */
  47. template <typename Allocator = std::allocator<void>>
  48. class use_future_t
  49. {
  50. public:
  51. /// The allocator type. The allocator is used when constructing the
  52. /// @c std::promise object for a given asynchronous operation.
  53. typedef Allocator allocator_type;
  54. /// Construct using default-constructed allocator.
  55. constexpr use_future_t()
  56. {
  57. }
  58. /// Construct using specified allocator.
  59. explicit use_future_t(const Allocator& allocator)
  60. : allocator_(allocator)
  61. {
  62. }
  63. /// Specify an alternate allocator.
  64. template <typename OtherAllocator>
  65. use_future_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
  66. {
  67. return use_future_t<OtherAllocator>(allocator);
  68. }
  69. /// Obtain allocator.
  70. allocator_type get_allocator() const
  71. {
  72. return allocator_;
  73. }
  74. /// Wrap a function object in a packaged task.
  75. /**
  76. * The @c package function is used to adapt a function object as a packaged
  77. * task. When this adapter is passed as a completion token to an asynchronous
  78. * operation, the result of the function object is returned via a std::future.
  79. *
  80. * @par Example
  81. *
  82. * @code std::future<std::size_t> fut =
  83. * my_socket.async_read_some(buffer,
  84. * use_future([](boost::system::error_code ec, std::size_t n)
  85. * {
  86. * return ec ? 0 : n;
  87. * }));
  88. * ...
  89. * std::size_t n = fut.get(); @endcode
  90. */
  91. template <typename Function>
  92. #if defined(GENERATING_DOCUMENTATION)
  93. unspecified
  94. #else // defined(GENERATING_DOCUMENTATION)
  95. detail::packaged_token<decay_t<Function>, Allocator>
  96. #endif // defined(GENERATING_DOCUMENTATION)
  97. operator()(Function&& f) const;
  98. private:
  99. // Helper type to ensure that use_future can be constexpr default-constructed
  100. // even when std::allocator<void> can't be.
  101. struct std_allocator_void
  102. {
  103. constexpr std_allocator_void()
  104. {
  105. }
  106. operator std::allocator<void>() const
  107. {
  108. return std::allocator<void>();
  109. }
  110. };
  111. conditional_t<
  112. is_same<std::allocator<void>, Allocator>::value,
  113. std_allocator_void, Allocator> allocator_;
  114. };
  115. /// A @ref completion_token object that causes an asynchronous operation to
  116. /// return a future.
  117. /**
  118. * See the documentation for boost::asio::use_future_t for a usage example.
  119. */
  120. BOOST_ASIO_INLINE_VARIABLE constexpr use_future_t<> use_future;
  121. } // namespace asio
  122. } // namespace boost
  123. #include <boost/asio/detail/pop_options.hpp>
  124. #include <boost/asio/impl/use_future.hpp>
  125. #endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS)
  126. // || defined(GENERATING_DOCUMENTATION)
  127. #endif // BOOST_ASIO_USE_FUTURE_HPP