fix doc example typo
[boost.git] / boost / asio / basic_deadline_timer.hpp
blob65256b870fa2ac05973f72f578cec127567c41af
1 //
2 // basic_deadline_timer.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 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/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <cstddef>
22 #include <boost/config.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
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>
30 namespace boost {
31 namespace asio {
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.
38 * Most applications will use the boost::asio::deadline_timer typedef.
40 * @par Thread Safety
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
44 * @par Examples
45 * Performing a blocking wait:
46 * @code
47 * // Construct a timer without setting an expiry time.
48 * boost::asio::deadline_timer timer(io_service);
50 * // Set an expiry time relative to now.
51 * timer.expires_from_now(boost::posix_time::seconds(5));
53 * // Wait for the timer to expire.
54 * timer.wait();
55 * @endcode
57 * @par
58 * Performing an asynchronous wait:
59 * @code
60 * void handler(const boost::system::error_code& error)
61 * {
62 * if (!error)
63 * {
64 * // Timer expired.
65 * }
66 * }
68 * ...
70 * // Construct a timer with an absolute expiry time.
71 * boost::asio::deadline_timer timer(io_service,
72 * boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));
74 * // Start an asynchronous wait.
75 * timer.async_wait(handler);
76 * @endcode
78 * @par Changing an active deadline_timer's expiry time
80 * Changing the expiry time of a timer while there are pending asynchronous
81 * waits causes those wait operations to be cancelled. To ensure that the action
82 * associated with the timer is performed only once, use something like this:
83 * used:
85 * @code
86 * void on_some_event()
87 * {
88 * if (my_timer.expires_from_now(seconds(5)) > 0)
89 * {
90 * // We managed to cancel the timer. Start new asynchronous wait.
91 * my_timer.async_wait(on_timeout);
92 * }
93 * else
94 * {
95 * // Too late, timer has already expired!
96 * }
97 * }
99 * void on_timeout(const boost::system::error_code& e)
101 * if (e != boost::asio::error::operation_aborted)
103 * // Timer was not cancelled, take necessary action.
106 * @endcode
108 * @li The boost::asio::basic_deadline_timer::expires_from_now() function
109 * cancels any pending asynchronous waits, and returns the number of
110 * asynchronous waits that were cancelled. If it returns 0 then you were too
111 * late and the wait handler has already been executed, or will soon be
112 * executed. If it returns 1 then the wait handler was successfully cancelled.
114 * @li If a wait handler is cancelled, the boost::system::error_code passed to
115 * it contains the value boost::asio::error::operation_aborted.
117 template <typename Time,
118 typename TimeTraits = boost::asio::time_traits<Time>,
119 typename TimerService = deadline_timer_service<Time, TimeTraits> >
120 class basic_deadline_timer
121 : public basic_io_object<TimerService>
123 public:
124 /// The time traits type.
125 typedef TimeTraits traits_type;
127 /// The time type.
128 typedef typename traits_type::time_type time_type;
130 /// The duration type.
131 typedef typename traits_type::duration_type duration_type;
133 /// Constructor.
135 * This constructor creates a timer without setting an expiry time. The
136 * expires_at() or expires_from_now() functions must be called to set an
137 * expiry time before the timer can be waited on.
139 * @param io_service The io_service object that the timer will use to dispatch
140 * handlers for any asynchronous operations performed on the timer.
142 explicit basic_deadline_timer(boost::asio::io_service& io_service)
143 : basic_io_object<TimerService>(io_service)
147 /// Constructor to set a particular expiry time as an absolute time.
149 * This constructor creates a timer and sets the expiry time.
151 * @param io_service The io_service object that the timer will use to dispatch
152 * handlers for any asynchronous operations performed on the timer.
154 * @param expiry_time The expiry time to be used for the timer, expressed
155 * as an absolute time.
157 basic_deadline_timer(boost::asio::io_service& io_service,
158 const time_type& expiry_time)
159 : basic_io_object<TimerService>(io_service)
161 boost::system::error_code ec;
162 this->service.expires_at(this->implementation, expiry_time, ec);
163 boost::asio::detail::throw_error(ec);
166 /// Constructor to set a particular expiry time relative to now.
168 * This constructor creates a timer and sets the expiry time.
170 * @param io_service The io_service object that the timer will use to dispatch
171 * handlers for any asynchronous operations performed on the timer.
173 * @param expiry_time The expiry time to be used for the timer, relative to
174 * now.
176 basic_deadline_timer(boost::asio::io_service& io_service,
177 const duration_type& expiry_time)
178 : basic_io_object<TimerService>(io_service)
180 boost::system::error_code ec;
181 this->service.expires_from_now(this->implementation, expiry_time, ec);
182 boost::asio::detail::throw_error(ec);
185 /// Cancel any asynchronous operations that are waiting on the timer.
187 * This function forces the completion of any pending asynchronous wait
188 * operations against the timer. The handler for each cancelled operation will
189 * be invoked with the boost::asio::error::operation_aborted error code.
191 * Cancelling the timer does not change the expiry time.
193 * @return The number of asynchronous operations that were cancelled.
195 * @throws boost::system::system_error Thrown on failure.
197 std::size_t cancel()
199 boost::system::error_code ec;
200 std::size_t s = this->service.cancel(this->implementation, ec);
201 boost::asio::detail::throw_error(ec);
202 return s;
205 /// Cancel any asynchronous operations that are waiting on the timer.
207 * This function forces the completion of any pending asynchronous wait
208 * operations against the timer. The handler for each cancelled operation will
209 * be invoked with the boost::asio::error::operation_aborted error code.
211 * Cancelling the timer does not change the expiry time.
213 * @param ec Set to indicate what error occurred, if any.
215 * @return The number of asynchronous operations that were cancelled.
217 std::size_t cancel(boost::system::error_code& ec)
219 return this->service.cancel(this->implementation, ec);
222 /// Get the timer's expiry time as an absolute time.
224 * This function may be used to obtain the timer's current expiry time.
225 * Whether the timer has expired or not does not affect this value.
227 time_type expires_at() const
229 return this->service.expires_at(this->implementation);
232 /// Set the timer's expiry time as an absolute time.
234 * This function sets the expiry time. Any pending asynchronous wait
235 * operations will be cancelled. The handler for each cancelled operation will
236 * be invoked with the boost::asio::error::operation_aborted error code.
238 * @param expiry_time The expiry time to be used for the timer.
240 * @return The number of asynchronous operations that were cancelled.
242 * @throws boost::system::system_error Thrown on failure.
244 std::size_t expires_at(const time_type& expiry_time)
246 boost::system::error_code ec;
247 std::size_t s = this->service.expires_at(
248 this->implementation, expiry_time, ec);
249 boost::asio::detail::throw_error(ec);
250 return s;
253 /// Set the timer's expiry time as an absolute time.
255 * This function sets the expiry time. Any pending asynchronous wait
256 * operations will be cancelled. The handler for each cancelled operation will
257 * be invoked with the boost::asio::error::operation_aborted error code.
259 * @param expiry_time The expiry time to be used for the timer.
261 * @param ec Set to indicate what error occurred, if any.
263 * @return The number of asynchronous operations that were cancelled.
265 std::size_t expires_at(const time_type& expiry_time,
266 boost::system::error_code& ec)
268 return this->service.expires_at(this->implementation, expiry_time, ec);
271 /// Get the timer's expiry time relative to now.
273 * This function may be used to obtain the timer's current expiry time.
274 * Whether the timer has expired or not does not affect this value.
276 duration_type expires_from_now() const
278 return this->service.expires_from_now(this->implementation);
281 /// Set the timer's expiry time relative to now.
283 * This function sets the expiry time. Any pending asynchronous wait
284 * operations will be cancelled. The handler for each cancelled operation will
285 * be invoked with the boost::asio::error::operation_aborted error code.
287 * @param expiry_time The expiry time to be used for the timer.
289 * @return The number of asynchronous operations that were cancelled.
291 * @throws boost::system::system_error Thrown on failure.
293 std::size_t expires_from_now(const duration_type& expiry_time)
295 boost::system::error_code ec;
296 std::size_t s = this->service.expires_from_now(
297 this->implementation, expiry_time, ec);
298 boost::asio::detail::throw_error(ec);
299 return s;
302 /// Set the timer's expiry time relative to now.
304 * This function sets the expiry time. Any pending asynchronous wait
305 * operations will be cancelled. The handler for each cancelled operation will
306 * be invoked with the boost::asio::error::operation_aborted error code.
308 * @param expiry_time The expiry time to be used for the timer.
310 * @param ec Set to indicate what error occurred, if any.
312 * @return The number of asynchronous operations that were cancelled.
314 std::size_t expires_from_now(const duration_type& expiry_time,
315 boost::system::error_code& ec)
317 return this->service.expires_from_now(
318 this->implementation, expiry_time, ec);
321 /// Perform a blocking wait on the timer.
323 * This function is used to wait for the timer to expire. This function
324 * blocks and does not return until the timer has expired.
326 * @throws boost::system::system_error Thrown on failure.
328 void wait()
330 boost::system::error_code ec;
331 this->service.wait(this->implementation, ec);
332 boost::asio::detail::throw_error(ec);
335 /// Perform a blocking wait on the timer.
337 * This function is used to wait for the timer to expire. This function
338 * blocks and does not return until the timer has expired.
340 * @param ec Set to indicate what error occurred, if any.
342 void wait(boost::system::error_code& ec)
344 this->service.wait(this->implementation, ec);
347 /// Start an asynchronous wait on the timer.
349 * This function may be used to initiate an asynchronous wait against the
350 * timer. It always returns immediately.
352 * For each call to async_wait(), the supplied handler will be called exactly
353 * once. The handler will be called when:
355 * @li The timer has expired.
357 * @li The timer was cancelled, in which case the handler is passed the error
358 * code boost::asio::error::operation_aborted.
360 * @param handler The handler to be called when the timer expires. Copies
361 * will be made of the handler as required. The function signature of the
362 * handler must be:
363 * @code void handler(
364 * const boost::system::error_code& error // Result of operation.
365 * ); @endcode
366 * Regardless of whether the asynchronous operation completes immediately or
367 * not, the handler will not be invoked from within this function. Invocation
368 * of the handler will be performed in a manner equivalent to using
369 * boost::asio::io_service::post().
371 template <typename WaitHandler>
372 void async_wait(WaitHandler handler)
374 this->service.async_wait(this->implementation, handler);
378 } // namespace asio
379 } // namespace boost
381 #include <boost/asio/detail/pop_options.hpp>
383 #endif // BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP