Add period query class

This commit is contained in:
Danny Robson 2013-09-20 17:33:08 +10:00
parent 6aa89a740d
commit 5bcc35612c
2 changed files with 32 additions and 0 deletions

View File

@ -92,6 +92,24 @@ delta_clock::seconds (void) {
return (time.curr - time.prev) / static_cast<double> (SECOND); return (time.curr - time.prev) / static_cast<double> (SECOND);
} }
// ----------------------------------------------------------------------------
util::period_query::period_query (double seconds) {
m_time.start = nanoseconds ();
m_time.period = seconds * SECOND;
}
bool
util::period_query::poll (void) {
uint64_t now = nanoseconds ();
uint64_t diff = now - m_time.start;
if (diff < m_time.period)
return false;
m_time.start += diff % m_time.period;
return true;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
util::rate_limiter::rate_limiter (unsigned rate): util::rate_limiter::rate_limiter (unsigned rate):

View File

@ -43,6 +43,20 @@ namespace util {
} time; } time;
}; };
// ------------------------------------------------------------------------
class period_query {
public:
period_query (double seconds);
bool poll (void);
protected:
struct {
uint64_t start;
uint64_t period;
} m_time;
};
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
class rate_limiter { class rate_limiter {
public: public: