28 using result_type = hi_typename function_type::result_type;
32 [[nodiscard]]
constexpr bool empty()
const noexcept
34 return _functions.empty();
46 return x.time_point > time_point;
49 hilet next_to_call = it == _functions.end();
51 auto token = std::make_shared<function_type>(
hi_forward(func));
66 return x.time_point > time_point;
69 auto token = std::make_shared<function_type>(
hi_forward(func));
70 it = _functions.emplace(it, time_point, period, token);
71 return {
std::move(token), it + 1 == _functions.end()};
83 return repeat_function(period, std::chrono::utc_clock::now(),
hi_forward(func));
92 if (_functions.empty()) {
93 return utc_nanoseconds::max();
95 return _functions.back().time_point;
104 void run_all(utc_nanoseconds current_time,
auto&&...args)
noexcept
106 while (current_deadline() <= current_time) {
107 run_one(current_time, args...);
113 utc_nanoseconds time_point;
115 weak_token_type token;
117 timer_type() noexcept = default;
118 timer_type(timer_type const&) noexcept = default;
119 timer_type(timer_type&&) noexcept = default;
120 timer_type& operator=(timer_type const&) noexcept = default;
121 timer_type& operator=(timer_type&&) noexcept = default;
123 timer_type(utc_nanoseconds time_point,
std::chrono::nanoseconds period, weak_token_type token) noexcept :
124 time_point(time_point), period(period), token(
std::move(token))
128 timer_type(utc_nanoseconds time_point, weak_token_type token) noexcept :
134 timer_type(std::chrono::utc_clock::now(), period,
std::move(token))
138 [[nodiscard]]
constexpr bool repeats() const noexcept
144 void remove_or_reinsert(utc_nanoseconds current_time)
noexcept
146 hi_axiom(not _functions.empty());
148 if (_functions.back().repeats() and not _functions.back().token.expired()) {
150 auto item =
std::move(_functions.back());
151 _functions.pop_back();
155 item.time_point += item.period;
156 if (item.time_point > current_time) {
157 item.time_point = current_time + item.period;
162 return x.time_point > time_point;
168 _functions.pop_back();
179 result_type run_one(utc_nanoseconds current_time,
auto&&...args)
181 hi_axiom(not _functions.empty());
183 if constexpr (std::is_same_v<result_type, void>) {
184 if (
auto token = _functions.back().token.lock()) {
187 remove_or_reinsert(current_time);
190 if (
auto token = _functions.back().token.lock()) {
192 remove_or_reinsert(current_time);
std::pair< token_type, bool > repeat_function(std::chrono::nanoseconds period, utc_nanoseconds time_point, auto &&func) noexcept
Add a function to be called repeatedly.
Definition function_timer.hpp:63
std::pair< token_type, bool > delay_function(utc_nanoseconds time_point, auto &&func) noexcept
Add a function to be called at a certain time.
Definition function_timer.hpp:43
std::pair< token_type, bool > repeat_function(std::chrono::nanoseconds period, auto &&func) noexcept
Add a function to be called repeatedly.
Definition function_timer.hpp:81
void run_all(utc_nanoseconds current_time, auto &&...args) noexcept
Run all the function that should have run by the current_time.
Definition function_timer.hpp:104