basic_deadline_timer.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. //
  2. // basic_deadline_timer.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef 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/detail/deadline_timer_service.hpp>
  20. #include <boost/asio/detail/handler_type_requirements.hpp>
  21. #include <boost/asio/detail/io_object_impl.hpp>
  22. #include <boost/asio/detail/non_const_lvalue.hpp>
  23. #include <boost/asio/detail/throw_error.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/execution_context.hpp>
  26. #include <boost/asio/executor.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 = executor>
  122. class basic_deadline_timer
  123. {
  124. public:
  125. /// The type of the executor associated with the object.
  126. typedef Executor executor_type;
  127. /// Rebinds the timer type to another executor.
  128. template <typename Executor1>
  129. struct rebind_executor
  130. {
  131. /// The timer type when rebound to the specified executor.
  132. typedef basic_deadline_timer<Time, TimeTraits, Executor1> other;
  133. };
  134. /// The time traits type.
  135. typedef TimeTraits traits_type;
  136. /// The time type.
  137. typedef typename traits_type::time_type time_type;
  138. /// The duration type.
  139. typedef typename traits_type::duration_type duration_type;
  140. /// Constructor.
  141. /**
  142. * This constructor creates a timer without setting an expiry time. The
  143. * expires_at() or expires_from_now() functions must be called to set an
  144. * expiry time before the timer can be waited on.
  145. *
  146. * @param ex The I/O executor that the timer will use, by default, to
  147. * dispatch handlers for any asynchronous operations performed on the timer.
  148. */
  149. explicit basic_deadline_timer(const executor_type& ex)
  150. : impl_(ex)
  151. {
  152. }
  153. /// Constructor.
  154. /**
  155. * This constructor creates a timer without setting an expiry time. The
  156. * expires_at() or expires_from_now() functions must be called to set an
  157. * expiry time before the timer can be waited on.
  158. *
  159. * @param context An execution context which provides the I/O executor that
  160. * the timer will use, by default, to dispatch handlers for any asynchronous
  161. * operations performed on the timer.
  162. */
  163. template <typename ExecutionContext>
  164. explicit basic_deadline_timer(ExecutionContext& context,
  165. typename enable_if<
  166. is_convertible<ExecutionContext&, execution_context&>::value
  167. >::type* = 0)
  168. : impl_(context)
  169. {
  170. }
  171. /// Constructor to set a particular expiry time as an absolute time.
  172. /**
  173. * This constructor creates a timer and sets the expiry time.
  174. *
  175. * @param ex The I/O executor that the timer will use, by default, to
  176. * dispatch handlers for any asynchronous operations performed on the timer.
  177. *
  178. * @param expiry_time The expiry time to be used for the timer, expressed
  179. * as an absolute time.
  180. */
  181. basic_deadline_timer(const executor_type& ex, const time_type& expiry_time)
  182. : impl_(ex)
  183. {
  184. boost::system::error_code ec;
  185. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  186. boost::asio::detail::throw_error(ec, "expires_at");
  187. }
  188. /// Constructor to set a particular expiry time as an absolute time.
  189. /**
  190. * This constructor creates a timer and sets the expiry time.
  191. *
  192. * @param context An execution context which provides the I/O executor that
  193. * the timer will use, by default, to dispatch handlers for any asynchronous
  194. * operations performed on the timer.
  195. *
  196. * @param expiry_time The expiry time to be used for the timer, expressed
  197. * as an absolute time.
  198. */
  199. template <typename ExecutionContext>
  200. basic_deadline_timer(ExecutionContext& context, const time_type& expiry_time,
  201. typename enable_if<
  202. is_convertible<ExecutionContext&, execution_context&>::value
  203. >::type* = 0)
  204. : impl_(context)
  205. {
  206. boost::system::error_code ec;
  207. impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
  208. boost::asio::detail::throw_error(ec, "expires_at");
  209. }
  210. /// Constructor to set a particular expiry time relative to now.
  211. /**
  212. * This constructor creates a timer and sets the expiry time.
  213. *
  214. * @param ex The I/O executor that the timer will use, by default, to
  215. * dispatch handlers for any asynchronous operations performed on the timer.
  216. *
  217. * @param expiry_time The expiry time to be used for the timer, relative to
  218. * now.
  219. */
  220. basic_deadline_timer(const executor_type& ex,
  221. const duration_type& expiry_time)
  222. : impl_(ex)
  223. {
  224. boost::system::error_code ec;
  225. impl_.get_service().expires_from_now(
  226. impl_.get_implementation(), expiry_time, ec);
  227. boost::asio::detail::throw_error(ec, "expires_from_now");
  228. }
  229. /// Constructor to set a particular expiry time relative to now.
  230. /**
  231. * This constructor creates a timer and sets the expiry time.
  232. *
  233. * @param context An execution context which provides the I/O executor that
  234. * the timer will use, by default, to dispatch handlers for any asynchronous
  235. * operations performed on the timer.
  236. *
  237. * @param expiry_time The expiry time to be used for the timer, relative to
  238. * now.
  239. */
  240. template <typename ExecutionContext>
  241. basic_deadline_timer(ExecutionContext& context,
  242. const duration_type& expiry_time,
  243. typename enable_if<
  244. is_convertible<ExecutionContext&, execution_context&>::value
  245. >::type* = 0)
  246. : impl_(context)
  247. {
  248. boost::system::error_code ec;
  249. impl_.get_service().expires_from_now(
  250. impl_.get_implementation(), expiry_time, ec);
  251. boost::asio::detail::throw_error(ec, "expires_from_now");
  252. }
  253. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  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. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  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. executor_type get_executor() BOOST_ASIO_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 always returns immediately.
  577. *
  578. * For each call to async_wait(), the supplied handler will be called exactly
  579. * once. The 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 boost::asio::error::operation_aborted.
  585. *
  586. * @param handler The handler to be called when the timer expires. Copies
  587. * will be made of the handler as required. The function signature of the
  588. * handler must be:
  589. * @code void handler(
  590. * const boost::system::error_code& error // Result of operation.
  591. * ); @endcode
  592. * Regardless of whether the asynchronous operation completes immediately or
  593. * not, the handler will not be invoked from within this function. On
  594. * immediate completion, invocation of the handler will be performed in a
  595. * manner equivalent to using boost::asio::post().
  596. */
  597. template <
  598. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  599. WaitHandler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  600. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitHandler,
  601. void (boost::system::error_code))
  602. async_wait(
  603. BOOST_ASIO_MOVE_ARG(WaitHandler) handler
  604. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  605. {
  606. return async_initiate<WaitHandler, void (boost::system::error_code)>(
  607. initiate_async_wait(this), handler);
  608. }
  609. private:
  610. // Disallow copying and assignment.
  611. basic_deadline_timer(const basic_deadline_timer&) BOOST_ASIO_DELETED;
  612. basic_deadline_timer& operator=(
  613. const basic_deadline_timer&) BOOST_ASIO_DELETED;
  614. class initiate_async_wait
  615. {
  616. public:
  617. typedef Executor executor_type;
  618. explicit initiate_async_wait(basic_deadline_timer* self)
  619. : self_(self)
  620. {
  621. }
  622. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  623. {
  624. return self_->get_executor();
  625. }
  626. template <typename WaitHandler>
  627. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler) const
  628. {
  629. // If you get an error on the following line it means that your handler
  630. // does not meet the documented type requirements for a WaitHandler.
  631. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  632. detail::non_const_lvalue<WaitHandler> handler2(handler);
  633. self_->impl_.get_service().async_wait(
  634. self_->impl_.get_implementation(), handler2.value,
  635. self_->impl_.get_implementation_executor());
  636. }
  637. private:
  638. basic_deadline_timer* self_;
  639. };
  640. detail::io_object_impl<
  641. detail::deadline_timer_service<TimeTraits>, Executor> impl_;
  642. };
  643. } // namespace asio
  644. } // namespace boost
  645. #include <boost/asio/detail/pop_options.hpp>
  646. #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  647. // || defined(GENERATING_DOCUMENTATION)
  648. #endif // BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP