deadline_timer_service.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // deadline_timer_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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 ASIO_DEADLINE_TIMER_SERVICE_HPP
  11. #define ASIO_DEADLINE_TIMER_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <cstddef>
  18. #include <boost/config.hpp>
  19. #include "asio/detail/pop_options.hpp"
  20. #include "asio/io_service.hpp"
  21. #include "asio/time_traits.hpp"
  22. #include "asio/detail/deadline_timer_service.hpp"
  23. #include "asio/detail/service_base.hpp"
  24. namespace asio {
  25. /// Default service implementation for a timer.
  26. template <typename TimeType,
  27. typename TimeTraits = asio::time_traits<TimeType> >
  28. class deadline_timer_service
  29. #if defined(GENERATING_DOCUMENTATION)
  30. : public asio::io_service::service
  31. #else
  32. : public asio::detail::service_base<
  33. deadline_timer_service<TimeType, TimeTraits> >
  34. #endif
  35. {
  36. public:
  37. #if defined(GENERATING_DOCUMENTATION)
  38. /// The unique service identifier.
  39. static asio::io_service::id id;
  40. #endif
  41. /// The time traits type.
  42. typedef TimeTraits traits_type;
  43. /// The time type.
  44. typedef typename traits_type::time_type time_type;
  45. /// The duration type.
  46. typedef typename traits_type::duration_type duration_type;
  47. private:
  48. // The type of the platform-specific implementation.
  49. typedef detail::deadline_timer_service<traits_type> service_impl_type;
  50. public:
  51. /// The implementation type of the deadline timer.
  52. #if defined(GENERATING_DOCUMENTATION)
  53. typedef implementation_defined implementation_type;
  54. #else
  55. typedef typename service_impl_type::implementation_type implementation_type;
  56. #endif
  57. /// Construct a new timer service for the specified io_service.
  58. explicit deadline_timer_service(asio::io_service& io_service)
  59. : asio::detail::service_base<
  60. deadline_timer_service<TimeType, TimeTraits> >(io_service),
  61. service_impl_(io_service)
  62. {
  63. }
  64. /// Destroy all user-defined handler objects owned by the service.
  65. void shutdown_service()
  66. {
  67. service_impl_.shutdown_service();
  68. }
  69. /// Construct a new timer implementation.
  70. void construct(implementation_type& impl)
  71. {
  72. service_impl_.construct(impl);
  73. }
  74. /// Destroy a timer implementation.
  75. void destroy(implementation_type& impl)
  76. {
  77. service_impl_.destroy(impl);
  78. }
  79. /// Cancel any asynchronous wait operations associated with the timer.
  80. std::size_t cancel(implementation_type& impl, asio::error_code& ec)
  81. {
  82. return service_impl_.cancel(impl, ec);
  83. }
  84. /// Get the expiry time for the timer as an absolute time.
  85. time_type expires_at(const implementation_type& impl) const
  86. {
  87. return service_impl_.expires_at(impl);
  88. }
  89. /// Set the expiry time for the timer as an absolute time.
  90. std::size_t expires_at(implementation_type& impl,
  91. const time_type& expiry_time, asio::error_code& ec)
  92. {
  93. return service_impl_.expires_at(impl, expiry_time, ec);
  94. }
  95. /// Get the expiry time for the timer relative to now.
  96. duration_type expires_from_now(const implementation_type& impl) const
  97. {
  98. return service_impl_.expires_from_now(impl);
  99. }
  100. /// Set the expiry time for the timer relative to now.
  101. std::size_t expires_from_now(implementation_type& impl,
  102. const duration_type& expiry_time, asio::error_code& ec)
  103. {
  104. return service_impl_.expires_from_now(impl, expiry_time, ec);
  105. }
  106. // Perform a blocking wait on the timer.
  107. void wait(implementation_type& impl, asio::error_code& ec)
  108. {
  109. service_impl_.wait(impl, ec);
  110. }
  111. // Start an asynchronous wait on the timer.
  112. template <typename WaitHandler>
  113. void async_wait(implementation_type& impl, WaitHandler handler)
  114. {
  115. service_impl_.async_wait(impl, handler);
  116. }
  117. private:
  118. // The platform-specific implementation.
  119. service_impl_type service_impl_;
  120. };
  121. } // namespace asio
  122. #include "asio/detail/pop_options.hpp"
  123. #endif // ASIO_DEADLINE_TIMER_SERVICE_HPP