]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
Merge pull request #109 from Justasic/insp20
[user/henk/code/inspircd.git] / include / timer.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #ifndef INSPIRCD_TIMER_H
23 #define INSPIRCD_TIMER_H
24
25 /** Timer class for one-second resolution timers
26  * Timer provides a facility which allows module
27  * developers to create one-shot timers. The timer
28  * can be made to trigger at any time up to a one-second
29  * resolution. To use Timer, inherit a class from
30  * Timer, then insert your inherited class into the
31  * queue using Server::AddTimer(). The Tick() method of
32  * your object (which you should override) will be called
33  * at the given time.
34  */
35 class CoreExport Timer
36 {
37  private:
38         /** The triggering time
39          */
40         time_t trigger;
41         /** Number of seconds between triggers
42          */
43         long secs;
44         /** True if this is a repeating timer
45          */
46         bool repeat;
47  public:
48         /** Default constructor, initializes the triggering time
49          * @param secs_from_now The number of seconds from now to trigger the timer
50          * @param now The time now
51          * @param repeating Repeat this timer every secs_from_now seconds if set to true
52          */
53         Timer(long secs_from_now, time_t now, bool repeating = false)
54         {
55                 trigger = now + secs_from_now;
56                 secs = secs_from_now;
57                 repeat = repeating;
58         }
59
60         /** Default destructor, does nothing.
61          */
62         virtual ~Timer() { }
63
64         /** Retrieve the current triggering time
65          */
66         virtual time_t GetTimer()
67         {
68                 return trigger;
69         }
70
71         /** Sets the trigger timeout to a new value
72          */
73         virtual void SetTimer(time_t t)
74         {
75                 trigger = t;
76         }
77
78         /** Called when the timer ticks.
79          * You should override this method with some useful code to
80          * handle the tick event.
81          */
82         virtual void Tick(time_t TIME) = 0;
83
84         /** Returns true if this timer is set to repeat
85          */
86         bool GetRepeat()
87         {
88                 return repeat;
89         }
90
91         /** Returns the interval (number of seconds between ticks)
92          * of this timer object.
93          */
94         long GetSecs()
95         {
96                 return secs;
97         }
98
99         /** Cancels the repeat state of a repeating timer.
100          * If you call this method, then the next time your
101          * timer ticks, it will be removed immediately after.
102          * You should use this method call to remove a recurring
103          * timer if you wish to do so within the timer's Tick
104          * event, as calling TimerManager::DelTimer() from within
105          * the Timer::Tick() method is dangerous and may
106          * cause a segmentation fault. Calling CancelRepeat()
107          * is safe in this case.
108          */
109         void CancelRepeat()
110         {
111                 repeat = false;
112         }
113 };
114
115
116 /** This class manages sets of Timers, and triggers them at their defined times.
117  * This will ensure timers are not missed, as well as removing timers that have
118  * expired and allowing the addition of new ones.
119  */
120 class CoreExport TimerManager
121 {
122  protected:
123         /** A list of all pending timers
124          */
125         std::vector<Timer *> Timers;
126
127  public:
128         /** Constructor
129          */
130         TimerManager();
131         ~TimerManager();
132
133         /** Tick all pending Timers
134          * @param TIME the current system time
135          */
136         void TickTimers(time_t TIME);
137
138         /** Add an Timer
139          * @param T an Timer derived class to add
140          * @param secs_from_now You may set this to the number of seconds
141          * from the current time when the timer will tick, or you may just
142          * leave this unset and the values set by the Timers constructor
143          * will be used. This is used internally for re-triggering repeating
144          * timers.
145          */
146         void AddTimer(Timer *T);
147
148         /** Delete an Timer
149          * @param T an Timer derived class to delete
150          */
151         void DelTimer(Timer* T);
152
153         /** Compares two timers
154          */
155         static bool TimerComparison( Timer *one,  Timer*two);
156 };
157
158 #endif
159