waitable_timer_service.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // waitable_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. #ifndef BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP
  11. #define BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #include <cstddef>
  18. #include <boost/asio/async_result.hpp>
  19. #include <boost/asio/detail/chrono_time_traits.hpp>
  20. #include <boost/asio/detail/deadline_timer_service.hpp>
  21. #include <boost/asio/io_context.hpp>
  22. #include <boost/asio/wait_traits.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. /// Default service implementation for a timer.
  27. template <typename Clock,
  28. typename WaitTraits = boost::asio::wait_traits<Clock> >
  29. class waitable_timer_service
  30. #if defined(GENERATING_DOCUMENTATION)
  31. : public boost::asio::io_context::service
  32. #else
  33. : public boost::asio::detail::service_base<
  34. waitable_timer_service<Clock, WaitTraits> >
  35. #endif
  36. {
  37. public:
  38. #if defined(GENERATING_DOCUMENTATION)
  39. /// The unique service identifier.
  40. static boost::asio::io_context::id id;
  41. #endif
  42. /// The clock type.
  43. typedef Clock clock_type;
  44. /// The duration type of the clock.
  45. typedef typename clock_type::duration duration;
  46. /// The time point type of the clock.
  47. typedef typename clock_type::time_point time_point;
  48. /// The wait traits type.
  49. typedef WaitTraits traits_type;
  50. private:
  51. // The type of the platform-specific implementation.
  52. typedef detail::deadline_timer_service<
  53. detail::chrono_time_traits<Clock, WaitTraits> > service_impl_type;
  54. public:
  55. /// The implementation type of the waitable timer.
  56. #if defined(GENERATING_DOCUMENTATION)
  57. typedef implementation_defined implementation_type;
  58. #else
  59. typedef typename service_impl_type::implementation_type implementation_type;
  60. #endif
  61. /// Construct a new timer service for the specified io_context.
  62. explicit waitable_timer_service(boost::asio::io_context& io_context)
  63. : boost::asio::detail::service_base<
  64. waitable_timer_service<Clock, WaitTraits> >(io_context),
  65. service_impl_(io_context)
  66. {
  67. }
  68. /// Construct a new timer implementation.
  69. void construct(implementation_type& impl)
  70. {
  71. service_impl_.construct(impl);
  72. }
  73. /// Destroy a timer implementation.
  74. void destroy(implementation_type& impl)
  75. {
  76. service_impl_.destroy(impl);
  77. }
  78. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  79. /// Move-construct a new timer implementation.
  80. void move_construct(implementation_type& impl,
  81. implementation_type& other_impl)
  82. {
  83. service_impl_.move_construct(impl, other_impl);
  84. }
  85. /// Move-assign from another timer implementation.
  86. void move_assign(implementation_type& impl,
  87. waitable_timer_service& other_service,
  88. implementation_type& other_impl)
  89. {
  90. service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
  91. }
  92. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  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. /// Cancels one asynchronous wait operation associated with the timer.
  99. std::size_t cancel_one(implementation_type& impl,
  100. boost::system::error_code& ec)
  101. {
  102. return service_impl_.cancel_one(impl, ec);
  103. }
  104. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  105. /// (Deprecated: Use expiry().) Get the expiry time for the timer as an
  106. /// absolute time.
  107. time_point expires_at(const implementation_type& impl) const
  108. {
  109. return service_impl_.expiry(impl);
  110. }
  111. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  112. /// Get the expiry time for the timer as an absolute time.
  113. time_point expiry(const implementation_type& impl) const
  114. {
  115. return service_impl_.expiry(impl);
  116. }
  117. /// Set the expiry time for the timer as an absolute time.
  118. std::size_t expires_at(implementation_type& impl,
  119. const time_point& expiry_time, boost::system::error_code& ec)
  120. {
  121. return service_impl_.expires_at(impl, expiry_time, ec);
  122. }
  123. /// Set the expiry time for the timer relative to now.
  124. std::size_t expires_after(implementation_type& impl,
  125. const duration& expiry_time, boost::system::error_code& ec)
  126. {
  127. return service_impl_.expires_after(impl, expiry_time, ec);
  128. }
  129. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  130. /// (Deprecated: Use expiry().) Get the expiry time for the timer relative to
  131. /// now.
  132. duration expires_from_now(const implementation_type& impl) const
  133. {
  134. typedef detail::chrono_time_traits<Clock, WaitTraits> traits;
  135. return traits::subtract(service_impl_.expiry(impl), traits::now());
  136. }
  137. /// (Deprecated: Use expires_after().) Set the expiry time for the timer
  138. /// relative to now.
  139. std::size_t expires_from_now(implementation_type& impl,
  140. const duration& expiry_time, boost::system::error_code& ec)
  141. {
  142. return service_impl_.expires_after(impl, expiry_time, ec);
  143. }
  144. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  145. // Perform a blocking wait on the timer.
  146. void wait(implementation_type& impl, boost::system::error_code& ec)
  147. {
  148. service_impl_.wait(impl, ec);
  149. }
  150. // Start an asynchronous wait on the timer.
  151. template <typename WaitHandler>
  152. BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
  153. void (boost::system::error_code))
  154. async_wait(implementation_type& impl,
  155. BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
  156. {
  157. async_completion<WaitHandler,
  158. void (boost::system::error_code)> init(handler);
  159. service_impl_.async_wait(impl, init.completion_handler);
  160. return init.result.get();
  161. }
  162. private:
  163. // Destroy all user-defined handler objects owned by the service.
  164. void shutdown()
  165. {
  166. service_impl_.shutdown();
  167. }
  168. // The platform-specific implementation.
  169. service_impl_type service_impl_;
  170. };
  171. } // namespace asio
  172. } // namespace boost
  173. #include <boost/asio/detail/pop_options.hpp>
  174. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  175. #endif // BOOST_ASIO_WAITABLE_TIMER_SERVICE_HPP