bind_default_executor.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_CORE_DETAIL_BIND_DEFAULT_EXECUTOR_HPP
  10. #define BOOST_BEAST_CORE_DETAIL_BIND_DEFAULT_EXECUTOR_HPP
  11. #include <boost/asio/associated_allocator.hpp>
  12. #include <boost/asio/associated_executor.hpp>
  13. #include <boost/asio/dispatch.hpp>
  14. #include <boost/asio/executor.hpp>
  15. #include <boost/asio/handler_alloc_hook.hpp>
  16. #include <boost/asio/handler_continuation_hook.hpp>
  17. #include <boost/asio/handler_invoke_hook.hpp>
  18. #include <boost/core/empty_value.hpp>
  19. #include <utility>
  20. namespace boost {
  21. namespace beast {
  22. namespace detail {
  23. template<class Handler, class Executor>
  24. class bind_default_executor_wrapper
  25. : private boost::empty_value<Executor>
  26. {
  27. Handler h_;
  28. public:
  29. template<class Handler_>
  30. bind_default_executor_wrapper(
  31. Handler_&& h,
  32. Executor const& ex)
  33. : boost::empty_value<Executor>(
  34. boost::empty_init_t{}, ex)
  35. , h_(std::forward<Handler_>(h))
  36. {
  37. }
  38. template<class... Args>
  39. void
  40. operator()(Args&&... args)
  41. {
  42. h_(std::forward<Args>(args)...);
  43. }
  44. using allocator_type =
  45. net::associated_allocator_t<Handler>;
  46. allocator_type
  47. get_allocator() const noexcept
  48. {
  49. return net::get_associated_allocator(h_);
  50. }
  51. using executor_type =
  52. net::associated_executor_t<Handler, Executor>;
  53. executor_type
  54. get_executor() const noexcept
  55. {
  56. return net::get_associated_executor(
  57. h_, this->get());
  58. }
  59. template<class Function>
  60. void
  61. asio_handler_invoke(Function&& f,
  62. bind_default_executor_wrapper* p)
  63. {
  64. net::dispatch(p->get_executor(), std::move(f));
  65. }
  66. friend
  67. void* asio_handler_allocate(
  68. std::size_t size, bind_default_executor_wrapper* p)
  69. {
  70. using net::asio_handler_allocate;
  71. return asio_handler_allocate(
  72. size, std::addressof(p->h_));
  73. }
  74. friend
  75. void asio_handler_deallocate(
  76. void* mem, std::size_t size,
  77. bind_default_executor_wrapper* p)
  78. {
  79. using net::asio_handler_deallocate;
  80. asio_handler_deallocate(mem, size,
  81. std::addressof(p->h_));
  82. }
  83. friend
  84. bool asio_handler_is_continuation(
  85. bind_default_executor_wrapper* p)
  86. {
  87. using net::asio_handler_is_continuation;
  88. return asio_handler_is_continuation(
  89. std::addressof(p->h_));
  90. }
  91. };
  92. template<class Executor, class Handler>
  93. auto
  94. bind_default_executor(Executor const& ex, Handler&& h) ->
  95. bind_default_executor_wrapper<
  96. typename std::decay<Handler>::type,
  97. Executor>
  98. {
  99. return bind_default_executor_wrapper<
  100. typename std::decay<Handler>::type,
  101. Executor>(std::forward<Handler>(h), ex);
  102. }
  103. } // detail
  104. } // beast
  105. } // boost
  106. #endif