]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
Comment on a lot of recently added stuff that wasnt properly documented (until now)
[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         /** Number of seconds between triggers
34          */
35         long secs;
36         /** True if this is a repeating timer
37          */
38         bool repeat;
39  public:
40         /** Default constructor, initializes the triggering time
41          * @param secs_from_now The number of seconds from now to trigger the timer
42          * @param now The time now
43          * @param repeating Repeat this timer every secs_from_now seconds if set to true
44          */
45         InspTimer(long secs_from_now,time_t now, bool repeating = false)
46         {
47                 trigger = now + secs_from_now;
48                 secs = secs_from_now;
49                 repeat = repeating;
50         }
51
52         /** Default destructor, does nothing.
53          */
54         virtual ~InspTimer() { }
55
56         /** Retrieve the current triggering time
57          */
58         virtual time_t GetTimer()
59         {
60                 return trigger;
61         }
62
63         /** Called when the timer ticks.
64          * You should override this method with some useful code to
65          * handle the tick event.
66          */
67         virtual void Tick(time_t TIME) = 0;
68
69         /** Returns true if this timer is set to repeat
70          */
71         bool GetRepeat()
72         {
73                 return repeat;
74         }
75
76         /** Returns the interval (number of seconds between ticks)
77          * of this timer object.
78          */
79         long GetSecs()
80         {
81                 return secs;
82         }
83
84         /** Cancels the repeat state of a repeating timer.
85          * If you call this method, then the next time your
86          * timer ticks, it will be removed immediately after.
87          * You should use this method call to remove a recurring
88          * timer if you wish to do so within the timer's Tick
89          * event, as calling TimerManager::DelTimer() from within
90          * the InspTimer::Tick() method is dangerous and may
91          * cause a segmentation fault. Calling CancelRepeat()
92          * is safe in this case.
93          */
94         void CancelRepeat()
95         {
96                 repeat = false;
97         }
98 };
99
100
101 /** This class manages sets of InspTimers, and triggers them at their defined times.
102  * This will ensure timers are not missed, as well as removing timers that have
103  * expired and allowing the addition of new ones.
104  */
105 class TimerManager : public Extensible
106 {
107  protected:
108         /** A group of timers all set to trigger at the same time
109          */
110         typedef std::vector<InspTimer*> timergroup;
111         /** A map of timergroups, each group has a specific trigger time
112          */
113         typedef std::map<time_t, timergroup*> timerlist;
114
115  private:
116
117         /** The current timer set, a map of timergroups
118          */
119         timerlist Timers;
120
121  public:
122         /** Tick all pending InspTimers
123          * @param TIME the current system time
124          */
125         void TickTimers(time_t TIME);
126         /** Add an InspTimer
127          * @param T an InspTimer derived class to add
128          * @param secs_from_now You may set this to the number of seconds
129          * from the current time when the timer will tick, or you may just
130          * leave this unset and the values set by the InspTimers constructor
131          * will be used. This is used internally for re-triggering repeating
132          * timers.
133          */
134         void AddTimer(InspTimer* T, long secs_from_now = 0);
135         /** Delete an InspTimer
136          * @param T an InspTimer derived class to delete
137          */
138         void DelTimer(InspTimer* T);
139         /** Tick any timers that have been missed due to lag
140          * @param TIME the current system time
141          */
142         void TickMissedTimers(time_t TIME);
143 };
144
145 #endif