2 // waitable_timer_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP
12 #define BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
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>
26 #include <boost/asio/io_context.hpp>
27 #include <boost/asio/wait_traits.hpp>
29 #include <boost/asio/detail/push_options.hpp>
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)
39 : public boost::asio::io_context::service
41 : public boost::asio::detail::service_base<
42 waitable_timer_service<Clock, WaitTraits> >
46 #if defined(GENERATING_DOCUMENTATION)
47 /// The unique service identifier.
48 static boost::asio::io_context::id id;
52 typedef Clock clock_type;
54 /// The duration type of the clock.
55 typedef typename clock_type::duration duration;
57 /// The time point type of the clock.
58 typedef typename clock_type::time_point time_point;
60 /// The wait traits type.
61 typedef WaitTraits traits_type;
64 // The type of the platform-specific implementation.
65 typedef detail::deadline_timer_service<
66 detail::chrono_time_traits<Clock, WaitTraits> > service_impl_type;
69 /// The implementation type of the waitable timer.
70 #if defined(GENERATING_DOCUMENTATION)
71 typedef implementation_defined implementation_type;
73 typedef typename service_impl_type::implementation_type implementation_type;
76 /// Construct a new timer service for the specified io_context.
77 explicit waitable_timer_service(boost::asio::io_context& io_context)
78 : boost::asio::detail::service_base<
79 waitable_timer_service<Clock, WaitTraits> >(io_context),
80 service_impl_(io_context)
84 /// Construct a new timer implementation.
85 void construct(implementation_type& impl)
87 service_impl_.construct(impl);
90 /// Destroy a timer implementation.
91 void destroy(implementation_type& impl)
93 service_impl_.destroy(impl);
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)
101 service_impl_.move_construct(impl, other_impl);
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)
109 service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
111 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
113 /// Cancel any asynchronous wait operations associated with the timer.
114 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
116 return service_impl_.cancel(impl, ec);
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)
123 return service_impl_.cancel_one(impl, ec);
126 #if !defined(BOOST_ASIO_NO_DEPRECATED)
127 /// (Deprecated: Use expiry().) Get the expiry time for the timer as an
129 time_point expires_at(const implementation_type& impl) const
131 return service_impl_.expiry(impl);
133 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
135 /// Get the expiry time for the timer as an absolute time.
136 time_point expiry(const implementation_type& impl) const
138 return service_impl_.expiry(impl);
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)
145 return service_impl_.expires_at(impl, expiry_time, ec);
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)
152 return service_impl_.expires_after(impl, expiry_time, ec);
155 #if !defined(BOOST_ASIO_NO_DEPRECATED)
156 /// (Deprecated: Use expiry().) Get the expiry time for the timer relative to
158 duration expires_from_now(const implementation_type& impl) const
160 typedef detail::chrono_time_traits<Clock, WaitTraits> traits;
161 return traits::subtract(service_impl_.expiry(impl), traits::now());
164 /// (Deprecated: Use expires_after().) Set the expiry time for the timer
166 std::size_t expires_from_now(implementation_type& impl,
167 const duration& expiry_time, boost::system::error_code& ec)
169 return service_impl_.expires_after(impl, expiry_time, ec);
171 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
173 // Perform a blocking wait on the timer.
174 void wait(implementation_type& impl, boost::system::error_code& ec)
176 service_impl_.wait(impl, ec);
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)
186 async_completion<WaitHandler,
187 void (boost::system::error_code)> init(handler);
189 service_impl_.async_wait(impl, init.completion_handler);
191 return init.result.get();
195 // Destroy all user-defined handler objects owned by the service.
198 service_impl_.shutdown();
201 // The platform-specific implementation.
202 service_impl_type service_impl_;
208 #include <boost/asio/detail/pop_options.hpp>
210 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
212 #endif // BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP