]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
Merge pull request #461 from SaberUK/master+header-cleanup
[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 #pragma once
23
24 /** Timer class for one-second resolution timers
25  * Timer provides a facility which allows module
26  * developers to create one-shot timers. The timer
27  * can be made to trigger at any time up to a one-second
28  * resolution. To use Timer, inherit a class from
29  * Timer, then insert your inherited class into the
30  * queue using Server::AddTimer(). The Tick() method of
31  * your object (which you should override) will be called
32  * at the given time.
33  */
34 class CoreExport Timer
35 {
36  private:
37         /** The triggering time
38          */
39         time_t trigger;
40         /** Number of seconds between triggers
41          */
42         long secs;
43         /** True if this is a repeating timer
44          */
45         bool repeat;
46  public:
47         /** Default constructor, initializes the triggering time
48          * @param secs_from_now The number of seconds from now to trigger the timer
49          * @param now The time now
50          * @param repeating Repeat this timer every secs_from_now seconds if set to true
51          */
52         Timer(long secs_from_now, time_t now, bool repeating = false)
53         {
54                 trigger = now + secs_from_now;
55                 secs = secs_from_now;
56                 repeat = repeating;
57         }
58
59         /** Default destructor, does nothing.
60          */
61         virtual ~Timer() { }
62
63         /** Retrieve the current triggering time
64          */
65         virtual time_t GetTimer()
66         {
67                 return trigger;
68         }
69
70         /** Sets the trigger timeout to a new value
71          */
72         virtual void SetTimer(time_t t)
73         {
74                 trigger = t;
75         }
76
77         /** Called when the timer ticks.
78          * You should override this method with some useful code to
79          * handle the tick event.
80          */
81         virtual void Tick(time_t TIME) = 0;
82
83         /** Returns true if this timer is set to repeat
84          */
85         bool GetRepeat()
86         {
87                 return repeat;
88         }
89
90         /** Returns the interval (number of seconds between ticks)
91          * of this timer object.
92          */
93         long GetSecs()
94         {
95                 return secs;
96         }
97
98         /** Cancels the repeat state of a repeating timer.
99          * If you call this method, then the next time your
100          * timer ticks, it will be removed immediately after.
101          * You should use this method call to remove a recurring
102          * timer if you wish to do so within the timer's Tick
103          * event, as calling TimerManager::DelTimer() from within
104          * the Timer::Tick() method is dangerous and may
105          * cause a segmentation fault. Calling CancelRepeat()
106          * is safe in this case.
107          */
108         void CancelRepeat()
109         {
110                 repeat = false;
111         }
112 };
113
114
115 /** This class manages sets of Timers, and triggers them at their defined times.
116  * This will ensure timers are not missed, as well as removing timers that have
117  * expired and allowing the addition of new ones.
118  */
119 class CoreExport TimerManager
120 {
121  protected:
122         /** A list of all pending timers
123          */
124         std::vector<Timer *> Timers;
125
126  public:
127         /** Constructor
128          */
129         TimerManager();
130         ~TimerManager();
131
132         /** Tick all pending Timers
133          * @param TIME the current system time
134          */
135         void TickTimers(time_t TIME);
136
137         /** Add an Timer
138          * @param T an Timer derived class to add
139          */
140         void AddTimer(Timer *T);
141
142         /** Delete an Timer
143          * @param T an Timer derived class to delete
144          */
145         void DelTimer(Timer* T);
146
147         /** Compares two timers
148          */
149         static bool TimerComparison( Timer *one,  Timer*two);
150 };