26 [[nodiscard]]
constexpr bool empty()
const noexcept
28 return _functions.empty();
37 template<forward_of<
void()> Func>
40 auto const it =
std::lower_bound(_functions.begin(), _functions.end(), time_point, [](
auto const& x,
auto const& time_point) {
41 return x.time_point > time_point;
44 auto const next_to_call = it == _functions.end();
46 auto token = callback<void()>{std::forward<Func>(func)};
58 template<forward_of<
void()> Func>
61 utc_nanoseconds time_point,
64 auto it =
std::lower_bound(_functions.begin(), _functions.end(), time_point, [](
auto const& x,
auto const& time_point) {
65 return x.time_point > time_point;
68 auto token = callback<void()>{std::forward<Func>(func)};
69 it = _functions.emplace(it, time_point, period, token);
70 return {
std::move(token), it + 1 == _functions.end()};
79 template<forward_of<
void()> Func>
82 return repeat_function(period, std::chrono::utc_clock::now(), std::forward<Func>(func));
91 if (_functions.empty()) {
92 return utc_nanoseconds::max();
94 return _functions.back().time_point;
103 void run_all(utc_nanoseconds current_time,
auto const&...args)
noexcept
105 while (current_deadline() <= current_time) {
106 run_one(current_time, args...);
112 utc_nanoseconds time_point;
114 weak_callback<void()> callback;
116 timer_type() noexcept = default;
117 timer_type(timer_type const&) noexcept = default;
118 timer_type(timer_type&&) noexcept = default;
119 timer_type& operator=(timer_type const&) noexcept = default;
120 timer_type& operator=(timer_type&&) noexcept = default;
122 timer_type(utc_nanoseconds time_point,
std::chrono::nanoseconds period, weak_callback<
void()> callback) noexcept :
123 time_point(time_point), period(period), callback(
std::move(callback))
127 timer_type(utc_nanoseconds time_point, weak_callback<
void()> callback) noexcept :
133 timer_type(std::chrono::utc_clock::now(), period,
std::move(callback))
137 [[nodiscard]]
constexpr bool repeats() const noexcept
143 void remove_or_reinsert(utc_nanoseconds current_time)
noexcept
145 hi_assert(not _functions.empty());
147 if (_functions.back().repeats() and not _functions.back().callback.expired()) {
149 auto item =
std::move(_functions.back());
150 _functions.pop_back();
154 item.time_point += item.period;
155 if (item.time_point > current_time) {
156 item.time_point = current_time + item.period;
160 auto const it =
std::lower_bound(_functions.begin(), _functions.end(), item.time_point, [](
auto const& x,
auto const& time_point) {
161 return x.time_point > time_point;
167 _functions.pop_back();
176 void run_one(utc_nanoseconds current_time)
178 hi_assert(not _functions.empty());
180 auto &callback = _functions.back().callback;
181 if (
auto cb = callback.lock()) {
184 remove_or_reinsert(current_time);
void run_all(utc_nanoseconds current_time, auto const &...args) noexcept
Run all the function that should have run by the current_time.
Definition function_timer.hpp:103
std::pair< callback< void()>, bool > delay_function(utc_nanoseconds time_point, Func &&func) noexcept
Add a function to be called at a certain time.
Definition function_timer.hpp:38
std::pair< callback< void()>, bool > repeat_function(std::chrono::nanoseconds period, Func &&func) noexcept
Add a function to be called repeatedly.
Definition function_timer.hpp:80
std::pair< callback< void()>, bool > repeat_function(std::chrono::nanoseconds period, utc_nanoseconds time_point, Func &&func) noexcept
Add a function to be called repeatedly.
Definition function_timer.hpp:59