1 //
2 // waitable_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_WAITABLE_TIMER_SERVICE_HPP
12 #define BOOST_ASIO_WAITABLE_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 #include <cstddef>
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>
25 
26 #include <boost/asio/detail/push_options.hpp>
27 
28 namespace boost {
29 namespace asio {
30 
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
37 #else
38   : public boost::asio::detail::service_base<
39       waitable_timer_service<Clock, WaitTraits> >
40 #endif
41 {
42 public:
43 #if defined(GENERATING_DOCUMENTATION)
44   /// The unique service identifier.
45   static boost::asio::io_service::id id;
46 #endif
47 
48   /// The clock type.
49   typedef Clock clock_type;
50 
51   /// The duration type of the clock.
52   typedef typename clock_type::duration duration;
53 
54   /// The time point type of the clock.
55   typedef typename clock_type::time_point time_point;
56 
57   /// The wait traits type.
58   typedef WaitTraits traits_type;
59 
60 private:
61   // The type of the platform-specific implementation.
62   typedef detail::deadline_timer_service<
63     detail::chrono_time_traits<Clock, WaitTraits> > service_impl_type;
64 
65 public:
66   /// The implementation type of the waitable 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.
waitable_timer_service(boost::asio::io_service & 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)
78   {
79   }
80 
81   /// Construct a new timer implementation.
construct(implementation_type & impl)82   void construct(implementation_type& impl)
83   {
84     service_impl_.construct(impl);
85   }
86 
87   /// Destroy a timer implementation.
destroy(implementation_type & impl)88   void destroy(implementation_type& impl)
89   {
90     service_impl_.destroy(impl);
91   }
92 
93   /// Cancel any asynchronous wait operations associated with the timer.
cancel(implementation_type & impl,boost::system::error_code & ec)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.
cancel_one(implementation_type & impl,boost::system::error_code & ec)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.
expires_at(const implementation_type & impl) const107   time_point 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.
expires_at(implementation_type & impl,const time_point & expiry_time,boost::system::error_code & ec)113   std::size_t expires_at(implementation_type& impl,
114       const time_point& 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.
expires_from_now(const implementation_type & impl) const120   duration 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.
expires_from_now(implementation_type & impl,const duration & expiry_time,boost::system::error_code & ec)126   std::size_t expires_from_now(implementation_type& impl,
127       const duration& 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.
wait(implementation_type & impl,boost::system::error_code & ec)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>
BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,void (boost::system::error_code))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 
154 private:
155   // Destroy all user-defined handler objects owned by the service.
shutdown_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 // BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP
171