1 #include <cpPlugins/Interface/Plugins.h>
2 #include <cpPlugins/OS/DLLManager.h>
3 #include <cpPlugins/OS/DirContents.h>
4 #include <cpPlugins/Utility.h>
6 // -------------------------------------------------------------------------
7 cpPlugins::Interface::Plugins::Pointer
8 cpPlugins::Interface::Plugins::m_Singleton = NULL;
10 // -------------------------------------------------------------------------
11 cpPlugins::Interface::Plugins::
12 Pointer cpPlugins::Interface::Plugins::
15 if( Self::m_Singleton.IsNull( ) )
16 Self::m_Singleton = new Self( );
17 return( Self::m_Singleton );
20 // -------------------------------------------------------------------------
21 itk::LightObject::Pointer cpPlugins::Interface::Plugins::
22 CreateAnother( ) const
24 itk::LightObject::Pointer smartPtr;
25 smartPtr = Self::m_Singleton;
29 // -------------------------------------------------------------------------
30 cpPlugins::Interface::Plugins::
31 Pointer cpPlugins::Interface::Plugins::
34 return( Self::m_Singleton );
37 // -------------------------------------------------------------------------
38 const cpPlugins::Interface::Plugins::
39 TFilters& cpPlugins::Interface::Plugins::
42 return( this->m_Filters );
45 // -------------------------------------------------------------------------
46 void cpPlugins::Interface::Plugins::
47 LoadPluginsFile( const std::string& libname )
49 std::map< std::string, std::set< std::string > > filters;
50 cpPlugins::OS::DLLManager::GetPluginsLibraryContents( filters, libname );
51 THandlers zero( NULL, NULL );
52 for( auto i : filters )
53 for( auto j : i.second )
54 this->m_Filters[ i.first ][ j ] = TLibData( libname, zero );
57 // -------------------------------------------------------------------------
58 void cpPlugins::Interface::Plugins::
59 LoadPluginsDirectory( const std::string& dir )
61 // Create a globbing pattern
62 std::stringstream glob;
63 glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
65 // Get possible shared libraries
66 std::set< std::string > files =
67 cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
68 this->m_PluginsPaths.insert( dir );
70 try { this->LoadPluginsFile( f ); } catch( ... ) { }
73 // -------------------------------------------------------------------------
74 void cpPlugins::Interface::Plugins::
77 // Create a globbing pattern
78 std::stringstream glob;
79 glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT;
81 // Update paths and get possible shared libraries
82 this->_ReadPluginsPathsVariable( );
83 for( auto dir : this->m_PluginsPaths )
85 std::set< std::string > files =
86 cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) );
88 try { this->LoadPluginsFile( f ); } catch( ... ) { }
93 // -------------------------------------------------------------------------
94 void cpPlugins::Interface::Plugins::
95 GuessEnvironment( const std::string& dir )
97 std::stringstream fname;
98 fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
100 if( cpPlugins::Read( buffer, fname.str( ) ) )
102 std::istringstream input( buffer );
103 for( std::string line; std::getline( input, line ); )
104 this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( line ) );
109 // -------------------------------------------------------------------------
110 bool cpPlugins::Interface::Plugins::
111 SaveEnvironment( const std::string& dir )
113 std::stringstream buffer;
114 for( auto p : this->m_PluginsPaths )
115 buffer << p << std::endl;
116 std::stringstream fname;
117 fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS;
118 return( cpPlugins::Write( buffer.str( ), fname.str( ) ) );
121 // -------------------------------------------------------------------------
122 cpPlugins::BaseObjects::ProcessObject::Pointer
123 cpPlugins::Interface::Plugins::
124 CreateFilter( const std::string& category, const std::string& name )
126 typedef void* ( *_TCreator )( );
127 typedef cpPlugins::BaseObjects::ProcessObject::Pointer _TPtr;
129 auto cat = this->m_Filters.find( category );
130 if( cat != this->m_Filters.end( ) )
132 auto nam = cat->second.find( name );
133 if( nam != cat->second.end( ) )
135 void* l_hnd = nam->second.second.first;
136 void* f_hnd = nam->second.second.second;
139 l_hnd = cpPlugins::OS::DLLManager::LoadPlugins( nam->second.first );
140 nam->second.second.first = l_hnd;
146 cpPlugins::OS::DLLManager::LoadCreator( l_hnd, category, name );
147 nam->second.second.second = f_hnd;
150 _TCreator creator = reinterpret_cast< _TCreator >( f_hnd );
151 if( creator != NULL )
153 void* a = creator( );
154 o = reinterpret_cast< _TPtr* >( a )->GetPointer( );
156 o->SetPluginName( nam->second.first );
164 throw std::runtime_error(
165 std::string( "Could not create a valid ProcessObject of type \"" ) +
166 category + std::string( ":" ) +
167 name + std::string( "\"" )
172 // -------------------------------------------------------------------------
173 cpPlugins::Interface::Plugins::
177 cpPlugins::OS::DLLManager::TeaseLoadedLibraries( );
180 // -------------------------------------------------------------------------
181 cpPlugins::Interface::Plugins::
186 // -------------------------------------------------------------------------
187 void cpPlugins::Interface::Plugins::
188 PrintSelf( std::ostream& os, itk::Indent indent ) const
190 for( auto i : this->m_Filters )
192 os << indent << "+ " << i.first << std::endl;
193 for( auto j : i.second )
194 os << indent << "|----> " << j.first << std::endl;
199 // -------------------------------------------------------------------------
200 void cpPlugins::Interface::Plugins::
201 _ReadPluginsPathsVariable( )
203 #ifdef cpPlugins_OS_Windows
206 _dupenv_s( &p, &size, cpPlugins_PATHS );
207 #else // cpPlugins_OS_Windows
208 char* p = std::getenv( cpPlugins_PATHS );
209 #endif // cpPlugins_OS_Windows
210 std::stringstream str;
212 str << p << cpPlugins_ENV_SEPARATOR;
214 std::vector< std::string > tokens;
215 cpPlugins::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR );
216 for( auto dir : tokens )
217 this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( dir ) );