1 #include <cpPlugins/Interface/Workspace.h>
3 // -------------------------------------------------------------------------
4 cpPlugins::Interface::Workspace::
6 : m_LastLoadedPlugin( "" )
10 // -------------------------------------------------------------------------
11 cpPlugins::Interface::Workspace::
16 // -------------------------------------------------------------------------
17 bool cpPlugins::Interface::Workspace::
18 LoadPluginsPath( const std::string& path, bool r )
20 // Load all plugins from given folder
21 std::list< std::string > files =
22 this->m_Interface.LoadFromFolder( path, r );
24 // Update a simple track
26 if( files.size( ) > 0 )
28 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
30 this->m_LoadedPlugins.insert( *fIt );
31 this->m_LastLoadedPlugin = *fIt;
34 this->_UpdateLoadedPluginsInformation( );
41 // -------------------------------------------------------------------------
42 bool cpPlugins::Interface::Workspace::
43 LoadPlugins( const std::string& fname )
45 // Is it already loaded?
47 if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
49 // Was it succesfully loaded?
50 ret = this->m_Interface.Load( fname );
52 // Update a simple track
55 this->m_LoadedPlugins.insert( fname );
56 this->m_LastLoadedPlugin = fname;
57 this->_UpdateLoadedPluginsInformation( );
65 // -------------------------------------------------------------------------
66 const cpPlugins::Interface::Workspace::
67 TStringContainer& cpPlugins::Interface::Workspace::
68 GetLoadedPlugins( ) const
70 return( this->m_LoadedPlugins );
73 // -------------------------------------------------------------------------
74 void cpPlugins::Interface::Workspace::
75 GetLoadedPluginCategories( TStringContainer& categories ) const
78 auto fIt = this->m_LoadedFilters.begin( );
79 for( ; fIt != this->m_LoadedFilters.end( ); ++fIt )
80 categories.insert( fIt->first );
83 // -------------------------------------------------------------------------
84 void cpPlugins::Interface::Workspace::
85 GetLoadedPluginFilters( TStringContainer& filters ) const
88 auto pIt = this->m_LoadedFilters.begin( );
89 for( ; pIt != this->m_LoadedFilters.end( ); ++pIt )
90 for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt )
91 filters.insert( *fIt );
94 // -------------------------------------------------------------------------
95 const cpPlugins::Interface::Workspace::
96 TStringContainer& cpPlugins::Interface::Workspace::
97 GetLoadedPluginFilters( const std::string& category ) const
99 static const TStringContainer EMPTY;
100 auto pIt = this->m_LoadedFilters.find( category );
101 if( pIt != this->m_LoadedFilters.end( ) )
102 return( pIt->second );
107 // -------------------------------------------------------------------------
108 bool cpPlugins::Interface::Workspace::
109 CreateFilter( const std::string& filter, const std::string& name )
111 // Get or create new filter from name
112 auto vIt = this->m_Vertices.find( name );
113 if( vIt == this->m_Vertices.end( ) )
115 TFilter::Pointer o = this->m_Interface.CreateObject( filter );
119 this->m_Vertices[ name ] = o;
129 // -------------------------------------------------------------------------
130 bool cpPlugins::Interface::Workspace::
132 const std::string& orig_filter, const std::string& dest_filter,
133 const std::string& output_name, const std::string& input_name
137 auto oIt = this->m_Vertices.find( orig_filter );
138 if( oIt == this->m_Vertices.end( ) )
140 auto dIt = this->m_Vertices.find( dest_filter );
141 if( dIt == this->m_Vertices.end( ) )
143 TFilter* orig = dynamic_cast< TFilter* >( oIt->second.GetPointer( ) );
144 TFilter* dest = dynamic_cast< TFilter* >( dIt->second.GetPointer( ) );
145 if( orig == NULL || dest == NULL )
149 dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) );
150 this->m_AdjMatrix[ orig_filter ][ dest_filter ].push_back(
151 TConnection( output_name, input_name )
156 // -------------------------------------------------------------------------
157 cpPlugins::Interface::Workspace::
158 TParameters* cpPlugins::Interface::Workspace::
159 GetParameters( const std::string& name )
161 auto vIt = this->m_Vertices.find( name );
162 if( vIt != this->m_Vertices.end( ) )
164 TFilter* f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
166 return( f->GetParameters( ) );
174 // -------------------------------------------------------------------------
175 const cpPlugins::Interface::Workspace::
176 TParameters* cpPlugins::Interface::Workspace::
177 GetParameters( const std::string& name ) const
179 auto vIt = this->m_Vertices.find( name );
180 if( vIt != this->m_Vertices.end( ) )
183 dynamic_cast< const TFilter* >( vIt->second.GetPointer( ) );
185 return( f->GetParameters( ) );
193 // -------------------------------------------------------------------------
194 bool cpPlugins::Interface::Workspace::
195 Reduce( const std::string& name )
199 // -------------------------------------------------------------------------
200 std::string cpPlugins::Interface::Workspace::
204 std::set< std::string > sinks;
205 auto vIt = this->m_Vertices.begin( );
206 for( ; vIt != this->m_Vertices.end( ); ++vIt )
207 sinks.insert( vIt->first );
208 auto mIt = this->m_AdjMatrix.begin( );
209 for( ; mIt != this->m_AdjMatrix.end( ); ++mIt )
210 sinks.erase( mIt->first );
213 std::string err = "";
214 for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
216 std::string lerr = this->Execute( *sIt );
218 err += lerr + std::string( "\n" );
224 // -------------------------------------------------------------------------
225 std::string cpPlugins::Interface::Workspace::
226 Execute( const std::string& name )
229 auto vIt = this->m_Vertices.find( name );
230 if( vIt == this->m_Vertices.end( ) )
232 std::string( "cpPlugins::Interface::Workspace: No filter \"" ) +
233 name + std::string( "\"" )
235 TFilter* f = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
238 std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
239 name + std::string( "\" is not a filter." )
242 // Execute and return
243 return( f->Update( ) );
246 // -------------------------------------------------------------------------
247 void cpPlugins::Interface::Workspace::
248 _UpdateLoadedPluginsInformation( )
250 this->m_LoadedFilters.clear( );
251 const TInterface::TClasses& cls = this->m_Interface.GetClasses( );
252 for( auto i = cls.begin( ); i != cls.end( ); ++i )
254 TFilter::Pointer o = this->m_Interface.CreateObject( i->first );
255 std::string name = o->GetClassName( );
256 std::string category = o->GetClassCategory( );
257 this->m_LoadedFilters[ category ].insert( name );