summaryrefslogtreecommitdiff
path: root/include/timers.h
blob: 3615074dfc6d6a251c78775986ca6e2ae87b339e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Timer include stuff.
 *
 * (C) 2003-2024 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 */

#pragma once

#include "anope.h"

class CoreExport Timer
{
private:
	/** The owner of the timer, if any
	 */
	Module *owner = nullptr;

	/** The triggering time
	 */
	time_t trigger;

	/** Number of seconds between triggers
	 */
	time_t secs;

	/** True if this is a repeating timer
	 */
	bool repeat;

public:
	/** Constructor, initializes the triggering time
	 * @param time_from_now The number of seconds from now to trigger the timer
	 * @param repeating Repeat this timer every time_from_now if this is true
	 */
	Timer(time_t time_from_now, bool repeating = false);

	/** Constructor, initializes the triggering time
	 * @param creator The creator of the timer
	 * @param time_from_now The number of seconds from now to trigger the timer
	 * @param repeating Repeat this timer every time_from_now if this is true
	 */
	Timer(Module *creator, time_t time_from_now, bool repeating = false);

	/** Destructor, removes the timer from the list
	 */
	virtual ~Timer();

	/** Set the trigger time to a new value
	 * @param t The new time
	 */
	void SetTimer(time_t t);

	/** Retrieve the triggering time
	 * @return The trigger time
	 */
	time_t GetTimer() const;

	/** Returns true if the timer is set to repeat
	 * @return Returns true if the timer is set to repeat
	 */
	bool GetRepeat() const;

	/** Set the interval between ticks
	 * @paramt t The new interval
	 */
	void SetSecs(time_t t);

	/** Returns the interval between ticks
	 * @return The interval
	 */
	long GetSecs() const;

	/** Returns the owner of this timer, if any
	 * @return The owner of the timer
	 */
	Module *GetOwner() const;

	/** Called when the timer ticks
	 * This should be overridden with something useful
	 */
	virtual void Tick() = 0;
};

/** This class manages sets of Timers, and triggers them at their defined times.
 * This will ensure timers are not missed, as well as removing timers that have
 * expired and allowing the addition of new ones.
 */
class CoreExport TimerManager final
{
	/** A list of timers
	 */
	static std::multimap<time_t, Timer *> Timers;
public:
	/** Add a timer to the list
	 * @param t A Timer derived class to add
	 */
	static void AddTimer(Timer *t);

	/** Deletes a timer
	 * @param t A Timer derived class to delete
	 */
	static void DelTimer(Timer *t);

	/** Tick all pending timers
	 * @param ctime The current time
	 */
	static void TickTimers();

	/** Deletes all timers owned by the given module
	 */
	static void DeleteTimersFor(Module *m);
};