channel_receive_op.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // experimental/detail/channel_receive_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_EXPERIMENTAL_DETAIL_CHANNEL_RECEIVE_OP_HPP
  11. #define BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_RECEIVE_OP_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/detail/bind_handler.hpp>
  17. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/experimental/detail/channel_handler.hpp>
  20. #include <boost/asio/experimental/detail/channel_operation.hpp>
  21. #include <boost/asio/experimental/detail/channel_payload.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace experimental {
  26. namespace detail {
  27. template <typename Payload>
  28. class channel_receive : public channel_operation
  29. {
  30. public:
  31. void immediate(Payload payload)
  32. {
  33. func_(this, immediate_op, &payload);
  34. }
  35. void post(Payload payload)
  36. {
  37. func_(this, post_op, &payload);
  38. }
  39. void dispatch(Payload payload)
  40. {
  41. func_(this, dispatch_op, &payload);
  42. }
  43. protected:
  44. channel_receive(func_type func)
  45. : channel_operation(func)
  46. {
  47. }
  48. };
  49. template <typename Payload, typename Handler, typename IoExecutor>
  50. class channel_receive_op : public channel_receive<Payload>
  51. {
  52. public:
  53. BOOST_ASIO_DEFINE_HANDLER_PTR(channel_receive_op);
  54. template <typename... Args>
  55. channel_receive_op(Handler& handler, const IoExecutor& io_ex)
  56. : channel_receive<Payload>(&channel_receive_op::do_action),
  57. handler_(static_cast<Handler&&>(handler)),
  58. work_(handler_, io_ex)
  59. {
  60. }
  61. static void do_action(channel_operation* base,
  62. channel_operation::action a, void* v)
  63. {
  64. // Take ownership of the operation object.
  65. channel_receive_op* o(static_cast<channel_receive_op*>(base));
  66. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  67. BOOST_ASIO_HANDLER_COMPLETION((*o));
  68. // Take ownership of the operation's outstanding work.
  69. channel_operation::handler_work<Handler, IoExecutor> w(
  70. static_cast<channel_operation::handler_work<Handler, IoExecutor>&&>(
  71. o->work_));
  72. // Make a copy of the handler so that the memory can be deallocated before
  73. // the handler is posted. Even if we're not about to post the handler, a
  74. // sub-object of the handler may be the true owner of the memory associated
  75. // with the handler. Consequently, a local copy of the handler is required
  76. // to ensure that any owning sub-object remains valid until after we have
  77. // deallocated the memory here.
  78. if (a != channel_operation::destroy_op)
  79. {
  80. Payload* payload = static_cast<Payload*>(v);
  81. channel_handler<Payload, Handler> handler(
  82. static_cast<Payload&&>(*payload), o->handler_);
  83. p.h = boost::asio::detail::addressof(handler.handler_);
  84. p.reset();
  85. BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
  86. if (a == channel_operation::immediate_op)
  87. w.immediate(handler, handler.handler_, 0);
  88. else if (a == channel_operation::dispatch_op)
  89. w.dispatch(handler, handler.handler_);
  90. else
  91. w.post(handler, handler.handler_);
  92. BOOST_ASIO_HANDLER_INVOCATION_END;
  93. }
  94. else
  95. {
  96. boost::asio::detail::binder0<Handler> handler(o->handler_);
  97. p.h = boost::asio::detail::addressof(handler.handler_);
  98. p.reset();
  99. }
  100. }
  101. private:
  102. Handler handler_;
  103. channel_operation::handler_work<Handler, IoExecutor> work_;
  104. };
  105. } // namespace detail
  106. } // namespace experimental
  107. } // namespace asio
  108. } // namespace boost
  109. #include <boost/asio/detail/pop_options.hpp>
  110. #endif // BOOST_ASIO_EXPERIMENTAL_DETAIL_CHANNEL_RECEIVE_OP_HPP