]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
cc5364f672f5bd84cfca8c72c46b0c0bcdf7f657
[user/henk/code/inspircd.git] / include / timer.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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  * Timer 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 Timer, inherit a class from
22  * Timer, 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 CoreExport Timer
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         Timer(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 ~Timer() { }
55
56         /** Retrieve the current triggering time
57          */
58         virtual time_t GetTimer()
59         {
60                 return trigger;
61         }
62
63         /** Sets the trigger timeout to a new value
64          */
65         virtual void SetTimer(time_t t)
66         {
67                 trigger = t;
68         }
69
70         /** Called when the timer ticks.
71          * You should override this method with some useful code to
72          * handle the tick event.
73          */
74         virtual void Tick(time_t TIME) = 0;
75
76         /** Returns true if this timer is set to repeat
77          */
78         bool GetRepeat()
79         {
80                 return repeat;
81         }
82
83         /** Returns the interval (number of seconds between ticks)
84          * of this timer object.
85          */
86         long GetSecs()
87         {
88                 return secs;
89         }
90
91         /** Cancels the repeat state of a repeating timer.
92          * If you call this method, then the next time your
93          * timer ticks, it will be removed immediately after.
94          * You should use this method call to remove a recurring
95          * timer if you wish to do so within the timer's Tick
96          * event, as calling TimerManager::DelTimer() from within
97          * the Timer::Tick() method is dangerous and may
98          * cause a segmentation fault. Calling CancelRepeat()
99          * is safe in this case.
100          */
101         void CancelRepeat()
102         {
103                 repeat = false;
104         }
105 };
106
107
108 /** This class manages sets of Timers, and triggers them at their defined times.
109  * This will ensure timers are not missed, as well as removing timers that have
110  * expired and allowing the addition of new ones.
111  */
112 class CoreExport TimerManager
113 {
114  protected:
115         /** A list of all pending timers
116          */
117         std::vector<Timer *> Timers;
118
119  public:
120         /** Constructor
121          */
122         TimerManager();
123         ~TimerManager();
124
125         /** Tick all pending Timers
126          * @param TIME the current system time
127          */
128         void TickTimers(time_t TIME);
129
130         /** Add an Timer
131          * @param T an Timer derived class to add
132          * @param secs_from_now You may set this to the number of seconds
133          * from the current time when the timer will tick, or you may just
134          * leave this unset and the values set by the Timers constructor
135          * will be used. This is used internally for re-triggering repeating
136          * timers.
137          */
138         void AddTimer(Timer *T);
139
140         /** Delete an Timer
141          * @param T an Timer derived class to delete
142          */
143         void DelTimer(Timer* T);
144
145         /** Compares two timers
146          */
147         static bool TimerComparison( Timer *one,  Timer*two);
148 };
149
150 #endif
151