]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
And now, just to force you to recompile the *whole* ircd.. updated headers on the...
[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  public:
34         /** Default constructor, initializes the triggering time
35          */
36         InspTimer(long secs_from_now,time_t now)
37         {
38                 trigger = now + secs_from_now;
39         }
40         /** Default destructor, does nothing.
41          */
42         virtual ~InspTimer() { }
43         /** Retrieve the current triggering time
44          */
45         virtual time_t GetTimer()
46         {
47                 return trigger;
48         }
49         /** Called when the timer ticks.
50          */
51         virtual void Tick(time_t TIME) = 0;
52 };
53
54
55 /** This class manages sets of InspTimers, and triggers them at their defined times.
56  * This will ensure timers are not missed, as well as removing timers that have
57  * expired and allowing the addition of new ones.
58  */
59 class TimerManager : public Extensible
60 {
61  protected:
62         /** A group of timers all set to trigger at the same time
63          */
64         typedef std::vector<InspTimer*> timergroup;
65         /** A map of timergroups, each group has a specific trigger time
66          */
67         typedef std::map<time_t, timergroup*> timerlist;
68
69  private:
70
71         /** The current timer set, a map of timergroups
72          */
73         timerlist Timers;
74
75  public:
76         /** Tick all pending InspTimers
77          * @param TIME the current system time
78          */
79         void TickTimers(time_t TIME);
80         /** Add an InspTimer
81          * @param T an InspTimer derived class to add
82          */
83         void AddTimer(InspTimer* T);
84         /** Delete an InspTimer
85          * @param T an InspTimer derived class to delete
86          */
87         void DelTimer(InspTimer* T);
88         /** Tick any timers that have been missed due to lag
89          * @param TIME the current system time
90          */
91         void TickMissedTimers(time_t TIME);
92 };
93
94 #endif