use_future.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // use_future.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. /// Class used to specify that an asynchronous operation should return a future.
  31. /**
  32. * The use_future_t class is used to indicate that an asynchronous operation
  33. * should return a std::future object. A use_future_t object may be passed as a
  34. * handler to an asynchronous operation, typically using the special value @c
  35. * boost::asio::use_future. For example:
  36. *
  37. * @code std::future<std::size_t> my_future
  38. * = my_socket.async_read_some(my_buffer, boost::asio::use_future); @endcode
  39. *
  40. * The initiating function (async_read_some in the above example) returns a
  41. * future that will receive the result of the operation. If the operation
  42. * completes with an error_code indicating failure, it is converted into a
  43. * system_error and passed back to the caller via the future.
  44. */
  45. template <typename Allocator = std::allocator<void> >
  46. class use_future_t
  47. {
  48. public:
  49. /// The allocator type. The allocator is used when constructing the
  50. /// @c std::promise object for a given asynchronous operation.
  51. typedef Allocator allocator_type;
  52. /// Construct using default-constructed allocator.
  53. BOOST_ASIO_CONSTEXPR use_future_t()
  54. {
  55. }
  56. /// Construct using specified allocator.
  57. explicit use_future_t(const Allocator& allocator)
  58. : allocator_(allocator)
  59. {
  60. }
  61. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  62. /// (Deprecated: Use rebind().) Specify an alternate allocator.
  63. template <typename OtherAllocator>
  64. use_future_t<OtherAllocator> operator[](const OtherAllocator& allocator) const
  65. {
  66. return use_future_t<OtherAllocator>(allocator);
  67. }
  68. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  69. /// Specify an alternate allocator.
  70. template <typename OtherAllocator>
  71. use_future_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
  72. {
  73. return use_future_t<OtherAllocator>(allocator);
  74. }
  75. /// Obtain allocator.
  76. allocator_type get_allocator() const
  77. {
  78. return allocator_;
  79. }
  80. /// Wrap a function object in a packaged task.
  81. /**
  82. * The @c package function is used to adapt a function object as a packaged
  83. * task. When this adapter is passed as a completion token to an asynchronous
  84. * operation, the result of the function object is retuned via a std::future.
  85. *
  86. * @par Example
  87. *
  88. * @code std::future<std::size_t> fut =
  89. * my_socket.async_read_some(buffer,
  90. * use_future([](boost::system::error_code ec, std::size_t n)
  91. * {
  92. * return ec ? 0 : n;
  93. * }));
  94. * ...
  95. * std::size_t n = fut.get(); @endcode
  96. */
  97. template <typename Function>
  98. #if defined(GENERATING_DOCUMENTATION)
  99. unspecified
  100. #else // defined(GENERATING_DOCUMENTATION)
  101. detail::packaged_token<typename decay<Function>::type, Allocator>
  102. #endif // defined(GENERATING_DOCUMENTATION)
  103. operator()(BOOST_ASIO_MOVE_ARG(Function) f) const;
  104. private:
  105. // Helper type to ensure that use_future can be constexpr default-constructed
  106. // even when std::allocator<void> can't be.
  107. struct std_allocator_void
  108. {
  109. BOOST_ASIO_CONSTEXPR std_allocator_void()
  110. {
  111. }
  112. operator std::allocator<void>() const
  113. {
  114. return std::allocator<void>();
  115. }
  116. };
  117. typename conditional<
  118. is_same<std::allocator<void>, Allocator>::value,
  119. std_allocator_void, Allocator>::type allocator_;
  120. };
  121. /// A special value, similar to std::nothrow.
  122. /**
  123. * See the documentation for boost::asio::use_future_t for a usage example.
  124. */
  125. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  126. constexpr use_future_t<> use_future;
  127. #elif defined(BOOST_ASIO_MSVC)
  128. __declspec(selectany) use_future_t<> use_future;
  129. #endif
  130. } // namespace asio
  131. } // namespace boost
  132. #include <boost/asio/detail/pop_options.hpp>
  133. #include <boost/asio/impl/use_future.hpp>
  134. #endif // defined(BOOST_ASIO_HAS_STD_FUTURE_CLASS)
  135. // || defined(GENERATING_DOCUMENTATION)
  136. #endif // BOOST_ASIO_USE_FUTURE_HPP