00001 // -*- C++ -*- 00019 /* 00020 * $Log: ModuleManager.h,v $ 00021 * Revision 1.2.4.1 2007/09/22 10:29:38 n-ando 00022 * The argument of the module init function was modified. 00023 * 00024 * Revision 1.2 2006/10/17 10:06:47 n-ando 00025 * Now class ModuleManager is in RTC namespace. 00026 * 00027 * Revision 1.1 2006/09/20 08:47:33 n-ando 00028 * The first commit of module management class. 00029 * 00030 * 00031 */ 00032 00033 #ifndef ModuleManager_h 00034 #define ModuleManager_h 00035 00036 // STL includes 00037 #include <string> 00038 #include <vector> 00039 #include <map> 00040 00041 // ACE includes 00042 #include <ace/DLL.h> 00043 00044 // RTC includes 00045 #include <rtm/Manager.h> 00046 #include <rtm/Properties.h> 00047 00048 00049 #define CONFIG_EXT "manager.modules.config_ext" 00050 #define CONFIG_PATH "manager.modules.config_path" 00051 #define DETECT_MOD "manager.modules.detect_loadable" 00052 #define MOD_LOADPTH "manager.modules.load_path" 00053 #define INITFUNC_SFX "manager.modules.init_func_suffix" 00054 #define INITFUNC_PFX "manager.modules.init_func_prefix" 00055 #define ALLOW_ABSPATH "manager.modules.abs_path_allowed" 00056 #define ALLOW_URL "manager.modules.download_allowed" 00057 #define MOD_DWNDIR "manager.modules.download_dir" 00058 #define MOD_DELMOD "manager.modules.download_cleanup" 00059 #define MOD_PRELOAD "manager.modules.preload" 00060 00061 namespace RTC 00062 { 00075 class ModuleManager 00076 { 00077 public: 00078 ModuleManager(Properties& prop); 00079 00080 ~ModuleManager(); 00081 00082 struct Error 00083 { 00084 Error(const std::string& _reason) 00085 : reason(_reason) {} 00086 std::string reason; 00087 }; 00088 00089 struct NotFound 00090 { 00091 NotFound(const std::string& _name) 00092 : name(_name) {} 00093 std::string name; 00094 }; 00095 00096 struct FileNotFound 00097 : public NotFound 00098 { 00099 FileNotFound(const std::string& _name) 00100 : NotFound(_name) {} 00101 }; 00102 00103 struct ModuleNotFound 00104 : public NotFound 00105 { 00106 ModuleNotFound(const std::string& _name) 00107 : NotFound(_name) {} 00108 }; 00109 00110 struct SymbolNotFound 00111 : public NotFound 00112 { 00113 SymbolNotFound(const std::string& _name) 00114 : NotFound(_name) {} 00115 }; 00116 00117 struct NotAllowedOperation 00118 : public Error 00119 { 00120 NotAllowedOperation(const std::string& _reason) 00121 : Error(_reason) {} 00122 }; 00123 00124 struct InvalidArguments 00125 : public Error 00126 { 00127 InvalidArguments(const std::string& _reason) 00128 : Error(_reason) {} 00129 }; 00130 00131 struct InvalidOperation 00132 : public Error 00133 { 00134 InvalidOperation(const std::string& _reason) 00135 : Error(_reason) {} 00136 }; 00137 typedef void (*ModuleInitFunc)(Manager*); 00163 std::string load(const std::string& file_name); 00164 std::string load(const std::string& file_name, const std::string& init_func); 00165 00173 void unload(const std::string& file_name); 00174 00175 00183 void unloadAll(); 00184 00185 00193 void* symbol(const std::string& file_name, const std::string& func_name) 00194 throw (ModuleNotFound, SymbolNotFound); 00195 00203 void setLoadpath(const std::vector<std::string>& load_path); 00204 inline std::vector<std::string> getLoadPath() 00205 { 00206 return m_loadPath; 00207 } 00208 00216 void addLoadpath(const std::vector<std::string>& load_path); 00217 00218 00226 std::vector<std::string> getLoadedModules(); 00227 00228 00236 std::vector<std::string> getLoadableModules(); 00237 00238 00246 inline void allowAbsolutePath() 00247 { 00248 m_absoluteAllowed = true; 00249 } 00250 00258 inline void disallowAbsolutePath() 00259 { 00260 m_absoluteAllowed = false; 00261 } 00262 00263 00271 inline void allowModuleDownload() 00272 { 00273 m_downloadAllowed = true; 00274 } 00275 00276 00284 inline void disallowModuleDownload() 00285 { 00286 m_downloadAllowed = false; 00287 } 00288 00289 00297 std::string findFile(const std::string& fname, 00298 const std::vector<std::string>& load_path); 00299 00300 00308 bool fileExist(const std::string& filename); 00309 00310 00318 std::string getInitFuncName(const std::string& file_path); 00319 00320 00321 protected: 00322 struct DLL 00323 { 00324 ACE_DLL dll; 00325 }; 00326 00327 typedef std::vector<std::string> StringVector; 00328 typedef StringVector::iterator StringVectorItr; 00329 typedef StringVector::const_iterator StringVectorConstItr; 00330 00331 typedef std::map<std::string, DLL> DllMap; 00332 typedef DllMap::iterator DllMapItr; 00333 typedef DllMap::const_iterator DllMapConstItr; 00334 00335 Properties& m_properties; 00336 00337 DllMap m_modules; 00338 00339 StringVector m_loadPath; 00340 StringVector m_configPath; 00341 bool m_downloadAllowed; 00342 bool m_absoluteAllowed; 00343 00344 std::string m_initFuncSuffix; 00345 std::string m_initFuncPrefix; 00346 00347 }; // class ModuleManager 00348 }; // namespace RTC 00349 00350 #endif // ModuleManager_h 00351