dispatch.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // impl/dispatch.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_IMPL_DISPATCH_HPP
  11. #define BOOST_ASIO_IMPL_DISPATCH_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_allocator.hpp>
  17. #include <boost/asio/associated_executor.hpp>
  18. #include <boost/asio/detail/work_dispatcher.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. template <typename CompletionToken>
  23. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) dispatch(
  24. BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  25. {
  26. typedef BOOST_ASIO_HANDLER_TYPE(CompletionToken, void()) handler;
  27. async_completion<CompletionToken, void()> init(token);
  28. typename associated_executor<handler>::type ex(
  29. (get_associated_executor)(init.completion_handler));
  30. typename associated_allocator<handler>::type alloc(
  31. (get_associated_allocator)(init.completion_handler));
  32. ex.dispatch(BOOST_ASIO_MOVE_CAST(handler)(init.completion_handler), alloc);
  33. return init.result.get();
  34. }
  35. template <typename Executor, typename CompletionToken>
  36. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) dispatch(
  37. const Executor& ex, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
  38. typename enable_if<is_executor<Executor>::value>::type*)
  39. {
  40. typedef BOOST_ASIO_HANDLER_TYPE(CompletionToken, void()) handler;
  41. async_completion<CompletionToken, void()> init(token);
  42. typename associated_allocator<handler>::type alloc(
  43. (get_associated_allocator)(init.completion_handler));
  44. ex.dispatch(detail::work_dispatcher<handler>(
  45. init.completion_handler), alloc);
  46. return init.result.get();
  47. }
  48. template <typename ExecutionContext, typename CompletionToken>
  49. inline BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) dispatch(
  50. ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
  51. typename enable_if<is_convertible<
  52. ExecutionContext&, execution_context&>::value>::type*)
  53. {
  54. return (dispatch)(ctx.get_executor(),
  55. BOOST_ASIO_MOVE_CAST(CompletionToken)(token));
  56. }
  57. } // namespace asio
  58. } // namespace boost
  59. #include <boost/asio/detail/pop_options.hpp>
  60. #endif // BOOST_ASIO_IMPL_DISPATCH_HPP