From 5bcc35612c2e636ca2e0bb9a7c3c875d3d63250a Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 20 Sep 2013 17:33:08 +1000 Subject: [PATCH] Add period query class --- time.cpp | 18 ++++++++++++++++++ time.hpp | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/time.cpp b/time.cpp index a96bfd3e..899659e7 100644 --- a/time.cpp +++ b/time.cpp @@ -92,6 +92,24 @@ delta_clock::seconds (void) { return (time.curr - time.prev) / static_cast (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): diff --git a/time.hpp b/time.hpp index 244d9a15..22105e18 100644 --- a/time.hpp +++ b/time.hpp @@ -43,6 +43,20 @@ namespace util { } 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 { public: