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; }
52 bool operator<(const SlotProxy &) const;
55 std::set<SlotProxy> slots;
63 Adds a timer that will be executed periodically as long as the timeout
64 signal hander returns true.
66 Slot &add(const TimeDelta &);
69 Adds a timer that will be executed once at a specific time. The return
70 value of the timeout signal handler is ignored.
72 Slot &add(const TimeStamp &);
75 Cancels a previously added timer.
80 Checks all timers, executing any that have timed out. If block is true,
81 waits until one times out.
83 Note: If there are no active timers when a blocking tick is executed, it
84 won't return until a timer is added from another thread.
86 void tick(bool block=true);
88 TimeStamp get_next_timeout() const;