work_dispatcher.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // detail/work_dispatcher.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_WORK_DISPATCHER_HPP
  11. #define BOOST_ASIO_DETAIL_WORK_DISPATCHER_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/associated_allocator.hpp>
  18. #include <boost/asio/executor_work_guard.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. template <typename Handler>
  24. class work_dispatcher
  25. {
  26. public:
  27. work_dispatcher(Handler& handler)
  28. : work_((get_associated_executor)(handler)),
  29. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
  30. {
  31. }
  32. #if defined(BOOST_ASIO_HAS_MOVE)
  33. work_dispatcher(const work_dispatcher& other)
  34. : work_(other.work_),
  35. handler_(other.handler_)
  36. {
  37. }
  38. work_dispatcher(work_dispatcher&& other)
  39. : work_(BOOST_ASIO_MOVE_CAST(executor_work_guard<
  40. typename associated_executor<Handler>::type>)(other.work_)),
  41. handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
  42. {
  43. }
  44. #endif // defined(BOOST_ASIO_HAS_MOVE)
  45. void operator()()
  46. {
  47. typename associated_allocator<Handler>::type alloc(
  48. (get_associated_allocator)(handler_));
  49. work_.get_executor().dispatch(
  50. BOOST_ASIO_MOVE_CAST(Handler)(handler_), alloc);
  51. work_.reset();
  52. }
  53. private:
  54. executor_work_guard<typename associated_executor<Handler>::type> work_;
  55. Handler handler_;
  56. };
  57. } // namespace detail
  58. } // namespace asio
  59. } // namespace boost
  60. #include <boost/asio/detail/pop_options.hpp>
  61. #endif // BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP