dispatch.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // impl/dispatch.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_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. namespace detail {
  23. class initiate_dispatch
  24. {
  25. public:
  26. template <typename CompletionHandler>
  27. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) const
  28. {
  29. typedef typename decay<CompletionHandler>::type DecayedHandler;
  30. typename associated_executor<DecayedHandler>::type ex(
  31. (get_associated_executor)(handler));
  32. typename associated_allocator<DecayedHandler>::type alloc(
  33. (get_associated_allocator)(handler));
  34. ex.dispatch(BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler), alloc);
  35. }
  36. };
  37. template <typename Executor>
  38. class initiate_dispatch_with_executor
  39. {
  40. public:
  41. typedef Executor executor_type;
  42. explicit initiate_dispatch_with_executor(const Executor& ex)
  43. : ex_(ex)
  44. {
  45. }
  46. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  47. {
  48. return ex_;
  49. }
  50. template <typename CompletionHandler>
  51. void operator()(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) const
  52. {
  53. typedef typename decay<CompletionHandler>::type DecayedHandler;
  54. typename associated_allocator<DecayedHandler>::type alloc(
  55. (get_associated_allocator)(handler));
  56. ex_.dispatch(detail::work_dispatcher<DecayedHandler>(
  57. BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
  58. }
  59. private:
  60. Executor ex_;
  61. };
  62. } // namespace detail
  63. template <BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
  64. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
  65. BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  66. {
  67. return async_initiate<CompletionToken, void()>(
  68. detail::initiate_dispatch(), token);
  69. }
  70. template <typename Executor,
  71. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
  72. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
  73. const Executor& ex, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
  74. typename enable_if<is_executor<Executor>::value>::type*)
  75. {
  76. return async_initiate<CompletionToken, void()>(
  77. detail::initiate_dispatch_with_executor<Executor>(ex), token);
  78. }
  79. template <typename ExecutionContext,
  80. BOOST_ASIO_COMPLETION_TOKEN_FOR(void()) CompletionToken>
  81. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) dispatch(
  82. ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
  83. typename enable_if<is_convertible<
  84. ExecutionContext&, execution_context&>::value>::type*)
  85. {
  86. return (dispatch)(ctx.get_executor(),
  87. BOOST_ASIO_MOVE_CAST(CompletionToken)(token));
  88. }
  89. } // namespace asio
  90. } // namespace boost
  91. #include <boost/asio/detail/pop_options.hpp>
  92. #endif // BOOST_ASIO_IMPL_DISPATCH_HPP