X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.hh;fp=src%2Fmoof%2Ftimer.hh;h=90c53ceb034452208b6262aeb039938ea650369f;hp=3ec4ef9d8e04bd23a1e6141d4c242e430f6d0d5e;hb=d6990468d297a6cbee98e4d0d33ab37e1b2352c9;hpb=1d4aa0d34b0410c7bc60a24bad7abb55eacc850a diff --git a/src/moof/timer.hh b/src/moof/timer.hh index 3ec4ef9..90c53ce 100644 --- a/src/moof/timer.hh +++ b/src/moof/timer.hh @@ -23,6 +23,7 @@ #include #include +#include namespace moof { @@ -80,9 +81,12 @@ public: * again at that many seconds from the expiration time. A repeating * timer can be invalidated manually using invalidate(). */ - timer(const function& function, scalar seconds, mode mode = relative) + timer(const function& function, + scalar seconds, + mode mode = relative, + runloop* runloop = runloop::current()) { - init(function, seconds, mode); + init(function, seconds, mode, runloop); } /** @@ -114,7 +118,8 @@ public: */ void init(const function& function, scalar seconds, - mode mode = relative); + mode mode = relative, + runloop* runloop = runloop::current()); /** @@ -122,7 +127,10 @@ public: * still scheduled to expired. You can get the time remaining from * seconds_remaining(). */ - bool is_valid() const; + bool is_valid() const + { + return mode_ != invalid; + } /** * Manually invalidated the timer, removing any schedule such that the @@ -137,23 +145,46 @@ public: * prematurely. If the timer is scheduled, it will be invalidated. If * the timer is already invalid (but is initialized with an event * handler), the event will be fired and the timer will remain invalid. + * \param t The absolute time passed to the timer event function. */ - void fire(); + void fire(scalar t = ticks()); + + + /** + * Fire the timer event if it is expired. + * \param t The absolute time used as a reference to determine if the + * timer is expired; defaults to the current time. + * \return The absolute time of the next expiration (if repeating), or + * 0.0 otherwise. + */ + scalar fire_if_expired(scalar t = ticks()) + { + if (is_expired()) fire(); + return absolute_; + } /** * Get the number of seconds remaining before the timer is scheduled to * expired. If the timer is invalid, the retured value will be * negative. + * \param t The absolute time used as a reference to determine the + * amount of time left; defaults to the current time. * \return Seconds. */ - scalar seconds_remaining() const; + scalar seconds_remaining(scalar t = ticks()) const + { + return next_expiration() - t; + } /** * Get the absolute time of the next expiration of this timer. * \return Seconds. */ - scalar next_expiration() const; + scalar next_expiration() const + { + return absolute_; + } /** @@ -162,15 +193,23 @@ public: * expiration time in the future. If the timer is expired but not * invalid, the timer event has not yet fired; the timer will be * invalidated when it does fire. + * \param t The absolute time used as a reference to determine if the + * timer is expired; defaults to the current time. * \return True if the timer is expired, false otherwise. */ - bool is_expired() const; + bool is_expired(scalar t = ticks()) const + { + return seconds_remaining(t) < SCALAR(0.0); + } /** * Get whether or not the timer is on a repeating schedule. * \return True if the timer is repeating, false otherwise. */ - bool is_repeating() const; + bool is_repeating() const + { + return mode_ == repeat; + } /** @@ -195,45 +234,13 @@ public: static void sleep(scalar seconds, mode mode = relative); - /** - * Get the absolute time when the next timer is scheduled to expire. - * \return Absolute time, in seconds. - */ - static scalar next_event() - { - return next_event_; - } - - - /** - * Fire any timers which are not yet invalidated but have an expiration - * time in the past. - */ - static void fire_expired_timers() - { - fire_expired_timers(ticks()); - } - - /** - * Fire any timers which are not yet invalidated but have an expiration - * time before a given absolute time. - */ - static void fire_expired_timers(scalar t); - - private: - static unsigned new_identifier(); - static scalar find_next_event(); - function function_; mode mode_; scalar absolute_; scalar interval_; - unsigned id_; - - static scalar next_event_; - static hash timers_; + runloop* runloop_; };