📄 plugin_manager.hpp
字号:
/// @param interface_name /// Target interface name /// @param plugin_name /// Plugin family name (dbapi, xloader, etc) /// @param driver_name /// Name of the driver (dblib, id1, etc) /// @param version /// Interface version CPluginManager_DllResolver (const string& interface_name, const string& driver_name = kEmptyStr, const CVersionInfo& version = CVersionInfo::kAny); // virtual ~CPluginManager_DllResolver(void); /// Search for plugin DLLs, resolve entry points. /// /// @param paths /// List of directories to scan for DLLs /// @return /// Reference on DLL resolver holding all entry points CDllResolver& Resolve(const vector<string>& paths); /// Search for plugin DLLs, resolve entry points. /// /// @param paths /// Path to scan for DLLs /// @return /// Reference on DLL resolver holding all entry points CDllResolver& Resolve(const string& path); /// Return dll file name. Name does not include path. /// /// Example: /// "ncbi_plugin_dbapi_ftds_3_1_7". /// "ncbi_pulgin_dbapi_ftds.so.3.1.7" /// In this case, the DLL will be searched for in the standard /// DLL search paths, with automatic addition of any platform-specific /// prefixes and suffixes. /// /// @param driver_name /// Driver name ("id1", "lds", etc) /// @param version /// Requested version of the driver virtual string GetDllName(const string& interface_name, const string& driver_name = kEmptyStr, const CVersionInfo& version = CVersionInfo::kAny) const; /// Return DLL name mask /// /// DLL name mask is used for DLL file search. /// /// Example: /// "ncbi_plugin_objmgr_*.dll" /// "ncbi_plugin_dbapi_ftds.so.*" virtual string GetDllNameMask(const string& interface_name, const string& driver_name = kEmptyStr, const CVersionInfo& version = CVersionInfo::kAny) const; /// Return DLL entry point name: /// /// Default name pattern is: /// - "NCBI_EntryPoint_interface_driver" /// Alternative variants: /// - "NCBI_EntryPoint" /// - "NCBI_EntryPoint_interface" /// - "NCBI_EntryPoint_driver" /// /// @sa GetEntryPointPrefix virtual string GetEntryPointName(const string& interface_name = kEmptyStr, const string& driver_name = kEmptyStr) const; /// Return DLL entry point prefix ("NCBI_EntryPoint") virtual string GetEntryPointPrefix() const; /// Return DLL file name prefix ("ncbi_plugin") virtual string GetDllNamePrefix() const; /// Set DLL file name prefix virtual void SetDllNamePrefix(const string& prefix); /// Return name of the driver const string& GetDriverName() const { return m_DriverName; }protected: CDllResolver* GetCreateDllResolver(); CDllResolver* CreateDllResolver() const;protected: string m_DllNamePrefix; string m_EntryPointPrefix; string m_InterfaceName; string m_DriverName; CVersionInfo m_Version; CDllResolver* m_DllResolver;};/* @} *//////////////////////////////////////////////////////////////////////////////// IMPLEMENTATION of INLINE functions/////////////////////////////////////////////////////////////////////////////template <class TClass, class TIfVer>typename CPluginManager<TClass, TIfVer>::TClassFactory* CPluginManager<TClass, TIfVer>::GetFactory(const string& driver, const CVersionInfo& version){ CFastMutexGuard guard(m_Mutex); TClassFactory* cf = 0; // Search among already registered factories cf = FindClassFactory(driver, version); if (cf) { return cf; } if (!m_BlockResolution) { typename TStringSet::const_iterator it = m_FreezeResolutionDrivers.find(driver); if (it == m_FreezeResolutionDrivers.end()) { // Trying to resolve the driver's factory Resolve(driver, version); // Re-scanning factories... cf = FindClassFactory(driver, version); if (cf) { return cf; } } } NCBI_THROW(CPluginManagerException, eResolveFailure, "Cannot resolve class factory");}template <class TClass, class TIfVer>typename CPluginManager<TClass, TIfVer>::TClassFactory* CPluginManager<TClass, TIfVer>::FindClassFactory(const string& driver, const CVersionInfo& version){ TClassFactory* best_factory = 0; int best_major = -1; int best_minor = -1; int best_patch_level = -1; NON_CONST_ITERATE(typename set<TClassFactory*>, it, m_Factories) { TClassFactory* cf = *it; typename TClassFactory::TDriverList drv_list; if (!cf) continue; cf->GetDriverVersions(drv_list); NON_CONST_ITERATE(typename TClassFactory::TDriverList, it2, drv_list) { typename TClassFactory::SDriverInfo& drv_info = *it2; if (!driver.empty()) { if (driver != drv_info.name) { continue; } } const CVersionInfo& vinfo = drv_info.version; if (IsBetterVersion(version, vinfo, best_major, best_minor, best_patch_level)) { best_factory = cf; } } } return best_factory;}template <class TClass, class TIfVer>void CPluginManager<TClass, TIfVer>::RegisterFactory(TClassFactory& factory){ CFastMutexGuard guard(m_Mutex); m_Factories.insert(&factory);}template <class TClass, class TIfVer>bool CPluginManager<TClass, TIfVer>::UnregisterFactory(TClassFactory& factory){ CFastMutexGuard guard(m_Mutex); typename set<TClassFactory*>::iterator it = m_Factories.find(&factory); if (it != m_Factories.end()) { delete *it; m_Factories.erase(it); } return true; //?}template <class TClass, class TIfVer>void CPluginManager<TClass, TIfVer>::RegisterWithEntryPoint(FNCBI_EntryPoint plugin_entry_point){ TDriverInfoList drv_list; plugin_entry_point(drv_list, eGetFactoryInfo); if ( !drv_list.empty() ) { plugin_entry_point(drv_list, eInstantiateFactory); NON_CONST_ITERATE(typename TDriverInfoList, it, drv_list) { if (it->factory) { RegisterFactory(*(it->factory)); } } }}template <class TClass, class TIfVer>void CPluginManager<TClass, TIfVer>::AddResolver(CPluginManager_DllResolver* resolver){ _ASSERT(resolver); m_Resolvers.push_back(resolver);}template <class TClass, class TIfVer>CPluginManager_DllResolver* CPluginManager<TClass, TIfVer>::DetachResolver(CPluginManager_DllResolver* resolver){ NON_CONST_ITERATE(TDllResolvers, it, m_Resolvers) { if (resolver == *it) { m_Resolvers.erase(it); return resolver; } } return 0;}template <class TClass, class TIfVer>void CPluginManager<TClass, TIfVer>::AddDllSearchPath(const string& path){ m_DllSearchPaths.push_back(path);}template <class TClass, class TIfVer>void CPluginManager<TClass, TIfVer>::FreezeResolution(const string& driver, bool value){ if (value) { m_FreezeResolutionDrivers.insert(driver); } else { m_FreezeResolutionDrivers.erase(driver); }}template <class TClass, class TIfVer>void CPluginManager<TClass, TIfVer>::Resolve(const string& /*driver*/, const CVersionInfo& /*version*/){ vector<CDllResolver*> resolvers; // Run all resolvers to search for driver ITERATE(vector<CPluginManager_DllResolver*>, it, m_Resolvers) { CDllResolver& dll_resolver = (*it)->Resolve(m_DllSearchPaths); resolvers.push_back(&dll_resolver); } // Now choose the DLL entry point to register the class factory NON_CONST_ITERATE(vector<CDllResolver*>, it, resolvers) { CDllResolver::TEntries& entry_points = (*it)->GetResolvedEntries(); NON_CONST_ITERATE(CDllResolver::TEntries, ite, entry_points) { CDllResolver::SResolvedEntry& entry = *ite; if (entry.entry_points.empty()) { continue; } CDllResolver::SNamedEntryPoint& epoint = entry.entry_points[0]; // TODO: // check if entry point provides the required interface-driver-version // and do not register otherwise... if (epoint.entry_point.func) { FNCBI_EntryPoint ep = (FNCBI_EntryPoint)epoint.entry_point.func; RegisterWithEntryPoint(ep); m_RegisteredEntries.push_back(entry); } } entry_points.resize(0); }}template <class TClass, class TIfVer>CPluginManager<TClass, TIfVer>::~CPluginManager(){ {{ typename set<TClassFactory*>::iterator it = m_Factories.begin(); typename set<TClassFactory*>::iterator it_end = m_Factories.end(); for (; it != it_end; ++it) { TClassFactory* f = *it; delete f; } }} {{ typename vector<CPluginManager_DllResolver*>::iterator it = m_Resolvers.begin(); typename vector<CPluginManager_DllResolver*>::iterator it_end = m_Resolvers.end(); for (; it != it_end; ++it) { CPluginManager_DllResolver* r = *it; delete r; } }} NON_CONST_ITERATE(TResolvedEntries, it, m_RegisteredEntries) { delete it->dll; }}END_NCBI_SCOPE/* * =========================================================================== * $Log: plugin_manager.hpp,v $ * Revision 1000.7 2004/06/01 19:08:16 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.22 * * Revision 1.22 2004/04/26 14:46:36 ucko * Fix a typo in FreezeResolution, and make sure to give * UnregisterFactory a return value per its prototype. * * Revision 1.21 2004/03/19 19:16:20 kuznets * Added group of functions to better control DLL resolution (FreezeResolution) * * Revision 1.20 2004/02/10 20:21:13 ucko * Make the interface version class a template parameter with the * appropriate default value to work around limitations in IBM's * VisualAge compiler. (It otherwise effectively ignores specializations * of CInterfaceVersion that follow the templates that use them.) * * Revision 1.19 2004/01/13 17:21:23 kuznets * Class factory CreateInstance method received an additional parameter * TPluginManagerParamTree (to specify initialization parameters or prefrences) * * Revision 1.18 2003/12/02 12:44:39 kuznets * Fixed minor compilation issue. * * Revision 1.17 2003/12/01 19:53:06 kuznets * Reflecting changes in CDllResolver * * Revision 1.16 2003/11/19 15:46:04 kuznets * Removed clumsy function pointer conversion from plugin manager. * (it is now covered by CDll) * * Revision 1.15 2003/11/19 13:48:20 kuznets * Helper classes migrated into a plugin_manager_impl.hpp * * Revision 1.14 2003/11/18 17:09:25 kuznets * Fixing compilation warnings * * Revision 1.13 2003/11/18 15:26:29 kuznets * Numerous fixes here and there as a result of testing and debugging. * * Revision 1.12 2003/11/17 17:04:11 kuznets * Cosmetic fixes * * Revision 1.11 2003/11/12 18:56:53 kuznets * Implemented dll resolution. * * Revision 1.10 2003/11/07 17:02:48 kuznets * Drafted CPluginManager_DllResolver. * * Revision 1.9 2003/11/03 20:08:01 kuznets * Fixing various compiler warnings * * Revision 1.8 2003/11/03 17:52:00 kuznets * Added CSimpleClassFactoryImpl template. * Helps quickly implement basic PM compatible class factory. * * Revision 1.7 2003/11/03 16:32:58 kuznets * Cleaning the code to be compatible with GCC, WorkShop 53 and MSVC at the * same time... * * Revision 1.6 2003/10/31 19:53:52 kuznets * +CHostEntryPointImpl * * Revision 1.5 2003/10/30 20:03:49 kuznets * Work in progress. Added implementations of CPluginManager<> methods. * * Revision 1.4 2003/10/29 23:35:46 vakatov * Just starting with CDllResolver... * * Revision 1.3 2003/10/29 19:34:43 vakatov * Comment out unfinished defined APIs (using "#if 0") * * Revision 1.2 2003/10/28 22:29:04 vakatov * Draft-done with: * general terminology * CInterfaceVersion<> * NCBI_PLUGIN_VERSION() * IClassFactory<> * CPluginManager<> * TODO: * Host-related API * DLL resolution * * Revision 1.1 2003/10/28 00:12:23 vakatov * Initial revision * * Work-in-progress, totally unfinished. * Note: DLL resolution shall be split and partially moved to the NCBIDLL. * * =========================================================================== */#endif /* CORELIB___PLUGIN_MANAGER__HPP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -