11 static_assert(
sizeof(std::chrono::microseconds::rep) >= 8,
"intervals need a 64-bit microsecond count");
14 constexpr std::chrono::microseconds
MAX_INTERVAL = std::chrono::hours(24 * 36525);
43 : updateInterval(
Timing::checked(interval)),
44 lastUpdateTime(
Timing::steadyNow())
50 [[deprecated(
"use Timing(std::chrono::microseconds) instead")]]
52 : updateInterval(static_cast<int64_t>(frequency)),
53 lastUpdateTime(
Timing::steadyNow())
62 if(this->updateInterval == 0)
65 int64_t current_time = now.count();
66 int64_t elapsed = current_time - this->lastUpdateTime;
70 if(elapsed >= this->updateInterval)
74 int64_t intervals = elapsed / this->updateInterval;
79 this->lastUpdateTime = current_time;
81 this->lastUpdateTime += intervals * this->updateInterval;
91 return this->
ShouldUpdate(std::chrono::microseconds(Timing::steadyNow()));
95 void Restart(std::chrono::microseconds now)
97 this->lastUpdateTime = now.count();
104 this->updateInterval = Timing::checked(interval);
110 return std::chrono::microseconds(this->updateInterval);
115 [[deprecated(
"use SetInterval(std::chrono::microseconds) instead")]]
118 this->updateInterval =
static_cast<int64_t
>(frequency);
124 [[deprecated(
"use GetInterval() instead")]]
127 return static_cast<uint32_t
>(std::min<int64_t>(this->updateInterval, UINT32_MAX));
131 int64_t updateInterval;
132 int64_t lastUpdateTime;
134 static int64_t checked(std::chrono::microseconds interval)
138 throw std::runtime_error(
"ecs::Timing: interval " + std::to_string(interval.count()) +
139 " us is outside 0 to " + std::to_string(
MAX_INTERVAL.count()) +
" us.");
141 return interval.count();
144 static int64_t steadyNow()
146 using namespace std::chrono;
147 return duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count();
A repeating schedule: says when the next update is due.
Definition Timing.hpp:38
void SetFrequency(uint32_t frequency)
Changes the interval to a bare number of microseconds, without a range check.
Definition Timing.hpp:116
bool ShouldUpdate()
Says whether an update is due now by the real steady clock and, when it is, moves the schedule on.
Definition Timing.hpp:89
std::chrono::microseconds GetInterval() const
Returns the interval between updates.
Definition Timing.hpp:108
uint32_t GetFrequency() const
Returns the interval in microseconds, or 4 294 967 295 if it is longer than that number can hold.
Definition Timing.hpp:125
void Restart(std::chrono::microseconds now)
Starts a new schedule at the given instant: the first update is due one full interval later.
Definition Timing.hpp:95
Timing(std::chrono::microseconds interval=DEFAULT_INTERVAL)
Creates a schedule with the given interval, first due one full interval from now.
Definition Timing.hpp:42
Timing(uint32_t frequency)
Creates a schedule from a bare number, which is the interval in microseconds.
Definition Timing.hpp:51
void SetInterval(std::chrono::microseconds interval)
Changes the interval.
Definition Timing.hpp:102
bool ShouldUpdate(std::chrono::microseconds now)
Says whether an update is due at the given instant and, when it is, moves the schedule on.
Definition Timing.hpp:60
constexpr std::chrono::microseconds MAX_INTERVAL
The longest interval or timer length that is accepted: 100 years of 365.25 days.
Definition Timing.hpp:14
constexpr uint32_t MAX_CATCHUP
Maximum number of intervals to catch up after a stall.
Definition Timing.hpp:21
constexpr std::chrono::microseconds DEFAULT_INTERVAL
The interval of a new schedule: 33 333 microseconds, about 30 updates a second.
Definition Timing.hpp:17