2 This file is part of libmspcore
3 Copyright © 2006 Mikko Rasa, Mikkosoft Productions
4 Distributed under the LGPL
7 #ifndef MSP_TIME_TIMER_H_
8 #define MSP_TIME_TIMER_H_
11 #include <sigc++/sigc++.h>
12 #include "../core/mutex.h"
13 #include "../core/semaphore.h"
14 #include "timedelta.h"
15 #include "timestamp.h"
21 A class for executing functions in a deferred or periodical fashion. The add a
22 timer, use one of the add functions and connect a functor to the timeout signal
25 This class is thread-safe, to allow running timers in a separate thread.
33 sigc::signal<bool> signal_timeout;
40 Slot(const TimeDelta &);
41 Slot(const TimeStamp &);
42 const TimeStamp &get_timeout() const { return timeout; }
44 bool operator<(const Slot &) const;
48 typedef bool (*fSlotCompare)(Slot *, Slot *);
50 std::priority_queue<Slot *, std::vector<Slot *>, fSlotCompare> slots;
59 Adds a timer that will be executed periodically as long as the timeout
60 signal hander returns true.
62 Slot &add(const TimeDelta &);
65 Adds a timer that will be executed once at a specific time. The return
66 value of the timeout signal handler is ignored.
68 Slot &add(const TimeStamp &);
71 Checks all timers, executing any that have timed out. If block is true,
72 waits until one times out.
74 Note: If there are no active timers when a blocking tick is executed, it
75 won't return until a timer is added from another thread.
77 void tick(bool block=true);
79 static bool slot_compare(Slot *, Slot *);