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