1 #include <cpPlugins/Interface/Interface.h>
8 # define PLUGIN_PREFIX ""
9 # define PLUGIN_EXT ".dll"
11 # define PLUGIN_PREFIX "lib"
12 # define PLUGIN_EXT ".so"
14 #define PLUGIN_CONFIG_FILE "plugins.cfg"
22 bool operator()( char c ) const
25 return( c == '\\' || c == '/' );
36 // -------------------------------------------------------------------------
37 cpPlugins::Interface::Interface::
40 this->m_Pluma.acceptProviderType< ProcessObjectProvider >( );
43 // -------------------------------------------------------------------------
44 cpPlugins::Interface::Interface::
50 // -------------------------------------------------------------------------
51 bool cpPlugins::Interface::Interface::
52 LoadDefaultConfiguration( const std::string& path )
54 std::ifstream file( PLUGIN_CONFIG_FILE );
58 while( file.getline( buffer, 1000 ) )
60 // std::string line( buffer );
61 std::istringstream line( buffer );
62 std::string name, folder;
63 std::getline( line, name, '@' );
64 std::getline( line, folder, '@' );
65 std::stringstream path;
66 path << folder << "/" << PLUGIN_PREFIX << name << PLUGIN_EXT;
67 this->Load( path.str( ) );
78 // -------------------------------------------------------------------------
79 bool cpPlugins::Interface::Interface::
80 SaveDefaultConfiguration( const std::string& path )
82 std::ofstream file( PLUGIN_CONFIG_FILE );
85 auto pIt = this->m_LoadedPlugins.begin( );
86 for( ; pIt != this->m_LoadedPlugins.end( ); ++pIt )
88 auto fIt = pIt->second.begin( );
89 for( ; fIt != pIt->second.end( ); ++fIt )
90 file << *fIt << "@" << pIt->first << std::endl;
100 // -------------------------------------------------------------------------
101 cpPlugins::Interface::Interface::
102 TClasses& cpPlugins::Interface::Interface::
105 return( this->m_Classes );
108 // -------------------------------------------------------------------------
109 const cpPlugins::Interface::Interface::
110 TClasses& cpPlugins::Interface::Interface::
113 return( this->m_Classes );
116 // -------------------------------------------------------------------------
117 cpPlugins::Interface::Interface::
118 TLoadedPlugins& cpPlugins::Interface::Interface::
121 return( this->m_LoadedPlugins );
124 // -------------------------------------------------------------------------
125 const cpPlugins::Interface::Interface::
126 TLoadedPlugins& cpPlugins::Interface::Interface::
127 GetLoadedPlugins( ) const
129 return( this->m_LoadedPlugins );
132 // -------------------------------------------------------------------------
133 cpPlugins::Interface::ProcessObject::Pointer
134 cpPlugins::Interface::Interface::
135 CreateObject( const std::string& name ) const
137 cpPlugins::Interface::ProcessObject::Pointer po = NULL;
138 auto catIt = this->m_Classes.begin( );
139 while( catIt != this->m_Classes.end( ) )
141 auto classIt = catIt->second.find( name );
142 if( classIt != catIt->second.end( ) )
144 ProcessObjectProvider* provider =
145 dynamic_cast< ProcessObjectProvider* >(
146 this->m_Providers[ classIt->second ]
148 if( provider != NULL )
150 po = provider->create( );
154 catIt = this->m_Classes.end( );
164 // -------------------------------------------------------------------------
165 bool cpPlugins::Interface::Interface::
166 Load( const std::string& path )
171 ret = this->m_Pluma.load( path );
174 std::string folder, name;
175 Self::_SepFName( path, folder, name );
176 this->m_LoadedPlugins[ folder ].push_back( name );
177 this->_LoadClasses( );
189 // -------------------------------------------------------------------------
190 bool cpPlugins::Interface::Interface::
191 Load( const std::string& folder, const std::string& name )
193 std::string real_folder = folder;
195 if( sep( folder[ folder.size( ) - 1 ] ) )
196 real_folder = folder.substr( 0, folder.size( ) - 1 );
197 real_folder = std::string( realpath( real_folder.c_str( ), NULL ) );
201 ret = this->m_Pluma.load( real_folder, name );
204 // Update loaded plugins
205 std::string prefix( PLUGIN_PREFIX );
206 std::string ext( PLUGIN_EXT );
207 std::string real_name = name;
209 real_name.replace( real_name.find( prefix ), prefix.size( ), "" );
210 real_name.replace( real_name.find( ext ), ext.size( ), "" );
211 this->m_LoadedPlugins[ real_folder ].push_back( real_name );
212 this->_LoadClasses( );
224 // -------------------------------------------------------------------------
225 bool cpPlugins::Interface::Interface::
226 LoadFromFolder( const std::string& folder, bool r )
230 std::list< std::string > f = this->m_Pluma.loadFromFolder( folder, r );
233 // Update loaded plugins
234 for( auto i = f.begin( ); i != f.end( ); ++i )
236 std::string folder, name;
237 Self::_SepFName( *i, folder, name );
238 this->m_LoadedPlugins[ folder ].push_back( name );
243 this->_LoadClasses( );
256 // -------------------------------------------------------------------------
257 bool cpPlugins::Interface::Interface::
258 Unload( const std::string& name )
263 ret = this->m_Pluma.unload( name );
266 this->m_Providers.clear( );
267 this->m_Classes.clear( );
269 // TODO: this->m_LoadedPlugins
271 this->_LoadClasses( );
283 // -------------------------------------------------------------------------
284 void cpPlugins::Interface::Interface::
289 this->m_Pluma.unloadAll( );
296 this->m_Providers.clear( );
297 this->m_Classes.clear( );
298 this->m_LoadedPlugins.clear( );
301 // -------------------------------------------------------------------------
302 bool cpPlugins::Interface::Interface::
303 IsLoaded( const std::string& name ) const
305 return( this->m_Pluma.isLoaded( name ) );
308 // -------------------------------------------------------------------------
309 void cpPlugins::Interface::Interface::
312 this->m_Providers.clear( );
313 this->m_Classes.clear( );
314 this->m_Pluma.getProviders( this->m_Providers );
316 // Get reader provider
317 for( unsigned int i = 0; i < this->m_Providers.size( ); ++i )
319 ProcessObject::Pointer d = this->m_Providers[ i ]->create( );
320 this->m_Classes[ d->GetClassCategory( ) ][ d->GetClassName( ) ] = i;
325 // -------------------------------------------------------------------------
326 void cpPlugins::Interface::Interface::
327 _SepFName( const std::string& path, std::string& folder, std::string& name )
332 std::string real_path = std::string( realpath( path.c_str( ), NULL ) );
336 std::find_if( real_path.rbegin( ), real_path.rend( ), sep ).base( ),
340 // Get containing folder
342 folder.replace( folder.find( name ), name.size( ), "" );
343 if( sep( folder[ folder.size( ) - 1 ] ) )
344 folder = folder.substr( 0, folder.size( ) - 1 );
346 // Erase prefix and extension from filename
347 std::string prefix( PLUGIN_PREFIX );
348 std::string ext( PLUGIN_EXT );
350 name.replace( name.find( prefix ), prefix.size( ), "" );
351 name.replace( name.find( ext ), ext.size( ), "" );