steady_timer.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // Copyright (c) 2025 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_IO_STEADY_TIMER_HPP
  8. #define BOOST_COBALT_IO_STEADY_TIMER_HPP
  9. #include <boost/cobalt/io/detail/config.hpp>
  10. #include <boost/cobalt/op.hpp>
  11. #include <boost/cobalt/io/ops.hpp>
  12. #include <boost/asio/basic_waitable_timer.hpp>
  13. #include <boost/system/result.hpp>
  14. namespace boost::cobalt::io
  15. {
  16. struct BOOST_SYMBOL_VISIBLE steady_timer
  17. {
  18. /// The clock type.
  19. typedef std::chrono::steady_clock clock_type;
  20. /// The duration type of the clock.
  21. typedef typename clock_type::duration duration;
  22. /// The time point type of the clock.
  23. typedef typename clock_type::time_point time_point;
  24. BOOST_COBALT_IO_DECL steady_timer(const cobalt::executor & executor = this_thread::get_executor());
  25. BOOST_COBALT_IO_DECL steady_timer(const time_point& expiry_time, const cobalt::executor & executor = this_thread::get_executor());
  26. BOOST_COBALT_IO_DECL steady_timer(const duration& expiry_time, const cobalt::executor & executor = this_thread::get_executor());
  27. void cancel();
  28. BOOST_COBALT_IO_DECL time_point expiry() const;
  29. BOOST_COBALT_IO_DECL void reset(const time_point& expiry_time);
  30. BOOST_COBALT_IO_DECL void reset(const duration& expiry_time);
  31. BOOST_COBALT_IO_DECL bool expired() const;
  32. [[nodiscard]] wait_op wait() { return {this, initiate_wait_, try_wait_}; }
  33. private:
  34. BOOST_COBALT_IO_DECL static void initiate_wait_(void *, boost::cobalt::completion_handler<system::error_code>);
  35. BOOST_COBALT_IO_DECL static void try_wait_(void *, boost::cobalt::handler<system::error_code>);
  36. asio::basic_waitable_timer<std::chrono::steady_clock,
  37. asio::wait_traits<std::chrono::steady_clock>,
  38. executor> timer_;
  39. };
  40. }
  41. #endif //BOOST_COBALT_IO_STEADY_TIMER_HPP