00001 // -*- C++ -*- 00020 #ifndef ModuleManager_h 00021 #define ModuleManager_h 00022 00023 // STL includes 00024 #include <string> 00025 #include <vector> 00026 #include <map> 00027 00028 // ACE includes 00029 #include <coil/DynamicLib.h> 00030 00031 // RTC includes 00032 #include <rtm/Manager.h> 00033 #include <coil/Properties.h> 00034 #include <rtm/ObjectManager.h> 00035 00036 00037 #define CONFIG_EXT "manager.modules.config_ext" 00038 #define CONFIG_PATH "manager.modules.config_path" 00039 #define DETECT_MOD "manager.modules.detect_loadable" 00040 #define MOD_LOADPTH "manager.modules.load_path" 00041 #define INITFUNC_SFX "manager.modules.init_func_suffix" 00042 #define INITFUNC_PFX "manager.modules.init_func_prefix" 00043 #define ALLOW_ABSPATH "manager.modules.abs_path_allowed" 00044 #define ALLOW_URL "manager.modules.download_allowed" 00045 #define MOD_DWNDIR "manager.modules.download_dir" 00046 #define MOD_DELMOD "manager.modules.download_cleanup" 00047 #define MOD_PRELOAD "manager.modules.preload" 00048 00049 #ifdef WIN32 00050 #pragma warning( disable : 4290 ) 00051 #endif 00052 00053 namespace RTC 00054 { 00074 class ModuleManager 00075 { 00076 public: 00098 ModuleManager(coil::Properties& prop); 00099 00111 ~ModuleManager(void); 00112 00120 struct Error 00121 { 00122 Error(const std::string& _reason) 00123 : reason(_reason) {} 00124 std::string reason; 00125 }; 00126 00135 struct NotFound 00136 { 00137 NotFound(const std::string& _name) 00138 : name(_name) {} 00139 std::string name; 00140 }; 00141 00150 struct FileNotFound 00151 : public NotFound 00152 { 00153 FileNotFound(const std::string& _name) 00154 : NotFound(_name) {} 00155 }; 00156 00165 struct ModuleNotFound 00166 : public NotFound 00167 { 00168 ModuleNotFound(const std::string& _name) 00169 : NotFound(_name) {} 00170 }; 00171 00180 struct SymbolNotFound 00181 : public NotFound 00182 { 00183 SymbolNotFound(const std::string& _name) 00184 : NotFound(_name) {} 00185 }; 00186 00195 struct NotAllowedOperation 00196 : public Error 00197 { 00198 NotAllowedOperation(const std::string& _reason) 00199 : Error(_reason) {} 00200 }; 00201 00210 struct InvalidArguments 00211 : public Error 00212 { 00213 InvalidArguments(const std::string& _reason) 00214 : Error(_reason) {} 00215 }; 00216 00225 struct InvalidOperation 00226 : public Error 00227 { 00228 InvalidOperation(const std::string& _reason) 00229 : Error(_reason) {} 00230 }; 00231 typedef void (*ModuleInitFunc)(Manager*); 00232 00278 std::string load(const std::string& file_name); 00279 00307 std::string load(const std::string& file_name, const std::string& init_func); 00308 00326 void unload(const std::string& file_name); 00327 00341 void unloadAll(); 00342 00350 void* symbol(const std::string& file_name, const std::string& func_name) 00351 throw (ModuleNotFound, SymbolNotFound); 00352 00370 void setLoadpath(const std::vector<std::string>& load_path); 00371 00389 inline std::vector<std::string> getLoadPath() 00390 { 00391 return m_loadPath; 00392 } 00393 00411 void addLoadpath(const std::vector<std::string>& load_path); 00412 00430 std::vector<coil::Properties> getLoadedModules(); 00431 00450 std::vector<coil::Properties> getLoadableModules(); 00451 00465 inline void allowAbsolutePath() 00466 { 00467 m_absoluteAllowed = true; 00468 } 00469 00483 inline void disallowAbsolutePath() 00484 { 00485 m_absoluteAllowed = false; 00486 } 00487 00505 inline void allowModuleDownload() 00506 { 00507 m_downloadAllowed = true; 00508 } 00509 00523 inline void disallowModuleDownload() 00524 { 00525 m_downloadAllowed = false; 00526 } 00527 00551 std::string findFile(const std::string& fname, 00552 const std::vector<std::string>& load_path); 00553 00575 bool fileExist(const std::string& filename); 00576 00598 std::string getInitFuncName(const std::string& file_path); 00599 00600 protected: 00601 00602 struct DLLEntity 00603 { 00604 coil::Properties properties; 00605 coil::DynamicLib dll; 00606 }; 00607 00608 typedef std::vector<std::string> StringVector; 00609 typedef StringVector::iterator StringVectorItr; 00610 typedef StringVector::const_iterator StringVectorConstItr; 00611 00612 typedef std::vector<DLLEntity> DllMap; 00613 typedef DllMap::iterator DllMapItr; 00614 typedef DllMap::const_iterator DllMapConstItr; 00615 00623 coil::Properties& m_properties; 00624 00632 class DllPred 00633 { 00634 std::string m_filepath; 00635 public: 00636 DllPred(const char* filepath) : m_filepath(filepath) {} 00637 DllPred(const DLLEntity* dll) : m_filepath(dll->properties["file_path"]) {} 00638 bool operator()(DLLEntity* dllentity) 00639 { 00640 return m_filepath == dllentity->properties.getProperty("file_path"); 00641 } 00642 }; 00643 // DllMap m_modules; 00644 ObjectManager<const char*, DLLEntity, DllPred> m_modules; 00645 00653 StringVector m_loadPath; 00661 StringVector m_configPath; 00669 bool m_downloadAllowed; 00677 bool m_absoluteAllowed; 00678 00686 std::string m_initFuncSuffix; 00694 std::string m_initFuncPrefix; 00695 00696 00697 class UnloadPred 00698 { 00699 public: 00700 UnloadPred(){} 00701 void operator()(DLLEntity* dll) 00702 { 00703 dll->dll.close(); 00704 delete dll; 00705 } 00706 }; 00707 00708 }; // class ModuleManager 00709 }; // namespace RTC 00710 00711 #ifdef WIN32 00712 #pragma warning( default : 4290 ) 00713 #endif 00714 00715 #endif // ModuleManager_h