immediate.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // immediate.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_IMMEDIATE_HPP
  11. #define BOOST_ASIO_IMMEDIATE_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/associated_immediate_executor.hpp>
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/asio/dispatch.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. template <typename Executor>
  24. class initiate_immediate
  25. {
  26. public:
  27. typedef Executor executor_type;
  28. explicit initiate_immediate(const Executor& ex)
  29. : ex_(ex)
  30. {
  31. }
  32. executor_type get_executor() const noexcept
  33. {
  34. return ex_;
  35. }
  36. template <typename CompletionHandler>
  37. void operator()(CompletionHandler&& handler) const
  38. {
  39. typename associated_immediate_executor<
  40. CompletionHandler, executor_type>::type ex =
  41. (get_associated_immediate_executor)(handler, ex_);
  42. (dispatch)(ex, static_cast<CompletionHandler&&>(handler));
  43. }
  44. private:
  45. Executor ex_;
  46. };
  47. } // namespace detail
  48. /// Launch a trivial asynchronous operation that completes immediately.
  49. /**
  50. * The async_immediate function is intended for use by composed operations,
  51. * which can delegate to this operation in order to implement the correct
  52. * semantics for immediate completion.
  53. *
  54. * @param ex The asynchronous operation's I/O executor.
  55. *
  56. * @param token The completion token.
  57. *
  58. * The completion handler is immediately submitted for execution by calling
  59. * boost::asio::dispatch() on the handler's associated immediate executor.
  60. *
  61. * If the completion handler does not have a customised associated immediate
  62. * executor, then the handler is submitted as if by calling boost::asio::post()
  63. * on the supplied I/O executor.
  64. *
  65. * @par Completion Signature
  66. * @code void() @endcode
  67. */
  68. template <typename Executor,
  69. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  70. = default_completion_token_t<Executor>>
  71. inline auto async_immediate(const Executor& ex,
  72. NullaryToken&& token = default_completion_token_t<Executor>(),
  73. constraint_t<
  74. (execution::is_executor<Executor>::value
  75. && can_require<Executor, execution::blocking_t::never_t>::value)
  76. || is_executor<Executor>::value
  77. > = 0)
  78. -> decltype(
  79. async_initiate<NullaryToken, void()>(
  80. declval<detail::initiate_immediate<Executor>>(), token))
  81. {
  82. return async_initiate<NullaryToken, void()>(
  83. detail::initiate_immediate<Executor>(ex), token);
  84. }
  85. /// Launch a trivial asynchronous operation that completes immediately.
  86. /**
  87. * The async_immediate function is intended for use by composed operations,
  88. * which can delegate to this operation in order to implement the correct
  89. * semantics for immediate completion.
  90. *
  91. * @param ex The execution context used to obtain the asynchronous operation's
  92. * I/O executor.
  93. *
  94. * @param token The completion token.
  95. *
  96. * The completion handler is immediately submitted for execution by calling
  97. * boost::asio::dispatch() on the handler's associated immediate executor.
  98. *
  99. * If the completion handler does not have a customised associated immediate
  100. * executor, then the handler is submitted as if by calling boost::asio::post()
  101. * on the I/O executor obtained from the supplied execution context.
  102. *
  103. * @par Completion Signature
  104. * @code void() @endcode
  105. */
  106. template <typename ExecutionContext,
  107. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) NullaryToken
  108. = default_completion_token_t<typename ExecutionContext::executor_type>>
  109. inline auto async_immediate(ExecutionContext& ctx,
  110. NullaryToken&& token = default_completion_token_t<
  111. typename ExecutionContext::executor_type>(),
  112. constraint_t<
  113. is_convertible<ExecutionContext&, execution_context&>::value
  114. > = 0)
  115. -> decltype(
  116. async_initiate<NullaryToken, void()>(
  117. declval<detail::initiate_immediate<
  118. typename ExecutionContext::executor_type>>(), token))
  119. {
  120. return async_initiate<NullaryToken, void()>(
  121. detail::initiate_immediate<
  122. typename ExecutionContext::executor_type>(
  123. ctx.get_executor()), token);
  124. }
  125. } // namespace asio
  126. } // namespace boost
  127. #include <boost/asio/detail/pop_options.hpp>
  128. #endif // BOOST_ASIO_IMMEDIATE_HPP