Clementine
basic_waitable_timer.hpp
1 //
2 // basic_waitable_timer.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 ASIO_BASIC_WAITABLE_TIMER_HPP
12 #define ASIO_BASIC_WAITABLE_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 "asio/detail/config.hpp"
19 #include <cstddef>
20 #include "asio/any_io_executor.hpp"
21 #include "asio/detail/chrono_time_traits.hpp"
22 #include "asio/detail/deadline_timer_service.hpp"
23 #include "asio/detail/handler_type_requirements.hpp"
24 #include "asio/detail/io_object_impl.hpp"
25 #include "asio/detail/non_const_lvalue.hpp"
26 #include "asio/detail/throw_error.hpp"
27 #include "asio/error.hpp"
28 #include "asio/wait_traits.hpp"
29 
30 #if defined(ASIO_HAS_MOVE)
31 # include <utility>
32 #endif // defined(ASIO_HAS_MOVE)
33 
34 #include "asio/detail/push_options.hpp"
35 
36 namespace asio {
37 
38 #if !defined(ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
39 #define ASIO_BASIC_WAITABLE_TIMER_FWD_DECL
40 
41 // Forward declaration with defaulted arguments.
42 template <typename Clock,
43  typename WaitTraits = asio::wait_traits<Clock>,
44  typename Executor = any_io_executor>
46 
47 #endif // !defined(ASIO_BASIC_WAITABLE_TIMER_FWD_DECL)
48 
50 
141 template <typename Clock, typename WaitTraits, typename Executor>
143 {
144 public:
146  typedef Executor executor_type;
147 
149  template <typename Executor1>
151  {
154  };
155 
157  typedef Clock clock_type;
158 
160  typedef typename clock_type::duration duration;
161 
163  typedef typename clock_type::time_point time_point;
164 
166  typedef WaitTraits traits_type;
167 
169 
177  explicit basic_waitable_timer(const executor_type& ex)
178  : impl_(ex)
179  {
180  }
181 
183 
192  template <typename ExecutionContext>
193  explicit basic_waitable_timer(ExecutionContext& context,
194  typename enable_if<
195  is_convertible<ExecutionContext&, execution_context&>::value
196  >::type* = 0)
197  : impl_(context)
198  {
199  }
200 
202 
211  basic_waitable_timer(const executor_type& ex, const time_point& expiry_time)
212  : impl_(ex)
213  {
214  asio::error_code ec;
215  impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
216  asio::detail::throw_error(ec, "expires_at");
217  }
218 
220 
230  template <typename ExecutionContext>
231  explicit basic_waitable_timer(ExecutionContext& context,
232  const time_point& expiry_time,
233  typename enable_if<
234  is_convertible<ExecutionContext&, execution_context&>::value
235  >::type* = 0)
236  : impl_(context)
237  {
238  asio::error_code ec;
239  impl_.get_service().expires_at(impl_.get_implementation(), expiry_time, ec);
240  asio::detail::throw_error(ec, "expires_at");
241  }
242 
244 
253  basic_waitable_timer(const executor_type& ex, const duration& expiry_time)
254  : impl_(ex)
255  {
256  asio::error_code ec;
257  impl_.get_service().expires_after(
258  impl_.get_implementation(), expiry_time, ec);
259  asio::detail::throw_error(ec, "expires_after");
260  }
261 
263 
273  template <typename ExecutionContext>
274  explicit basic_waitable_timer(ExecutionContext& context,
275  const duration& expiry_time,
276  typename enable_if<
277  is_convertible<ExecutionContext&, execution_context&>::value
278  >::type* = 0)
279  : impl_(context)
280  {
281  asio::error_code ec;
282  impl_.get_service().expires_after(
283  impl_.get_implementation(), expiry_time, ec);
284  asio::detail::throw_error(ec, "expires_after");
285  }
286 
287 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
288 
300  : impl_(std::move(other.impl_))
301  {
302  }
303 
305 
317  {
318  impl_ = std::move(other.impl_);
319  return *this;
320  }
321 
322  // All timers have access to each other's implementations.
323  template <typename Clock1, typename WaitTraits1, typename Executor1>
324  friend class basic_waitable_timer;
325 
327 
337  template <typename Executor1>
340  typename enable_if<
341  is_convertible<Executor1, Executor>::value
342  >::type* = 0)
343  : impl_(std::move(other.impl_))
344  {
345  }
346 
348 
359  template <typename Executor1>
360  typename enable_if<
361  is_convertible<Executor1, Executor>::value,
364  {
365  basic_waitable_timer tmp(std::move(other));
366  impl_ = std::move(tmp.impl_);
367  return *this;
368  }
369 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
370 
372 
377  {
378  }
379 
381  executor_type get_executor() ASIO_NOEXCEPT
382  {
383  return impl_.get_executor();
384  }
385 
387 
408  std::size_t cancel()
409  {
410  asio::error_code ec;
411  std::size_t s = impl_.get_service().cancel(impl_.get_implementation(), ec);
412  asio::detail::throw_error(ec, "cancel");
413  return s;
414  }
415 
416 #if !defined(ASIO_NO_DEPRECATED)
417 
440  std::size_t cancel(asio::error_code& ec)
441  {
442  return impl_.get_service().cancel(impl_.get_implementation(), ec);
443  }
444 #endif // !defined(ASIO_NO_DEPRECATED)
445 
447 
470  std::size_t cancel_one()
471  {
472  asio::error_code ec;
473  std::size_t s = impl_.get_service().cancel_one(
474  impl_.get_implementation(), ec);
475  asio::detail::throw_error(ec, "cancel_one");
476  return s;
477  }
478 
479 #if !defined(ASIO_NO_DEPRECATED)
480 
505  std::size_t cancel_one(asio::error_code& ec)
506  {
507  return impl_.get_service().cancel_one(impl_.get_implementation(), ec);
508  }
509 
512 
516  time_point expires_at() const
517  {
518  return impl_.get_service().expires_at(impl_.get_implementation());
519  }
520 #endif // !defined(ASIO_NO_DEPRECATED)
521 
523 
527  time_point expiry() const
528  {
529  return impl_.get_service().expiry(impl_.get_implementation());
530  }
531 
533 
554  std::size_t expires_at(const time_point& expiry_time)
555  {
556  asio::error_code ec;
557  std::size_t s = impl_.get_service().expires_at(
558  impl_.get_implementation(), expiry_time, ec);
559  asio::detail::throw_error(ec, "expires_at");
560  return s;
561  }
562 
563 #if !defined(ASIO_NO_DEPRECATED)
564 
587  std::size_t expires_at(const time_point& expiry_time,
588  asio::error_code& ec)
589  {
590  return impl_.get_service().expires_at(
591  impl_.get_implementation(), expiry_time, ec);
592  }
593 #endif // !defined(ASIO_NO_DEPRECATED)
594 
596 
617  std::size_t expires_after(const duration& expiry_time)
618  {
619  asio::error_code ec;
620  std::size_t s = impl_.get_service().expires_after(
621  impl_.get_implementation(), expiry_time, ec);
622  asio::detail::throw_error(ec, "expires_after");
623  return s;
624  }
625 
626 #if !defined(ASIO_NO_DEPRECATED)
627 
632  duration expires_from_now() const
633  {
634  return impl_.get_service().expires_from_now(impl_.get_implementation());
635  }
636 
639 
660  std::size_t expires_from_now(const duration& expiry_time)
661  {
662  asio::error_code ec;
663  std::size_t s = impl_.get_service().expires_from_now(
664  impl_.get_implementation(), expiry_time, ec);
665  asio::detail::throw_error(ec, "expires_from_now");
666  return s;
667  }
668 
671 
692  std::size_t expires_from_now(const duration& expiry_time,
693  asio::error_code& ec)
694  {
695  return impl_.get_service().expires_from_now(
696  impl_.get_implementation(), expiry_time, ec);
697  }
698 #endif // !defined(ASIO_NO_DEPRECATED)
699 
701 
707  void wait()
708  {
709  asio::error_code ec;
710  impl_.get_service().wait(impl_.get_implementation(), ec);
711  asio::detail::throw_error(ec, "wait");
712  }
713 
715 
722  {
723  impl_.get_service().wait(impl_.get_implementation(), ec);
724  }
725 
727 
750  template <
751  ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code))
752  WaitHandler ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
754  void (asio::error_code))
755  async_wait(
756  ASIO_MOVE_ARG(WaitHandler) handler
757  ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
758  {
759  return async_initiate<WaitHandler, void (asio::error_code)>(
760  initiate_async_wait(this), handler);
761  }
762 
763 private:
764  // Disallow copying and assignment.
765  basic_waitable_timer(const basic_waitable_timer&) ASIO_DELETED;
766  basic_waitable_timer& operator=(
767  const basic_waitable_timer&) ASIO_DELETED;
768 
769  class initiate_async_wait
770  {
771  public:
772  typedef Executor executor_type;
773 
774  explicit initiate_async_wait(basic_waitable_timer* self)
775  : self_(self)
776  {
777  }
778 
779  executor_type get_executor() const ASIO_NOEXCEPT
780  {
781  return self_->get_executor();
782  }
783 
784  template <typename WaitHandler>
785  void operator()(ASIO_MOVE_ARG(WaitHandler) handler) const
786  {
787  // If you get an error on the following line it means that your handler
788  // does not meet the documented type requirements for a WaitHandler.
789  ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
790 
791  detail::non_const_lvalue<WaitHandler> handler2(handler);
792  self_->impl_.get_service().async_wait(
793  self_->impl_.get_implementation(),
794  handler2.value, self_->impl_.get_executor());
795  }
796 
797  private:
798  basic_waitable_timer* self_;
799  };
800 
804  executor_type > impl_;
805 };
806 
807 } // namespace asio
808 
809 #include "asio/detail/pop_options.hpp"
810 
811 #endif // ASIO_BASIC_WAITABLE_TIMER_HPP
std::size_t cancel_one(asio::error_code &ec)
(Deprecated: Use non-error_code overload.) Cancels one asynchronous operation that is waiting on the ...
Definition: basic_waitable_timer.hpp:505
clock_type::duration duration
The duration type of the clock.
Definition: basic_waitable_timer.hpp:160
std::size_t expires_from_now(const duration &expiry_time)
(Deprecated: Use expires_after().) Set the timer&#39;s expiry time relative to now.
Definition: basic_waitable_timer.hpp:660
basic_waitable_timer(ExecutionContext &context, const time_point &expiry_time, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Constructor to set a particular expiry time as an absolute time.
Definition: basic_waitable_timer.hpp:231
Definition: deadline_timer_service.hpp:45
std::size_t cancel()
Cancel any asynchronous operations that are waiting on the timer.
Definition: basic_waitable_timer.hpp:408
std::size_t expires_from_now(const duration &expiry_time, asio::error_code &ec)
(Deprecated: Use expires_after().) Set the timer&#39;s expiry time relative to now.
Definition: basic_waitable_timer.hpp:692
Definition: blocking.hpp:208
Definition: chrono_time_traits.hpp:34
Rebinds the timer type to another executor.
Definition: basic_waitable_timer.hpp:150
void wait()
Perform a blocking wait on the timer.
Definition: basic_waitable_timer.hpp:707
time_point expiry() const
Get the timer&#39;s expiry time as an absolute time.
Definition: basic_waitable_timer.hpp:527
basic_waitable_timer(ExecutionContext &context, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Constructor.
Definition: basic_waitable_timer.hpp:193
basic_waitable_timer(ExecutionContext &context, const duration &expiry_time, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Constructor to set a particular expiry time relative to now.
Definition: basic_waitable_timer.hpp:274
basic_waitable_timer< Clock, WaitTraits, Executor1 > other
The timer type when rebound to the specified executor.
Definition: basic_waitable_timer.hpp:153
time_point expires_at() const
(Deprecated: Use expiry().) Get the timer&#39;s expiry time as an absolute time.
Definition: basic_waitable_timer.hpp:516
std::size_t cancel_one()
Cancels one asynchronous operation that is waiting on the timer.
Definition: basic_waitable_timer.hpp:470
Definition: type_traits.hpp:97
Definition: non_const_lvalue.hpp:27
WaitTraits traits_type
The wait traits type.
Definition: basic_waitable_timer.hpp:166
Clock clock_type
The clock type.
Definition: basic_waitable_timer.hpp:157
executor_type get_executor() ASIO_NOEXCEPT
Get the executor associated with the object.
Definition: basic_waitable_timer.hpp:381
clock_type::time_point time_point
The time point type of the clock.
Definition: basic_waitable_timer.hpp:163
Wait traits suitable for use with the basic_waitable_timer class template.
Definition: netfwd.hpp:111
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: io_object_impl.hpp:33
std::size_t cancel(asio::error_code &ec)
(Deprecated: Use non-error_code overload.) Cancel any asynchronous operations that are waiting on the...
Definition: basic_waitable_timer.hpp:440
Executor executor_type
The type of the executor associated with the object.
Definition: basic_waitable_timer.hpp:146
basic_waitable_timer(const executor_type &ex)
Constructor.
Definition: basic_waitable_timer.hpp:177
Provides waitable timer functionality.
Definition: basic_waitable_timer.hpp:45
ASIO_INITFN_AUTO_RESULT_TYPE(WaitHandler, void(asio::error_code)) async_wait(ASIO_MOVE_ARG(WaitHandler) handler ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
Start an asynchronous wait on the timer.
Definition: basic_waitable_timer.hpp:753
std::size_t expires_at(const time_point &expiry_time)
Set the timer&#39;s expiry time as an absolute time.
Definition: basic_waitable_timer.hpp:554
std::size_t expires_after(const duration &expiry_time)
Set the timer&#39;s expiry time relative to now.
Definition: basic_waitable_timer.hpp:617
basic_waitable_timer(const executor_type &ex, const time_point &expiry_time)
Constructor to set a particular expiry time as an absolute time.
Definition: basic_waitable_timer.hpp:211
Definition: any_io_executor.hpp:28
~basic_waitable_timer()
Destroys the timer.
Definition: basic_waitable_timer.hpp:376
void wait(asio::error_code &ec)
Perform a blocking wait on the timer.
Definition: basic_waitable_timer.hpp:721
basic_waitable_timer(const executor_type &ex, const duration &expiry_time)
Constructor to set a particular expiry time relative to now.
Definition: basic_waitable_timer.hpp:253
duration expires_from_now() const
(Deprecated: Use expiry().) Get the timer&#39;s expiry time relative to now.
Definition: basic_waitable_timer.hpp:632
std::size_t expires_at(const time_point &expiry_time, asio::error_code &ec)
(Deprecated: Use non-error_code overload.) Set the timer&#39;s expiry time as an absolute time...
Definition: basic_waitable_timer.hpp:587