anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

timers.h (3080B)

      1 /* Timer include stuff.
      2  *
      3  * (C) 2003-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  *
      8  * Based on the original code of Epona by Lara.
      9  * Based on the original code of Services by Andy Church.
     10  */
     11 
     12 #ifndef TIMERS_H
     13 #define TIMERS_H
     14 
     15 #include "anope.h"
     16 
     17 class CoreExport Timer
     18 {
     19  private:
     20 	/** The owner of the timer, if any
     21 	 */
     22 	Module *owner;
     23 
     24 	/** The time this was created
     25 	 */
     26 	time_t settime;
     27 
     28 	/** The triggering time
     29 	 */
     30 	time_t trigger;
     31 
     32 	/** Numer of seconds between triggers
     33 	 */
     34 	long secs;
     35 
     36 	/** True if this is a repeating timer
     37 	 */
     38 	bool repeat;
     39 
     40  public:
     41 	/** Constructor, initializes the triggering time
     42 	 * @param time_from_now The number of seconds from now to trigger the timer
     43 	 * @param now The time now
     44 	 * @param repeating Repeat this timer every time_from_now if this is true
     45 	 */
     46 	Timer(long time_from_now, time_t now = Anope::CurTime, bool repeating = false);
     47 
     48 	/** Constructor, initializes the triggering time
     49 	 * @param creator The creator of the timer
     50 	 * @param time_from_now The number of seconds from now to trigger the timer
     51 	 * @param now The time now
     52 	 * @param repeating Repeat this timer every time_from_now if this is true
     53 	 */
     54 	Timer(Module *creator, long time_from_now, time_t now = Anope::CurTime, bool repeating = false);
     55 
     56 	/** Destructor, removes the timer from the list
     57 	 */
     58 	virtual ~Timer();
     59 
     60 	/** Set the trigger time to a new value
     61 	 * @param t The new time
     62 	 */
     63 	void SetTimer(time_t t);
     64 
     65 	/** Retrieve the triggering time
     66 	 * @return The trigger time
     67 	 */
     68 	time_t GetTimer() const;
     69 
     70 	/** Returns true if the timer is set to repeat
     71 	 * @return Returns true if the timer is set to repeat
     72 	 */
     73 	bool GetRepeat() const;
     74 
     75 	/** Set the interval between ticks
     76 	 * @paramt t The new interval
     77 	 */
     78 	void SetSecs(time_t t);
     79 
     80 	/** Returns the interval between ticks
     81 	 * @return The interval
     82 	 */
     83 	long GetSecs() const;
     84 
     85 	/** Returns the time this timer was created
     86 	 * @return The time this timer was created
     87 	 */
     88 	time_t GetSetTime() const;
     89 
     90 	/** Returns the owner of this timer, if any
     91 	 * @return The owner of the timer
     92 	 */
     93 	Module *GetOwner() const;
     94 
     95 	/** Called when the timer ticks
     96 	 * This should be overridden with something useful
     97 	 */
     98 	virtual void Tick(time_t ctime) = 0;
     99 };
    100 
    101 /** This class manages sets of Timers, and triggers them at their defined times.
    102  * This will ensure timers are not missed, as well as removing timers that have
    103  * expired and allowing the addition of new ones.
    104  */
    105 class CoreExport TimerManager
    106 {
    107 	/** A list of timers
    108 	 */
    109 	static std::multimap<time_t, Timer *> Timers;
    110  public:
    111 	/** Add a timer to the list
    112 	 * @param t A Timer derived class to add
    113 	 */
    114 	static void AddTimer(Timer *t);
    115 
    116 	/** Deletes a timer
    117 	 * @param t A Timer derived class to delete
    118 	 */
    119 	static void DelTimer(Timer *t);
    120 
    121 	/** Tick all pending timers
    122 	 * @param ctime The current time
    123 	 */
    124 	static void TickTimers(time_t ctime = Anope::CurTime);
    125 
    126 	/** Deletes all timers owned by the given module
    127 	 */
    128 	static void DeleteTimersFor(Module *m);
    129 };
    130 
    131 #endif // TIMERS_H