Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

waitable_timer_service.hpp 5.8KB

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