work_dispatcher.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // detail/work_dispatcher.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_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. template <typename CompletionHandler>
  28. explicit work_dispatcher(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler)
  29. : work_((get_associated_executor)(handler)),
  30. handler_(BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler))
  31. {
  32. }
  33. #if defined(BOOST_ASIO_HAS_MOVE)
  34. work_dispatcher(const work_dispatcher& other)
  35. : work_(other.work_),
  36. handler_(other.handler_)
  37. {
  38. }
  39. work_dispatcher(work_dispatcher&& other)
  40. : work_(BOOST_ASIO_MOVE_CAST(executor_work_guard<
  41. typename associated_executor<Handler>::type>)(other.work_)),
  42. handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
  43. {
  44. }
  45. #endif // defined(BOOST_ASIO_HAS_MOVE)
  46. void operator()()
  47. {
  48. typename associated_allocator<Handler>::type alloc(
  49. (get_associated_allocator)(handler_));
  50. work_.get_executor().dispatch(
  51. BOOST_ASIO_MOVE_CAST(Handler)(handler_), alloc);
  52. work_.reset();
  53. }
  54. private:
  55. executor_work_guard<typename associated_executor<Handler>::type> work_;
  56. Handler handler_;
  57. };
  58. } // namespace detail
  59. } // namespace asio
  60. } // namespace boost
  61. #include <boost/asio/detail/pop_options.hpp>
  62. #endif // BOOST_ASIO_DETAIL_WORK_DISPATCHER_HPP