00001 // 00002 // io_service.ipp 00003 // ~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_IO_SERVICE_IPP 00012 #define ASIO_IO_SERVICE_IPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/detail/push_options.hpp" 00021 #include <limits> 00022 #include "asio/detail/pop_options.hpp" 00023 00024 #include "asio/detail/epoll_reactor.hpp" 00025 #include "asio/detail/kqueue_reactor.hpp" 00026 #include "asio/detail/select_reactor.hpp" 00027 #include "asio/detail/service_registry.hpp" 00028 #include "asio/detail/task_io_service.hpp" 00029 #include "asio/detail/throw_error.hpp" 00030 #include "asio/detail/win_iocp_io_service.hpp" 00031 00032 namespace asio { 00033 00034 inline io_service::io_service() 00035 : service_registry_(new asio::detail::service_registry(*this)), 00036 impl_(service_registry_->use_service<impl_type>()) 00037 { 00038 impl_.init((std::numeric_limits<std::size_t>::max)()); 00039 } 00040 00041 inline io_service::io_service(std::size_t concurrency_hint) 00042 : service_registry_(new asio::detail::service_registry(*this)), 00043 impl_(service_registry_->use_service<impl_type>()) 00044 { 00045 impl_.init(concurrency_hint); 00046 } 00047 00048 inline io_service::~io_service() 00049 { 00050 delete service_registry_; 00051 } 00052 00053 inline std::size_t io_service::run() 00054 { 00055 asio::error_code ec; 00056 std::size_t s = impl_.run(ec); 00057 asio::detail::throw_error(ec); 00058 return s; 00059 } 00060 00061 inline std::size_t io_service::run(asio::error_code& ec) 00062 { 00063 return impl_.run(ec); 00064 } 00065 00066 inline std::size_t io_service::run_one() 00067 { 00068 asio::error_code ec; 00069 std::size_t s = impl_.run_one(ec); 00070 asio::detail::throw_error(ec); 00071 return s; 00072 } 00073 00074 inline std::size_t io_service::run_one(asio::error_code& ec) 00075 { 00076 return impl_.run_one(ec); 00077 } 00078 00079 inline std::size_t io_service::poll() 00080 { 00081 asio::error_code ec; 00082 std::size_t s = impl_.poll(ec); 00083 asio::detail::throw_error(ec); 00084 return s; 00085 } 00086 00087 inline std::size_t io_service::poll(asio::error_code& ec) 00088 { 00089 return impl_.poll(ec); 00090 } 00091 00092 inline std::size_t io_service::poll_one() 00093 { 00094 asio::error_code ec; 00095 std::size_t s = impl_.poll_one(ec); 00096 asio::detail::throw_error(ec); 00097 return s; 00098 } 00099 00100 inline std::size_t io_service::poll_one(asio::error_code& ec) 00101 { 00102 return impl_.poll_one(ec); 00103 } 00104 00105 inline void io_service::stop() 00106 { 00107 impl_.stop(); 00108 } 00109 00110 inline void io_service::reset() 00111 { 00112 impl_.reset(); 00113 } 00114 00115 template <typename Handler> 00116 inline void io_service::dispatch(Handler handler) 00117 { 00118 impl_.dispatch(handler); 00119 } 00120 00121 template <typename Handler> 00122 inline void io_service::post(Handler handler) 00123 { 00124 impl_.post(handler); 00125 } 00126 00127 template <typename Handler> 00128 #if defined(GENERATING_DOCUMENTATION) 00129 unspecified 00130 #else 00131 inline detail::wrapped_handler<io_service, Handler> 00132 #endif 00133 io_service::wrap(Handler handler) 00134 { 00135 return detail::wrapped_handler<io_service, Handler>(*this, handler); 00136 } 00137 00138 inline io_service::work::work(asio::io_service& io_service) 00139 : io_service_(io_service) 00140 { 00141 io_service_.impl_.work_started(); 00142 } 00143 00144 inline io_service::work::work(const work& other) 00145 : io_service_(other.io_service_) 00146 { 00147 io_service_.impl_.work_started(); 00148 } 00149 00150 inline io_service::work::~work() 00151 { 00152 io_service_.impl_.work_finished(); 00153 } 00154 00155 inline asio::io_service& io_service::work::io_service() 00156 { 00157 return io_service_; 00158 } 00159 00160 inline io_service::service::service(asio::io_service& owner) 00161 : owner_(owner), 00162 type_info_(0), 00163 next_(0) 00164 { 00165 } 00166 00167 inline io_service::service::~service() 00168 { 00169 } 00170 00171 inline asio::io_service& io_service::service::io_service() 00172 { 00173 return owner_; 00174 } 00175 00176 template <typename Service> 00177 inline Service& use_service(io_service& ios) 00178 { 00179 // Check that Service meets the necessary type requirements. 00180 (void)static_cast<io_service::service*>(static_cast<Service*>(0)); 00181 (void)static_cast<const io_service::id*>(&Service::id); 00182 00183 return ios.service_registry_->template use_service<Service>(); 00184 } 00185 00186 template <typename Service> 00187 void add_service(io_service& ios, Service* svc) 00188 { 00189 // Check that Service meets the necessary type requirements. 00190 (void)static_cast<io_service::service*>(static_cast<Service*>(0)); 00191 (void)static_cast<const io_service::id*>(&Service::id); 00192 00193 if (&ios != &svc->io_service()) 00194 boost::throw_exception(invalid_service_owner()); 00195 if (!ios.service_registry_->template add_service<Service>(svc)) 00196 boost::throw_exception(service_already_exists()); 00197 } 00198 00199 template <typename Service> 00200 bool has_service(io_service& ios) 00201 { 00202 // Check that Service meets the necessary type requirements. 00203 (void)static_cast<io_service::service*>(static_cast<Service*>(0)); 00204 (void)static_cast<const io_service::id*>(&Service::id); 00205 00206 return ios.service_registry_->template has_service<Service>(); 00207 } 00208 00209 } // namespace asio 00210 00211 #include "asio/detail/pop_options.hpp" 00212 00213 #endif // ASIO_IO_SERVICE_IPP
1.5.6