handler_work.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // detail/handler_work.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_DETAIL_HANDLER_WORK_HPP
  11. #define BOOST_ASIO_DETAIL_HANDLER_WORK_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/associated_executor.hpp>
  17. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. // A helper class template to allow completion handlers to be dispatched
  23. // through either the new executors framework or the old invocaton hook. The
  24. // primary template uses the new executors framework.
  25. template <typename Handler,
  26. typename IoExecutor = system_executor, typename HandlerExecutor
  27. = typename associated_executor<Handler, IoExecutor>::type>
  28. class handler_work
  29. {
  30. public:
  31. explicit handler_work(Handler& handler) BOOST_ASIO_NOEXCEPT
  32. : io_executor_(),
  33. executor_(boost::asio::get_associated_executor(handler, io_executor_))
  34. {
  35. }
  36. handler_work(Handler& handler, const IoExecutor& io_ex) BOOST_ASIO_NOEXCEPT
  37. : io_executor_(io_ex),
  38. executor_(boost::asio::get_associated_executor(handler, io_executor_))
  39. {
  40. }
  41. static void start(Handler& handler) BOOST_ASIO_NOEXCEPT
  42. {
  43. HandlerExecutor ex(boost::asio::get_associated_executor(handler));
  44. ex.on_work_started();
  45. }
  46. static void start(Handler& handler,
  47. const IoExecutor& io_ex) BOOST_ASIO_NOEXCEPT
  48. {
  49. HandlerExecutor ex(boost::asio::get_associated_executor(handler, io_ex));
  50. ex.on_work_started();
  51. io_ex.on_work_started();
  52. }
  53. ~handler_work()
  54. {
  55. io_executor_.on_work_finished();
  56. executor_.on_work_finished();
  57. }
  58. template <typename Function>
  59. void complete(Function& function, Handler& handler)
  60. {
  61. executor_.dispatch(BOOST_ASIO_MOVE_CAST(Function)(function),
  62. boost::asio::get_associated_allocator(handler));
  63. }
  64. private:
  65. // Disallow copying and assignment.
  66. handler_work(const handler_work&);
  67. handler_work& operator=(const handler_work&);
  68. IoExecutor io_executor_;
  69. HandlerExecutor executor_;
  70. };
  71. // This specialisation dispatches a handler through the old invocation hook.
  72. // The specialisation is not strictly required for correctness, as the
  73. // system_executor will dispatch through the hook anyway. However, by doing
  74. // this we avoid an extra copy of the handler.
  75. template <typename Handler>
  76. class handler_work<Handler, system_executor, system_executor>
  77. {
  78. public:
  79. explicit handler_work(Handler&) BOOST_ASIO_NOEXCEPT {}
  80. static void start(Handler&) BOOST_ASIO_NOEXCEPT {}
  81. ~handler_work() {}
  82. template <typename Function>
  83. void complete(Function& function, Handler& handler)
  84. {
  85. boost_asio_handler_invoke_helpers::invoke(function, handler);
  86. }
  87. private:
  88. // Disallow copying and assignment.
  89. handler_work(const handler_work&);
  90. handler_work& operator=(const handler_work&);
  91. };
  92. } // namespace detail
  93. } // namespace asio
  94. } // namespace boost
  95. #include <boost/asio/detail/pop_options.hpp>
  96. #endif // BOOST_ASIO_DETAIL_HANDLER_WORK_HPP