compose.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // compose.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_COMPOSE_HPP
  11. #define BOOST_ASIO_COMPOSE_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/async_result.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) \
  21. || defined(GENERATING_DOCUMENTATION)
  22. /// Launch an asynchronous operation with a stateful implementation.
  23. /**
  24. * The async_compose function simplifies the implementation of composed
  25. * asynchronous operations automatically wrapping a stateful function object
  26. * with a conforming intermediate completion handler.
  27. *
  28. * @param implementation A function object that contains the implementation of
  29. * the composed asynchronous operation. The first argument to the function
  30. * object is a non-const reference to the enclosing intermediate completion
  31. * handler. The remaining arguments are any arguments that originate from the
  32. * completion handlers of any asynchronous operations performed by the
  33. * implementation.
  34. * @param token The completion token.
  35. *
  36. * @param io_objects_or_executors Zero or more I/O objects or I/O executors for
  37. * which outstanding work must be maintained.
  38. *
  39. * @par Example:
  40. *
  41. * @code struct async_echo_implementation
  42. * {
  43. * tcp::socket& socket_;
  44. * boost::asio::mutable_buffer buffer_;
  45. * enum { starting, reading, writing } state_;
  46. *
  47. * template <typename Self>
  48. * void operator()(Self& self,
  49. * boost::system::error_code error = {},
  50. * std::size_t n = 0)
  51. * {
  52. * switch (state_)
  53. * {
  54. * case starting:
  55. * state_ = reading;
  56. * socket_.async_read_some(
  57. * buffer_, std::move(self));
  58. * break;
  59. * case reading:
  60. * if (error)
  61. * {
  62. * self.complete(error, 0);
  63. * }
  64. * else
  65. * {
  66. * state_ = writing;
  67. * boost::asio::async_write(socket_, buffer_,
  68. * boost::asio::transfer_exactly(n),
  69. * std::move(self));
  70. * }
  71. * break;
  72. * case writing:
  73. * self.complete(error, n);
  74. * break;
  75. * }
  76. * }
  77. * };
  78. *
  79. * template <typename CompletionToken>
  80. * auto async_echo(tcp::socket& socket,
  81. * boost::asio::mutable_buffer buffer,
  82. * CompletionToken&& token) ->
  83. * typename boost::asio::async_result<
  84. * typename std::decay<CompletionToken>::type,
  85. * void(boost::system::error_code, std::size_t)>::return_type
  86. * {
  87. * return boost::asio::async_compose<CompletionToken,
  88. * void(boost::system::error_code, std::size_t)>(
  89. * async_echo_implementation{socket, buffer,
  90. * async_echo_implementation::starting},
  91. * token, socket);
  92. * } @endcode
  93. */
  94. template <typename CompletionToken, typename Signature,
  95. typename Implementation, typename... IoObjectsOrExecutors>
  96. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature)
  97. async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation,
  98. BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token,
  99. BOOST_ASIO_MOVE_ARG(IoObjectsOrExecutors)... io_objects_or_executors);
  100. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  101. // || defined(GENERATING_DOCUMENTATION)
  102. template <typename CompletionToken, typename Signature, typename Implementation>
  103. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature)
  104. async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation,
  105. BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token);
  106. #define BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF(n) \
  107. template <typename CompletionToken, typename Signature, \
  108. typename Implementation, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  109. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature) \
  110. async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation, \
  111. BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, \
  112. BOOST_ASIO_VARIADIC_MOVE_PARAMS(n));
  113. /**/
  114. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF)
  115. #undef BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF
  116. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  117. // || defined(GENERATING_DOCUMENTATION)
  118. } // namespace asio
  119. } // namespace boost
  120. #include <boost/asio/detail/pop_options.hpp>
  121. #include <boost/asio/impl/compose.hpp>
  122. #endif // BOOST_ASIO_COMPOSE_HPP