2 // waitable_timer_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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 #include <boost/asio/async_result.hpp>
21 #include <boost/asio/detail/chrono_time_traits.hpp>
22 #include <boost/asio/detail/deadline_timer_service.hpp>
23 #include <boost/asio/io_service.hpp>
24 #include <boost/asio/wait_traits.hpp>
26 #include <boost/asio/detail/push_options.hpp>
31 /// Default service implementation for a timer.
32 template <typename Clock,
33 typename WaitTraits = boost::asio::wait_traits<Clock> >
34 class waitable_timer_service
35 #if defined(GENERATING_DOCUMENTATION)
36 : public boost::asio::io_service::service
38 : public boost::asio::detail::service_base<
39 waitable_timer_service<Clock, WaitTraits> >
43 #if defined(GENERATING_DOCUMENTATION)
44 /// The unique service identifier.
45 static boost::asio::io_service::id id;
49 typedef Clock clock_type;
51 /// The duration type of the clock.
52 typedef typename clock_type::duration duration;
54 /// The time point type of the clock.
55 typedef typename clock_type::time_point time_point;
57 /// The wait traits type.
58 typedef WaitTraits traits_type;
61 // The type of the platform-specific implementation.
62 typedef detail::deadline_timer_service<
63 detail::chrono_time_traits<Clock, WaitTraits> > service_impl_type;
66 /// The implementation type of the waitable timer.
67 #if defined(GENERATING_DOCUMENTATION)
68 typedef implementation_defined implementation_type;
70 typedef typename service_impl_type::implementation_type implementation_type;
73 /// Construct a new timer service for the specified io_service.
74 explicit waitable_timer_service(boost::asio::io_service& io_service)
75 : boost::asio::detail::service_base<
76 waitable_timer_service<Clock, WaitTraits> >(io_service),
77 service_impl_(io_service)
81 /// Construct a new timer implementation.
82 void construct(implementation_type& impl)
84 service_impl_.construct(impl);
87 /// Destroy a timer implementation.
88 void destroy(implementation_type& impl)
90 service_impl_.destroy(impl);
93 /// Cancel any asynchronous wait operations associated with the timer.
94 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
96 return service_impl_.cancel(impl, ec);
99 /// Cancels one asynchronous wait operation associated with the timer.
100 std::size_t cancel_one(implementation_type& impl,
101 boost::system::error_code& ec)
103 return service_impl_.cancel_one(impl, ec);
106 /// Get the expiry time for the timer as an absolute time.
107 time_point expires_at(const implementation_type& impl) const
109 return service_impl_.expires_at(impl);
112 /// Set the expiry time for the timer as an absolute time.
113 std::size_t expires_at(implementation_type& impl,
114 const time_point& expiry_time, boost::system::error_code& ec)
116 return service_impl_.expires_at(impl, expiry_time, ec);
119 /// Get the expiry time for the timer relative to now.
120 duration expires_from_now(const implementation_type& impl) const
122 return service_impl_.expires_from_now(impl);
125 /// Set the expiry time for the timer relative to now.
126 std::size_t expires_from_now(implementation_type& impl,
127 const duration& expiry_time, boost::system::error_code& ec)
129 return service_impl_.expires_from_now(impl, expiry_time, ec);
132 // Perform a blocking wait on the timer.
133 void wait(implementation_type& impl, boost::system::error_code& ec)
135 service_impl_.wait(impl, ec);
138 // Start an asynchronous wait on the timer.
139 template <typename WaitHandler>
140 BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
141 void (boost::system::error_code))
142 async_wait(implementation_type& impl,
143 BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
145 detail::async_result_init<
146 WaitHandler, void (boost::system::error_code)> init(
147 BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
149 service_impl_.async_wait(impl, init.handler);
151 return init.result.get();
155 // Destroy all user-defined handler objects owned by the service.
156 void shutdown_service()
158 service_impl_.shutdown_service();
161 // The platform-specific implementation.
162 service_impl_type service_impl_;
168 #include <boost/asio/detail/pop_options.hpp>
170 #endif // BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP