handler_work.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // detail/handler_work.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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, typename Executor
  26. = typename associated_executor<Handler>::type>
  27. class handler_work
  28. {
  29. public:
  30. explicit handler_work(Handler& handler) BOOST_ASIO_NOEXCEPT
  31. : executor_(associated_executor<Handler>::get(handler))
  32. {
  33. }
  34. static void start(Handler& handler) BOOST_ASIO_NOEXCEPT
  35. {
  36. Executor ex(associated_executor<Handler>::get(handler));
  37. ex.on_work_started();
  38. }
  39. ~handler_work()
  40. {
  41. executor_.on_work_finished();
  42. }
  43. template <typename Function>
  44. void complete(Function& function, Handler& handler)
  45. {
  46. executor_.dispatch(BOOST_ASIO_MOVE_CAST(Function)(function),
  47. associated_allocator<Handler>::get(handler));
  48. }
  49. private:
  50. // Disallow copying and assignment.
  51. handler_work(const handler_work&);
  52. handler_work& operator=(const handler_work&);
  53. typename associated_executor<Handler>::type executor_;
  54. };
  55. // This specialisation dispatches a handler through the old invocation hook.
  56. // The specialisation is not strictly required for correctness, as the
  57. // system_executor will dispatch through the hook anyway. However, by doing
  58. // this we avoid an extra copy of the handler.
  59. template <typename Handler>
  60. class handler_work<Handler, system_executor>
  61. {
  62. public:
  63. explicit handler_work(Handler&) BOOST_ASIO_NOEXCEPT {}
  64. static void start(Handler&) BOOST_ASIO_NOEXCEPT {}
  65. ~handler_work() {}
  66. template <typename Function>
  67. void complete(Function& function, Handler& handler)
  68. {
  69. boost_asio_handler_invoke_helpers::invoke(function, handler);
  70. }
  71. private:
  72. // Disallow copying and assignment.
  73. handler_work(const handler_work&);
  74. handler_work& operator=(const handler_work&);
  75. };
  76. } // namespace detail
  77. } // namespace asio
  78. } // namespace boost
  79. #include <boost/asio/detail/pop_options.hpp>
  80. #endif // BOOST_ASIO_DETAIL_HANDLER_WORK_HPP