2 // deadline_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_DEADLINE_TIMER_SERVICE_HPP
12 #define BOOST_ASIO_DEADLINE_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_HAS_BOOST_DATE_TIME) \
21 || defined(GENERATING_DOCUMENTATION)
24 #include <boost/asio/async_result.hpp>
25 #include <boost/asio/detail/deadline_timer_service.hpp>
26 #include <boost/asio/io_service.hpp>
27 #include <boost/asio/time_traits.hpp>
28 #include <boost/asio/detail/timer_queue_ptime.hpp>
30 #include <boost/asio/detail/push_options.hpp>
35 /// Default service implementation for a timer.
36 template <typename TimeType,
37 typename TimeTraits = boost::asio::time_traits<TimeType> >
38 class deadline_timer_service
39 #if defined(GENERATING_DOCUMENTATION)
40 : public boost::asio::io_service::service
42 : public boost::asio::detail::service_base<
43 deadline_timer_service<TimeType, TimeTraits> >
47 #if defined(GENERATING_DOCUMENTATION)
48 /// The unique service identifier.
49 static boost::asio::io_service::id id;
52 /// The time traits type.
53 typedef TimeTraits traits_type;
56 typedef typename traits_type::time_type time_type;
58 /// The duration type.
59 typedef typename traits_type::duration_type duration_type;
62 // The type of the platform-specific implementation.
63 typedef detail::deadline_timer_service<traits_type> service_impl_type;
66 /// The implementation type of the deadline 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 deadline_timer_service(boost::asio::io_service& io_service)
75 : boost::asio::detail::service_base<
76 deadline_timer_service<TimeType, TimeTraits> >(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_type 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_type& 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_type 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_type& 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 // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
171 // || defined(GENERATING_DOCUMENTATION)
173 #endif // BOOST_ASIO_DEADLINE_TIMER_SERVICE_HPP