#include #include #include #include // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins::Pointer cpPlugins::Interface::Plugins::m_Singleton = NULL; // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins:: Pointer cpPlugins::Interface::Plugins:: New( ) { if( Self::m_Singleton.IsNull( ) ) Self::m_Singleton = new Self( ); return( Self::m_Singleton ); } // ------------------------------------------------------------------------- itk::LightObject::Pointer cpPlugins::Interface::Plugins:: CreateAnother( ) const { itk::LightObject::Pointer smartPtr; smartPtr = Self::m_Singleton; return( smartPtr ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins:: Pointer cpPlugins::Interface::Plugins:: Clone( ) const { return( Self::m_Singleton ); } // ------------------------------------------------------------------------- const cpPlugins::Interface::Plugins:: TFilters& cpPlugins::Interface::Plugins:: GetFilters( ) const { return( this->m_Filters ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: LoadPluginsFile( const std::string& libname ) { std::map< std::string, std::set< std::string > > filters; cpPlugins::OS::DLLManager::GetPluginsLibraryContents( filters, libname ); THandlers zero( NULL, NULL ); for( auto i : filters ) for( auto j : i.second ) this->m_Filters[ i.first ][ j ] = TLibData( libname, zero ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: LoadPluginsDirectory( const std::string& dir ) { // Create a globbing pattern std::stringstream glob; glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT; // Get possible shared libraries std::set< std::string > files = cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) ); this->m_PluginsPaths.insert( dir ); for( auto f : files ) try { this->LoadPluginsFile( f ); } catch( ... ) { } } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: GuessPlugins( ) { // Create a globbing pattern std::stringstream glob; glob << cpPlugins_LIB_PREFIX << "*" << cpPlugins_LIB_EXT; // Update paths and get possible shared libraries this->_ReadPluginsPathsVariable( ); for( auto dir : this->m_PluginsPaths ) { std::set< std::string > files = cpPlugins::OS::LoadDirContents( dir, false, glob.str( ) ); for( auto f : files ) try { this->LoadPluginsFile( f ); } catch( ... ) { } } // rof } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: GuessEnvironment( const std::string& dir ) { std::stringstream fname; fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS; std::string buffer; if( cpPlugins::Read( buffer, fname.str( ) ) ) { std::istringstream input( buffer ); for( std::string line; std::getline( input, line ); ) this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( line ) ); } // fi } // ------------------------------------------------------------------------- bool cpPlugins::Interface::Plugins:: SaveEnvironment( const std::string& dir ) { std::stringstream buffer; for( auto p : this->m_PluginsPaths ) buffer << p << std::endl; std::stringstream fname; fname << dir << cpPlugins_PATH_SEPARATOR << cpPlugins_PATHS; return( cpPlugins::Write( buffer.str( ), fname.str( ) ) ); } // ------------------------------------------------------------------------- cpPlugins::BaseObjects::ProcessObject::Pointer cpPlugins::Interface::Plugins:: CreateFilter( const std::string& category, const std::string& name ) { std::cout << "1" << std::endl; typedef void* ( *_TCreator )( ); std::cout << "2" << std::endl; typedef cpPlugins::BaseObjects::ProcessObject::Pointer _TPtr; std::cout << "3" << std::endl; _TPtr o = NULL; std::cout << "4" << std::endl; auto cat = this->m_Filters.find( category ); std::cout << "5" << std::endl; if( cat != this->m_Filters.end( ) ) { std::cout << "6" << std::endl; auto nam = cat->second.find( name ); std::cout << "7" << std::endl; if( nam != cat->second.end( ) ) { std::cout << "8" << std::endl; void* l_hnd = nam->second.second.first; std::cout << "9" << std::endl; void* f_hnd = nam->second.second.second; std::cout << "10" << std::endl; if( l_hnd == NULL ) { std::cout << "11" << std::endl; l_hnd = cpPlugins::OS::DLLManager::LoadPlugins( nam->second.first ); std::cout << "12" << std::endl; nam->second.second.first = l_hnd; std::cout << "13" << std::endl; } // fi if( f_hnd == NULL ) { std::cout << "14" << std::endl; f_hnd = cpPlugins::OS::DLLManager::LoadCreator( l_hnd, category, name ); std::cout << "15" << std::endl; nam->second.second.second = f_hnd; std::cout << "16" << std::endl; } // fi std::cout << "17" << std::endl; _TCreator creator = reinterpret_cast< _TCreator >( f_hnd ); std::cout << "18" << std::endl; if( creator != NULL ) { std::cout << "19 " << creator << " " << f_hnd << std::endl; void* a = creator( ); std::cout << "20" << std::endl; std::cout << "20 " << creator << " " << a << std::endl; std::cout << "21" << std::endl; o = reinterpret_cast< _TPtr* >( a )->GetPointer( ); std::cout << "22" << std::endl; o->SetName( name ); std::cout << "23" << std::endl; o->SetPluginName( nam->second.first ); std::cout << "24" << std::endl; } // fi } // fi } // fi std::cout << "25" << std::endl; if( o.IsNull( ) ) throw std::runtime_error( std::string( "Could not create a valid ProcessObject of type \"" ) + category + std::string( ":" ) + name + std::string( "\"" ) ); std::cout << "26" << std::endl; return( o ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins:: Plugins( ) : Superclass( ) { cpPlugins::OS::DLLManager::TeaseLoadedLibraries( ); } // ------------------------------------------------------------------------- cpPlugins::Interface::Plugins:: ~Plugins( ) { } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: PrintSelf( std::ostream& os, itk::Indent indent ) const { for( auto i : this->m_Filters ) { os << indent << "+ " << i.first << std::endl; for( auto j : i.second ) os << indent << "|----> " << j.first << std::endl; } // rof } // ------------------------------------------------------------------------- void cpPlugins::Interface::Plugins:: _ReadPluginsPathsVariable( ) { #ifdef cpPlugins_OS_Windows char* p; size_t size; _dupenv_s( &p, &size, cpPlugins_PATHS ); #else // cpPlugins_OS_Windows char* p = std::getenv( cpPlugins_PATHS ); #endif // cpPlugins_OS_Windows std::stringstream str; if( p != NULL ) str << p << cpPlugins_ENV_SEPARATOR; str << "."; std::vector< std::string > tokens; cpPlugins::Tokenize( tokens, str.str( ), cpPlugins_ENV_SEPARATOR ); for( auto dir : tokens ) this->m_PluginsPaths.insert( cpPlugins::CanonicalPath( dir ) ); } // eof - $RCSfile$