From 7e7556f06445d4d8e607ef514c9fb5009899db73 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 30 Mar 2013 23:38:40 -0500 Subject: Merge usefulness of Timer and CallBack classes into Timer, and fix it to really work --- src/module.cpp | 15 ++------------- src/modulemanager.cpp | 6 ------ src/timers.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/module.cpp b/src/module.cpp index 4b13d7e94..91a7ed350 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -57,9 +57,9 @@ Module::~Module() { /* Detach all event hooks for this module */ ModuleManager::DetachAll(this); - /* Clear any active callbacks this module has */ - ModuleManager::ClearCallBacks(this); IdentifyRequest::ModuleUnload(this); + /* Clear any active timers this module has */ + TimerManager::DeleteTimersFor(this); std::list::iterator it = std::find(ModuleManager::Modules.begin(), ModuleManager::Modules.end(), this); if (it != ModuleManager::Modules.end()) @@ -111,14 +111,3 @@ int ModuleVersion::GetPatch() const return this->version_patch; } -CallBack::CallBack(Module *mod, long time_from_now, time_t now, bool repeating) : Timer(time_from_now, now, repeating), m(mod) -{ -} - -CallBack::~CallBack() -{ - std::list::iterator it = std::find(m->callbacks.begin(), m->callbacks.end(), this); - if (it != m->callbacks.end()) - m->callbacks.erase(it); -} - diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp index 6c54058b7..823fda868 100644 --- a/src/modulemanager.cpp +++ b/src/modulemanager.cpp @@ -449,12 +449,6 @@ bool ModuleManager::SetPriority(Module *mod, Implementation i, Priority s, Modul return true; } -void ModuleManager::ClearCallBacks(Module *m) -{ - while (!m->callbacks.empty()) - delete m->callbacks.front(); -} - void ModuleManager::UnloadAll() { std::vector modules[MT_END]; diff --git a/src/timers.cpp b/src/timers.cpp index 94a6fbb3d..827d81df7 100644 --- a/src/timers.cpp +++ b/src/timers.cpp @@ -14,6 +14,18 @@ std::multimap TimerManager::Timers; Timer::Timer(long time_from_now, time_t now, bool repeating) { + owner = NULL; + trigger = now + time_from_now; + secs = time_from_now; + repeat = repeating; + settime = now; + + TimerManager::AddTimer(this); +} + +Timer::Timer(Module *creator, long time_from_now, time_t now, bool repeating) +{ + owner = creator; trigger = now + time_from_now; secs = time_from_now; repeat = repeating; @@ -62,6 +74,11 @@ long Timer::GetSecs() const return secs; } +Module *Timer::GetOwner() const +{ + return owner; +} + void TimerManager::AddTimer(Timer *t) { Timers.insert(std::make_pair(t->GetTimer(), t)); @@ -98,3 +115,14 @@ void TimerManager::TickTimers(time_t ctime) delete t; } } + +void TimerManager::DeleteTimersFor(Module *m) +{ + for (std::multimap::iterator it = Timers.begin(), it_next = it; it != Timers.end(); it = it_next) + { + ++it_next; + if (it->second->GetOwner() == m) + delete it->second; + } +} + -- cgit