1 #include <cpPlugins/Interface.h>
7 #ifdef cpPlugins_SYS_WINDOWS
8 #else // cpPlugins_SYS_WINDOWS
10 #endif // cpPlugins_SYS_WINDOWS
13 // -------------------------------------------------------------------------
14 cpPlugins::Interface::
19 // -------------------------------------------------------------------------
20 cpPlugins::Interface::
26 // -------------------------------------------------------------------------
27 const cpPlugins::Interface::
28 TFilters& cpPlugins::Interface::
31 return( this->m_Filters );
34 // -------------------------------------------------------------------------
35 void cpPlugins::Interface::
36 LoadPluginFile( const std::string& filename )
38 auto canonical_fn = cpPlugins::PathHelper::CanonicalPath( filename );
40 std::map< std::string, std::set< std::string > > filters;
41 #ifdef cpPlugins_SYS_WINDOWS
43 #else // cpPlugins_SYS_WINDOWS
45 // Try to load canonical filename and clean error messages
46 hnd = dlopen( canonical_fn.c_str( ), RTLD_NOW | RTLD_GLOBAL );
48 throw std::runtime_error(
49 std::string( "cpPlugins::Interface: Could not load library \"" ) +
56 typedef const std::map< std::string, std::set< std::string > > ( *func_t )( );
57 auto func = ( func_t ) dlsym( hnd, "LoadedFilters" );
58 const char* func_error = dlerror( );
59 if( func_error != NULL )
62 throw std::runtime_error(
63 std::string( "cpPlugins::Interface: Library \"" ) +
65 std::string( "\" not recognized as a cpPlugins library." )
70 #endif // cpPlugins_SYS_WINDOWS
73 // Save the loaded filters names
74 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
76 auto colIt = this->m_Filters.find( cIt->first );
77 for( auto fIt = cIt->second.begin( ); fIt != cIt->second.end( ); ++fIt )
80 if( colIt != this->m_Filters.end( ) )
82 auto rowIt = colIt->second.find( *fIt );
83 if( rowIt != colIt->second.end( ) )
88 this->m_Filters[ cIt->first ][ *fIt ] = canonical_fn;
95 this->m_Plugins[ canonical_fn ] = hnd;
98 throw std::runtime_error(
99 "cpPlugins::Interface: Operative system not yet supported."
103 // -------------------------------------------------------------------------
104 void cpPlugins::Interface::
107 auto pIt = this->m_Plugins.begin( );
108 for( ; pIt != this->m_Plugins.end( ); ++pIt )
110 #ifdef cpPlugins_SYS_WINDOWS
111 #else // cpPlugins_SYS_WINDOWS
112 dlclose( pIt->second );
113 #endif // cpPlugins_SYS_WINDOWS
115 this->m_Plugins.clear( );
116 this->m_Filters.clear( );
119 // -------------------------------------------------------------------------
120 cpPlugins::ProcessObject::Pointer cpPlugins::Interface::
121 Create( const std::string& category, const std::string& name )
123 cpPlugins::ProcessObject::Pointer filter;
124 auto cIt = this->m_Filters.find( category );
125 if( cIt != this->m_Filters.end( ) )
127 auto nIt = cIt->second.find( name );
128 if( nIt != cIt->second.end( ) )
130 auto pIt = this->m_Plugins.find( nIt->second );
131 if( pIt != this->m_Plugins.end( ) )
133 #ifdef cpPlugins_SYS_WINDOWS
134 #else // cpPlugins_SYS_WINDOWS
135 std::string func_name = category + "_" + name;
136 typedef cpPlugins::ProcessObject::Pointer ( *func_t )( );
137 auto func = ( func_t ) dlsym( pIt->second, func_name.c_str( ) );
140 throw std::runtime_error(
141 std::string( "cpPlugins::Interface: Class \"" ) +
142 category + std::string( ":" ) + name +
143 std::string( "\" does not have a valid creator function." )
148 #endif // cpPlugins_SYS_WINDOWS