From: stephan b. <sg...@us...> - 2004-12-25 19:33:55
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses/Plugin In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22956/include/pclasses/Plugin Modified Files: Plugin.h Log Message: Replaced PluginsList with PluginsMap, mapping DLL names to SharedLibs. Added an #if to allow the blocking out of the FactoryInstanceHook, to disable DLL lookups in the default API. Index: Plugin.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/Plugin/Plugin.h,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- Plugin.h 25 Dec 2004 16:47:11 -0000 1.8 +++ Plugin.h 25 Dec 2004 19:33:46 -0000 1.9 @@ -3,6 +3,7 @@ #include <list> #include <string> +#include <map> #include "pclasses/pclasses-config.h" #include "pclasses/Factory.h" #include "pclasses/NonCopyable.h" @@ -41,6 +42,11 @@ typedef typename ParentType::ResultType ResultType; /** + A type used to hold SharedLib handles. + */ + typedef std::map<std::string,::P::System::SharedLib *> PluginsMap; + + /** Creates a PluginManager with a default search path and a platform-specific DLL file extension added to it's searchPath(). @@ -56,7 +62,7 @@ virtual ~PluginManager() throw() { // reminder: openSharedLib() owns - // the objects in m_holder. + // the objects in m_map. } @@ -138,7 +144,7 @@ } ::P::System::SharedLib * shl = ::P::System::openSharedLib( fn ); //CERR << "Opened fn="<<fn<<"\n"; - this->m_holder.push_back( shl ); + m_smap.insert( std::make_pair( fn, shl ) ); return shl; } @@ -146,7 +152,7 @@ Returns a shared instance to an object of this type. Note that this shadows a static member of the same - name in the base class! + name in the base class, but returns a more-derived type. */ static ThisType & instance() { @@ -169,10 +175,6 @@ return this->m_path; } - /** - A type used to hold SharedLib handles. - */ - typedef std::list< ::P::System::SharedLib * > PluginList; /** Returns the list of SharedLibs opened by this type. @@ -186,15 +188,22 @@ provided by the DLL, they will crash when they use it next. In practice it is safest to not close the DLLs and to let the OS do it at app shutdown. + + ACHTUNG: this code has no way of knowing when + a SharedLib dies off, and any given entry + in the list may refer to a no-longer-loaded + lib. That said, the default lib-opening routines + do not destroy SharedLibs loaded via this class + until post-main(). */ - PluginList & sharedLibs() + PluginsMap & loadedPlugins() { - return this->m_holder; + return this->m_smap; } - const PluginList & sharedLibs() const + const PluginsMap & loadedPlugins() const { - return this->m_holder; + return this->m_smap; } /** @@ -216,7 +225,7 @@ this->m_path.addPath( PCLASSES_LIB_DIR ); this->m_path.addExtension(std::string(".")+::P::System::SharedLib::extension()); } - PluginList m_holder; + PluginsMap m_smap; ::P::System::PathFinder m_path; // ^^^^ todo: consider making this static. }; @@ -231,7 +240,8 @@ } /** - Convenience function to add a plugin dir to pluginManager<T>(). + Convenience function returning + pluginManager<T>().addPluginDir(pluginname). */ template <typename T> void addPluginDir( const std::string & path ) throw(RuntimeError) @@ -240,7 +250,8 @@ } /** - Convenience function to open a plugin using pluginManager<T>(). + Convenience function returning + pluginManager<T>().addPlugin(pluginname). */ template <typename T> ::P::System::SharedLib * @@ -250,7 +261,8 @@ } /** - Convenience function to call pluginManager<T>().findPlugin(name). + Convenience function returning + pluginManager<T>().findPlugin(name). */ template <typename T> std::string findPlugin( const std::string & name ) @@ -262,6 +274,10 @@ } // namespace Plugin +#ifndef PCLASSES_DEFAULT_FACTORY_KNOWS_SHAREDLIBS +# define PCLASSES_DEFAULT_FACTORY_KNOWS_SHAREDLIBS 1 +#endif +#if PCLASSES_DEFAULT_FACTORY_KNOWS_SHAREDLIBS namespace Hook { /** @@ -277,6 +293,8 @@ typedef FactoryInstanceHook<FactoryType> ThisType; /** + Initializes the passed-in factory. i.e., it does + nothing. It's here for demonstration purposes. */ void operator()( FactoryType & ) throw() { @@ -296,6 +314,8 @@ }; } // ns Hook +#endif // PCLASSES_DEFAULT_FACTORY_KNOWS_SHAREDLIBS + } // namespace P |