X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Ftimer.hh;h=90c53ceb034452208b6262aeb039938ea650369f;hp=a86f445d4168a9e2b19073a88593cc4790d9e9a0;hb=d6990468d297a6cbee98e4d0d33ab37e1b2352c9;hpb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c diff --git a/src/moof/timer.hh b/src/moof/timer.hh index a86f445..90c53ce 100644 --- a/src/moof/timer.hh +++ b/src/moof/timer.hh @@ -19,9 +19,11 @@ #include #include +#include #include #include +#include namespace moof { @@ -33,7 +35,7 @@ namespace moof { * runloop associated with the current running view will make an attempt to * fire any expired timers as close to their scheduled times as possible. */ -class timer +class timer : public boost::noncopyable { public: @@ -79,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); } /** @@ -113,7 +118,8 @@ public: */ void init(const function& function, scalar seconds, - mode mode = relative); + mode mode = relative, + runloop* runloop = runloop::current()); /** @@ -121,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 @@ -136,17 +145,47 @@ 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(scalar t = ticks()) const + { + return next_expiration() - t; + } + + /** + * Get the absolute time of the next expiration of this timer. * \return Seconds. */ - scalar seconds_remaining() const; + scalar next_expiration() const + { + return absolute_; + } + /** * Get whether or not the timer is expired. A timer on a repeating @@ -154,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; + } /** @@ -187,42 +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_expiration() - { - return next_expiration_; - } - - - /** - * Fire any timers which are not yet invalidated but have an expiration - * time in the past. - */ - static void fire_expired_timers(); - - /** - * 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_expiration(); - function function_; mode mode_; scalar absolute_; scalar interval_; - unsigned id_; - - static scalar next_expiration_; - static hash timers_; + runloop* runloop_; };