#include <moof/hash.hh>
#include <moof/math.hh>
+#include <moof/runloop.hh>
namespace moof {
* 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);
}
/**
*/
void init(const function& function,
scalar seconds,
- mode mode = relative);
+ mode mode = relative,
+ runloop* runloop = runloop::current());
/**
* 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
* 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_;
+ }
/**
* 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;
+ }
/**
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<unsigned,timer*,hash_function> timers_;
+ runloop* runloop_;
};