compose.hpp 4.1 KB

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