channel_receive_op.hpp 3.8 KB

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