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