basic_deadline_timer.hpp 24 KB

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