1//
2// deadline_timer_service.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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
20#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
21 || defined(GENERATING_DOCUMENTATION)
22
23#include <cstddef>
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>
29
30#include <boost/asio/detail/push_options.hpp>
31
32namespace boost {
33namespace asio {
34
35/// Default service implementation for a timer.
36template <typename TimeType,
37 typename TimeTraits = boost::asio::time_traits<TimeType> >
38class deadline_timer_service
39#if defined(GENERATING_DOCUMENTATION)
40 : public boost::asio::io_service::service
41#else
42 : public boost::asio::detail::service_base<
43 deadline_timer_service<TimeType, TimeTraits> >
44#endif
45{
46public:
47#if defined(GENERATING_DOCUMENTATION)
48 /// The unique service identifier.
49 static boost::asio::io_service::id id;
50#endif
51
52 /// The time traits type.
53 typedef TimeTraits traits_type;
54
55 /// The time type.
56 typedef typename traits_type::time_type time_type;
57
58 /// The duration type.
59 typedef typename traits_type::duration_type duration_type;
60
61private:
62 // The type of the platform-specific implementation.
63 typedef detail::deadline_timer_service<traits_type> service_impl_type;
64
65public:
66 /// The implementation type of the deadline timer.
67#if defined(GENERATING_DOCUMENTATION)
68 typedef implementation_defined implementation_type;
69#else
70 typedef typename service_impl_type::implementation_type implementation_type;
71#endif
72
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)
78 {
79 }
80
81 /// Construct a new timer implementation.
82 void construct(implementation_type& impl)
83 {
84 service_impl_.construct(impl);
85 }
86
87 /// Destroy a timer implementation.
88 void destroy(implementation_type& impl)
89 {
90 service_impl_.destroy(impl);
91 }
92
93 /// Cancel any asynchronous wait operations associated with the timer.
94 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
95 {
96 return service_impl_.cancel(impl, ec);
97 }
98
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)
102 {
103 return service_impl_.cancel_one(impl, ec);
104 }
105
106 /// Get the expiry time for the timer as an absolute time.
107 time_type expires_at(const implementation_type& impl) const
108 {
109 return service_impl_.expires_at(impl);
110 }
111
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)
115 {
116 return service_impl_.expires_at(impl, expiry_time, ec);
117 }
118
119 /// Get the expiry time for the timer relative to now.
120 duration_type expires_from_now(const implementation_type& impl) const
121 {
122 return service_impl_.expires_from_now(impl);
123 }
124
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)
128 {
129 return service_impl_.expires_from_now(impl, expiry_time, ec);
130 }
131
132 // Perform a blocking wait on the timer.
133 void wait(implementation_type& impl, boost::system::error_code& ec)
134 {
135 service_impl_.wait(impl, ec);
136 }
137
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)
144 {
145 detail::async_result_init<
146 WaitHandler, void (boost::system::error_code)> init(
147 BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
148
149 service_impl_.async_wait(impl, init.handler);
150
151 return init.result.get();
152 }
153
154private:
155 // Destroy all user-defined handler objects owned by the service.
156 void shutdown_service()
157 {
158 service_impl_.shutdown_service();
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
173#endif // BOOST_ASIO_DEADLINE_TIMER_SERVICE_HPP
174