1 #include <cpPlugins/Interface/Interface.h>
7 # define PLUGIN_PREFIX ""
8 # define PLUGIN_EXT ".dll"
10 # define PLUGIN_PREFIX "lib"
11 # define PLUGIN_EXT ".so"
13 #define PLUGIN_CONFIG_FILE "plugins.cfg"
21 bool operator()( char c ) const
24 return( c == '\\' || c == '/' );
35 // -------------------------------------------------------------------------
36 cpPlugins::Interface::Interface::
39 this->m_Pluma.acceptProviderType< ProcessObjectProvider >( );
42 // -------------------------------------------------------------------------
43 cpPlugins::Interface::Interface::
49 // -------------------------------------------------------------------------
50 bool cpPlugins::Interface::Interface::
51 LoadDefaultConfiguration( const std::string& path )
53 std::ifstream file( PLUGIN_CONFIG_FILE );
57 while( file.getline( buffer, 1000 ) )
59 // std::string line( buffer );
60 std::istringstream line( buffer );
61 std::string name, folder;
62 std::getline( line, name, '@' );
63 std::getline( line, folder, '@' );
64 std::stringstream path;
65 path << folder << "/" << PLUGIN_PREFIX << name << PLUGIN_EXT;
66 this->Load( path.str( ) );
77 // -------------------------------------------------------------------------
78 bool cpPlugins::Interface::Interface::
79 SaveDefaultConfiguration( const std::string& path )
81 std::ofstream file( PLUGIN_CONFIG_FILE );
84 auto pIt = this->m_LoadedPlugins.begin( );
85 for( ; pIt != this->m_LoadedPlugins.end( ); ++pIt )
87 auto fIt = pIt->second.begin( );
88 for( ; fIt != pIt->second.end( ); ++fIt )
89 file << *fIt << "@" << pIt->first << std::endl;
99 // -------------------------------------------------------------------------
100 cpPlugins::Interface::Interface::
101 TClasses& cpPlugins::Interface::Interface::
104 return( this->m_Classes );
107 // -------------------------------------------------------------------------
108 const cpPlugins::Interface::Interface::
109 TClasses& cpPlugins::Interface::Interface::
112 return( this->m_Classes );
115 // -------------------------------------------------------------------------
116 cpPlugins::Interface::Interface::
117 TLoadedPlugins& cpPlugins::Interface::Interface::
120 return( this->m_LoadedPlugins );
123 // -------------------------------------------------------------------------
124 const cpPlugins::Interface::Interface::
125 TLoadedPlugins& cpPlugins::Interface::Interface::
126 GetLoadedPlugins( ) const
128 return( this->m_LoadedPlugins );
131 // -------------------------------------------------------------------------
132 cpPlugins::Interface::ProcessObject::Pointer
133 cpPlugins::Interface::Interface::
134 CreateObject( const std::string& name ) const
136 cpPlugins::Interface::ProcessObject::Pointer po = NULL;
137 auto catIt = this->m_Classes.begin( );
138 while( catIt != this->m_Classes.end( ) )
140 auto classIt = catIt->second.find( name );
141 if( classIt != catIt->second.end( ) )
143 ProcessObjectProvider* provider =
144 dynamic_cast< ProcessObjectProvider* >(
145 this->m_Providers[ classIt->second ]
147 if( provider != NULL )
149 po = provider->create( );
153 catIt = this->m_Classes.end( );
163 // -------------------------------------------------------------------------
164 bool cpPlugins::Interface::Interface::
165 Load( const std::string& path )
170 ret = this->m_Pluma.load( path );
173 std::string folder, name;
174 Self::_SepFName( path, folder, name );
175 this->m_LoadedPlugins[ folder ].push_back( name );
176 this->_LoadClasses( );
188 // -------------------------------------------------------------------------
189 bool cpPlugins::Interface::Interface::
190 Load( const std::string& folder, const std::string& name )
192 std::string real_folder = folder;
194 if( sep( folder[ folder.size( ) - 1 ] ) )
195 real_folder = folder.substr( 0, folder.size( ) - 1 );
196 real_folder = std::string( realpath( real_folder.c_str( ), NULL ) );
200 ret = this->m_Pluma.load( real_folder, name );
203 // Update loaded plugins
204 std::string prefix( PLUGIN_PREFIX );
205 std::string ext( PLUGIN_EXT );
206 std::string real_name = name;
208 real_name.replace( real_name.find( prefix ), prefix.size( ), "" );
209 real_name.replace( real_name.find( ext ), ext.size( ), "" );
210 this->m_LoadedPlugins[ real_folder ].push_back( real_name );
211 this->_LoadClasses( );
223 // -------------------------------------------------------------------------
224 bool cpPlugins::Interface::Interface::
225 LoadFromFolder( const std::string& folder, bool r )
229 std::list< std::string > f = this->m_Pluma.loadFromFolder( folder, r );
232 // Update loaded plugins
233 for( auto i = f.begin( ); i != f.end( ); ++i )
235 std::string folder, name;
236 Self::_SepFName( *i, folder, name );
237 this->m_LoadedPlugins[ folder ].push_back( name );
242 this->_LoadClasses( );
255 // -------------------------------------------------------------------------
256 bool cpPlugins::Interface::Interface::
257 Unload( const std::string& name )
262 ret = this->m_Pluma.unload( name );
265 this->m_Providers.clear( );
266 this->m_Classes.clear( );
268 // TODO: this->m_LoadedPlugins
270 this->_LoadClasses( );
282 // -------------------------------------------------------------------------
283 void cpPlugins::Interface::Interface::
288 this->m_Pluma.unloadAll( );
295 this->m_Providers.clear( );
296 this->m_Classes.clear( );
297 this->m_LoadedPlugins.clear( );
300 // -------------------------------------------------------------------------
301 bool cpPlugins::Interface::Interface::
302 IsLoaded( const std::string& name ) const
304 return( this->m_Pluma.isLoaded( name ) );
307 // -------------------------------------------------------------------------
308 void cpPlugins::Interface::Interface::
311 this->m_Providers.clear( );
312 this->m_Classes.clear( );
313 this->m_Pluma.getProviders( this->m_Providers );
315 // Get reader provider
316 for( unsigned int i = 0; i < this->m_Providers.size( ); ++i )
318 ProcessObject::Pointer d = this->m_Providers[ i ]->create( );
319 this->m_Classes[ d->GetClassCategory( ) ][ d->GetClassName( ) ] = i;
324 // -------------------------------------------------------------------------
325 void cpPlugins::Interface::Interface::
326 _SepFName( const std::string& path, std::string& folder, std::string& name )
331 std::string real_path = std::string( realpath( path.c_str( ), NULL ) );
335 std::find_if( real_path.rbegin( ), real_path.rend( ), sep ).base( ),
339 // Get containing folder
341 folder.replace( folder.find( name ), name.size( ), "" );
342 if( sep( folder[ folder.size( ) - 1 ] ) )
343 folder = folder.substr( 0, folder.size( ) - 1 );
345 // Erase prefix and extension from filename
346 std::string prefix( PLUGIN_PREFIX );
347 std::string ext( PLUGIN_EXT );
349 name.replace( name.find( prefix ), prefix.size( ), "" );
350 name.replace( name.find( ext ), ext.size( ), "" );