basic_deadline_timer.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 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. typename constraint<
  168. is_convertible<ExecutionContext&, execution_context&>::value
  169. >::type = 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. typename constraint<
  204. is_convertible<ExecutionContext&, execution_context&>::value
  205. >::type = 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. typename constraint<
  246. is_convertible<ExecutionContext&, execution_context&>::value
  247. >::type = 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. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  256. /// Move-construct a basic_deadline_timer from another.
  257. /**
  258. * This constructor moves a timer from one object to another.
  259. *
  260. * @param other The other basic_deadline_timer object from which the move will
  261. * occur.
  262. *
  263. * @note Following the move, the moved-from object is in the same state as if
  264. * constructed using the @c basic_deadline_timer(const executor_type&)
  265. * constructor.
  266. */
  267. basic_deadline_timer(basic_deadline_timer&& other)
  268. : impl_(std::move(other.impl_))
  269. {
  270. }
  271. /// Move-assign a basic_deadline_timer from another.
  272. /**
  273. * This assignment operator moves a timer from one object to another. Cancels
  274. * any outstanding asynchronous operations associated with the target object.
  275. *
  276. * @param other The other basic_deadline_timer object from which the move will
  277. * occur.
  278. *
  279. * @note Following the move, the moved-from object is in the same state as if
  280. * constructed using the @c basic_deadline_timer(const executor_type&)
  281. * constructor.
  282. */
  283. basic_deadline_timer& operator=(basic_deadline_timer&& other)
  284. {
  285. impl_ = std::move(other.impl_);
  286. return *this;
  287. }
  288. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  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() BOOST_ASIO_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::post().
  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 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  618. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_PREFIX(WaitToken,
  619. void (boost::system::error_code))
  620. async_wait(
  621. BOOST_ASIO_MOVE_ARG(WaitToken) token
  622. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  623. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE_SUFFIX((
  624. async_initiate<WaitToken, void (boost::system::error_code)>(
  625. declval<initiate_async_wait>(), token)))
  626. {
  627. return async_initiate<WaitToken, void (boost::system::error_code)>(
  628. initiate_async_wait(this), token);
  629. }
  630. private:
  631. // Disallow copying and assignment.
  632. basic_deadline_timer(const basic_deadline_timer&) BOOST_ASIO_DELETED;
  633. basic_deadline_timer& operator=(
  634. const basic_deadline_timer&) BOOST_ASIO_DELETED;
  635. class initiate_async_wait
  636. {
  637. public:
  638. typedef Executor executor_type;
  639. explicit initiate_async_wait(basic_deadline_timer* self)
  640. : self_(self)
  641. {
  642. }
  643. const executor_type& get_executor() const BOOST_ASIO_NOEXCEPT
  644. {
  645. return self_->get_executor();
  646. }
  647. template <typename WaitHandler>
  648. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler) const
  649. {
  650. // If you get an error on the following line it means that your handler
  651. // does not meet the documented type requirements for a WaitHandler.
  652. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  653. detail::non_const_lvalue<WaitHandler> handler2(handler);
  654. self_->impl_.get_service().async_wait(
  655. self_->impl_.get_implementation(),
  656. handler2.value, self_->impl_.get_executor());
  657. }
  658. private:
  659. basic_deadline_timer* self_;
  660. };
  661. detail::io_object_impl<
  662. detail::deadline_timer_service<TimeTraits>, Executor> impl_;
  663. };
  664. } // namespace asio
  665. } // namespace boost
  666. #include <boost/asio/detail/pop_options.hpp>
  667. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  668. // || defined(GENERATING_DOCUMENTATION)
  669. #endif // BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP