BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
72afe56
Branches
Tags
master
Blinker
src
noarch
ModuleMgr_impl.h
split ModuleMgr.h into declaration and implementation files
Stefan Schuermans
commited
72afe56
at 2011-12-11 00:04:04
ModuleMgr_impl.h
Blame
History
Raw
/* Blinker Copyright 2011 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #ifndef MODULEMGR_IMPL_H #define MODULEMGR_IMPL_H #include <list> #include "CallMgr.h" #include "Directory.h" #include "Module.h" #include "ModuleMgr.h" #include "StreamMgr.h" #include "TimeCallee.h" namespace Blinker { /** * @brief constructor * @param[in] callMgr callback manager * @param[in] streamMgr stream manager * @param[in] dirBase base directory */ template<typename MODULE> ModuleMgr<MODULE>::ModuleMgr(CallMgr &callMgr, StreamMgr &streamMgr, const Directory &dirBase): m_callMgr(callMgr), m_streamMgr(streamMgr), m_dirBase(dirBase) { updateModuleListFull(); // request call in 1s m_callMgr.requestTimeCall(this, Time::now() + Time(1)); } /// destructor template<typename MODULE> ModuleMgr<MODULE>::~ModuleMgr() { // free all modules while (!m_moduleList.empty()) { m_moduleList.back().destroyModule(); m_moduleList.pop_back(); } } /// callback when requsted time reached template<typename MODULE> void ModuleMgr<MODULE>::timeCall() { updateConfig(); // request next call in 1s m_callMgr.requestTimeCall(this, Time::now() + Time(1)); } /// check for update of configuration template<typename MODULE> void ModuleMgr<MODULE>::updateConfig() { // module list update (base directory modified -> full, otherwise -> light) if (m_dirBase.checkModified()) updateModuleListFull(); else updateModuleListLight(); } /// light update of module list, i.e. call all modules in current list template<typename MODULE> void ModuleMgr<MODULE>::updateModuleListLight() { typename ModuleList::iterator itEntry; for (itEntry = m_moduleList.begin(); itEntry != m_moduleList.end(); ++itEntry) { // call module to check for changes itEntry->m_pModule->updateConfig(); } // for itEntry } /// full update of module list, i.e. scan all subdirs in base directory template<typename MODULE> void ModuleMgr<MODULE>::updateModuleListFull() { // get list of subdirectories in base directory typedef std::list<std::string> Subdirlist; Subdirlist curSubdirs; m_dirBase.getEntries(Directory::TypeSubdir, curSubdirs); // walk through current module list and subdir list simultaneously Subdirlist::const_iterator itSubdir = curSubdirs.begin(); typename ModuleList::iterator itEntry = m_moduleList.begin(); while (itSubdir != curSubdirs.end() || itEntry != m_moduleList.end()) { // new module inserted if (itEntry == m_moduleList.end() || (itSubdir != curSubdirs.end() && *itSubdir < itEntry->m_name)) { // create module Entry entry(*itSubdir); entry.createModule(*this); // insert module list entry m_moduleList.insert(itEntry, entry); // advance to next subdir ++itSubdir; } // module removed else if (itSubdir == curSubdirs.end() || *itSubdir > itEntry->m_name) { // remove entry itEntry->destroyModule(); itEntry = m_moduleList.erase(itEntry); // do not advance to next subdir } // module stayed in list else { // call module to check for changes itEntry->m_pModule->updateConfig(); // advance to next subdir and next entry ++itSubdir; ++itEntry; } } // while itSubdir itEntry } /* #################### # ModuleMgr::Entry # #################### */ /// constructor template<typename MODULE> ModuleMgr<MODULE>::Entry::Entry(const std::string &name): m_name(name), m_pModule(NULL) { } /// create module template<typename MODULE> void ModuleMgr<MODULE>::Entry::createModule(ModuleMgr &mgr) { destroyModule(); m_pModule = new MODULE(mgr.m_callMgr, mgr.m_streamMgr, mgr.m_dirBase.getSubdir(m_name)); } /// destroy module template<typename MODULE> void ModuleMgr<MODULE>::Entry::destroyModule() { if (m_pModule) { delete m_pModule; m_pModule = NULL; } } } // namespace Blinker #endif // #ifndef MODULEMGR_IMPL_H