dispatch.hpp 4.3 KB

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