2 // deadline_timer_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2011 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 #include <boost/asio/detail/deadline_timer_service.hpp>
21 #include <boost/asio/io_service.hpp>
22 #include <boost/asio/time_traits.hpp>
24 #include <boost/asio/detail/push_options.hpp>
29 /// Default service implementation for a timer.
30 template <typename TimeType,
31 typename TimeTraits = boost::asio::time_traits<TimeType> >
32 class deadline_timer_service
33 #if defined(GENERATING_DOCUMENTATION)
34 : public boost::asio::io_service::service
36 : public boost::asio::detail::service_base<
37 deadline_timer_service<TimeType, TimeTraits> >
41 #if defined(GENERATING_DOCUMENTATION)
42 /// The unique service identifier.
43 static boost::asio::io_service::id id;
46 /// The time traits type.
47 typedef TimeTraits traits_type;
50 typedef typename traits_type::time_type time_type;
52 /// The duration type.
53 typedef typename traits_type::duration_type duration_type;
56 // The type of the platform-specific implementation.
57 typedef detail::deadline_timer_service<traits_type> service_impl_type;
60 /// The implementation type of the deadline timer.
61 #if defined(GENERATING_DOCUMENTATION)
62 typedef implementation_defined implementation_type;
64 typedef typename service_impl_type::implementation_type implementation_type;
67 /// Construct a new timer service for the specified io_service.
68 explicit deadline_timer_service(boost::asio::io_service& io_service)
69 : boost::asio::detail::service_base<
70 deadline_timer_service<TimeType, TimeTraits> >(io_service),
71 service_impl_(io_service)
75 /// Construct a new timer implementation.
76 void construct(implementation_type& impl)
78 service_impl_.construct(impl);
81 /// Destroy a timer implementation.
82 void destroy(implementation_type& impl)
84 service_impl_.destroy(impl);
87 /// Cancel any asynchronous wait operations associated with the timer.
88 std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
90 return service_impl_.cancel(impl, ec);
93 /// Cancels one asynchronous wait operation associated with the timer.
94 std::size_t cancel_one(implementation_type& impl,
95 boost::system::error_code& ec)
97 return service_impl_.cancel_one(impl, ec);
100 /// Get the expiry time for the timer as an absolute time.
101 time_type expires_at(const implementation_type& impl) const
103 return service_impl_.expires_at(impl);
106 /// Set the expiry time for the timer as an absolute time.
107 std::size_t expires_at(implementation_type& impl,
108 const time_type& expiry_time, boost::system::error_code& ec)
110 return service_impl_.expires_at(impl, expiry_time, ec);
113 /// Get the expiry time for the timer relative to now.
114 duration_type expires_from_now(const implementation_type& impl) const
116 return service_impl_.expires_from_now(impl);
119 /// Set the expiry time for the timer relative to now.
120 std::size_t expires_from_now(implementation_type& impl,
121 const duration_type& expiry_time, boost::system::error_code& ec)
123 return service_impl_.expires_from_now(impl, expiry_time, ec);
126 // Perform a blocking wait on the timer.
127 void wait(implementation_type& impl, boost::system::error_code& ec)
129 service_impl_.wait(impl, ec);
132 // Start an asynchronous wait on the timer.
133 template <typename WaitHandler>
134 void async_wait(implementation_type& impl,
135 BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
137 service_impl_.async_wait(impl, BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
141 // Destroy all user-defined handler objects owned by the service.
142 void shutdown_service()
144 service_impl_.shutdown_service();
147 // The platform-specific implementation.
148 service_impl_type service_impl_;
154 #include <boost/asio/detail/pop_options.hpp>
156 #endif // BOOST_ASIO_DEADLINE_TIMER_SERVICE_HPP