use_future.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // use_future.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  64. /// (Deprecated: Use rebind().) Specify an alternate allocator.
  65. template <typename OtherAllocator>
  66. use_future_t<OtherAllocator> operator[](const OtherAllocator& allocator) const
  67. {
  68. return use_future_t<OtherAllocator>(allocator);
  69. }
  70. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  71. /// Specify an alternate allocator.
  72. template <typename OtherAllocator>
  73. use_future_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
  74. {
  75. return use_future_t<OtherAllocator>(allocator);
  76. }
  77. /// Obtain allocator.
  78. allocator_type get_allocator() const
  79. {
  80. return allocator_;
  81. }
  82. /// Wrap a function object in a packaged task.
  83. /**
  84. * The @c package function is used to adapt a function object as a packaged
  85. * task. When this adapter is passed as a completion token to an asynchronous
  86. * operation, the result of the function object is retuned via a std::future.
  87. *
  88. * @par Example
  89. *
  90. * @code std::future<std::size_t> fut =
  91. * my_socket.async_read_some(buffer,
  92. * use_future([](boost::system::error_code ec, std::size_t n)
  93. * {
  94. * return ec ? 0 : n;
  95. * }));
  96. * ...
  97. * std::size_t n = fut.get(); @endcode
  98. */
  99. template <typename Function>
  100. #if defined(GENERATING_DOCUMENTATION)
  101. unspecified
  102. #else // defined(GENERATING_DOCUMENTATION)
  103. detail::packaged_token<decay_t<Function>, Allocator>
  104. #endif // defined(GENERATING_DOCUMENTATION)
  105. operator()(Function&& f) const;
  106. private:
  107. // Helper type to ensure that use_future can be constexpr default-constructed
  108. // even when std::allocator<void> can't be.
  109. struct std_allocator_void
  110. {
  111. constexpr std_allocator_void()
  112. {
  113. }
  114. operator std::allocator<void>() const
  115. {
  116. return std::allocator<void>();
  117. }
  118. };
  119. conditional_t<
  120. is_same<std::allocator<void>, Allocator>::value,
  121. std_allocator_void, Allocator> allocator_;
  122. };
  123. /// A @ref completion_token object that causes an asynchronous operation to
  124. /// return a future.
  125. /**
  126. * See the documentation for boost::asio::use_future_t for a usage example.
  127. */
  128. BOOST_ASIO_INLINE_VARIABLE constexpr use_future_t<> use_future;
  129. } // namespace asio
  130. } // namespace boost
  131. #include <boost/asio/detail/pop_options.hpp>
  132. #include <boost/asio/impl/use_future.hpp>
  133. #endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS)
  134. // || defined(GENERATING_DOCUMENTATION)
  135. #endif // BOOST_ASIO_USE_FUTURE_HPP