async_result.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // async_result.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_ASYNC_RESULT_HPP
  11. #define BOOST_ASIO_ASYNC_RESULT_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/type_traits.hpp>
  17. #include <boost/asio/handler_type.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. /// An interface for customising the behaviour of an initiating function.
  22. /**
  23. * The async_result traits class is used for determining:
  24. *
  25. * @li the concrete completion handler type to be called at the end of the
  26. * asynchronous operation;
  27. *
  28. * @li the initiating function return type; and
  29. *
  30. * @li how the return value of the initiating function is obtained.
  31. *
  32. * The trait allows the handler and return types to be determined at the point
  33. * where the specific completion handler signature is known.
  34. *
  35. * This template may be specialised for user-defined completion token types.
  36. * The primary template assumes that the CompletionToken is the completion
  37. * handler.
  38. */
  39. #if defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  40. template <typename CompletionToken, typename Signature>
  41. #else // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  42. template <typename CompletionToken, typename Signature = void>
  43. #endif // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  44. class async_result
  45. {
  46. public:
  47. #if defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  48. /// The concrete completion handler type for the specific signature.
  49. typedef CompletionToken completion_handler_type;
  50. /// The return type of the initiating function.
  51. typedef void return_type;
  52. #else // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  53. // For backward compatibility, determine the concrete completion handler type
  54. // by using the legacy handler_type trait.
  55. typedef typename handler_type<CompletionToken, Signature>::type
  56. completion_handler_type;
  57. // For backward compatibility, determine the initiating function return type
  58. // using the legacy single-parameter version of async_result.
  59. typedef typename async_result<completion_handler_type>::type return_type;
  60. #endif // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  61. /// Construct an async result from a given handler.
  62. /**
  63. * When using a specalised async_result, the constructor has an opportunity
  64. * to initialise some state associated with the completion handler, which is
  65. * then returned from the initiating function.
  66. */
  67. explicit async_result(completion_handler_type& h)
  68. #if defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  69. // No data members to initialise.
  70. #else // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  71. : legacy_result_(h)
  72. #endif // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  73. {
  74. (void)h;
  75. }
  76. /// Obtain the value to be returned from the initiating function.
  77. return_type get()
  78. {
  79. #if defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  80. // Nothing to do.
  81. #else // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  82. return legacy_result_.get();
  83. #endif // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  84. }
  85. private:
  86. async_result(const async_result&) BOOST_ASIO_DELETED;
  87. async_result& operator=(const async_result&) BOOST_ASIO_DELETED;
  88. #if defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  89. // No data members.
  90. #else // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  91. async_result<completion_handler_type> legacy_result_;
  92. #endif // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
  93. };
  94. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  95. /// (Deprecated: Use two-parameter version of async_result.) An interface for
  96. /// customising the behaviour of an initiating function.
  97. /**
  98. * This template may be specialised for user-defined handler types.
  99. */
  100. template <typename Handler>
  101. class async_result<Handler>
  102. {
  103. public:
  104. /// The return type of the initiating function.
  105. typedef void type;
  106. /// Construct an async result from a given handler.
  107. /**
  108. * When using a specalised async_result, the constructor has an opportunity
  109. * to initialise some state associated with the handler, which is then
  110. * returned from the initiating function.
  111. */
  112. explicit async_result(Handler&)
  113. {
  114. }
  115. /// Obtain the value to be returned from the initiating function.
  116. type get()
  117. {
  118. }
  119. };
  120. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  121. /// Helper template to deduce the handler type from a CompletionToken, capture
  122. /// a local copy of the handler, and then create an async_result for the
  123. /// handler.
  124. template <typename CompletionToken, typename Signature>
  125. struct async_completion
  126. {
  127. /// The real handler type to be used for the asynchronous operation.
  128. typedef typename boost::asio::async_result<
  129. typename decay<CompletionToken>::type,
  130. Signature>::completion_handler_type completion_handler_type;
  131. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  132. /// Constructor.
  133. /**
  134. * The constructor creates the concrete completion handler and makes the link
  135. * between the handler and the asynchronous result.
  136. */
  137. explicit async_completion(CompletionToken& token)
  138. : completion_handler(static_cast<typename conditional<
  139. is_same<CompletionToken, completion_handler_type>::value,
  140. completion_handler_type&, CompletionToken&&>::type>(token)),
  141. result(completion_handler)
  142. {
  143. }
  144. #else // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  145. explicit async_completion(typename decay<CompletionToken>::type& token)
  146. : completion_handler(token),
  147. result(completion_handler)
  148. {
  149. }
  150. explicit async_completion(const typename decay<CompletionToken>::type& token)
  151. : completion_handler(token),
  152. result(completion_handler)
  153. {
  154. }
  155. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  156. /// A copy of, or reference to, a real handler object.
  157. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  158. typename conditional<
  159. is_same<CompletionToken, completion_handler_type>::value,
  160. completion_handler_type&, completion_handler_type>::type completion_handler;
  161. #else // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  162. completion_handler_type completion_handler;
  163. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  164. /// The result of the asynchronous operation's initiating function.
  165. async_result<typename decay<CompletionToken>::type, Signature> result;
  166. };
  167. namespace detail {
  168. template <typename CompletionToken, typename Signature>
  169. struct async_result_helper
  170. : async_result<typename decay<CompletionToken>::type, Signature>
  171. {
  172. };
  173. } // namespace detail
  174. } // namespace asio
  175. } // namespace boost
  176. #include <boost/asio/detail/pop_options.hpp>
  177. #if defined(GENERATING_DOCUMENTATION)
  178. # define BOOST_ASIO_INITFN_RESULT_TYPE(ct, sig) \
  179. void_or_deduced
  180. #elif defined(_MSC_VER) && (_MSC_VER < 1500)
  181. # define BOOST_ASIO_INITFN_RESULT_TYPE(ct, sig) \
  182. typename ::boost::asio::detail::async_result_helper< \
  183. ct, sig>::return_type
  184. #define BOOST_ASIO_HANDLER_TYPE(ct, sig) \
  185. typename ::boost::asio::detail::async_result_helper< \
  186. ct, sig>::completion_handler_type
  187. #else
  188. # define BOOST_ASIO_INITFN_RESULT_TYPE(ct, sig) \
  189. typename ::boost::asio::async_result< \
  190. typename ::boost::asio::decay<ct>::type, sig>::return_type
  191. #define BOOST_ASIO_HANDLER_TYPE(ct, sig) \
  192. typename ::boost::asio::async_result< \
  193. typename ::boost::asio::decay<ct>::type, sig>::completion_handler_type
  194. #endif
  195. #endif // BOOST_ASIO_ASYNC_RESULT_HPP