io_service.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // impl/io_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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 ASIO_IMPL_IO_SERVICE_HPP
  11. #define ASIO_IMPL_IO_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/handler_type_requirements.hpp"
  16. #include "asio/detail/service_registry.hpp"
  17. #include "asio/detail/push_options.hpp"
  18. namespace asio {
  19. template <typename Service>
  20. inline Service& use_service(io_service& ios)
  21. {
  22. // Check that Service meets the necessary type requirements.
  23. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  24. (void)static_cast<const io_service::id*>(&Service::id);
  25. return ios.service_registry_->template use_service<Service>();
  26. }
  27. template <>
  28. inline detail::io_service_impl& use_service<detail::io_service_impl>(
  29. io_service& ios)
  30. {
  31. return ios.impl_;
  32. }
  33. template <typename Service>
  34. inline void add_service(io_service& ios, Service* svc)
  35. {
  36. // Check that Service meets the necessary type requirements.
  37. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  38. (void)static_cast<const io_service::id*>(&Service::id);
  39. ios.service_registry_->template add_service<Service>(svc);
  40. }
  41. template <typename Service>
  42. inline bool has_service(io_service& ios)
  43. {
  44. // Check that Service meets the necessary type requirements.
  45. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  46. (void)static_cast<const io_service::id*>(&Service::id);
  47. return ios.service_registry_->template has_service<Service>();
  48. }
  49. } // namespace asio
  50. #include "asio/detail/pop_options.hpp"
  51. #if defined(ASIO_HAS_IOCP)
  52. # include "asio/detail/win_iocp_io_service.hpp"
  53. #else
  54. # include "asio/detail/task_io_service.hpp"
  55. #endif
  56. #include "asio/detail/push_options.hpp"
  57. namespace asio {
  58. template <typename CompletionHandler>
  59. inline ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  60. io_service::dispatch(ASIO_MOVE_ARG(CompletionHandler) handler)
  61. {
  62. // If you get an error on the following line it means that your handler does
  63. // not meet the documented type requirements for a CompletionHandler.
  64. ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
  65. detail::async_result_init<
  66. CompletionHandler, void ()> init(
  67. ASIO_MOVE_CAST(CompletionHandler)(handler));
  68. impl_.dispatch(init.handler);
  69. return init.result.get();
  70. }
  71. template <typename CompletionHandler>
  72. inline ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  73. io_service::post(ASIO_MOVE_ARG(CompletionHandler) handler)
  74. {
  75. // If you get an error on the following line it means that your handler does
  76. // not meet the documented type requirements for a CompletionHandler.
  77. ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check;
  78. detail::async_result_init<
  79. CompletionHandler, void ()> init(
  80. ASIO_MOVE_CAST(CompletionHandler)(handler));
  81. impl_.post(init.handler);
  82. return init.result.get();
  83. }
  84. template <typename Handler>
  85. #if defined(GENERATING_DOCUMENTATION)
  86. unspecified
  87. #else
  88. inline detail::wrapped_handler<io_service&, Handler>
  89. #endif
  90. io_service::wrap(Handler handler)
  91. {
  92. return detail::wrapped_handler<io_service&, Handler>(*this, handler);
  93. }
  94. inline io_service::work::work(asio::io_service& io_service)
  95. : io_service_impl_(io_service.impl_)
  96. {
  97. io_service_impl_.work_started();
  98. }
  99. inline io_service::work::work(const work& other)
  100. : io_service_impl_(other.io_service_impl_)
  101. {
  102. io_service_impl_.work_started();
  103. }
  104. inline io_service::work::~work()
  105. {
  106. io_service_impl_.work_finished();
  107. }
  108. inline asio::io_service& io_service::work::get_io_service()
  109. {
  110. return io_service_impl_.get_io_service();
  111. }
  112. inline asio::io_service& io_service::service::get_io_service()
  113. {
  114. return owner_;
  115. }
  116. } // namespace asio
  117. #include "asio/detail/pop_options.hpp"
  118. #endif // ASIO_IMPL_IO_SERVICE_HPP