]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
Someone forgot to make correct deps for timer.h. Fixed.
[user/henk/code/inspircd.git] / include / timer.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef INSPIRCD_TIMER_H
15 #define INSPIRCD_TIMER_H
16
17 /** Timer class for one-second resolution timers
18  * InspTimer provides a facility which allows module
19  * developers to create one-shot timers. The timer
20  * can be made to trigger at any time up to a one-second
21  * resolution. To use InspTimer, inherit a class from
22  * InspTimer, then insert your inherited class into the
23  * queue using Server::AddTimer(). The Tick() method of
24  * your object (which you should override) will be called
25  * at the given time.
26  */
27 class InspTimer : public Extensible
28 {
29  private:
30         /** The triggering time
31          */
32         time_t trigger;
33         long secs;
34         bool repeat;
35  public:
36         /** Default constructor, initializes the triggering time
37          * @param secs_from_now The number of seconds from now to trigger the timer
38          * @param now The time now
39          * @param repeating Repeat this timer every secs_from_now seconds if set to true
40          */
41         InspTimer(long secs_from_now,time_t now, bool repeating = false)
42         {
43                 trigger = now + secs_from_now;
44                 secs = secs_from_now;
45                 repeat = repeating;
46         }
47         /** Default destructor, does nothing.
48          */
49         virtual ~InspTimer() { }
50         /** Retrieve the current triggering time
51          */
52         virtual time_t GetTimer()
53         {
54                 return trigger;
55         }
56         /** Called when the timer ticks.
57          */
58         virtual void Tick(time_t TIME) = 0;
59
60         bool GetRepeat()
61         {
62                 return repeat;
63         }
64
65         long GetSecs()
66         {
67                 return secs;
68         }
69
70         void CancelRepeat()
71         {
72                 repeat = false;
73         }
74 };
75
76
77 /** This class manages sets of InspTimers, and triggers them at their defined times.
78  * This will ensure timers are not missed, as well as removing timers that have
79  * expired and allowing the addition of new ones.
80  */
81 class TimerManager : public Extensible
82 {
83  protected:
84         /** A group of timers all set to trigger at the same time
85          */
86         typedef std::vector<InspTimer*> timergroup;
87         /** A map of timergroups, each group has a specific trigger time
88          */
89         typedef std::map<time_t, timergroup*> timerlist;
90
91  private:
92
93         /** The current timer set, a map of timergroups
94          */
95         timerlist Timers;
96
97  public:
98         /** Tick all pending InspTimers
99          * @param TIME the current system time
100          */
101         void TickTimers(time_t TIME);
102         /** Add an InspTimer
103          * @param T an InspTimer derived class to add
104          * @param secs_from_now You may set this to the number of seconds
105          * from the current time when the timer will tick, or you may just
106          * leave this unset and the values set by the InspTimers constructor
107          * will be used. This is used internally for re-triggering repeating
108          * timers.
109          */
110         void AddTimer(InspTimer* T, long secs_from_now = 0);
111         /** Delete an InspTimer
112          * @param T an InspTimer derived class to delete
113          */
114         void DelTimer(InspTimer* T);
115         /** Tick any timers that have been missed due to lag
116          * @param TIME the current system time
117          */
118         void TickMissedTimers(time_t TIME);
119 };
120
121 #endif