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 | |
11 | #ifndef BOOST_ASIO_IMPL_IO_SERVICE_HPP |
12 | #define BOOST_ASIO_IMPL_IO_SERVICE_HPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/handler_type_requirements.hpp> |
19 | #include <boost/asio/detail/service_registry.hpp> |
20 | |
21 | #include <boost/asio/detail/push_options.hpp> |
22 | |
23 | namespace boost { |
24 | namespace asio { |
25 | |
26 | template <typename Service> |
27 | inline Service& use_service(io_service& ios) |
28 | { |
29 | // Check that Service meets the necessary type requirements. |
30 | (void)static_cast<io_service::service*>(static_cast<Service*>(0)); |
31 | (void)static_cast<const io_service::id*>(&Service::id); |
32 | |
33 | return ios.service_registry_->template use_service<Service>(); |
34 | } |
35 | |
36 | template <> |
37 | inline detail::io_service_impl& use_service<detail::io_service_impl>( |
38 | io_service& ios) |
39 | { |
40 | return ios.impl_; |
41 | } |
42 | |
43 | template <typename Service> |
44 | inline void add_service(io_service& ios, Service* svc) |
45 | { |
46 | // Check that Service meets the necessary type requirements. |
47 | (void)static_cast<io_service::service*>(static_cast<Service*>(0)); |
48 | (void)static_cast<const io_service::id*>(&Service::id); |
49 | |
50 | ios.service_registry_->template add_service<Service>(svc); |
51 | } |
52 | |
53 | template <typename Service> |
54 | inline bool has_service(io_service& ios) |
55 | { |
56 | // Check that Service meets the necessary type requirements. |
57 | (void)static_cast<io_service::service*>(static_cast<Service*>(0)); |
58 | (void)static_cast<const io_service::id*>(&Service::id); |
59 | |
60 | return ios.service_registry_->template has_service<Service>(); |
61 | } |
62 | |
63 | } // namespace asio |
64 | } // namespace boost |
65 | |
66 | #include <boost/asio/detail/pop_options.hpp> |
67 | |
68 | #if defined(BOOST_ASIO_HAS_IOCP) |
69 | # include <boost/asio/detail/win_iocp_io_service.hpp> |
70 | #else |
71 | # include <boost/asio/detail/task_io_service.hpp> |
72 | #endif |
73 | |
74 | #include <boost/asio/detail/push_options.hpp> |
75 | |
76 | namespace boost { |
77 | namespace asio { |
78 | |
79 | template <typename CompletionHandler> |
80 | inline BOOST_ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ()) |
81 | io_service::dispatch(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) |
82 | { |
83 | // If you get an error on the following line it means that your handler does |
84 | // not meet the documented type requirements for a CompletionHandler. |
85 | BOOST_ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check; |
86 | |
87 | detail::async_result_init< |
88 | CompletionHandler, void ()> init( |
89 | BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)); |
90 | |
91 | impl_.dispatch(init.handler); |
92 | |
93 | return init.result.get(); |
94 | } |
95 | |
96 | template <typename CompletionHandler> |
97 | inline BOOST_ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ()) |
98 | io_service::post(BOOST_ASIO_MOVE_ARG(CompletionHandler) handler) |
99 | { |
100 | // If you get an error on the following line it means that your handler does |
101 | // not meet the documented type requirements for a CompletionHandler. |
102 | BOOST_ASIO_COMPLETION_HANDLER_CHECK(CompletionHandler, handler) type_check; |
103 | |
104 | detail::async_result_init< |
105 | CompletionHandler, void ()> init( |
106 | BOOST_ASIO_MOVE_CAST(CompletionHandler)(handler)); |
107 | |
108 | impl_.post(init.handler); |
109 | |
110 | return init.result.get(); |
111 | } |
112 | |
113 | template <typename Handler> |
114 | #if defined(GENERATING_DOCUMENTATION) |
115 | unspecified |
116 | #else |
117 | inline detail::wrapped_handler<io_service&, Handler> |
118 | #endif |
119 | io_service::wrap(Handler handler) |
120 | { |
121 | return detail::wrapped_handler<io_service&, Handler>(*this, handler); |
122 | } |
123 | |
124 | inline io_service::work::work(boost::asio::io_service& io_service) |
125 | : io_service_impl_(io_service.impl_) |
126 | { |
127 | io_service_impl_.work_started(); |
128 | } |
129 | |
130 | inline io_service::work::work(const work& other) |
131 | : io_service_impl_(other.io_service_impl_) |
132 | { |
133 | io_service_impl_.work_started(); |
134 | } |
135 | |
136 | inline io_service::work::~work() |
137 | { |
138 | io_service_impl_.work_finished(); |
139 | } |
140 | |
141 | inline boost::asio::io_service& io_service::work::get_io_service() |
142 | { |
143 | return io_service_impl_.get_io_service(); |
144 | } |
145 | |
146 | inline boost::asio::io_service& io_service::service::get_io_service() |
147 | { |
148 | return owner_; |
149 | } |
150 | |
151 | } // namespace asio |
152 | } // namespace boost |
153 | |
154 | #include <boost/asio/detail/pop_options.hpp> |
155 | |
156 | #endif // BOOST_ASIO_IMPL_IO_SERVICE_HPP |
157 | |