summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/modules.h47
-rw-r--r--include/timers.h6
-rw-r--r--src/core/os_defcon.c2
-rw-r--r--src/module.cpp4
-rw-r--r--src/modulemanager.cpp14
-rw-r--r--src/modules.c42
-rw-r--r--src/modules/cs_tban.c6
-rw-r--r--src/nickserv.c14
-rw-r--r--src/timers.cpp28
9 files changed, 47 insertions, 116 deletions
diff --git a/include/modules.h b/include/modules.h
index cc42f4abc..4c5c4cf6b 100644
--- a/include/modules.h
+++ b/include/modules.h
@@ -311,6 +311,9 @@ class Version
const unsigned GetBuild();
};
+/* Forward declaration of CallBack class for the Module class */
+class CallBack;
+
/** Every module in Anope is actually a class.
*/
class CoreExport Module
@@ -326,9 +329,9 @@ class CoreExport Module
*/
std::string filename;
- /** Timers used in this module
+ /** Callbacks used in this module
*/
- std::list<Timer *> CallBacks;
+ std::list<CallBack *> CallBacks;
ano_module_t handle;
time_t created;
@@ -432,20 +435,6 @@ class CoreExport Module
*/
int DelCommand(CommandHash * cmdTable[], const char *name);
- /**
- * Adds a timer to the current module
- * The timer handling will take care of everything for this timer, this is only here
- * so we have a list of timers to destroy when this module is unloaded
- * @param t A timer derived class
- */
- void AddCallBack(Timer *t);
-
- /**
- * Deletes a timer for the current module
- * @param t The timer
- */
- bool DelCallBack(Timer *t);
-
/** Called on NickServ HELP
* @param u The user requesting help
*/
@@ -1201,10 +1190,10 @@ class CoreExport ModuleManager
*/
static void Attach(Implementation* i, Module* mod, size_t sz);
- /** Delete all timers attached to a module
+ /** Delete all callbacks attached to a module
* @param m The module
*/
- static void ClearTimers(Module *m);
+ static void ClearCallBacks(Module *m);
private:
/** Call the module_delete function to safely delete the module
@@ -1213,7 +1202,27 @@ private:
static void DeleteModule(Module *m);
};
-
+/** Class used for callbacks within modules
+ */
+class CallBack : public Timer
+{
+ private:
+ Module *m;
+ public:
+ CallBack(Module *mod, long time_from_now, time_t now = time(NULL), bool repeating = false) : Timer(time_from_now, now, repeating), m(mod)
+ {
+ m->CallBacks.push_back(this);
+ }
+
+ virtual ~CallBack()
+ {
+ std::list<CallBack *>::iterator it = std::find(m->CallBacks.begin(), m->CallBacks.end(), this);
+ if (it != m->CallBacks.end())
+ {
+ m->CallBacks.erase(it);
+ }
+ }
+};
struct ModuleHash_ {
char *name;
diff --git a/include/timers.h b/include/timers.h
index 0de49b17d..30a49acff 100644
--- a/include/timers.h
+++ b/include/timers.h
@@ -103,12 +103,6 @@ class CoreExport TimerManager : public Extensible
*/
static void DelTimer(Timer *T);
- /** Check if something is a timer
- * @param T A pointer
- * @return true or false
- */
- static bool IsTimer(Timer *T);
-
/** Tick all pending timers
* @param ctime The current time
*/
diff --git a/src/core/os_defcon.c b/src/core/os_defcon.c
index c06cf8c62..a3c4c46fa 100644
--- a/src/core/os_defcon.c
+++ b/src/core/os_defcon.c
@@ -86,7 +86,7 @@ class CommandOSDEFCON : public Command
if (timeout)
{
- TimerManager::DelTimer(timeout);
+ delete timeout;
timeout = NULL;
}
diff --git a/src/module.cpp b/src/module.cpp
index fa4e41cd8..6aebd8644 100644
--- a/src/module.cpp
+++ b/src/module.cpp
@@ -66,8 +66,8 @@ Module::~Module()
Command *c;
- /* Kill any active timers this module has */
- ModuleManager::ClearTimers(this);
+ /* Clear any active callbacks this module has */
+ ModuleManager::ClearCallBacks(this);
/**
* ok, im going to walk every hash looking for commands we own, now, not exactly elegant or efficiant :)
diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp
index 90c76319c..0716e4503 100644
--- a/src/modulemanager.cpp
+++ b/src/modulemanager.cpp
@@ -467,16 +467,12 @@ bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Modul
return true;
}
-/** Delete all timers attached to a module
+/** Delete all callbacks attached to a module
* @param m The module
*/
-void ModuleManager::ClearTimers(Module *m)
+void ModuleManager::ClearCallBacks(Module *m)
{
- std::list<Timer *>::iterator it;
-
- for (it = m->CallBacks.begin(); it != m->CallBacks.end(); ++it)
- {
- TimerManager::DelTimer(*it);
- }
- m->CallBacks.clear();
+ while (!m->CallBacks.empty())
+ delete m->CallBacks.front();
}
+
diff --git a/src/modules.c b/src/modules.c
index 3bc2e490f..d3453efad 100644
--- a/src/modules.c
+++ b/src/modules.c
@@ -596,48 +596,6 @@ int destroyMessage(Message * m)
* Module Callback Functions
*******************************************************************************/
-/**
- * Adds a timer to the current module
- * The timer handling will take care of everything for this timer, this is only here
- * so we have a list of timers to destroy when this module is unloaded
- * @param t A timer derived class
- */
-void Module::AddCallBack(Timer *t)
-{
- /* Remove no longer valid Timers from the modules internal list */
- std::list<Timer *>::iterator it, it2;
- for (it = this->CallBacks.begin(); it != this->CallBacks.end(); it = it2)
- {
- it2 = it;
- ++it2;
-
- if (!TimerManager::IsTimer(*it))
- {
- this->CallBacks.erase(it);
- }
- }
-
- this->CallBacks.push_back(t);
-}
-
-/**
- * Deletes a timer for the current module
- * @param t The timer
- */
-bool Module::DelCallBack(Timer *t)
-{
- std::list<Timer *>::iterator it = std::find(this->CallBacks.begin(), this->CallBacks.end(), t);
-
- if (it != this->CallBacks.end())
- {
- TimerManager::DelTimer(*it);
- this->CallBacks.erase(it);
- return true;
- }
-
- return false;
-}
-
/* Check the current version of anope against a given version number
* Specifiying -1 for minor,patch or build
* @param major The major version of anope, the first part of the verison number
diff --git a/src/modules/cs_tban.c b/src/modules/cs_tban.c
index 07f7f6c0f..46c678522 100644
--- a/src/modules/cs_tban.c
+++ b/src/modules/cs_tban.c
@@ -168,14 +168,14 @@ void mySendResponse(User *u, const char *channel, char *mask, const char *time)
me->NoticeLang(Config.s_ChanServ, u, TBAN_RESPONSE, mask, channel, time);
}
-class TempBan : public Timer
+class TempBan : public CallBack
{
private:
std::string chan;
std::string mask;
public:
- TempBan(time_t seconds, const std::string &channel, const std::string &banmask) : Timer(seconds), chan(channel), mask(banmask) { }
+ TempBan(time_t seconds, const std::string &channel, const std::string &banmask) : CallBack(me, seconds), chan(channel), mask(banmask) { }
void Tick(time_t ctime)
{
@@ -192,7 +192,7 @@ void addBan(Channel *c, time_t timeout, char *banmask)
{
c->SetMode(NULL, CMODE_BAN, banmask);
- me->AddCallBack(new TempBan(timeout, c->name, banmask));
+ new TempBan(timeout, c->name, banmask);
}
int canBanUser(Channel * c, User * u, User * u2)
diff --git a/src/nickserv.c b/src/nickserv.c
index a0994e07d..3fe43fde1 100644
--- a/src/nickserv.c
+++ b/src/nickserv.c
@@ -40,7 +40,7 @@ NickServCollide::NickServCollide(NickAlias *nickalias, time_t delay) : Timer(del
std::map<NickAlias *, NickServCollide *>::iterator nit = NickServCollides.find(nickalias);
if (nit != NickServCollides.end())
{
- TimerManager::DelTimer(nit->second);
+ delete nit->second;
}
it = NickServCollides.insert(std::make_pair(nickalias, this));
@@ -70,13 +70,10 @@ void NickServCollide::Tick(time_t ctime)
void NickServCollide::ClearTimers(NickAlias *na)
{
std::map<NickAlias *, NickServCollide *>::iterator i = NickServCollides.find(na);
- NickServCollide *t;
if (i != NickServCollides.end())
{
- t = i->second;
-
- TimerManager::DelTimer(t);
+ delete i->second;
}
}
@@ -86,7 +83,7 @@ NickServRelease::NickServRelease(NickAlias *nickalias, time_t delay) : Timer(del
std::map<NickAlias *, NickServRelease *>::iterator nit = NickServReleases.find(nickalias);
if (nit != NickServReleases.end())
{
- TimerManager::DelTimer(nit->second);
+ delete nit->second;
}
it = NickServReleases.insert(std::make_pair(nickalias, this));
@@ -119,16 +116,13 @@ void NickServRelease::Tick(time_t ctime)
void NickServRelease::ClearTimers(NickAlias *na, bool dorelease)
{
std::map<NickAlias *, NickServRelease *>::iterator i = NickServReleases.find(na);
- NickServRelease *t;
if (i != NickServReleases.end())
{
- t = i->second;
-
if (dorelease)
release(na, 1);
- TimerManager::DelTimer(t);
+ delete i->second;
}
}
diff --git a/src/timers.cpp b/src/timers.cpp
index be4289a7c..cd7dbd1fd 100644
--- a/src/timers.cpp
+++ b/src/timers.cpp
@@ -34,6 +34,7 @@ Timer::Timer(long time_from_now, time_t now, bool repeating)
*/
Timer::~Timer()
{
+ TimerManager::DelTimer(this);
}
/** Set the trigger time to a new value
@@ -94,39 +95,18 @@ void TimerManager::DelTimer(Timer *T)
if (i != Timers.end())
{
- delete (*i);
Timers.erase(i);
}
}
-/** Check if something is a timer
- * @param T A pointer
- * @return true or false
- */
-bool TimerManager::IsTimer(Timer *T)
-{
- std::vector<Timer *>::iterator i = std::find(Timers.begin(), Timers.end(), T);
-
- if (i != Timers.end())
- {
- return true;
- }
-
- return false;
-}
-
/** Tick all pending timers
* @param ctime The current time
*/
void TimerManager::TickTimers(time_t ctime)
{
- std::vector<Timer *>::iterator i;
- Timer *t;
-
- while ((Timers.size()) && (ctime > (*Timers.begin())->GetTimer()))
+ while (Timers.size() && (ctime > Timers.front()->GetTimer()))
{
- i = Timers.begin();
- t = *i;
+ Timer *t = Timers.front();
t->Tick(ctime);
@@ -136,7 +116,7 @@ void TimerManager::TickTimers(time_t ctime)
sort(Timers.begin(), Timers.end(), TimerManager::TimerComparison);
}
else
- TimerManager::DelTimer(t);
+ delete t;
}
}