basic_deadline_timer.hpp 24 KB

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