bind_handler.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_BIND_HANDLER_HPP
  10. #define BOOST_BEAST_BIND_HANDLER_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/bind_handler.hpp>
  13. #include <type_traits>
  14. #include <utility>
  15. namespace boost {
  16. namespace beast {
  17. /** Bind parameters to a completion handler, creating a new handler.
  18. This function creates a new handler which, when invoked, calls
  19. the original handler with the list of bound arguments. Any
  20. parameters passed in the invocation will be substituted for
  21. placeholders present in the list of bound arguments. Parameters
  22. which are not matched to placeholders are silently discarded.
  23. The passed handler and arguments are forwarded into the returned
  24. handler, whose associated allocator and associated executor will
  25. will be the same as those of the original handler.
  26. @par Example
  27. This function posts the invocation of the specified completion
  28. handler with bound arguments:
  29. @code
  30. template <class AsyncReadStream, class ReadHandler>
  31. void
  32. signal_aborted (AsyncReadStream& stream, ReadHandler&& handler)
  33. {
  34. net::post(
  35. stream.get_executor(),
  36. bind_handler (std::forward <ReadHandler> (handler),
  37. net::error::operation_aborted, 0));
  38. }
  39. @endcode
  40. @param handler The handler to wrap.
  41. The implementation takes ownership of the handler by performing a decay-copy.
  42. @param args A list of arguments to bind to the handler.
  43. The arguments are forwarded into the returned object. These
  44. arguments may include placeholders, which will operate in
  45. a fashion identical to a call to `std::bind`.
  46. */
  47. template<class Handler, class... Args>
  48. #if BOOST_BEAST_DOXYGEN
  49. __implementation_defined__
  50. #else
  51. detail::bind_wrapper<
  52. typename std::decay<Handler>::type,
  53. typename std::decay<Args>::type...>
  54. #endif
  55. bind_handler(Handler&& handler, Args&&... args)
  56. {
  57. return detail::bind_wrapper<
  58. typename std::decay<Handler>::type,
  59. typename std::decay<Args>::type...>(
  60. std::forward<Handler>(handler),
  61. std::forward<Args>(args)...);
  62. }
  63. /** Bind parameters to a completion handler, creating a new handler.
  64. This function creates a new handler which, when invoked, calls
  65. the original handler with the list of bound arguments. Any
  66. parameters passed in the invocation will be forwarded in
  67. the parameter list after the bound arguments.
  68. The passed handler and arguments are forwarded into the returned
  69. handler, whose associated allocator and associated executor will
  70. will be the same as those of the original handler.
  71. @par Example
  72. This function posts the invocation of the specified completion
  73. handler with bound arguments:
  74. @code
  75. template <class AsyncReadStream, class ReadHandler>
  76. void
  77. signal_eof (AsyncReadStream& stream, ReadHandler&& handler)
  78. {
  79. net::post(
  80. stream.get_executor(),
  81. bind_front_handler (std::forward<ReadHandler> (handler),
  82. net::error::eof, 0));
  83. }
  84. @endcode
  85. @param handler The handler to wrap.
  86. The implementation takes ownership of the handler by performing a decay-copy.
  87. @param args A list of arguments to bind to the handler.
  88. The arguments are forwarded into the returned object.
  89. */
  90. template<class Handler, class... Args>
  91. #if BOOST_BEAST_DOXYGEN
  92. __implementation_defined__
  93. #else
  94. auto
  95. #endif
  96. bind_front_handler(
  97. Handler&& handler,
  98. Args&&... args) ->
  99. detail::bind_front_wrapper<
  100. typename std::decay<Handler>::type,
  101. typename std::decay<Args>::type...>
  102. {
  103. return detail::bind_front_wrapper<
  104. typename std::decay<Handler>::type,
  105. typename std::decay<Args>::type...>(
  106. std::forward<Handler>(handler),
  107. std::forward<Args>(args)...);
  108. }
  109. } // beast
  110. } // boost
  111. #endif