async_result.hpp 7.6 KB

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