24 using callback_proto = Proto;
29 using result_type = hi_typename function_type::result_type;
33 [[nodiscard]]
constexpr bool empty()
const noexcept
35 return _functions.empty();
48 return x.time_point > time_point;
51 hilet next_to_call = it == _functions.end();
53 auto token = std::make_shared<function_type>(
hi_forward(callback));
67 utc_nanoseconds time_point,
71 return x.time_point > time_point;
74 auto token = std::make_shared<function_type>(
hi_forward(callback));
75 it = _functions.emplace(it, time_point, period, token);
76 return {
std::move(token), it + 1 == _functions.end()};
88 return repeat_function(period, std::chrono::utc_clock::now(),
hi_forward(callback));
97 if (_functions.empty()) {
98 return utc_nanoseconds::max();
100 return _functions.back().time_point;
109 void run_all(utc_nanoseconds current_time,
auto const&...args)
noexcept
111 while (current_deadline() <= current_time) {
112 run_one(current_time, args...);
118 utc_nanoseconds time_point;
120 weak_callback_token token;
122 timer_type() noexcept = default;
123 timer_type(timer_type const&) noexcept = default;
124 timer_type(timer_type&&) noexcept = default;
125 timer_type& operator=(timer_type const&) noexcept = default;
126 timer_type& operator=(timer_type&&) noexcept = default;
128 timer_type(utc_nanoseconds time_point,
std::chrono::nanoseconds period, weak_callback_token token) noexcept :
129 time_point(time_point), period(period), token(
std::move(token))
133 timer_type(utc_nanoseconds time_point, weak_callback_token token) noexcept :
139 timer_type(std::chrono::utc_clock::now(), period,
std::move(token))
143 [[nodiscard]]
constexpr bool repeats() const noexcept
149 void remove_or_reinsert(utc_nanoseconds current_time)
noexcept
153 if (_functions.back().repeats() and not _functions.back().token.expired()) {
155 auto item =
std::move(_functions.back());
156 _functions.pop_back();
160 item.time_point += item.period;
161 if (item.time_point > current_time) {
162 item.time_point = current_time + item.period;
167 return x.time_point > time_point;
173 _functions.pop_back();
184 result_type run_one(utc_nanoseconds current_time,
auto&&...args)
188 if constexpr (std::is_same_v<result_type, void>) {
189 if (
auto token = _functions.back().token.lock()) {
192 remove_or_reinsert(current_time);
195 if (
auto token = _functions.back().token.lock()) {
197 remove_or_reinsert(current_time);
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
std::pair< callback_token, bool > repeat_function(std::chrono::nanoseconds period, utc_nanoseconds time_point, forward_of< callback_proto > auto &&callback) noexcept
Add a function to be called repeatedly.
Definition function_timer.hpp:65
std::pair< callback_token, bool > repeat_function(std::chrono::nanoseconds period, forward_of< callback_proto > auto &&callback) noexcept
Add a function to be called repeatedly.
Definition function_timer.hpp:86
std::pair< callback_token, bool > delay_function(utc_nanoseconds time_point, forward_of< callback_proto > auto &&callback) noexcept
Add a function to be called at a certain time.
Definition function_timer.hpp:45
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:109