00001 #ifndef MODULESNAVIGATOR_H_ 00002 #define MODULESNAVIGATOR_H_ 00003 00004 #include <map> 00005 #include <string> 00006 #include <iostream> 00007 #include <typeinfo> // std::bad_cast 00008 #include "ModulesFactory.h" 00009 #include "ModulesReader.h" 00010 class TFile; 00011 00012 namespace modules{ 00013 class navigator; 00014 class reader; 00015 // helper typedefs 00016 typedef std::pair<std::string,modules::BaseModule*> ordered_element; 00017 typedef std::vector<ordered_element > ordered_list; 00018 typedef std::multimap<std::string,BaseModule*> list; 00019 typedef ordered_list::iterator iterator; 00020 typedef ordered_list::const_iterator const_iterator; 00021 } 00022 00027 class modules::navigator{ 00028 00029 navigator(); 00030 ~navigator(){}; 00031 00032 public: 00034 static inline navigator* Instance(); 00035 00036 public: 00039 int LoadConfigFile(const char* filename); 00040 00043 int MakeModules(); 00044 00045 public: 00047 // @return NULL if the module doesn't exist 00048 inline BaseModule* GetModule(const std::string& module)const; 00049 00053 inline BaseModule* GetModule(const int& i)const; 00054 00055 // Get a named module and cast it to the correct type 00057 template <typename T> 00058 inline T* GetModule(const std::string& module)const; 00059 00061 int HowMany(const std::string& name)const; 00062 00065 bool Before(const std::string& first, const std::string& second)const; 00066 00068 modules::iterator Begin(){return fModules.begin();}; 00070 modules::const_iterator Begin()const{return fModules.begin();}; 00072 modules::iterator End(){return fModules.end();}; 00074 modules::const_iterator End()const{return fModules.end();}; 00076 unsigned int Size()const{return fModules.size();}; 00078 unsigned int GetNumModules()const{return fModules.size();}; 00079 00080 void SetDebug(bool d=true){fDebug=d;} 00081 bool Debug()const{return fDebug;} 00082 00083 bool DumpInputFile()const {return fDumpInputFile;} 00084 00086 void SetOutFile(TFile* file){fOutFile=file;} 00087 00088 private: 00089 void AddModule(const std::string&, BaseModule*); 00090 00091 private: 00092 bool fModulesLoaded; 00093 bool fModulesMade; 00094 bool fDebug; 00095 bool fDumpInputFile; 00096 modules::ordered_list fModules; 00097 modules::list fModulesSearch; 00098 modules::reader fModulesFile; 00099 TFile* fOutFile; 00100 }; 00101 00102 inline modules::navigator* modules::navigator::Instance(){ 00103 static modules::navigator* instance=new navigator(); 00104 return instance; 00105 } 00106 00107 inline modules::BaseModule* modules::navigator::GetModule(const std::string& module)const{ 00108 modules::list::const_iterator mod=fModulesSearch.find(module.c_str()); 00109 if(mod==fModulesSearch.end()) return NULL; 00110 return mod->second; 00111 } 00112 00113 template <typename T> 00114 inline T* modules::navigator::GetModule(const std::string& module)const{ 00115 BaseModule* base_mod=GetModule(module); 00116 if(!base_mod) return NULL; 00117 T* mod=NULL; 00118 try 00119 { 00120 mod=dynamic_cast<T*>(base_mod); 00121 } 00122 catch (std::bad_cast& bc) 00123 { 00124 std::cerr << "ModulesNavigator::GetModule() bad_cast caught: " << bc.what() << std::endl; 00125 return NULL; 00126 } 00127 return mod; 00128 } 00129 00130 inline modules::BaseModule* modules::navigator::GetModule(const int& i)const{ 00131 return fModules[i].second; 00132 } 00133 00134 00135 #endif //MODULESNAVIGATOR_H_