]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // waitable_timer_service.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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_WAITABLE_TIMER_SERVICE_HPP | |
12 | #define BOOST_ASIO_WAITABLE_TIMER_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/config.hpp> | |
b32b8144 FG |
19 | |
20 | #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) | |
21 | ||
7c673cae FG |
22 | #include <cstddef> |
23 | #include <boost/asio/async_result.hpp> | |
24 | #include <boost/asio/detail/chrono_time_traits.hpp> | |
25 | #include <boost/asio/detail/deadline_timer_service.hpp> | |
b32b8144 | 26 | #include <boost/asio/io_context.hpp> |
7c673cae FG |
27 | #include <boost/asio/wait_traits.hpp> |
28 | ||
29 | #include <boost/asio/detail/push_options.hpp> | |
30 | ||
31 | namespace boost { | |
32 | namespace asio { | |
33 | ||
34 | /// Default service implementation for a timer. | |
35 | template <typename Clock, | |
36 | typename WaitTraits = boost::asio::wait_traits<Clock> > | |
37 | class waitable_timer_service | |
38 | #if defined(GENERATING_DOCUMENTATION) | |
b32b8144 | 39 | : public boost::asio::io_context::service |
7c673cae FG |
40 | #else |
41 | : public boost::asio::detail::service_base< | |
42 | waitable_timer_service<Clock, WaitTraits> > | |
43 | #endif | |
44 | { | |
45 | public: | |
46 | #if defined(GENERATING_DOCUMENTATION) | |
47 | /// The unique service identifier. | |
b32b8144 | 48 | static boost::asio::io_context::id id; |
7c673cae FG |
49 | #endif |
50 | ||
51 | /// The clock type. | |
52 | typedef Clock clock_type; | |
53 | ||
54 | /// The duration type of the clock. | |
55 | typedef typename clock_type::duration duration; | |
56 | ||
57 | /// The time point type of the clock. | |
58 | typedef typename clock_type::time_point time_point; | |
59 | ||
60 | /// The wait traits type. | |
61 | typedef WaitTraits traits_type; | |
62 | ||
63 | private: | |
64 | // The type of the platform-specific implementation. | |
65 | typedef detail::deadline_timer_service< | |
66 | detail::chrono_time_traits<Clock, WaitTraits> > service_impl_type; | |
67 | ||
68 | public: | |
69 | /// The implementation type of the waitable timer. | |
70 | #if defined(GENERATING_DOCUMENTATION) | |
71 | typedef implementation_defined implementation_type; | |
72 | #else | |
73 | typedef typename service_impl_type::implementation_type implementation_type; | |
74 | #endif | |
75 | ||
b32b8144 FG |
76 | /// Construct a new timer service for the specified io_context. |
77 | explicit waitable_timer_service(boost::asio::io_context& io_context) | |
7c673cae | 78 | : boost::asio::detail::service_base< |
b32b8144 FG |
79 | waitable_timer_service<Clock, WaitTraits> >(io_context), |
80 | service_impl_(io_context) | |
7c673cae FG |
81 | { |
82 | } | |
83 | ||
84 | /// Construct a new timer implementation. | |
85 | void construct(implementation_type& impl) | |
86 | { | |
87 | service_impl_.construct(impl); | |
88 | } | |
89 | ||
90 | /// Destroy a timer implementation. | |
91 | void destroy(implementation_type& impl) | |
92 | { | |
93 | service_impl_.destroy(impl); | |
94 | } | |
95 | ||
b32b8144 FG |
96 | #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) |
97 | /// Move-construct a new timer implementation. | |
98 | void move_construct(implementation_type& impl, | |
99 | implementation_type& other_impl) | |
100 | { | |
101 | service_impl_.move_construct(impl, other_impl); | |
102 | } | |
103 | ||
104 | /// Move-assign from another timer implementation. | |
105 | void move_assign(implementation_type& impl, | |
106 | waitable_timer_service& other_service, | |
107 | implementation_type& other_impl) | |
108 | { | |
109 | service_impl_.move_assign(impl, other_service.service_impl_, other_impl); | |
110 | } | |
111 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
112 | ||
7c673cae FG |
113 | /// Cancel any asynchronous wait operations associated with the timer. |
114 | std::size_t cancel(implementation_type& impl, boost::system::error_code& ec) | |
115 | { | |
116 | return service_impl_.cancel(impl, ec); | |
117 | } | |
118 | ||
119 | /// Cancels one asynchronous wait operation associated with the timer. | |
120 | std::size_t cancel_one(implementation_type& impl, | |
121 | boost::system::error_code& ec) | |
122 | { | |
123 | return service_impl_.cancel_one(impl, ec); | |
124 | } | |
125 | ||
b32b8144 FG |
126 | #if !defined(BOOST_ASIO_NO_DEPRECATED) |
127 | /// (Deprecated: Use expiry().) Get the expiry time for the timer as an | |
128 | /// absolute time. | |
7c673cae FG |
129 | time_point expires_at(const implementation_type& impl) const |
130 | { | |
b32b8144 FG |
131 | return service_impl_.expiry(impl); |
132 | } | |
133 | #endif // !defined(BOOST_ASIO_NO_DEPRECATED) | |
134 | ||
135 | /// Get the expiry time for the timer as an absolute time. | |
136 | time_point expiry(const implementation_type& impl) const | |
137 | { | |
138 | return service_impl_.expiry(impl); | |
7c673cae FG |
139 | } |
140 | ||
141 | /// Set the expiry time for the timer as an absolute time. | |
142 | std::size_t expires_at(implementation_type& impl, | |
143 | const time_point& expiry_time, boost::system::error_code& ec) | |
144 | { | |
145 | return service_impl_.expires_at(impl, expiry_time, ec); | |
146 | } | |
147 | ||
b32b8144 FG |
148 | /// Set the expiry time for the timer relative to now. |
149 | std::size_t expires_after(implementation_type& impl, | |
150 | const duration& expiry_time, boost::system::error_code& ec) | |
151 | { | |
152 | return service_impl_.expires_after(impl, expiry_time, ec); | |
153 | } | |
154 | ||
155 | #if !defined(BOOST_ASIO_NO_DEPRECATED) | |
156 | /// (Deprecated: Use expiry().) Get the expiry time for the timer relative to | |
157 | /// now. | |
7c673cae FG |
158 | duration expires_from_now(const implementation_type& impl) const |
159 | { | |
b32b8144 FG |
160 | typedef detail::chrono_time_traits<Clock, WaitTraits> traits; |
161 | return traits::subtract(service_impl_.expiry(impl), traits::now()); | |
7c673cae FG |
162 | } |
163 | ||
b32b8144 FG |
164 | /// (Deprecated: Use expires_after().) Set the expiry time for the timer |
165 | /// relative to now. | |
7c673cae FG |
166 | std::size_t expires_from_now(implementation_type& impl, |
167 | const duration& expiry_time, boost::system::error_code& ec) | |
168 | { | |
b32b8144 | 169 | return service_impl_.expires_after(impl, expiry_time, ec); |
7c673cae | 170 | } |
b32b8144 | 171 | #endif // !defined(BOOST_ASIO_NO_DEPRECATED) |
7c673cae FG |
172 | |
173 | // Perform a blocking wait on the timer. | |
174 | void wait(implementation_type& impl, boost::system::error_code& ec) | |
175 | { | |
176 | service_impl_.wait(impl, ec); | |
177 | } | |
178 | ||
179 | // Start an asynchronous wait on the timer. | |
180 | template <typename WaitHandler> | |
181 | BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler, | |
182 | void (boost::system::error_code)) | |
183 | async_wait(implementation_type& impl, | |
184 | BOOST_ASIO_MOVE_ARG(WaitHandler) handler) | |
185 | { | |
b32b8144 FG |
186 | async_completion<WaitHandler, |
187 | void (boost::system::error_code)> init(handler); | |
7c673cae | 188 | |
b32b8144 | 189 | service_impl_.async_wait(impl, init.completion_handler); |
7c673cae FG |
190 | |
191 | return init.result.get(); | |
192 | } | |
193 | ||
194 | private: | |
195 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 196 | void shutdown() |
7c673cae | 197 | { |
b32b8144 | 198 | service_impl_.shutdown(); |
7c673cae FG |
199 | } |
200 | ||
201 | // The platform-specific implementation. | |
202 | service_impl_type service_impl_; | |
203 | }; | |
204 | ||
205 | } // namespace asio | |
206 | } // namespace boost | |
207 | ||
208 | #include <boost/asio/detail/pop_options.hpp> | |
209 | ||
b32b8144 FG |
210 | #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
211 | ||
7c673cae | 212 | #endif // BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP |