]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // deadline_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_DEADLINE_TIMER_SERVICE_HPP | |
12 | #define BOOST_ASIO_DEADLINE_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> | |
19 | ||
b32b8144 FG |
20 | #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
21 | ||
7c673cae FG |
22 | #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \ |
23 | || defined(GENERATING_DOCUMENTATION) | |
24 | ||
25 | #include <cstddef> | |
26 | #include <boost/asio/async_result.hpp> | |
27 | #include <boost/asio/detail/deadline_timer_service.hpp> | |
b32b8144 | 28 | #include <boost/asio/io_context.hpp> |
7c673cae | 29 | #include <boost/asio/time_traits.hpp> |
7c673cae FG |
30 | |
31 | #include <boost/asio/detail/push_options.hpp> | |
32 | ||
33 | namespace boost { | |
34 | namespace asio { | |
35 | ||
36 | /// Default service implementation for a timer. | |
37 | template <typename TimeType, | |
38 | typename TimeTraits = boost::asio::time_traits<TimeType> > | |
39 | class deadline_timer_service | |
40 | #if defined(GENERATING_DOCUMENTATION) | |
b32b8144 | 41 | : public boost::asio::io_context::service |
7c673cae FG |
42 | #else |
43 | : public boost::asio::detail::service_base< | |
44 | deadline_timer_service<TimeType, TimeTraits> > | |
45 | #endif | |
46 | { | |
47 | public: | |
48 | #if defined(GENERATING_DOCUMENTATION) | |
49 | /// The unique service identifier. | |
b32b8144 | 50 | static boost::asio::io_context::id id; |
7c673cae FG |
51 | #endif |
52 | ||
53 | /// The time traits type. | |
54 | typedef TimeTraits traits_type; | |
55 | ||
56 | /// The time type. | |
57 | typedef typename traits_type::time_type time_type; | |
58 | ||
59 | /// The duration type. | |
60 | typedef typename traits_type::duration_type duration_type; | |
61 | ||
62 | private: | |
63 | // The type of the platform-specific implementation. | |
64 | typedef detail::deadline_timer_service<traits_type> service_impl_type; | |
65 | ||
66 | public: | |
67 | /// The implementation type of the deadline timer. | |
68 | #if defined(GENERATING_DOCUMENTATION) | |
69 | typedef implementation_defined implementation_type; | |
70 | #else | |
71 | typedef typename service_impl_type::implementation_type implementation_type; | |
72 | #endif | |
73 | ||
b32b8144 FG |
74 | /// Construct a new timer service for the specified io_context. |
75 | explicit deadline_timer_service(boost::asio::io_context& io_context) | |
7c673cae | 76 | : boost::asio::detail::service_base< |
b32b8144 FG |
77 | deadline_timer_service<TimeType, TimeTraits> >(io_context), |
78 | service_impl_(io_context) | |
7c673cae FG |
79 | { |
80 | } | |
81 | ||
82 | /// Construct a new timer implementation. | |
83 | void construct(implementation_type& impl) | |
84 | { | |
85 | service_impl_.construct(impl); | |
86 | } | |
87 | ||
88 | /// Destroy a timer implementation. | |
89 | void destroy(implementation_type& impl) | |
90 | { | |
91 | service_impl_.destroy(impl); | |
92 | } | |
93 | ||
94 | /// Cancel any asynchronous wait operations associated with the timer. | |
95 | std::size_t cancel(implementation_type& impl, boost::system::error_code& ec) | |
96 | { | |
97 | return service_impl_.cancel(impl, ec); | |
98 | } | |
99 | ||
100 | /// Cancels one asynchronous wait operation associated with the timer. | |
101 | std::size_t cancel_one(implementation_type& impl, | |
102 | boost::system::error_code& ec) | |
103 | { | |
104 | return service_impl_.cancel_one(impl, ec); | |
105 | } | |
106 | ||
107 | /// Get the expiry time for the timer as an absolute time. | |
108 | time_type expires_at(const implementation_type& impl) const | |
109 | { | |
b32b8144 | 110 | return service_impl_.expiry(impl); |
7c673cae FG |
111 | } |
112 | ||
113 | /// Set the expiry time for the timer as an absolute time. | |
114 | std::size_t expires_at(implementation_type& impl, | |
115 | const time_type& expiry_time, boost::system::error_code& ec) | |
116 | { | |
117 | return service_impl_.expires_at(impl, expiry_time, ec); | |
118 | } | |
119 | ||
120 | /// Get the expiry time for the timer relative to now. | |
121 | duration_type expires_from_now(const implementation_type& impl) const | |
122 | { | |
b32b8144 | 123 | return TimeTraits::subtract(service_impl_.expiry(impl), TimeTraits::now()); |
7c673cae FG |
124 | } |
125 | ||
126 | /// Set the expiry time for the timer relative to now. | |
127 | std::size_t expires_from_now(implementation_type& impl, | |
128 | const duration_type& expiry_time, boost::system::error_code& ec) | |
129 | { | |
b32b8144 | 130 | return service_impl_.expires_after(impl, expiry_time, ec); |
7c673cae FG |
131 | } |
132 | ||
133 | // Perform a blocking wait on the timer. | |
134 | void wait(implementation_type& impl, boost::system::error_code& ec) | |
135 | { | |
136 | service_impl_.wait(impl, ec); | |
137 | } | |
138 | ||
139 | // Start an asynchronous wait on the timer. | |
140 | template <typename WaitHandler> | |
141 | BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler, | |
142 | void (boost::system::error_code)) | |
143 | async_wait(implementation_type& impl, | |
144 | BOOST_ASIO_MOVE_ARG(WaitHandler) handler) | |
145 | { | |
b32b8144 FG |
146 | async_completion<WaitHandler, |
147 | void (boost::system::error_code)> init(handler); | |
7c673cae | 148 | |
b32b8144 | 149 | service_impl_.async_wait(impl, init.completion_handler); |
7c673cae FG |
150 | |
151 | return init.result.get(); | |
152 | } | |
153 | ||
154 | private: | |
155 | // Destroy all user-defined handler objects owned by the service. | |
b32b8144 | 156 | void shutdown() |
7c673cae | 157 | { |
b32b8144 | 158 | service_impl_.shutdown(); |
7c673cae FG |
159 | } |
160 | ||
161 | // The platform-specific implementation. | |
162 | service_impl_type service_impl_; | |
163 | }; | |
164 | ||
165 | } // namespace asio | |
166 | } // namespace boost | |
167 | ||
168 | #include <boost/asio/detail/pop_options.hpp> | |
169 | ||
170 | #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) | |
171 | // || defined(GENERATING_DOCUMENTATION) | |
172 | ||
b32b8144 FG |
173 | #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) |
174 | ||
7c673cae | 175 | #endif // BOOST_ASIO_DEADLINE_TIMER_SERVICE_HPP |