dispatch.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // dispatch.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_DISPATCH_HPP
  11. #define BOOST_ASIO_DISPATCH_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/type_traits.hpp>
  18. #include <boost/asio/execution_context.hpp>
  19. #include <boost/asio/execution/executor.hpp>
  20. #include <boost/asio/is_executor.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. /// Submits a completion token or function object for execution.
  25. /**
  26. * This function submits an object for execution using the object's associated
  27. * executor. The function object may be called from the current thread prior to
  28. * returning from <tt>dispatch()</tt>. Otherwise, it is queued for execution.
  29. *
  30. * This function has the following effects:
  31. *
  32. * @li Constructs a function object handler of type @c Handler, initialized
  33. * with <tt>handler(forward<CompletionToken>(token))</tt>.
  34. *
  35. * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
  36. * initializing the object as <tt>result(handler)</tt>.
  37. *
  38. * @li Obtains the handler's associated executor object @c ex by performing
  39. * <tt>get_associated_executor(handler)</tt>.
  40. *
  41. * @li Obtains the handler's associated allocator object @c alloc by performing
  42. * <tt>get_associated_allocator(handler)</tt>.
  43. *
  44. * @li Performs <tt>ex.dispatch(std::move(handler), alloc)</tt>.
  45. *
  46. * @li Returns <tt>result.get()</tt>.
  47. */
  48. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
  49. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
  50. BOOST_ASIO_MOVE_ARG(CompletionToken) token);
  51. /// Submits a completion token or function object for execution.
  52. /**
  53. * This function submits an object for execution using the specified executor.
  54. * The function object may be called from the current thread prior to returning
  55. * from <tt>dispatch()</tt>. Otherwise, it is queued for execution.
  56. *
  57. * This function has the following effects:
  58. *
  59. * @li Constructs a function object handler of type @c Handler, initialized
  60. * with <tt>handler(forward<CompletionToken>(token))</tt>.
  61. *
  62. * @li Constructs an object @c result of type <tt>async_result<Handler></tt>,
  63. * initializing the object as <tt>result(handler)</tt>.
  64. *
  65. * @li Obtains the handler's associated executor object @c ex1 by performing
  66. * <tt>get_associated_executor(handler)</tt>.
  67. *
  68. * @li Creates a work object @c w by performing <tt>make_work(ex1)</tt>.
  69. *
  70. * @li Obtains the handler's associated allocator object @c alloc by performing
  71. * <tt>get_associated_allocator(handler)</tt>.
  72. *
  73. * @li Constructs a function object @c f with a function call operator that
  74. * performs <tt>ex1.dispatch(std::move(handler), alloc)</tt> followed by
  75. * <tt>w.reset()</tt>.
  76. *
  77. * @li Performs <tt>Executor(ex).dispatch(std::move(f), alloc)</tt>.
  78. *
  79. * @li Returns <tt>result.get()</tt>.
  80. */
  81. template <typename Executor,
  82. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken
  83. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
  84. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
  85. const Executor& ex,
  86. BOOST_ASIO_MOVE_ARG(CompletionToken) token
  87. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
  88. typename constraint<
  89. execution::is_executor<Executor>::value || is_executor<Executor>::value
  90. >::type = 0);
  91. /// Submits a completion token or function object for execution.
  92. /**
  93. * @returns <tt>dispatch(ctx.get_executor(),
  94. * forward<CompletionToken>(token))</tt>.
  95. */
  96. template <typename ExecutionContext,
  97. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken
  98. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(
  99. typename ExecutionContext::executor_type)>
  100. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
  101. ExecutionContext& ctx,
  102. BOOST_ASIO_MOVE_ARG(CompletionToken) token
  103. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(
  104. typename ExecutionContext::executor_type),
  105. typename constraint<is_convertible<
  106. ExecutionContext&, execution_context&>::value>::type = 0);
  107. } // namespace asio
  108. } // namespace boost
  109. #include <boost/asio/detail/pop_options.hpp>
  110. #include <boost/asio/impl/dispatch.hpp>
  111. #endif // BOOST_ASIO_DISPATCH_HPP