3 This file is part of libmspcore
4 Copyright © 2006, 2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_TIME_TIMER_H_
9 #define MSP_TIME_TIMER_H_
12 #include <sigc++/sigc++.h>
13 #include "../core/mutex.h"
14 #include "../core/semaphore.h"
15 #include "timedelta.h"
16 #include "timestamp.h"
22 A class for executing functions in a deferred or periodical fashion. The add a
23 timer, use one of the add functions and connect a functor to the timeout signal
26 This class is thread-safe, to allow running timers in a separate thread.
34 sigc::signal<bool> signal_timeout;
41 Slot(const TimeDelta &);
42 Slot(const TimeStamp &);
43 const TimeStamp &get_timeout() const { return timeout; }
53 bool operator<(const SlotProxy &) const;
56 std::vector<SlotProxy> slots;
64 Adds a timer that will be executed periodically as long as the timeout
65 signal hander returns true.
67 Slot &add(const TimeDelta &);
70 Adds a timer that will be executed once at a specific time. The return
71 value of the timeout signal handler is ignored.
73 Slot &add(const TimeStamp &);
76 Cancels a previously added timer.
81 Checks all timers, executing any that have timed out. If block is true,
82 waits until one times out.
84 Note: If there are no active timers when a blocking tick is executed, it
85 won't return until a timer is added from another thread.
87 void tick(bool block = true);
89 TimeStamp get_next_timeout() const;