9
3

defer.hpp 4.9 KB

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