123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813 |
- //
- // basic_waitable_timer.hpp
- // ~~~~~~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP
- #define BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/config.hpp>
- #include <cstddef>
- #include <boost/asio/any_io_executor.hpp>
- #include <boost/asio/detail/chrono_time_traits.hpp>
- #include <boost/asio/detail/deadline_timer_service.hpp>
- #include <boost/asio/detail/handler_type_requirements.hpp>
- #include <boost/asio/detail/io_object_impl.hpp>
- #include <boost/asio/detail/non_const_lvalue.hpp>
- #include <boost/asio/detail/throw_error.hpp>
- #include <boost/asio/error.hpp>
- #include <boost/asio/wait_traits.hpp>
- #if defined(BOOST_ASIO_HAS_MOVE)
- # include <utility>
- #endif // defined(BOOST_ASIO_HAS_MOVE)
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- #if !defined(BOOST_ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
- #define BOOST_ASIO_BASIC_WAITABLE_TIMER_FWD_DECL
- // Forward declaration with defaulted arguments.
- template <typename Clock,
- typename WaitTraits = boost::asio::wait_traits<Clock>,
- typename Executor = any_io_executor>
- class basic_waitable_timer;
- #endif // !defined(BOOST_ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
- /// Provides waitable timer functionality.
- /**
- * The basic_waitable_timer class template provides the ability to perform a
- * blocking or asynchronous wait for a timer to expire.
- *
- * A waitable timer is always in one of two states: "expired" or "not expired".
- * If the wait() or async_wait() function is called on an expired timer, the
- * wait operation will complete immediately.
- *
- * Most applications will use one of the boost::asio::steady_timer,
- * boost::asio::system_timer or boost::asio::high_resolution_timer typedefs.
- *
- * @note This waitable timer functionality is for use with the C++11 standard
- * library's @c <chrono> facility, or with the Boost.Chrono library.
- *
- * @par Thread Safety
- * @e Distinct @e objects: Safe.@n
- * @e Shared @e objects: Unsafe.
- *
- * @par Examples
- * Performing a blocking wait (C++11):
- * @code
- * // Construct a timer without setting an expiry time.
- * boost::asio::steady_timer timer(my_context);
- *
- * // Set an expiry time relative to now.
- * timer.expires_after(std::chrono::seconds(5));
- *
- * // Wait for the timer to expire.
- * timer.wait();
- * @endcode
- *
- * @par
- * Performing an asynchronous wait (C++11):
- * @code
- * void handler(const boost::system::error_code& error)
- * {
- * if (!error)
- * {
- * // Timer expired.
- * }
- * }
- *
- * ...
- *
- * // Construct a timer with an absolute expiry time.
- * boost::asio::steady_timer timer(my_context,
- * std::chrono::steady_clock::now() + std::chrono::seconds(60));
- *
- * // Start an asynchronous wait.
- * timer.async_wait(handler);
- * @endcode
- *
- * @par Changing an active waitable timer's expiry time
- *
- * Changing the expiry time of a timer while there are pending asynchronous
- * waits causes those wait operations to be cancelled. To ensure that the action
- * associated with the timer is performed only once, use something like this:
- * used:
- *
- * @code
- * void on_some_event()
- * {
- * if (my_timer.expires_after(seconds(5)) > 0)
- * {
- * // We managed to cancel the timer. Start new asynchronous wait.
- * my_timer.async_wait(on_timeout);
- * }
- * else
- * {
- * // Too late, timer has already expired!
- * }
- * }
- *
- * void on_timeout(const boost::system::error_code& e)
- * {
- * if (e != boost::asio::error::operation_aborted)
- * {
- * // Timer was not cancelled, take necessary action.
- * }
- * }
- * @endcode
- *
- * @li The boost::asio::basic_waitable_timer::expires_after() function
- * cancels any pending asynchronous waits, and returns the number of
- * asynchronous waits that were cancelled. If it returns 0 then you were too
- * late and the wait handler has already been executed, or will soon be
- * executed. If it returns 1 then the wait handler was successfully cancelled.
- *
- * @li If a wait handler is cancelled, the boost::system::error_code passed to
- * it contains the value boost::asio::error::operation_aborted.
- */
- template <typename Clock, typename WaitTraits, typename Executor>
- class basic_waitable_timer
- {
- public:
- /// The type of the executor associated with the object.
- typedef Executor executor_type;
- /// Rebinds the timer type to another executor.
- template <typename Executor1>
- struct rebind_executor
- {
- /// The timer type when rebound to the specified executor.
- typedef basic_waitable_timer<Clock, WaitTraits, Executor1> other;
- };
- /// The clock type.
- typedef Clock clock_type;
- /// The duration type of the clock.
- typedef typename clock_type::duration duration;
- /// The time point type of the clock.
- typedef typename clock_type::time_point time_point;
- /// The wait traits type.
- typedef WaitTraits traits_type;
- /// Constructor.
- /**
- * This constructor creates a timer without setting an expiry time. The
- * expires_at() or expires_after() functions must be called to set an expiry
- * time before the timer can be waited on.
- *
- * @param ex The I/O executor that the timer will use, by default, to
- * dispatch handlers for any asynchronous operations performed on the timer.
- */
- explicit basic_waitable_timer(const executor_type& ex)
- : impl_(0, ex)
- {
- }
- /// Constructor.
- /**
- * This constructor creates a timer without setting an expiry time. The
- * expires_at() or expires_after() functions must be called to set an expiry
- * time before the timer can be waited on.
- *
- * @param context An execution context which provides the I/O executor that
- * the timer will use, by default, to dispatch handlers for any asynchronous
- * operations performed on the timer.
- */
- template <typename ExecutionContext>
- explicit basic_waitable_timer(ExecutionContext& context,
- typename constraint<
- is_convertible<ExecutionContext&, execution_context&>::value
- >::type = 0)
- : impl_(0, 0, context)
- {
- }
- /// Constructor to set a particular expiry time as an absolute time.
- /**
- * This constructor creates a timer and sets the expiry time.
- *
- * @param ex The I/O executor object that the timer will use, by default, to
- * dispatch handlers for any asynchronous operations performed on the timer.
- *
- * @param expiry_time The expiry time to be used for the timer, expressed
- * as an absolute time.
- */
- basic_waitable_timer(const executor_type& ex, const time_point& expiry_time)
- : impl_(0, ex)
- {
- boost::system::error_code ec;
- impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
- boost::asio::detail::throw_error(ec, "expires_at");
- }
- /// Constructor to set a particular expiry time as an absolute time.
- /**
- * This constructor creates a timer and sets the expiry time.
- *
- * @param context An execution context which provides the I/O executor that
- * the timer will use, by default, to dispatch handlers for any asynchronous
- * operations performed on the timer.
- *
- * @param expiry_time The expiry time to be used for the timer, expressed
- * as an absolute time.
- */
- template <typename ExecutionContext>
- explicit basic_waitable_timer(ExecutionContext& context,
- const time_point& expiry_time,
- typename constraint<
- is_convertible<ExecutionContext&, execution_context&>::value
- >::type = 0)
- : impl_(0, 0, context)
- {
- boost::system::error_code ec;
- impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
- boost::asio::detail::throw_error(ec, "expires_at");
- }
- /// Constructor to set a particular expiry time relative to now.
- /**
- * This constructor creates a timer and sets the expiry time.
- *
- * @param ex The I/O executor that the timer will use, by default, to
- * dispatch handlers for any asynchronous operations performed on the timer.
- *
- * @param expiry_time The expiry time to be used for the timer, relative to
- * now.
- */
- basic_waitable_timer(const executor_type& ex, const duration& expiry_time)
- : impl_(0, ex)
- {
- boost::system::error_code ec;
- impl_.get_service().expires_after(
- impl_.get_implementation(), expiry_time, ec);
- boost::asio::detail::throw_error(ec, "expires_after");
- }
- /// Constructor to set a particular expiry time relative to now.
- /**
- * This constructor creates a timer and sets the expiry time.
- *
- * @param context An execution context which provides the I/O executor that
- * the timer will use, by default, to dispatch handlers for any asynchronous
- * operations performed on the timer.
- *
- * @param expiry_time The expiry time to be used for the timer, relative to
- * now.
- */
- template <typename ExecutionContext>
- explicit basic_waitable_timer(ExecutionContext& context,
- const duration& expiry_time,
- typename constraint<
- is_convertible<ExecutionContext&, execution_context&>::value
- >::type = 0)
- : impl_(0, 0, context)
- {
- boost::system::error_code ec;
- impl_.get_service().expires_after(
- impl_.get_implementation(), expiry_time, ec);
- boost::asio::detail::throw_error(ec, "expires_after");
- }
- #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
- /// Move-construct a basic_waitable_timer from another.
- /**
- * This constructor moves a timer from one object to another.
- *
- * @param other The other basic_waitable_timer object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_waitable_timer(const executor_type&)
- * constructor.
- */
- basic_waitable_timer(basic_waitable_timer&& other)
- : impl_(std::move(other.impl_))
- {
- }
- /// Move-assign a basic_waitable_timer from another.
- /**
- * This assignment operator moves a timer from one object to another. Cancels
- * any outstanding asynchronous operations associated with the target object.
- *
- * @param other The other basic_waitable_timer object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_waitable_timer(const executor_type&)
- * constructor.
- */
- basic_waitable_timer& operator=(basic_waitable_timer&& other)
- {
- impl_ = std::move(other.impl_);
- return *this;
- }
- // All timers have access to each other's implementations.
- template <typename Clock1, typename WaitTraits1, typename Executor1>
- friend class basic_waitable_timer;
- /// Move-construct a basic_waitable_timer from another.
- /**
- * This constructor moves a timer from one object to another.
- *
- * @param other The other basic_waitable_timer object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_waitable_timer(const executor_type&)
- * constructor.
- */
- template <typename Executor1>
- basic_waitable_timer(
- basic_waitable_timer<Clock, WaitTraits, Executor1>&& other,
- typename constraint<
- is_convertible<Executor1, Executor>::value
- >::type = 0)
- : impl_(std::move(other.impl_))
- {
- }
- /// Move-assign a basic_waitable_timer from another.
- /**
- * This assignment operator moves a timer from one object to another. Cancels
- * any outstanding asynchronous operations associated with the target object.
- *
- * @param other The other basic_waitable_timer object from which the move will
- * occur.
- *
- * @note Following the move, the moved-from object is in the same state as if
- * constructed using the @c basic_waitable_timer(const executor_type&)
- * constructor.
- */
- template <typename Executor1>
- typename constraint<
- is_convertible<Executor1, Executor>::value,
- basic_waitable_timer&
- >::type operator=(basic_waitable_timer<Clock, WaitTraits, Executor1>&& other)
- {
- basic_waitable_timer tmp(std::move(other));
- impl_ = std::move(tmp.impl_);
- return *this;
- }
- #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
- /// Destroys the timer.
- /**
- * This function destroys the timer, cancelling any outstanding asynchronous
- * wait operations associated with the timer as if by calling @c cancel.
- */
- ~basic_waitable_timer()
- {
- }
- /// Get the executor associated with the object.
- executor_type get_executor() BOOST_ASIO_NOEXCEPT
- {
- return impl_.get_executor();
- }
- /// Cancel any asynchronous operations that are waiting on the timer.
- /**
- * This function forces the completion of any pending asynchronous wait
- * operations against the timer. The handler for each cancelled operation will
- * be invoked with the boost::asio::error::operation_aborted error code.
- *
- * Cancelling the timer does not change the expiry time.
- *
- * @return The number of asynchronous operations that were cancelled.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @note If the timer has already expired when cancel() is called, then the
- * handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t cancel()
- {
- boost::system::error_code ec;
- std::size_t s = impl_.get_service().cancel(impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "cancel");
- return s;
- }
- #if !defined(BOOST_ASIO_NO_DEPRECATED)
- /// (Deprecated: Use non-error_code overload.) Cancel any asynchronous
- /// operations that are waiting on the timer.
- /**
- * This function forces the completion of any pending asynchronous wait
- * operations against the timer. The handler for each cancelled operation will
- * be invoked with the boost::asio::error::operation_aborted error code.
- *
- * Cancelling the timer does not change the expiry time.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @return The number of asynchronous operations that were cancelled.
- *
- * @note If the timer has already expired when cancel() is called, then the
- * handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t cancel(boost::system::error_code& ec)
- {
- return impl_.get_service().cancel(impl_.get_implementation(), ec);
- }
- #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
- /// Cancels one asynchronous operation that is waiting on the timer.
- /**
- * This function forces the completion of one pending asynchronous wait
- * operation against the timer. Handlers are cancelled in FIFO order. The
- * handler for the cancelled operation will be invoked with the
- * boost::asio::error::operation_aborted error code.
- *
- * Cancelling the timer does not change the expiry time.
- *
- * @return The number of asynchronous operations that were cancelled. That is,
- * either 0 or 1.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @note If the timer has already expired when cancel_one() is called, then
- * the handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t cancel_one()
- {
- boost::system::error_code ec;
- std::size_t s = impl_.get_service().cancel_one(
- impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "cancel_one");
- return s;
- }
- #if !defined(BOOST_ASIO_NO_DEPRECATED)
- /// (Deprecated: Use non-error_code overload.) Cancels one asynchronous
- /// operation that is waiting on the timer.
- /**
- * This function forces the completion of one pending asynchronous wait
- * operation against the timer. Handlers are cancelled in FIFO order. The
- * handler for the cancelled operation will be invoked with the
- * boost::asio::error::operation_aborted error code.
- *
- * Cancelling the timer does not change the expiry time.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @return The number of asynchronous operations that were cancelled. That is,
- * either 0 or 1.
- *
- * @note If the timer has already expired when cancel_one() is called, then
- * the handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t cancel_one(boost::system::error_code& ec)
- {
- return impl_.get_service().cancel_one(impl_.get_implementation(), ec);
- }
- /// (Deprecated: Use expiry().) Get the timer's expiry time as an absolute
- /// time.
- /**
- * This function may be used to obtain the timer's current expiry time.
- * Whether the timer has expired or not does not affect this value.
- */
- time_point expires_at() const
- {
- return impl_.get_service().expires_at(impl_.get_implementation());
- }
- #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
- /// Get the timer's expiry time as an absolute time.
- /**
- * This function may be used to obtain the timer's current expiry time.
- * Whether the timer has expired or not does not affect this value.
- */
- time_point expiry() const
- {
- return impl_.get_service().expiry(impl_.get_implementation());
- }
- /// Set the timer's expiry time as an absolute time.
- /**
- * This function sets the expiry time. Any pending asynchronous wait
- * operations will be cancelled. The handler for each cancelled operation will
- * be invoked with the boost::asio::error::operation_aborted error code.
- *
- * @param expiry_time The expiry time to be used for the timer.
- *
- * @return The number of asynchronous operations that were cancelled.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @note If the timer has already expired when expires_at() is called, then
- * the handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t expires_at(const time_point& expiry_time)
- {
- boost::system::error_code ec;
- std::size_t s = impl_.get_service().expires_at(
- impl_.get_implementation(), expiry_time, ec);
- boost::asio::detail::throw_error(ec, "expires_at");
- return s;
- }
- #if !defined(BOOST_ASIO_NO_DEPRECATED)
- /// (Deprecated: Use non-error_code overload.) Set the timer's expiry time as
- /// an absolute time.
- /**
- * This function sets the expiry time. Any pending asynchronous wait
- * operations will be cancelled. The handler for each cancelled operation will
- * be invoked with the boost::asio::error::operation_aborted error code.
- *
- * @param expiry_time The expiry time to be used for the timer.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @return The number of asynchronous operations that were cancelled.
- *
- * @note If the timer has already expired when expires_at() is called, then
- * the handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t expires_at(const time_point& expiry_time,
- boost::system::error_code& ec)
- {
- return impl_.get_service().expires_at(
- impl_.get_implementation(), expiry_time, ec);
- }
- #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
- /// Set the timer's expiry time relative to now.
- /**
- * This function sets the expiry time. Any pending asynchronous wait
- * operations will be cancelled. The handler for each cancelled operation will
- * be invoked with the boost::asio::error::operation_aborted error code.
- *
- * @param expiry_time The expiry time to be used for the timer.
- *
- * @return The number of asynchronous operations that were cancelled.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @note If the timer has already expired when expires_after() is called,
- * then the handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t expires_after(const duration& expiry_time)
- {
- boost::system::error_code ec;
- std::size_t s = impl_.get_service().expires_after(
- impl_.get_implementation(), expiry_time, ec);
- boost::asio::detail::throw_error(ec, "expires_after");
- return s;
- }
- #if !defined(BOOST_ASIO_NO_DEPRECATED)
- /// (Deprecated: Use expiry().) Get the timer's expiry time relative to now.
- /**
- * This function may be used to obtain the timer's current expiry time.
- * Whether the timer has expired or not does not affect this value.
- */
- duration expires_from_now() const
- {
- return impl_.get_service().expires_from_now(impl_.get_implementation());
- }
- /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
- /// to now.
- /**
- * This function sets the expiry time. Any pending asynchronous wait
- * operations will be cancelled. The handler for each cancelled operation will
- * be invoked with the boost::asio::error::operation_aborted error code.
- *
- * @param expiry_time The expiry time to be used for the timer.
- *
- * @return The number of asynchronous operations that were cancelled.
- *
- * @throws boost::system::system_error Thrown on failure.
- *
- * @note If the timer has already expired when expires_from_now() is called,
- * then the handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t expires_from_now(const duration& expiry_time)
- {
- boost::system::error_code ec;
- std::size_t s = impl_.get_service().expires_from_now(
- impl_.get_implementation(), expiry_time, ec);
- boost::asio::detail::throw_error(ec, "expires_from_now");
- return s;
- }
- /// (Deprecated: Use expires_after().) Set the timer's expiry time relative
- /// to now.
- /**
- * This function sets the expiry time. Any pending asynchronous wait
- * operations will be cancelled. The handler for each cancelled operation will
- * be invoked with the boost::asio::error::operation_aborted error code.
- *
- * @param expiry_time The expiry time to be used for the timer.
- *
- * @param ec Set to indicate what error occurred, if any.
- *
- * @return The number of asynchronous operations that were cancelled.
- *
- * @note If the timer has already expired when expires_from_now() is called,
- * then the handlers for asynchronous wait operations will:
- *
- * @li have already been invoked; or
- *
- * @li have been queued for invocation in the near future.
- *
- * These handlers can no longer be cancelled, and therefore are passed an
- * error code that indicates the successful completion of the wait operation.
- */
- std::size_t expires_from_now(const duration& expiry_time,
- boost::system::error_code& ec)
- {
- return impl_.get_service().expires_from_now(
- impl_.get_implementation(), expiry_time, ec);
- }
- #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
- /// Perform a blocking wait on the timer.
- /**
- * This function is used to wait for the timer to expire. This function
- * blocks and does not return until the timer has expired.
- *
- * @throws boost::system::system_error Thrown on failure.
- */
- void wait()
- {
- boost::system::error_code ec;
- impl_.get_service().wait(impl_.get_implementation(), ec);
- boost::asio::detail::throw_error(ec, "wait");
- }
- /// Perform a blocking wait on the timer.
- /**
- * This function is used to wait for the timer to expire. This function
- * blocks and does not return until the timer has expired.
- *
- * @param ec Set to indicate what error occurred, if any.
- */
- void wait(boost::system::error_code& ec)
- {
- impl_.get_service().wait(impl_.get_implementation(), ec);
- }
- /// Start an asynchronous wait on the timer.
- /**
- * This function may be used to initiate an asynchronous wait against the
- * timer. It always returns immediately.
- *
- * For each call to async_wait(), the supplied handler will be called exactly
- * once. The handler will be called when:
- *
- * @li The timer has expired.
- *
- * @li The timer was cancelled, in which case the handler is passed the error
- * code boost::asio::error::operation_aborted.
- *
- * @param handler The handler to be called when the timer expires. Copies
- * will be made of the handler as required. The function signature of the
- * handler must be:
- * @code void handler(
- * const boost::system::error_code& error // Result of operation.
- * ); @endcode
- * Regardless of whether the asynchronous operation completes immediately or
- * not, the handler will not be invoked from within this function. On
- * immediate completion, invocation of the handler will be performed in a
- * manner equivalent to using boost::asio::post().
- */
- template <
- BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
- WaitHandler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
- BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitHandler,
- void (boost::system::error_code))
- async_wait(
- BOOST_ASIO_MOVE_ARG(WaitHandler) handler
- BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
- {
- return async_initiate<WaitHandler, void (boost::system::error_code)>(
- initiate_async_wait(this), handler);
- }
- private:
- // Disallow copying and assignment.
- basic_waitable_timer(const basic_waitable_timer&) BOOST_ASIO_DELETED;
- basic_waitable_timer& operator=(
- const basic_waitable_timer&) BOOST_ASIO_DELETED;
- class initiate_async_wait
- {
- public:
- typedef Executor executor_type;
- explicit initiate_async_wait(basic_waitable_timer* self)
- : self_(self)
- {
- }
- executor_type get_executor() const BOOST_ASIO_NOEXCEPT
- {
- return self_->get_executor();
- }
- template <typename WaitHandler>
- void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler) const
- {
- // If you get an error on the following line it means that your handler
- // does not meet the documented type requirements for a WaitHandler.
- BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
- detail::non_const_lvalue<WaitHandler> handler2(handler);
- self_->impl_.get_service().async_wait(
- self_->impl_.get_implementation(),
- handler2.value, self_->impl_.get_executor());
- }
- private:
- basic_waitable_timer* self_;
- };
- detail::io_object_impl<
- detail::deadline_timer_service<
- detail::chrono_time_traits<Clock, WaitTraits> >,
- executor_type > impl_;
- };
- } // namespace asio
- } // namespace boost
- #include <boost/asio/detail/pop_options.hpp>
- #endif // BOOST_ASIO_BASIC_WAITABLE_TIMER_HPP
|