/src/contrib/boost/asio/impl/io_service.ipp

http://pythonocc.googlecode.com/ · C++ Header · 226 lines · 173 code · 41 blank · 12 comment · 4 complexity · cf8586cf03e6fe703e992c7a71bba568 MD5 · raw file

  1. //
  2. // io_service.ipp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_IO_SERVICE_IPP
  11. #define BOOST_ASIO_IO_SERVICE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/limits.hpp>
  18. #include <boost/asio/detail/pop_options.hpp>
  19. #include <boost/asio/detail/service_registry.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #if defined(BOOST_ASIO_HAS_IOCP)
  22. # include <boost/asio/detail/win_iocp_io_service.hpp>
  23. #else
  24. # include <boost/asio/detail/task_io_service.hpp>
  25. # include <boost/asio/detail/reactor.hpp>
  26. #endif
  27. namespace boost {
  28. namespace asio {
  29. inline io_service::io_service()
  30. : service_registry_(new boost::asio::detail::service_registry(*this)),
  31. impl_(service_registry_->use_service<impl_type>())
  32. {
  33. impl_.init((std::numeric_limits<std::size_t>::max)());
  34. }
  35. inline io_service::io_service(std::size_t concurrency_hint)
  36. : service_registry_(new boost::asio::detail::service_registry(*this)),
  37. impl_(service_registry_->use_service<impl_type>())
  38. {
  39. impl_.init(concurrency_hint);
  40. }
  41. inline io_service::~io_service()
  42. {
  43. delete service_registry_;
  44. }
  45. inline std::size_t io_service::run()
  46. {
  47. boost::system::error_code ec;
  48. std::size_t s = impl_.run(ec);
  49. boost::asio::detail::throw_error(ec);
  50. return s;
  51. }
  52. inline std::size_t io_service::run(boost::system::error_code& ec)
  53. {
  54. return impl_.run(ec);
  55. }
  56. inline std::size_t io_service::run_one()
  57. {
  58. boost::system::error_code ec;
  59. std::size_t s = impl_.run_one(ec);
  60. boost::asio::detail::throw_error(ec);
  61. return s;
  62. }
  63. inline std::size_t io_service::run_one(boost::system::error_code& ec)
  64. {
  65. return impl_.run_one(ec);
  66. }
  67. inline std::size_t io_service::poll()
  68. {
  69. boost::system::error_code ec;
  70. std::size_t s = impl_.poll(ec);
  71. boost::asio::detail::throw_error(ec);
  72. return s;
  73. }
  74. inline std::size_t io_service::poll(boost::system::error_code& ec)
  75. {
  76. return impl_.poll(ec);
  77. }
  78. inline std::size_t io_service::poll_one()
  79. {
  80. boost::system::error_code ec;
  81. std::size_t s = impl_.poll_one(ec);
  82. boost::asio::detail::throw_error(ec);
  83. return s;
  84. }
  85. inline std::size_t io_service::poll_one(boost::system::error_code& ec)
  86. {
  87. return impl_.poll_one(ec);
  88. }
  89. inline void io_service::stop()
  90. {
  91. impl_.stop();
  92. }
  93. inline void io_service::reset()
  94. {
  95. impl_.reset();
  96. }
  97. template <typename Handler>
  98. inline void io_service::dispatch(Handler handler)
  99. {
  100. impl_.dispatch(handler);
  101. }
  102. template <typename Handler>
  103. inline void io_service::post(Handler handler)
  104. {
  105. impl_.post(handler);
  106. }
  107. template <typename Handler>
  108. #if defined(GENERATING_DOCUMENTATION)
  109. unspecified
  110. #else
  111. inline detail::wrapped_handler<io_service&, Handler>
  112. #endif
  113. io_service::wrap(Handler handler)
  114. {
  115. return detail::wrapped_handler<io_service&, Handler>(*this, handler);
  116. }
  117. inline io_service::work::work(boost::asio::io_service& io_service)
  118. : io_service_(io_service)
  119. {
  120. io_service_.impl_.work_started();
  121. }
  122. inline io_service::work::work(const work& other)
  123. : io_service_(other.io_service_)
  124. {
  125. io_service_.impl_.work_started();
  126. }
  127. inline io_service::work::~work()
  128. {
  129. io_service_.impl_.work_finished();
  130. }
  131. inline boost::asio::io_service& io_service::work::io_service()
  132. {
  133. return io_service_;
  134. }
  135. inline boost::asio::io_service& io_service::work::get_io_service()
  136. {
  137. return io_service_;
  138. }
  139. inline io_service::service::service(boost::asio::io_service& owner)
  140. : owner_(owner),
  141. next_(0)
  142. {
  143. }
  144. inline io_service::service::~service()
  145. {
  146. }
  147. inline boost::asio::io_service& io_service::service::io_service()
  148. {
  149. return owner_;
  150. }
  151. inline boost::asio::io_service& io_service::service::get_io_service()
  152. {
  153. return owner_;
  154. }
  155. template <typename Service>
  156. inline Service& use_service(io_service& ios)
  157. {
  158. // Check that Service meets the necessary type requirements.
  159. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  160. (void)static_cast<const io_service::id*>(&Service::id);
  161. return ios.service_registry_->template use_service<Service>();
  162. }
  163. template <typename Service>
  164. void add_service(io_service& ios, Service* svc)
  165. {
  166. // Check that Service meets the necessary type requirements.
  167. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  168. (void)static_cast<const io_service::id*>(&Service::id);
  169. if (&ios != &svc->io_service())
  170. boost::throw_exception(invalid_service_owner());
  171. if (!ios.service_registry_->template add_service<Service>(svc))
  172. boost::throw_exception(service_already_exists());
  173. }
  174. template <typename Service>
  175. bool has_service(io_service& ios)
  176. {
  177. // Check that Service meets the necessary type requirements.
  178. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  179. (void)static_cast<const io_service::id*>(&Service::id);
  180. return ios.service_registry_->template has_service<Service>();
  181. }
  182. } // namespace asio
  183. } // namespace boost
  184. #include <boost/asio/detail/pop_options.hpp>
  185. #endif // BOOST_ASIO_IO_SERVICE_IPP