basic_deadline_timer.hpp 24 KB

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