post.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // impl/post.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_POST_HPP
  11. #define BOOST_ASIO_IMPL_POST_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()) post(
  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.post(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()) post(
  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.post(detail::work_dispatcher<handler>(init.completion_handler), alloc);
  45. return init.result.get();
  46. }
  47. template <typename ExecutionContext, typename CompletionToken>
  48. inline BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) post(
  49. ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(CompletionToken) token,
  50. typename enable_if<is_convertible<
  51. ExecutionContext&, execution_context&>::value>::type*)
  52. {
  53. return (post)(ctx.get_executor(),
  54. BOOST_ASIO_MOVE_CAST(CompletionToken)(token));
  55. }
  56. } // namespace asio
  57. } // namespace boost
  58. #include <boost/asio/detail/pop_options.hpp>
  59. #endif // BOOST_ASIO_IMPL_POST_HPP