BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
9a96213
Branches
Tags
master
Blinker
src
noarch
ModuleMgr.h
whitespace fixes
Stefan Schuermans
commited
9a96213
at 2011-10-25 21:18:20
ModuleMgr.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_H #define MODULEMGR_H #include <list> #include "CallMgr.h" #include "Directory.h" #include "Module.h" #include "StreamMgr.h" #include "TimeCallee.h" namespace Blinker { /// manager for modules of one type template<typename MODULE> class ModuleMgr: public TimeCallee { protected: /// module list entry struct Entry { std::string m_name; ///< name of module MODULE *m_pModule; ///< module object Entry(const std::string &name); ///< constructor void createModule(ModuleMgr &mgr); ///< create module void destroyModule(); ///< destroy module }; /// module list typedef std::list<Entry> ModuleList; public: /** * @brief constructor * @param[in] callMgr callback manager * @param[in] streamMgr stream manager * @param[in] dirBase base directory */ ModuleMgr(CallMgr &callMgr, StreamMgr &streamMgr, const Directory &dirBase); /// destructor ~ModuleMgr(); private: /// copy constructor disabled ModuleMgr(const ModuleMgr &that); /// assignment operator disabled const ModuleMgr & operator=(const ModuleMgr &that); public: /// callback when requsted time reached virtual void timeCall(); protected: /// check for update of configuration void updateConfig(); /// light update of module list, i.e. call all modules in current list void updateModuleListLight(); /// full update of module list, i.e. scan all subdirs in base directory void updateModuleListFull(); protected: CallMgr &m_callMgr; ///< callback manager StreamMgr &m_streamMgr; ///< stream manager Directory m_dirBase; ///< base directory ModuleList m_moduleList; ///< module list }; // class ModuleMgr /* ############# # ModuleMgr # ############# */ /** * @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_H