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 std::cout << "1" << std::endl;
127 typedef void* ( *_TCreator )( );
128 std::cout << "2" << std::endl;
129 typedef cpPlugins::BaseObjects::ProcessObject::Pointer _TPtr;
130 std::cout << "3" << std::endl;
132 std::cout << "4" << std::endl;
133 auto cat = this->m_Filters.find( category );
134 std::cout << "5" << std::endl;
135 if( cat != this->m_Filters.end( ) )
137 std::cout << "6" << std::endl;
138 auto nam = cat->second.find( name );
139 std::cout << "7" << std::endl;
140 if( nam != cat->second.end( ) )
142 std::cout << "8" << std::endl;
143 void* l_hnd = nam->second.second.first;
144 std::cout << "9" << std::endl;
145 void* f_hnd = nam->second.second.second;
146 std::cout << "10" << std::endl;
149 std::cout << "11" << std::endl;
150 l_hnd = cpPlugins::OS::DLLManager::LoadPlugins( nam->second.first );
151 std::cout << "12" << std::endl;
152 nam->second.second.first = l_hnd;
153 std::cout << "13" << std::endl;
158 std::cout << "14" << std::endl;
160 cpPlugins::OS::DLLManager::LoadCreator( l_hnd, category, name );
161 std::cout << "15" << std::endl;
162 nam->second.second.second = f_hnd;
163 std::cout << "16" << std::endl;
166 std::cout << "17" << std::endl;
167 _TCreator creator = reinterpret_cast< _TCreator >( f_hnd );
168 std::cout << "18" << std::endl;
169 if( creator != NULL )
171 std::cout << "19 " << creator << " " << f_hnd << std::endl;
172 void* a = creator( );
173 std::cout << "20" << std::endl;
174 std::cout << "20 " << creator << " " << a << std::endl;
175 std::cout << "21" << std::endl;
176 o = reinterpret_cast< _TPtr* >( a )->GetPointer( );
177 std::cout << "22" << std::endl;
179 std::cout << "23" << std::endl;
180 o->SetPluginName( nam->second.first );
181 std::cout << "24" << std::endl;
188 std::cout << "25" << std::endl;
190 throw std::runtime_error(
191 std::string( "Could not create a valid ProcessObject of type \"" ) +
192 category + std::string( ":" ) +
193 name + std::string( "\"" )
195 std::cout << "26" << std::endl;
199 // -------------------------------------------------------------------------
200 cpPlugins::Interface::Plugins::
204 cpPlugins::OS::DLLManager::TeaseLoadedLibraries( );
207 // -------------------------------------------------------------------------
208 cpPlugins::Interface::Plugins::
213 // -------------------------------------------------------------------------
214 void cpPlugins::Interface::Plugins::
215 PrintSelf( std::ostream& os, itk::Indent indent ) const
217 for( auto i : this->m_Filters )
219 os << indent << "+ " << i.first << std::endl;
220 for( auto j : i.second )
221 os << indent << "|----> " << j.first << std::endl;
226 // -------------------------------------------------------------------------
227 void cpPlugins::Interface::Plugins::
228 _ReadPluginsPathsVariable( )
230 #ifdef cpPlugins_OS_Windows
233 _dupenv_s( &p, &size, cpPlugins_PATHS );
234 #else // cpPlugins_OS_Windows
235 char* p = std::getenv( cpPlugins_PATHS );
236 #endif // cpPlugins_OS_Windows
237 std::stringstream str;
239 str << p << cpPlugins_ENV_SEPARATOR;
241 std::vector< std::string > tokens;
242 cpPlugins::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR );
243 for( auto dir : tokens )
244 this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( dir ) );