Project revived from Feb2017
[EroSomnia.git] / deps / boost_1_63_0 / boost / asio / basic_deadline_timer.hpp
blobb6c83f527e11a17cb30b260350ed6c6b4f34ece0
1 //
2 // basic_deadline_timer.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2016 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 //
11 #ifndef BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP
12 #define BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
21 || defined(GENERATING_DOCUMENTATION)
23 #include <cstddef>
24 #include <boost/asio/basic_io_object.hpp>
25 #include <boost/asio/deadline_timer_service.hpp>
26 #include <boost/asio/detail/handler_type_requirements.hpp>
27 #include <boost/asio/detail/throw_error.hpp>
28 #include <boost/asio/error.hpp>
30 #include <boost/asio/detail/push_options.hpp>
32 namespace boost {
33 namespace asio {
35 /// Provides waitable timer functionality.
36 /**
37 * The basic_deadline_timer class template provides the ability to perform a
38 * blocking or asynchronous wait for a timer to expire.
40 * A deadline timer is always in one of two states: "expired" or "not expired".
41 * If the wait() or async_wait() function is called on an expired timer, the
42 * wait operation will complete immediately.
44 * Most applications will use the boost::asio::deadline_timer typedef.
46 * @par Thread Safety
47 * @e Distinct @e objects: Safe.@n
48 * @e Shared @e objects: Unsafe.
50 * @par Examples
51 * Performing a blocking wait:
52 * @code
53 * // Construct a timer without setting an expiry time.
54 * boost::asio::deadline_timer timer(io_service);
56 * // Set an expiry time relative to now.
57 * timer.expires_from_now(boost::posix_time::seconds(5));
59 * // Wait for the timer to expire.
60 * timer.wait();
61 * @endcode
63 * @par
64 * Performing an asynchronous wait:
65 * @code
66 * void handler(const boost::system::error_code& error)
67 * {
68 * if (!error)
69 * {
70 * // Timer expired.
71 * }
72 * }
74 * ...
76 * // Construct a timer with an absolute expiry time.
77 * boost::asio::deadline_timer timer(io_service,
78 * boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));
80 * // Start an asynchronous wait.
81 * timer.async_wait(handler);
82 * @endcode
84 * @par Changing an active deadline_timer's expiry time
86 * Changing the expiry time of a timer while there are pending asynchronous
87 * waits causes those wait operations to be cancelled. To ensure that the action
88 * associated with the timer is performed only once, use something like this:
89 * used:
91 * @code
92 * void on_some_event()
93 * {
94 * if (my_timer.expires_from_now(seconds(5)) > 0)
95 * {
96 * // We managed to cancel the timer. Start new asynchronous wait.
97 * my_timer.async_wait(on_timeout);
98 * }
99 * else
101 * // Too late, timer has already expired!
105 * void on_timeout(const boost::system::error_code& e)
107 * if (e != boost::asio::error::operation_aborted)
109 * // Timer was not cancelled, take necessary action.
112 * @endcode
114 * @li The boost::asio::basic_deadline_timer::expires_from_now() function
115 * cancels any pending asynchronous waits, and returns the number of
116 * asynchronous waits that were cancelled. If it returns 0 then you were too
117 * late and the wait handler has already been executed, or will soon be
118 * executed. If it returns 1 then the wait handler was successfully cancelled.
120 * @li If a wait handler is cancelled, the boost::system::error_code passed to
121 * it contains the value boost::asio::error::operation_aborted.
123 template <typename Time,
124 typename TimeTraits = boost::asio::time_traits<Time>,
125 typename TimerService = deadline_timer_service<Time, TimeTraits> >
126 class basic_deadline_timer
127 : public basic_io_object<TimerService>
129 public:
130 /// The time traits type.
131 typedef TimeTraits traits_type;
133 /// The time type.
134 typedef typename traits_type::time_type time_type;
136 /// The duration type.
137 typedef typename traits_type::duration_type duration_type;
139 /// Constructor.
141 * This constructor creates a timer without setting an expiry time. The
142 * expires_at() or expires_from_now() functions must be called to set an
143 * expiry time before the timer can be waited on.
145 * @param io_service The io_service object that the timer will use to dispatch
146 * handlers for any asynchronous operations performed on the timer.
148 explicit basic_deadline_timer(boost::asio::io_service& io_service)
149 : basic_io_object<TimerService>(io_service)
153 /// Constructor to set a particular expiry time as an absolute time.
155 * This constructor creates a timer and sets the expiry time.
157 * @param io_service The io_service object that the timer will use to dispatch
158 * handlers for any asynchronous operations performed on the timer.
160 * @param expiry_time The expiry time to be used for the timer, expressed
161 * as an absolute time.
163 basic_deadline_timer(boost::asio::io_service& io_service,
164 const time_type& expiry_time)
165 : basic_io_object<TimerService>(io_service)
167 boost::system::error_code ec;
168 this->service.expires_at(this->implementation, expiry_time, ec);
169 boost::asio::detail::throw_error(ec, "expires_at");
172 /// Constructor to set a particular expiry time relative to now.
174 * This constructor creates a timer and sets the expiry time.
176 * @param io_service The io_service object that the timer will use to dispatch
177 * handlers for any asynchronous operations performed on the timer.
179 * @param expiry_time The expiry time to be used for the timer, relative to
180 * now.
182 basic_deadline_timer(boost::asio::io_service& io_service,
183 const duration_type& expiry_time)
184 : basic_io_object<TimerService>(io_service)
186 boost::system::error_code ec;
187 this->service.expires_from_now(this->implementation, expiry_time, ec);
188 boost::asio::detail::throw_error(ec, "expires_from_now");
191 /// Cancel any asynchronous operations that are waiting on the timer.
193 * This function forces the completion of any pending asynchronous wait
194 * operations against the timer. The handler for each cancelled operation will
195 * be invoked with the boost::asio::error::operation_aborted error code.
197 * Cancelling the timer does not change the expiry time.
199 * @return The number of asynchronous operations that were cancelled.
201 * @throws boost::system::system_error Thrown on failure.
203 * @note If the timer has already expired when cancel() is called, then the
204 * handlers for asynchronous wait operations will:
206 * @li have already been invoked; or
208 * @li have been queued for invocation in the near future.
210 * These handlers can no longer be cancelled, and therefore are passed an
211 * error code that indicates the successful completion of the wait operation.
213 std::size_t cancel()
215 boost::system::error_code ec;
216 std::size_t s = this->service.cancel(this->implementation, ec);
217 boost::asio::detail::throw_error(ec, "cancel");
218 return s;
221 /// Cancel any asynchronous operations that are waiting on the timer.
223 * This function forces the completion of any pending asynchronous wait
224 * operations against the timer. The handler for each cancelled operation will
225 * be invoked with the boost::asio::error::operation_aborted error code.
227 * Cancelling the timer does not change the expiry time.
229 * @param ec Set to indicate what error occurred, if any.
231 * @return The number of asynchronous operations that were cancelled.
233 * @note If the timer has already expired when cancel() is called, then the
234 * handlers for asynchronous wait operations will:
236 * @li have already been invoked; or
238 * @li have been queued for invocation in the near future.
240 * These handlers can no longer be cancelled, and therefore are passed an
241 * error code that indicates the successful completion of the wait operation.
243 std::size_t cancel(boost::system::error_code& ec)
245 return this->service.cancel(this->implementation, ec);
248 /// Cancels one asynchronous operation that is waiting on the timer.
250 * This function forces the completion of one pending asynchronous wait
251 * operation against the timer. Handlers are cancelled in FIFO order. The
252 * handler for the cancelled operation will be invoked with the
253 * boost::asio::error::operation_aborted error code.
255 * Cancelling the timer does not change the expiry time.
257 * @return The number of asynchronous operations that were cancelled. That is,
258 * either 0 or 1.
260 * @throws boost::system::system_error Thrown on failure.
262 * @note If the timer has already expired when cancel_one() is called, then
263 * the handlers for asynchronous wait operations will:
265 * @li have already been invoked; or
267 * @li have been queued for invocation in the near future.
269 * These handlers can no longer be cancelled, and therefore are passed an
270 * error code that indicates the successful completion of the wait operation.
272 std::size_t cancel_one()
274 boost::system::error_code ec;
275 std::size_t s = this->service.cancel_one(this->implementation, ec);
276 boost::asio::detail::throw_error(ec, "cancel_one");
277 return s;
280 /// Cancels one asynchronous operation that is waiting on the timer.
282 * This function forces the completion of one pending asynchronous wait
283 * operation against the timer. Handlers are cancelled in FIFO order. The
284 * handler for the cancelled operation will be invoked with the
285 * boost::asio::error::operation_aborted error code.
287 * Cancelling the timer does not change the expiry time.
289 * @param ec Set to indicate what error occurred, if any.
291 * @return The number of asynchronous operations that were cancelled. That is,
292 * either 0 or 1.
294 * @note If the timer has already expired when cancel_one() is called, then
295 * the handlers for asynchronous wait operations will:
297 * @li have already been invoked; or
299 * @li have been queued for invocation in the near future.
301 * These handlers can no longer be cancelled, and therefore are passed an
302 * error code that indicates the successful completion of the wait operation.
304 std::size_t cancel_one(boost::system::error_code& ec)
306 return this->service.cancel_one(this->implementation, ec);
309 /// Get the timer's expiry time as an absolute time.
311 * This function may be used to obtain the timer's current expiry time.
312 * Whether the timer has expired or not does not affect this value.
314 time_type expires_at() const
316 return this->service.expires_at(this->implementation);
319 /// Set the timer's expiry time as an absolute time.
321 * This function sets the expiry time. Any pending asynchronous wait
322 * operations will be cancelled. The handler for each cancelled operation will
323 * be invoked with the boost::asio::error::operation_aborted error code.
325 * @param expiry_time The expiry time to be used for the timer.
327 * @return The number of asynchronous operations that were cancelled.
329 * @throws boost::system::system_error Thrown on failure.
331 * @note If the timer has already expired when expires_at() is called, then
332 * the handlers for asynchronous wait operations will:
334 * @li have already been invoked; or
336 * @li have been queued for invocation in the near future.
338 * These handlers can no longer be cancelled, and therefore are passed an
339 * error code that indicates the successful completion of the wait operation.
341 std::size_t expires_at(const time_type& expiry_time)
343 boost::system::error_code ec;
344 std::size_t s = this->service.expires_at(
345 this->implementation, expiry_time, ec);
346 boost::asio::detail::throw_error(ec, "expires_at");
347 return s;
350 /// Set the timer's expiry time as an absolute time.
352 * This function sets the expiry time. Any pending asynchronous wait
353 * operations will be cancelled. The handler for each cancelled operation will
354 * be invoked with the boost::asio::error::operation_aborted error code.
356 * @param expiry_time The expiry time to be used for the timer.
358 * @param ec Set to indicate what error occurred, if any.
360 * @return The number of asynchronous operations that were cancelled.
362 * @note If the timer has already expired when expires_at() is called, then
363 * the handlers for asynchronous wait operations will:
365 * @li have already been invoked; or
367 * @li have been queued for invocation in the near future.
369 * These handlers can no longer be cancelled, and therefore are passed an
370 * error code that indicates the successful completion of the wait operation.
372 std::size_t expires_at(const time_type& expiry_time,
373 boost::system::error_code& ec)
375 return this->service.expires_at(this->implementation, expiry_time, ec);
378 /// Get the timer's expiry time relative to now.
380 * This function may be used to obtain the timer's current expiry time.
381 * Whether the timer has expired or not does not affect this value.
383 duration_type expires_from_now() const
385 return this->service.expires_from_now(this->implementation);
388 /// Set the timer's expiry time relative to now.
390 * This function sets the expiry time. Any pending asynchronous wait
391 * operations will be cancelled. The handler for each cancelled operation will
392 * be invoked with the boost::asio::error::operation_aborted error code.
394 * @param expiry_time The expiry time to be used for the timer.
396 * @return The number of asynchronous operations that were cancelled.
398 * @throws boost::system::system_error Thrown on failure.
400 * @note If the timer has already expired when expires_from_now() is called,
401 * then the handlers for asynchronous wait operations will:
403 * @li have already been invoked; or
405 * @li have been queued for invocation in the near future.
407 * These handlers can no longer be cancelled, and therefore are passed an
408 * error code that indicates the successful completion of the wait operation.
410 std::size_t expires_from_now(const duration_type& expiry_time)
412 boost::system::error_code ec;
413 std::size_t s = this->service.expires_from_now(
414 this->implementation, expiry_time, ec);
415 boost::asio::detail::throw_error(ec, "expires_from_now");
416 return s;
419 /// Set the timer's expiry time relative to now.
421 * This function sets the expiry time. Any pending asynchronous wait
422 * operations will be cancelled. The handler for each cancelled operation will
423 * be invoked with the boost::asio::error::operation_aborted error code.
425 * @param expiry_time The expiry time to be used for the timer.
427 * @param ec Set to indicate what error occurred, if any.
429 * @return The number of asynchronous operations that were cancelled.
431 * @note If the timer has already expired when expires_from_now() is called,
432 * then the handlers for asynchronous wait operations will:
434 * @li have already been invoked; or
436 * @li have been queued for invocation in the near future.
438 * These handlers can no longer be cancelled, and therefore are passed an
439 * error code that indicates the successful completion of the wait operation.
441 std::size_t expires_from_now(const duration_type& expiry_time,
442 boost::system::error_code& ec)
444 return this->service.expires_from_now(
445 this->implementation, expiry_time, ec);
448 /// Perform a blocking wait on the timer.
450 * This function is used to wait for the timer to expire. This function
451 * blocks and does not return until the timer has expired.
453 * @throws boost::system::system_error Thrown on failure.
455 void wait()
457 boost::system::error_code ec;
458 this->service.wait(this->implementation, ec);
459 boost::asio::detail::throw_error(ec, "wait");
462 /// Perform a blocking wait on the timer.
464 * This function is used to wait for the timer to expire. This function
465 * blocks and does not return until the timer has expired.
467 * @param ec Set to indicate what error occurred, if any.
469 void wait(boost::system::error_code& ec)
471 this->service.wait(this->implementation, ec);
474 /// Start an asynchronous wait on the timer.
476 * This function may be used to initiate an asynchronous wait against the
477 * timer. It always returns immediately.
479 * For each call to async_wait(), the supplied handler will be called exactly
480 * once. The handler will be called when:
482 * @li The timer has expired.
484 * @li The timer was cancelled, in which case the handler is passed the error
485 * code boost::asio::error::operation_aborted.
487 * @param handler The handler to be called when the timer expires. Copies
488 * will be made of the handler as required. The function signature of the
489 * handler must be:
490 * @code void handler(
491 * const boost::system::error_code& error // Result of operation.
492 * ); @endcode
493 * Regardless of whether the asynchronous operation completes immediately or
494 * not, the handler will not be invoked from within this function. Invocation
495 * of the handler will be performed in a manner equivalent to using
496 * boost::asio::io_service::post().
498 template <typename WaitHandler>
499 BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
500 void (boost::system::error_code))
501 async_wait(BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
503 // If you get an error on the following line it means that your handler does
504 // not meet the documented type requirements for a WaitHandler.
505 BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
507 return this->service.async_wait(this->implementation,
508 BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
512 } // namespace asio
513 } // namespace boost
515 #include <boost/asio/detail/pop_options.hpp>
517 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
518 // || defined(GENERATING_DOCUMENTATION)
520 #endif // BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP