PageRenderTime 42ms CodeModel.GetById 13ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/contrib/boost/asio/basic_deadline_timer.hpp

http://pythonocc.googlecode.com/
C++ Header | 447 lines | 112 code | 26 blank | 309 comment | 1 complexity | 5f56287581d96a6b9ffd767d2740a03f MD5 | raw file
  1//
  2// basic_deadline_timer.hpp
  3// ~~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2010 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
 11#ifndef BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP
 12#define BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP
 13
 14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 15# pragma once
 16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 17
 18#include <boost/asio/detail/push_options.hpp>
 19
 20#include <boost/asio/detail/push_options.hpp>
 21#include <cstddef>
 22#include <boost/config.hpp>
 23#include <boost/asio/detail/pop_options.hpp>
 24
 25#include <boost/asio/basic_io_object.hpp>
 26#include <boost/asio/deadline_timer_service.hpp>
 27#include <boost/asio/error.hpp>
 28#include <boost/asio/detail/throw_error.hpp>
 29
 30namespace boost {
 31namespace asio {
 32
 33/// Provides waitable timer functionality.
 34/**
 35 * The basic_deadline_timer class template provides the ability to perform a
 36 * blocking or asynchronous wait for a timer to expire.
 37 *
 38 * A deadline timer is always in one of two states: "expired" or "not expired".
 39 * If the wait() or async_wait() function is called on an expired timer, the
 40 * wait operation will complete immediately.
 41 *
 42 * Most applications will use the boost::asio::deadline_timer typedef.
 43 *
 44 * @par Thread Safety
 45 * @e Distinct @e objects: Safe.@n
 46 * @e Shared @e objects: Unsafe.
 47 *
 48 * @par Examples
 49 * Performing a blocking wait:
 50 * @code
 51 * // Construct a timer without setting an expiry time.
 52 * boost::asio::deadline_timer timer(io_service);
 53 *
 54 * // Set an expiry time relative to now.
 55 * timer.expires_from_now(boost::posix_time::seconds(5));
 56 *
 57 * // Wait for the timer to expire.
 58 * timer.wait();
 59 * @endcode
 60 *
 61 * @par 
 62 * Performing an asynchronous wait:
 63 * @code
 64 * void handler(const boost::system::error_code& error)
 65 * {
 66 *   if (!error)
 67 *   {
 68 *     // Timer expired.
 69 *   }
 70 * }
 71 *
 72 * ...
 73 *
 74 * // Construct a timer with an absolute expiry time.
 75 * boost::asio::deadline_timer timer(io_service,
 76 *     boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));
 77 *
 78 * // Start an asynchronous wait.
 79 * timer.async_wait(handler);
 80 * @endcode
 81 *
 82 * @par Changing an active deadline_timer's expiry time
 83 *
 84 * Changing the expiry time of a timer while there are pending asynchronous
 85 * waits causes those wait operations to be cancelled. To ensure that the action
 86 * associated with the timer is performed only once, use something like this:
 87 * used:
 88 *
 89 * @code
 90 * void on_some_event()
 91 * {
 92 *   if (my_timer.expires_from_now(seconds(5)) > 0)
 93 *   {
 94 *     // We managed to cancel the timer. Start new asynchronous wait.
 95 *     my_timer.async_wait(on_timeout);
 96 *   }
 97 *   else
 98 *   {
 99 *     // Too late, timer has already expired!
100 *   }
101 * }
102 *
103 * void on_timeout(const boost::system::error_code& e)
104 * {
105 *   if (e != boost::asio::error::operation_aborted)
106 *   {
107 *     // Timer was not cancelled, take necessary action.
108 *   }
109 * }
110 * @endcode
111 *
112 * @li The boost::asio::basic_deadline_timer::expires_from_now() function
113 * cancels any pending asynchronous waits, and returns the number of
114 * asynchronous waits that were cancelled. If it returns 0 then you were too
115 * late and the wait handler has already been executed, or will soon be
116 * executed. If it returns 1 then the wait handler was successfully cancelled.
117 *
118 * @li If a wait handler is cancelled, the boost::system::error_code passed to
119 * it contains the value boost::asio::error::operation_aborted.
120 */
121template <typename Time,
122    typename TimeTraits = boost::asio::time_traits<Time>,
123    typename TimerService = deadline_timer_service<Time, TimeTraits> >
124class basic_deadline_timer
125  : public basic_io_object<TimerService>
126{
127public:
128  /// The time traits type.
129  typedef TimeTraits traits_type;
130
131  /// The time type.
132  typedef typename traits_type::time_type time_type;
133
134  /// The duration type.
135  typedef typename traits_type::duration_type duration_type;
136
137  /// Constructor.
138  /**
139   * This constructor creates a timer without setting an expiry time. The
140   * expires_at() or expires_from_now() functions must be called to set an
141   * expiry time before the timer can be waited on.
142   *
143   * @param io_service The io_service object that the timer will use to dispatch
144   * handlers for any asynchronous operations performed on the timer.
145   */
146  explicit basic_deadline_timer(boost::asio::io_service& io_service)
147    : basic_io_object<TimerService>(io_service)
148  {
149  }
150
151  /// Constructor to set a particular expiry time as an absolute time.
152  /**
153   * This constructor creates a timer and sets the expiry time.
154   *
155   * @param io_service The io_service object that the timer will use to dispatch
156   * handlers for any asynchronous operations performed on the timer.
157   *
158   * @param expiry_time The expiry time to be used for the timer, expressed
159   * as an absolute time.
160   */
161  basic_deadline_timer(boost::asio::io_service& io_service,
162      const time_type& expiry_time)
163    : basic_io_object<TimerService>(io_service)
164  {
165    boost::system::error_code ec;
166    this->service.expires_at(this->implementation, expiry_time, ec);
167    boost::asio::detail::throw_error(ec);
168  }
169
170  /// Constructor to set a particular expiry time relative to now.
171  /**
172   * This constructor creates a timer and sets the expiry time.
173   *
174   * @param io_service The io_service object that the timer will use to dispatch
175   * handlers for any asynchronous operations performed on the timer.
176   *
177   * @param expiry_time The expiry time to be used for the timer, relative to
178   * now.
179   */
180  basic_deadline_timer(boost::asio::io_service& io_service,
181      const duration_type& expiry_time)
182    : basic_io_object<TimerService>(io_service)
183  {
184    boost::system::error_code ec;
185    this->service.expires_from_now(this->implementation, expiry_time, ec);
186    boost::asio::detail::throw_error(ec);
187  }
188
189  /// Cancel any asynchronous operations that are waiting on the timer.
190  /**
191   * This function forces the completion of any pending asynchronous wait
192   * operations against the timer. The handler for each cancelled operation will
193   * be invoked with the boost::asio::error::operation_aborted error code.
194   *
195   * Cancelling the timer does not change the expiry time.
196   *
197   * @return The number of asynchronous operations that were cancelled.
198   *
199   * @throws boost::system::system_error Thrown on failure.
200   *
201   * @note If the timer has already expired when cancel() is called, then the
202   * handlers for asynchronous wait operations will:
203   *
204   * @li have already been invoked; or
205   *
206   * @li have been queued for invocation in the near future.
207   *
208   * These handlers can no longer be cancelled, and therefore are passed an
209   * error code that indicates the successful completion of the wait operation.
210   */
211  std::size_t cancel()
212  {
213    boost::system::error_code ec;
214    std::size_t s = this->service.cancel(this->implementation, ec);
215    boost::asio::detail::throw_error(ec);
216    return s;
217  }
218
219  /// Cancel any asynchronous operations that are waiting on the timer.
220  /**
221   * This function forces the completion of any pending asynchronous wait
222   * operations against the timer. The handler for each cancelled operation will
223   * be invoked with the boost::asio::error::operation_aborted error code.
224   *
225   * Cancelling the timer does not change the expiry time.
226   *
227   * @param ec Set to indicate what error occurred, if any.
228   *
229   * @return The number of asynchronous operations that were cancelled.
230   *
231   * @note If the timer has already expired when cancel() is called, then the
232   * handlers for asynchronous wait operations will:
233   *
234   * @li have already been invoked; or
235   *
236   * @li have been queued for invocation in the near future.
237   *
238   * These handlers can no longer be cancelled, and therefore are passed an
239   * error code that indicates the successful completion of the wait operation.
240   */
241  std::size_t cancel(boost::system::error_code& ec)
242  {
243    return this->service.cancel(this->implementation, ec);
244  }
245
246  /// Get the timer's expiry time as an absolute time.
247  /**
248   * This function may be used to obtain the timer's current expiry time.
249   * Whether the timer has expired or not does not affect this value.
250   */
251  time_type expires_at() const
252  {
253    return this->service.expires_at(this->implementation);
254  }
255
256  /// Set the timer's expiry time as an absolute time.
257  /**
258   * This function sets the expiry time. Any pending asynchronous wait
259   * operations will be cancelled. The handler for each cancelled operation will
260   * be invoked with the boost::asio::error::operation_aborted error code.
261   *
262   * @param expiry_time The expiry time to be used for the timer.
263   *
264   * @return The number of asynchronous operations that were cancelled.
265   *
266   * @throws boost::system::system_error Thrown on failure.
267   *
268   * @note If the timer has already expired when expires_at() is called, then
269   * the handlers for asynchronous wait operations will:
270   *
271   * @li have already been invoked; or
272   *
273   * @li have been queued for invocation in the near future.
274   *
275   * These handlers can no longer be cancelled, and therefore are passed an
276   * error code that indicates the successful completion of the wait operation.
277   */
278  std::size_t expires_at(const time_type& expiry_time)
279  {
280    boost::system::error_code ec;
281    std::size_t s = this->service.expires_at(
282        this->implementation, expiry_time, ec);
283    boost::asio::detail::throw_error(ec);
284    return s;
285  }
286
287  /// Set the timer's expiry time as an absolute time.
288  /**
289   * This function sets the expiry time. Any pending asynchronous wait
290   * operations will be cancelled. The handler for each cancelled operation will
291   * be invoked with the boost::asio::error::operation_aborted error code.
292   *
293   * @param expiry_time The expiry time to be used for the timer.
294   *
295   * @param ec Set to indicate what error occurred, if any.
296   *
297   * @return The number of asynchronous operations that were cancelled.
298   *
299   * @note If the timer has already expired when expires_at() is called, then
300   * the handlers for asynchronous wait operations will:
301   *
302   * @li have already been invoked; or
303   *
304   * @li have been queued for invocation in the near future.
305   *
306   * These handlers can no longer be cancelled, and therefore are passed an
307   * error code that indicates the successful completion of the wait operation.
308   */
309  std::size_t expires_at(const time_type& expiry_time,
310      boost::system::error_code& ec)
311  {
312    return this->service.expires_at(this->implementation, expiry_time, ec);
313  }
314
315  /// Get the timer's expiry time relative to now.
316  /**
317   * This function may be used to obtain the timer's current expiry time.
318   * Whether the timer has expired or not does not affect this value.
319   */
320  duration_type expires_from_now() const
321  {
322    return this->service.expires_from_now(this->implementation);
323  }
324
325  /// Set the timer's expiry time relative to now.
326  /**
327   * This function sets the expiry time. Any pending asynchronous wait
328   * operations will be cancelled. The handler for each cancelled operation will
329   * be invoked with the boost::asio::error::operation_aborted error code.
330   *
331   * @param expiry_time The expiry time to be used for the timer.
332   *
333   * @return The number of asynchronous operations that were cancelled.
334   *
335   * @throws boost::system::system_error Thrown on failure.
336   *
337   * @note If the timer has already expired when expires_from_now() is called,
338   * then the handlers for asynchronous wait operations will:
339   *
340   * @li have already been invoked; or
341   *
342   * @li have been queued for invocation in the near future.
343   *
344   * These handlers can no longer be cancelled, and therefore are passed an
345   * error code that indicates the successful completion of the wait operation.
346   */
347  std::size_t expires_from_now(const duration_type& expiry_time)
348  {
349    boost::system::error_code ec;
350    std::size_t s = this->service.expires_from_now(
351        this->implementation, expiry_time, ec);
352    boost::asio::detail::throw_error(ec);
353    return s;
354  }
355
356  /// Set the timer's expiry time relative to now.
357  /**
358   * This function sets the expiry time. Any pending asynchronous wait
359   * operations will be cancelled. The handler for each cancelled operation will
360   * be invoked with the boost::asio::error::operation_aborted error code.
361   *
362   * @param expiry_time The expiry time to be used for the timer.
363   *
364   * @param ec Set to indicate what error occurred, if any.
365   *
366   * @return The number of asynchronous operations that were cancelled.
367   *
368   * @note If the timer has already expired when expires_from_now() is called,
369   * then the handlers for asynchronous wait operations will:
370   *
371   * @li have already been invoked; or
372   *
373   * @li have been queued for invocation in the near future.
374   *
375   * These handlers can no longer be cancelled, and therefore are passed an
376   * error code that indicates the successful completion of the wait operation.
377   */
378  std::size_t expires_from_now(const duration_type& expiry_time,
379      boost::system::error_code& ec)
380  {
381    return this->service.expires_from_now(
382        this->implementation, expiry_time, ec);
383  }
384
385  /// Perform a blocking wait on the timer.
386  /**
387   * This function is used to wait for the timer to expire. This function
388   * blocks and does not return until the timer has expired.
389   *
390   * @throws boost::system::system_error Thrown on failure.
391   */
392  void wait()
393  {
394    boost::system::error_code ec;
395    this->service.wait(this->implementation, ec);
396    boost::asio::detail::throw_error(ec);
397  }
398
399  /// Perform a blocking wait on the timer.
400  /**
401   * This function is used to wait for the timer to expire. This function
402   * blocks and does not return until the timer has expired.
403   *
404   * @param ec Set to indicate what error occurred, if any.
405   */
406  void wait(boost::system::error_code& ec)
407  {
408    this->service.wait(this->implementation, ec);
409  }
410
411  /// Start an asynchronous wait on the timer.
412  /**
413   * This function may be used to initiate an asynchronous wait against the
414   * timer. It always returns immediately.
415   *
416   * For each call to async_wait(), the supplied handler will be called exactly
417   * once. The handler will be called when:
418   *
419   * @li The timer has expired.
420   *
421   * @li The timer was cancelled, in which case the handler is passed the error
422   * code boost::asio::error::operation_aborted.
423   *
424   * @param handler The handler to be called when the timer expires. Copies
425   * will be made of the handler as required. The function signature of the
426   * handler must be:
427   * @code void handler(
428   *   const boost::system::error_code& error // Result of operation.
429   * ); @endcode
430   * Regardless of whether the asynchronous operation completes immediately or
431   * not, the handler will not be invoked from within this function. Invocation
432   * of the handler will be performed in a manner equivalent to using
433   * boost::asio::io_service::post().
434   */
435  template <typename WaitHandler>
436  void async_wait(WaitHandler handler)
437  {
438    this->service.async_wait(this->implementation, handler);
439  }
440};
441
442} // namespace asio
443} // namespace boost
444
445#include <boost/asio/detail/pop_options.hpp>
446
447#endif // BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP