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.

737 lines
25KB

  1. //
  2. // basic_waitable_timer.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_BASIC_WAITABLE_TIMER_HPP
  11. #define ASIO_BASIC_WAITABLE_TIMER_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/chrono_time_traits.hpp"
  18. #include "asio/detail/deadline_timer_service.hpp"
  19. #include "asio/detail/handler_type_requirements.hpp"
  20. #include "asio/detail/io_object_impl.hpp"
  21. #include "asio/detail/non_const_lvalue.hpp"
  22. #include "asio/detail/throw_error.hpp"
  23. #include "asio/error.hpp"
  24. #include "asio/executor.hpp"
  25. #include "asio/wait_traits.hpp"
  26. #if defined(ASIO_HAS_MOVE)
  27. # include <utility>
  28. #endif // defined(ASIO_HAS_MOVE)
  29. #include "asio/detail/push_options.hpp"
  30. namespace asio {
  31. #if !defined(ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
  32. #define ASIO_BASIC_WAITABLE_TIMER_FWD_DECL
  33. // Forward declaration with defaulted arguments.
  34. template <typename Clock,
  35. typename WaitTraits = asio::wait_traits<Clock>,
  36. typename Executor = executor>
  37. class basic_waitable_timer;
  38. #endif // !defined(ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
  39. /// Provides waitable timer functionality.
  40. /**
  41. * The basic_waitable_timer class template provides the ability to perform a
  42. * blocking or asynchronous wait for a timer to expire.
  43. *
  44. * A waitable timer is always in one of two states: "expired" or "not expired".
  45. * If the wait() or async_wait() function is called on an expired timer, the
  46. * wait operation will complete immediately.
  47. *
  48. * Most applications will use one of the asio::steady_timer,
  49. * asio::system_timer or asio::high_resolution_timer typedefs.
  50. *
  51. * @note This waitable timer functionality is for use with the C++11 standard
  52. * library's @c &lt;chrono&gt; facility, or with the Boost.Chrono library.
  53. *
  54. * @par Thread Safety
  55. * @e Distinct @e objects: Safe.@n
  56. * @e Shared @e objects: Unsafe.
  57. *
  58. * @par Examples
  59. * Performing a blocking wait (C++11):
  60. * @code
  61. * // Construct a timer without setting an expiry time.
  62. * asio::steady_timer timer(my_context);
  63. *
  64. * // Set an expiry time relative to now.
  65. * timer.expires_after(std::chrono::seconds(5));
  66. *
  67. * // Wait for the timer to expire.
  68. * timer.wait();
  69. * @endcode
  70. *
  71. * @par
  72. * Performing an asynchronous wait (C++11):
  73. * @code
  74. * void handler(const asio::error_code& error)
  75. * {
  76. * if (!error)
  77. * {
  78. * // Timer expired.
  79. * }
  80. * }
  81. *
  82. * ...
  83. *
  84. * // Construct a timer with an absolute expiry time.
  85. * asio::steady_timer timer(my_context,
  86. * std::chrono::steady_clock::now() + std::chrono::seconds(60));
  87. *
  88. * // Start an asynchronous wait.
  89. * timer.async_wait(handler);
  90. * @endcode
  91. *
  92. * @par Changing an active waitable timer's expiry time
  93. *
  94. * Changing the expiry time of a timer while there are pending asynchronous
  95. * waits causes those wait operations to be cancelled. To ensure that the action
  96. * associated with the timer is performed only once, use something like this:
  97. * used:
  98. *
  99. * @code
  100. * void on_some_event()
  101. * {
  102. * if (my_timer.expires_after(seconds(5)) > 0)
  103. * {
  104. * // We managed to cancel the timer. Start new asynchronous wait.
  105. * my_timer.async_wait(on_timeout);
  106. * }
  107. * else
  108. * {
  109. * // Too late, timer has already expired!
  110. * }
  111. * }
  112. *
  113. * void on_timeout(const asio::error_code& e)
  114. * {
  115. * if (e != asio::error::operation_aborted)
  116. * {
  117. * // Timer was not cancelled, take necessary action.
  118. * }
  119. * }
  120. * @endcode
  121. *
  122. * @li The asio::basic_waitable_timer::expires_after() function
  123. * cancels any pending asynchronous waits, and returns the number of
  124. * asynchronous waits that were cancelled. If it returns 0 then you were too
  125. * late and the wait handler has already been executed, or will soon be
  126. * executed. If it returns 1 then the wait handler was successfully cancelled.
  127. *
  128. * @li If a wait handler is cancelled, the asio::error_code passed to
  129. * it contains the value asio::error::operation_aborted.
  130. */
  131. template <typename Clock, typename WaitTraits, typename Executor>
  132. class basic_waitable_timer
  133. {
  134. public:
  135. /// The type of the executor associated with the object.
  136. typedef Executor executor_type;
  137. /// The clock type.
  138. typedef Clock clock_type;
  139. /// The duration type of the clock.
  140. typedef typename clock_type::duration duration;
  141. /// The time point type of the clock.
  142. typedef typename clock_type::time_point time_point;
  143. /// The wait traits type.
  144. typedef WaitTraits traits_type;
  145. /// Constructor.
  146. /**
  147. * This constructor creates a timer without setting an expiry time. The
  148. * expires_at() or expires_after() functions must be called to set an expiry
  149. * time before the timer can be waited on.
  150. *
  151. * @param ex The I/O executor that the timer will use, by default, to
  152. * dispatch handlers for any asynchronous operations performed on the timer.
  153. */
  154. explicit basic_waitable_timer(const executor_type& ex)
  155. : impl_(ex)
  156. {
  157. }
  158. /// Constructor.
  159. /**
  160. * This constructor creates a timer without setting an expiry time. The
  161. * expires_at() or expires_after() functions must be called to set an expiry
  162. * time before the timer can be waited on.
  163. *
  164. * @param context An execution context which provides the I/O executor that
  165. * the timer will use, by default, to dispatch handlers for any asynchronous
  166. * operations performed on the timer.
  167. */
  168. template <typename ExecutionContext>
  169. explicit basic_waitable_timer(ExecutionContext& context,
  170. typename enable_if<
  171. is_convertible<ExecutionContext&, execution_context&>::value
  172. >::type* = 0)
  173. : impl_(context)
  174. {
  175. }
  176. /// Constructor to set a particular expiry time as an absolute time.
  177. /**
  178. * This constructor creates a timer and sets the expiry time.
  179. *
  180. * @param ex The I/O executor object that the timer will use, by default, to
  181. * dispatch handlers for any asynchronous operations performed on the timer.
  182. *
  183. * @param expiry_time The expiry time to be used for the timer, expressed
  184. * as an absolute time.
  185. */
  186. basic_waitable_timer(const executor_type& ex, const time_point& expiry_time)
  187. : impl_(ex)
  188. {
  189. asio::error_code ec;
  190. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  191. asio::detail::throw_error(ec, "expires_at");
  192. }
  193. /// Constructor to set a particular expiry time as an absolute time.
  194. /**
  195. * This constructor creates a timer and sets the expiry time.
  196. *
  197. * @param context An execution context which provides the I/O executor that
  198. * the timer will use, by default, to dispatch handlers for any asynchronous
  199. * operations performed on the timer.
  200. *
  201. * @param expiry_time The expiry time to be used for the timer, expressed
  202. * as an absolute time.
  203. */
  204. template <typename ExecutionContext>
  205. explicit basic_waitable_timer(ExecutionContext& context,
  206. const time_point& expiry_time,
  207. typename enable_if<
  208. is_convertible<ExecutionContext&, execution_context&>::value
  209. >::type* = 0)
  210. : impl_(context)
  211. {
  212. asio::error_code ec;
  213. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  214. asio::detail::throw_error(ec, "expires_at");
  215. }
  216. /// Constructor to set a particular expiry time relative to now.
  217. /**
  218. * This constructor creates a timer and sets the expiry time.
  219. *
  220. * @param ex The I/O executor that the timer will use, by default, to
  221. * dispatch handlers for any asynchronous operations performed on the timer.
  222. *
  223. * @param expiry_time The expiry time to be used for the timer, relative to
  224. * now.
  225. */
  226. basic_waitable_timer(const executor_type& ex, const duration& expiry_time)
  227. : impl_(ex)
  228. {
  229. asio::error_code ec;
  230. impl_.get_service().expires_after(
  231. impl_.get_implementation(), expiry_time, ec);
  232. asio::detail::throw_error(ec, "expires_after");
  233. }
  234. /// Constructor to set a particular expiry time relative to now.
  235. /**
  236. * This constructor creates a timer and sets the expiry time.
  237. *
  238. * @param context An execution context which provides the I/O executor that
  239. * the timer will use, by default, to dispatch handlers for any asynchronous
  240. * operations performed on the timer.
  241. *
  242. * @param expiry_time The expiry time to be used for the timer, relative to
  243. * now.
  244. */
  245. template <typename ExecutionContext>
  246. explicit basic_waitable_timer(ExecutionContext& context,
  247. const duration& expiry_time,
  248. typename enable_if<
  249. is_convertible<ExecutionContext&, execution_context&>::value
  250. >::type* = 0)
  251. : impl_(context)
  252. {
  253. asio::error_code ec;
  254. impl_.get_service().expires_after(
  255. impl_.get_implementation(), expiry_time, ec);
  256. asio::detail::throw_error(ec, "expires_after");
  257. }
  258. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  259. /// Move-construct a basic_waitable_timer from another.
  260. /**
  261. * This constructor moves a timer from one object to another.
  262. *
  263. * @param other The other basic_waitable_timer object from which the move will
  264. * occur.
  265. *
  266. * @note Following the move, the moved-from object is in the same state as if
  267. * constructed using the @c basic_waitable_timer(const executor_type&)
  268. * constructor.
  269. */
  270. basic_waitable_timer(basic_waitable_timer&& other)
  271. : impl_(std::move(other.impl_))
  272. {
  273. }
  274. /// Move-assign a basic_waitable_timer from another.
  275. /**
  276. * This assignment operator moves a timer from one object to another. Cancels
  277. * any outstanding asynchronous operations associated with the target object.
  278. *
  279. * @param other The other basic_waitable_timer object from which the move will
  280. * occur.
  281. *
  282. * @note Following the move, the moved-from object is in the same state as if
  283. * constructed using the @c basic_waitable_timer(const executor_type&)
  284. * constructor.
  285. */
  286. basic_waitable_timer& operator=(basic_waitable_timer&& other)
  287. {
  288. impl_ = std::move(other.impl_);
  289. return *this;
  290. }
  291. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  292. /// Destroys the timer.
  293. /**
  294. * This function destroys the timer, cancelling any outstanding asynchronous
  295. * wait operations associated with the timer as if by calling @c cancel.
  296. */
  297. ~basic_waitable_timer()
  298. {
  299. }
  300. /// Get the executor associated with the object.
  301. executor_type get_executor() ASIO_NOEXCEPT
  302. {
  303. return impl_.get_executor();
  304. }
  305. /// Cancel any asynchronous operations that are waiting on the timer.
  306. /**
  307. * This function forces the completion of any pending asynchronous wait
  308. * operations against the timer. The handler for each cancelled operation will
  309. * be invoked with the asio::error::operation_aborted error code.
  310. *
  311. * Cancelling the timer does not change the expiry time.
  312. *
  313. * @return The number of asynchronous operations that were cancelled.
  314. *
  315. * @throws asio::system_error Thrown on failure.
  316. *
  317. * @note If the timer has already expired when cancel() is called, then the
  318. * handlers for asynchronous wait operations will:
  319. *
  320. * @li have already been invoked; or
  321. *
  322. * @li have been queued for invocation in the near future.
  323. *
  324. * These handlers can no longer be cancelled, and therefore are passed an
  325. * error code that indicates the successful completion of the wait operation.
  326. */
  327. std::size_t cancel()
  328. {
  329. asio::error_code ec;
  330. std::size_t s = impl_.get_service().cancel(impl_.get_implementation(), ec);
  331. asio::detail::throw_error(ec, "cancel");
  332. return s;
  333. }
  334. #if !defined(ASIO_NO_DEPRECATED)
  335. /// (Deprecated: Use non-error_code overload.) Cancel any asynchronous
  336. /// operations that are waiting on the timer.
  337. /**
  338. * This function forces the completion of any pending asynchronous wait
  339. * operations against the timer. The handler for each cancelled operation will
  340. * be invoked with the asio::error::operation_aborted error code.
  341. *
  342. * Cancelling the timer does not change the expiry time.
  343. *
  344. * @param ec Set to indicate what error occurred, if any.
  345. *
  346. * @return The number of asynchronous operations that were cancelled.
  347. *
  348. * @note If the timer has already expired when cancel() is called, then the
  349. * handlers for asynchronous wait operations will:
  350. *
  351. * @li have already been invoked; or
  352. *
  353. * @li have been queued for invocation in the near future.
  354. *
  355. * These handlers can no longer be cancelled, and therefore are passed an
  356. * error code that indicates the successful completion of the wait operation.
  357. */
  358. std::size_t cancel(asio::error_code& ec)
  359. {
  360. return impl_.get_service().cancel(impl_.get_implementation(), ec);
  361. }
  362. #endif // !defined(ASIO_NO_DEPRECATED)
  363. /// Cancels one asynchronous operation that is waiting on the timer.
  364. /**
  365. * This function forces the completion of one pending asynchronous wait
  366. * operation against the timer. Handlers are cancelled in FIFO order. The
  367. * handler for the cancelled operation will be invoked with the
  368. * asio::error::operation_aborted error code.
  369. *
  370. * Cancelling the timer does not change the expiry time.
  371. *
  372. * @return The number of asynchronous operations that were cancelled. That is,
  373. * either 0 or 1.
  374. *
  375. * @throws asio::system_error Thrown on failure.
  376. *
  377. * @note If the timer has already expired when cancel_one() is called, then
  378. * the handlers for asynchronous wait operations will:
  379. *
  380. * @li have already been invoked; or
  381. *
  382. * @li have been queued for invocation in the near future.
  383. *
  384. * These handlers can no longer be cancelled, and therefore are passed an
  385. * error code that indicates the successful completion of the wait operation.
  386. */
  387. std::size_t cancel_one()
  388. {
  389. asio::error_code ec;
  390. std::size_t s = impl_.get_service().cancel_one(
  391. impl_.get_implementation(), ec);
  392. asio::detail::throw_error(ec, "cancel_one");
  393. return s;
  394. }
  395. #if !defined(ASIO_NO_DEPRECATED)
  396. /// (Deprecated: Use non-error_code overload.) Cancels one asynchronous
  397. /// operation that is waiting on the timer.
  398. /**
  399. * This function forces the completion of one pending asynchronous wait
  400. * operation against the timer. Handlers are cancelled in FIFO order. The
  401. * handler for the cancelled operation will be invoked with the
  402. * asio::error::operation_aborted error code.
  403. *
  404. * Cancelling the timer does not change the expiry time.
  405. *
  406. * @param ec Set to indicate what error occurred, if any.
  407. *
  408. * @return The number of asynchronous operations that were cancelled. That is,
  409. * either 0 or 1.
  410. *
  411. * @note If the timer has already expired when cancel_one() is called, then
  412. * the handlers for asynchronous wait operations will:
  413. *
  414. * @li have already been invoked; or
  415. *
  416. * @li have been queued for invocation in the near future.
  417. *
  418. * These handlers can no longer be cancelled, and therefore are passed an
  419. * error code that indicates the successful completion of the wait operation.
  420. */
  421. std::size_t cancel_one(asio::error_code& ec)
  422. {
  423. return impl_.get_service().cancel_one(impl_.get_implementation(), ec);
  424. }
  425. /// (Deprecated: Use expiry().) Get the timer's expiry time as an absolute
  426. /// time.
  427. /**
  428. * This function may be used to obtain the timer's current expiry time.
  429. * Whether the timer has expired or not does not affect this value.
  430. */
  431. time_point expires_at() const
  432. {
  433. return impl_.get_service().expires_at(impl_.get_implementation());
  434. }
  435. #endif // !defined(ASIO_NO_DEPRECATED)
  436. /// Get the timer's expiry time as an absolute time.
  437. /**
  438. * This function may be used to obtain the timer's current expiry time.
  439. * Whether the timer has expired or not does not affect this value.
  440. */
  441. time_point expiry() const
  442. {
  443. return impl_.get_service().expiry(impl_.get_implementation());
  444. }
  445. /// Set the timer's expiry time as an absolute time.
  446. /**
  447. * This function sets the expiry time. Any pending asynchronous wait
  448. * operations will be cancelled. The handler for each cancelled operation will
  449. * be invoked with the asio::error::operation_aborted error code.
  450. *
  451. * @param expiry_time The expiry time to be used for the timer.
  452. *
  453. * @return The number of asynchronous operations that were cancelled.
  454. *
  455. * @throws asio::system_error Thrown on failure.
  456. *
  457. * @note If the timer has already expired when expires_at() is called, then
  458. * the handlers for asynchronous wait operations will:
  459. *
  460. * @li have already been invoked; or
  461. *
  462. * @li have been queued for invocation in the near future.
  463. *
  464. * These handlers can no longer be cancelled, and therefore are passed an
  465. * error code that indicates the successful completion of the wait operation.
  466. */
  467. std::size_t expires_at(const time_point& expiry_time)
  468. {
  469. asio::error_code ec;
  470. std::size_t s = impl_.get_service().expires_at(
  471. impl_.get_implementation(), expiry_time, ec);
  472. asio::detail::throw_error(ec, "expires_at");
  473. return s;
  474. }
  475. #if !defined(ASIO_NO_DEPRECATED)
  476. /// (Deprecated: Use non-error_code overload.) Set the timer's expiry time as
  477. /// an absolute time.
  478. /**
  479. * This function sets the expiry time. Any pending asynchronous wait
  480. * operations will be cancelled. The handler for each cancelled operation will
  481. * be invoked with the asio::error::operation_aborted error code.
  482. *
  483. * @param expiry_time The expiry time to be used for the timer.
  484. *
  485. * @param ec Set to indicate what error occurred, if any.
  486. *
  487. * @return The number of asynchronous operations that were cancelled.
  488. *
  489. * @note If the timer has already expired when expires_at() is called, then
  490. * the handlers for asynchronous wait operations will:
  491. *
  492. * @li have already been invoked; or
  493. *
  494. * @li have been queued for invocation in the near future.
  495. *
  496. * These handlers can no longer be cancelled, and therefore are passed an
  497. * error code that indicates the successful completion of the wait operation.
  498. */
  499. std::size_t expires_at(const time_point& expiry_time,
  500. asio::error_code& ec)
  501. {
  502. return impl_.get_service().expires_at(
  503. impl_.get_implementation(), expiry_time, ec);
  504. }
  505. #endif // !defined(ASIO_NO_DEPRECATED)
  506. /// Set the timer's expiry time relative to now.
  507. /**
  508. * This function sets the expiry time. Any pending asynchronous wait
  509. * operations will be cancelled. The handler for each cancelled operation will
  510. * be invoked with the asio::error::operation_aborted error code.
  511. *
  512. * @param expiry_time The expiry time to be used for the timer.
  513. *
  514. * @return The number of asynchronous operations that were cancelled.
  515. *
  516. * @throws asio::system_error Thrown on failure.
  517. *
  518. * @note If the timer has already expired when expires_after() is called,
  519. * then the handlers for asynchronous wait operations will:
  520. *
  521. * @li have already been invoked; or
  522. *
  523. * @li have been queued for invocation in the near future.
  524. *
  525. * These handlers can no longer be cancelled, and therefore are passed an
  526. * error code that indicates the successful completion of the wait operation.
  527. */
  528. std::size_t expires_after(const duration& expiry_time)
  529. {
  530. asio::error_code ec;
  531. std::size_t s = impl_.get_service().expires_after(
  532. impl_.get_implementation(), expiry_time, ec);
  533. asio::detail::throw_error(ec, "expires_after");
  534. return s;
  535. }
  536. #if !defined(ASIO_NO_DEPRECATED)
  537. /// (Deprecated: Use expiry().) Get the timer's expiry time relative to now.
  538. /**
  539. * This function may be used to obtain the timer's current expiry time.
  540. * Whether the timer has expired or not does not affect this value.
  541. */
  542. duration expires_from_now() const
  543. {
  544. return impl_.get_service().expires_from_now(impl_.get_implementation());
  545. }
  546. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  547. /// to now.
  548. /**
  549. * This function sets the expiry time. Any pending asynchronous wait
  550. * operations will be cancelled. The handler for each cancelled operation will
  551. * be invoked with the asio::error::operation_aborted error code.
  552. *
  553. * @param expiry_time The expiry time to be used for the timer.
  554. *
  555. * @return The number of asynchronous operations that were cancelled.
  556. *
  557. * @throws asio::system_error Thrown on failure.
  558. *
  559. * @note If the timer has already expired when expires_from_now() is called,
  560. * then the handlers for asynchronous wait operations will:
  561. *
  562. * @li have already been invoked; or
  563. *
  564. * @li have been queued for invocation in the near future.
  565. *
  566. * These handlers can no longer be cancelled, and therefore are passed an
  567. * error code that indicates the successful completion of the wait operation.
  568. */
  569. std::size_t expires_from_now(const duration& expiry_time)
  570. {
  571. asio::error_code ec;
  572. std::size_t s = impl_.get_service().expires_from_now(
  573. impl_.get_implementation(), expiry_time, ec);
  574. asio::detail::throw_error(ec, "expires_from_now");
  575. return s;
  576. }
  577. /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
  578. /// to now.
  579. /**
  580. * This function sets the expiry time. Any pending asynchronous wait
  581. * operations will be cancelled. The handler for each cancelled operation will
  582. * be invoked with the asio::error::operation_aborted error code.
  583. *
  584. * @param expiry_time The expiry time to be used for the timer.
  585. *
  586. * @param ec Set to indicate what error occurred, if any.
  587. *
  588. * @return The number of asynchronous operations that were cancelled.
  589. *
  590. * @note If the timer has already expired when expires_from_now() is called,
  591. * then the handlers for asynchronous wait operations will:
  592. *
  593. * @li have already been invoked; or
  594. *
  595. * @li have been queued for invocation in the near future.
  596. *
  597. * These handlers can no longer be cancelled, and therefore are passed an
  598. * error code that indicates the successful completion of the wait operation.
  599. */
  600. std::size_t expires_from_now(const duration& expiry_time,
  601. asio::error_code& ec)
  602. {
  603. return impl_.get_service().expires_from_now(
  604. impl_.get_implementation(), expiry_time, ec);
  605. }
  606. #endif // !defined(ASIO_NO_DEPRECATED)
  607. /// Perform a blocking wait on the timer.
  608. /**
  609. * This function is used to wait for the timer to expire. This function
  610. * blocks and does not return until the timer has expired.
  611. *
  612. * @throws asio::system_error Thrown on failure.
  613. */
  614. void wait()
  615. {
  616. asio::error_code ec;
  617. impl_.get_service().wait(impl_.get_implementation(), ec);
  618. asio::detail::throw_error(ec, "wait");
  619. }
  620. /// Perform a blocking wait on the timer.
  621. /**
  622. * This function is used to wait for the timer to expire. This function
  623. * blocks and does not return until the timer has expired.
  624. *
  625. * @param ec Set to indicate what error occurred, if any.
  626. */
  627. void wait(asio::error_code& ec)
  628. {
  629. impl_.get_service().wait(impl_.get_implementation(), ec);
  630. }
  631. /// Start an asynchronous wait on the timer.
  632. /**
  633. * This function may be used to initiate an asynchronous wait against the
  634. * timer. It always returns immediately.
  635. *
  636. * For each call to async_wait(), the supplied handler will be called exactly
  637. * once. The handler will be called when:
  638. *
  639. * @li The timer has expired.
  640. *
  641. * @li The timer was cancelled, in which case the handler is passed the error
  642. * code asio::error::operation_aborted.
  643. *
  644. * @param handler The handler to be called when the timer expires. Copies
  645. * will be made of the handler as required. The function signature of the
  646. * handler must be:
  647. * @code void handler(
  648. * const asio::error_code& error // Result of operation.
  649. * ); @endcode
  650. * Regardless of whether the asynchronous operation completes immediately or
  651. * not, the handler will not be invoked from within this function. On
  652. * immediate completion, invocation of the handler will be performed in a
  653. * manner equivalent to using asio::post().
  654. */
  655. template <typename WaitHandler>
  656. ASIO_INITFN_RESULT_TYPE(WaitHandler,
  657. void (asio::error_code))
  658. async_wait(ASIO_MOVE_ARG(WaitHandler) handler)
  659. {
  660. return async_initiate<WaitHandler, void (asio::error_code)>(
  661. initiate_async_wait(), handler, this);
  662. }
  663. private:
  664. // Disallow copying and assignment.
  665. basic_waitable_timer(const basic_waitable_timer&) ASIO_DELETED;
  666. basic_waitable_timer& operator=(
  667. const basic_waitable_timer&) ASIO_DELETED;
  668. struct initiate_async_wait
  669. {
  670. template <typename WaitHandler>
  671. void operator()(ASIO_MOVE_ARG(WaitHandler) handler,
  672. basic_waitable_timer* self) const
  673. {
  674. // If you get an error on the following line it means that your handler
  675. // does not meet the documented type requirements for a WaitHandler.
  676. ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  677. detail::non_const_lvalue<WaitHandler> handler2(handler);
  678. self->impl_.get_service().async_wait(
  679. self->impl_.get_implementation(), handler2.value,
  680. self->impl_.get_implementation_executor());
  681. }
  682. };
  683. detail::io_object_impl<
  684. detail::deadline_timer_service<
  685. detail::chrono_time_traits<Clock, WaitTraits> >,
  686. executor_type > impl_;
  687. };
  688. } // namespace asio
  689. #include "asio/detail/pop_options.hpp"
  690. #endif // ASIO_BASIC_WAITABLE_TIMER_HPP