deadline_timer_service.hpp 4.0 KB

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