bind_continuation.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_BIND_CONTINUATION_HPP
  10. #define BOOST_BEAST_DETAIL_BIND_CONTINUATION_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/remap_post_to_defer.hpp>
  13. #include <boost/asio/bind_executor.hpp>
  14. #include <boost/core/empty_value.hpp>
  15. #include <type_traits>
  16. #include <utility>
  17. namespace boost {
  18. namespace beast {
  19. namespace detail {
  20. #if 0
  21. /** Mark a completion handler as a continuation.
  22. This function wraps a completion handler to associate it with an
  23. executor whose `post` operation is remapped to the `defer` operation.
  24. It is used by composed asynchronous operation implementations to
  25. indicate that a completion handler submitted to an initiating
  26. function represents a continuation of the current asynchronous
  27. flow of control.
  28. @param handler The handler to wrap.
  29. The implementation takes ownership of the handler by performing a decay-copy.
  30. @see
  31. @li <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4242.html">[N4242] Executors and Asynchronous Operations, Revision 1</a>
  32. */
  33. template<class CompletionHandler>
  34. #if BOOST_BEAST_DOXYGEN
  35. __implementation_defined__
  36. #else
  37. net::executor_binder<
  38. typename std::decay<CompletionHandler>::type,
  39. detail::remap_post_to_defer<
  40. net::associated_executor_t<CompletionHandler>>>
  41. #endif
  42. bind_continuation(CompletionHandler&& handler)
  43. {
  44. return net::bind_executor(
  45. detail::remap_post_to_defer<
  46. net::associated_executor_t<CompletionHandler>>(
  47. net::get_associated_executor(handler)),
  48. std::forward<CompletionHandler>(handler));
  49. }
  50. /** Mark a completion handler as a continuation.
  51. This function wraps a completion handler to associate it with an
  52. executor whose `post` operation is remapped to the `defer` operation.
  53. It is used by composed asynchronous operation implementations to
  54. indicate that a completion handler submitted to an initiating
  55. function represents a continuation of the current asynchronous
  56. flow of control.
  57. @param ex The executor to use
  58. @param handler The handler to wrap
  59. The implementation takes ownership of the handler by performing a decay-copy.
  60. @see
  61. @li <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4242.html">[N4242] Executors and Asynchronous Operations, Revision 1</a>
  62. */
  63. template<class Executor, class CompletionHandler>
  64. #if BOOST_BEAST_DOXYGEN
  65. __implementation_defined__
  66. #else
  67. net::executor_binder<typename
  68. std::decay<CompletionHandler>::type,
  69. detail::remap_post_to_defer<Executor>>
  70. #endif
  71. bind_continuation(
  72. Executor const& ex, CompletionHandler&& handler)
  73. {
  74. return net::bind_executor(
  75. detail::remap_post_to_defer<Executor>(ex),
  76. std::forward<CompletionHandler>(handler));
  77. }
  78. #else
  79. // VFALCO I turned these off at the last minute because they cause
  80. // the completion handler to be moved before the initiating
  81. // function is invoked rather than after, which is a foot-gun.
  82. //
  83. // REMINDER: Uncomment the tests when this is put back
  84. template<class F>
  85. F&&
  86. bind_continuation(F&& f)
  87. {
  88. return std::forward<F>(f);
  89. }
  90. #endif
  91. } // detail
  92. } // beast
  93. } // boost
  94. #endif