2 // deadline_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_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_ENABLE_OLD_SERVICES)
22 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
23 || defined(GENERATING_DOCUMENTATION)
26 #include <boost/asio/async_result.hpp>
27 #include <boost/asio/detail/deadline_timer_service.hpp>
28 #include <boost/asio/io_context.hpp>
29 #include <boost/asio/time_traits.hpp>
31 #include <boost/asio/detail/push_options.hpp>
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)
41 : public boost::asio::io_context::service
43 : public boost::asio::detail::service_base<
44 deadline_timer_service<TimeType, TimeTraits> >
48 #if defined(GENERATING_DOCUMENTATION)
49 /// The unique service identifier.
50 static boost::asio::io_context::id id;
53 /// The time traits type.
54 typedef TimeTraits traits_type;
57 typedef typename traits_type::time_type time_type;
59 /// The duration type.
60 typedef typename traits_type::duration_type duration_type;
63 // The type of the platform-specific implementation.
64 typedef detail::deadline_timer_service<traits_type> service_impl_type;
67 /// The implementation type of the deadline timer.
68 #if defined(GENERATING_DOCUMENTATION)
69 typedef implementation_defined implementation_type;
71 typedef typename service_impl_type::implementation_type implementation_type;
74 /// Construct a new timer service for the specified io_context.
75 explicit deadline_timer_service(boost::asio::io_context& io_context)
76 : boost::asio::detail::service_base<
77 deadline_timer_service<TimeType, TimeTraits> >(io_context),
78 service_impl_(io_context)
82 /// Construct a new timer implementation.
83 void construct(implementation_type& impl)
85 service_impl_.construct(impl);
88 /// Destroy a timer implementation.
89 void destroy(implementation_type& impl)
91 service_impl_.destroy(impl);
94 /// Cancel any asynchronous wait operations associated with the timer.
95 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
97 return service_impl_.cancel(impl, ec);
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)
104 return service_impl_.cancel_one(impl, ec);
107 /// Get the expiry time for the timer as an absolute time.
108 time_type expires_at(const implementation_type& impl) const
110 return service_impl_.expiry(impl);
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)
117 return service_impl_.expires_at(impl, expiry_time, ec);
120 /// Get the expiry time for the timer relative to now.
121 duration_type expires_from_now(const implementation_type& impl) const
123 return TimeTraits::subtract(service_impl_.expiry(impl), TimeTraits::now());
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)
130 return service_impl_.expires_after(impl, expiry_time, ec);
133 // Perform a blocking wait on the timer.
134 void wait(implementation_type& impl, boost::system::error_code& ec)
136 service_impl_.wait(impl, ec);
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)
146 async_completion<WaitHandler,
147 void (boost::system::error_code)> init(handler);
149 service_impl_.async_wait(impl, init.completion_handler);
151 return init.result.get();
155 // Destroy all user-defined handler objects owned by the service.
158 service_impl_.shutdown();
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 // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
175 #endif // BOOST_ASIO_DEADLINE_TIMER_SERVICE_HPP