]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/timer.h
Speaking of misleading comments, 'Delete and InspTImer' isn't particularly descriptive
[user/henk/code/inspircd.git] / include / timer.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef INSPIRCD_TIMER_H
18 #define INSPIRCD_TIMER_H
19
20 /** Timer class for one-second resolution timers
21  * InspTimer provides a facility which allows module
22  * developers to create one-shot timers. The timer
23  * can be made to trigger at any time up to a one-second
24  * resolution. To use InspTimer, inherit a class from
25  * InspTimer, then insert your inherited class into the
26  * queue using Server::AddTimer(). The Tick() method of
27  * your object (which you should override) will be called
28  * at the given time.
29  */
30 class InspTimer : public Extensible
31 {
32  private:
33         /** The triggering time
34          */
35         time_t trigger;
36  public:
37         /** Default constructor, initializes the triggering time
38          */
39         InspTimer(long secs_from_now,time_t now)
40         {
41                 trigger = now + secs_from_now;
42         }
43         /** Default destructor, does nothing.
44          */
45         virtual ~InspTimer() { }
46         /** Retrieve the current triggering time
47          */
48         virtual time_t GetTimer()
49         {
50                 return trigger;
51         }
52         /** Called when the timer ticks.
53          */
54         virtual void Tick(time_t TIME) = 0;
55 };
56
57
58 /** This class manages sets of InspTimers, and triggers them at their defined times.
59  * This will ensure timers are not missed, as well as removing timers that have
60  * expired and allowing the addition of new ones.
61  */
62 class TimerManager : public Extensible
63 {
64  protected:
65         /** A group of timers all set to trigger at the same time
66          */
67         typedef std::vector<InspTimer*> timergroup;
68         /** A map of timergroups, each group has a specific trigger time
69          */
70         typedef std::map<time_t, timergroup*> timerlist;
71
72  private:
73
74         /** The current timer set, a map of timergroups
75          */
76         timerlist Timers;
77
78  public:
79         /** Tick all pending InspTimers
80          * @param TIME the current system time
81          */
82         void TickTimers(time_t TIME);
83         /** Add an InspTimer
84          * @param T an InspTimer derived class to add
85          */
86         void AddTimer(InspTimer* T);
87         /** Delete an InspTimer
88          * @param T an InspTimer derived class to delete
89          */
90         void DelTimer(InspTimer* T);
91         /** Tick any timers that have been missed due to lag
92          * @param TIME the current system time
93          */
94         void TickMissedTimers(time_t TIME);
95 };
96
97 #endif