27 using callback_proto = Proto;
32 using result_type = function_type::result_type;
36 [[nodiscard]]
constexpr bool empty()
const noexcept
38 return _functions.empty();
48 delay_function(utc_nanoseconds time_point, forward_of<callback_proto>
auto&& callback)
noexcept
50 hilet it =
std::lower_bound(_functions.begin(), _functions.end(), time_point, [](hilet& x, hilet& time_point) {
51 return x.time_point > time_point;
54 hilet next_to_call = it == _functions.end();
56 auto token = std::make_shared<function_type>(hi_forward(callback));
70 utc_nanoseconds time_point,
71 forward_of<callback_proto>
auto&& callback)
noexcept
73 auto it =
std::lower_bound(_functions.begin(), _functions.end(), time_point, [](hilet& x, hilet& time_point) {
74 return x.time_point > time_point;
77 auto token = std::make_shared<function_type>(hi_forward(callback));
78 it = _functions.emplace(it, time_point, period, token);
79 return {
std::move(token), it + 1 == _functions.end()};
91 return repeat_function(period, std::chrono::utc_clock::now(), hi_forward(callback));
100 if (_functions.empty()) {
101 return utc_nanoseconds::max();
103 return _functions.back().time_point;
112 void run_all(utc_nanoseconds current_time,
auto const&...args)
noexcept
114 while (current_deadline() <= current_time) {
115 run_one(current_time, args...);
121 utc_nanoseconds time_point;
123 weak_callback_token token;
125 timer_type() noexcept = default;
126 timer_type(timer_type const&) noexcept = default;
127 timer_type(timer_type&&) noexcept = default;
128 timer_type& operator=(timer_type const&) noexcept = default;
129 timer_type& operator=(timer_type&&) noexcept = default;
131 timer_type(utc_nanoseconds time_point,
std::chrono::nanoseconds period, weak_callback_token token) noexcept :
132 time_point(time_point), period(period), token(
std::move(token))
136 timer_type(utc_nanoseconds time_point, weak_callback_token token) noexcept :
142 timer_type(std::chrono::utc_clock::now(), period,
std::move(token))
146 [[nodiscard]]
constexpr bool repeats() const noexcept
152 void remove_or_reinsert(utc_nanoseconds current_time)
noexcept
154 hi_assert(not _functions.empty());
156 if (_functions.back().repeats() and not _functions.back().token.expired()) {
158 auto item =
std::move(_functions.back());
159 _functions.pop_back();
163 item.time_point += item.period;
164 if (item.time_point > current_time) {
165 item.time_point = current_time + item.period;
169 hilet it =
std::lower_bound(_functions.begin(), _functions.end(), item.time_point, [](hilet& x, hilet& time_point) {
170 return x.time_point > time_point;
176 _functions.pop_back();
187 result_type run_one(utc_nanoseconds current_time,
auto&&...args)
189 hi_assert(not _functions.empty());
191 if constexpr (std::is_same_v<result_type, void>) {
192 if (
auto token = _functions.back().token.lock()) {
193 (*token)(hi_forward(args)...);
195 remove_or_reinsert(current_time);
198 if (
auto token = _functions.back().token.lock()) {
199 auto result = (*token)(hi_forward(args)...);
200 remove_or_reinsert(current_time);
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:68
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:89
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:48
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:112