1 #include <cpPlugins/Interface/Workspace.h>
2 #include <third_party/tinyxml/tinyxml.h>
4 // -------------------------------------------------------------------------
5 std::string cpPlugins::Interface::Workspace::
6 LoadWorkspace( const std::string& fname )
8 TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) );
10 TiXmlElement* root = doc->RootElement( );
12 return( "cpPlugins::Interface::Workspace: No valid file" );
13 if( std::string( root->Value( ) ) != "cpPlugins_Workspace" )
14 return( "cpPlugins::Interface::Workspace: No valid workspace" );
15 std::stringstream err;
18 TiXmlElement* plugins = root->FirstChildElement( "plugins" );
19 while( plugins != NULL )
21 const char* value = plugins->Attribute( "filename" );
24 if( !( this->LoadPlugins( value ) ) )
25 err << "no valid plugins file \"" << value << "\"" << std::endl;
28 plugins = plugins->NextSiblingElement( "plugins" );
33 plugins = root->FirstChildElement( "plugins_dir" );
34 while( plugins != NULL )
36 const char* value = plugins->Attribute( "path" );
40 if( plugins->QueryIntAttribute( "recursive", &recursive ) != TIXML_SUCCESS )
42 if( !( this->LoadPluginsPath( value, recursive == 1 ) ) )
43 err << "No valid plugins path \"" << value << "\"" << std::endl;
46 plugins = plugins->NextSiblingElement( "plugins_dir" );
51 TiXmlElement* filter = root->FirstChildElement( "filter" );
52 while( filter != NULL )
54 const char* class_value = filter->Attribute( "class" );
55 const char* name_value = filter->Attribute( "name" );
56 if( class_value != NULL && name_value != NULL )
58 if( this->CreateFilter( class_value, name_value ) )
61 TParameters* parameters = this->GetParameters( name_value );
64 TiXmlElement* param = filter->FirstChildElement( "parameter" );
65 while( param != NULL )
67 const char* param_name = param->Attribute( "name" );
68 const char* param_type = param->Attribute( "type" );
69 if( param_name != NULL && param_type != NULL )
71 std::string param_type_str( param_type );
72 const char* value = param->Attribute( "value" );
75 if( param_type_str == "String" )
76 parameters->ConfigureAsString( param_name );
77 else if( param_type_str == "Bool" )
78 parameters->ConfigureAsBool( param_name );
79 else if( param_type_str == "Int" )
80 parameters->ConfigureAsInt( param_name );
81 else if( param_type_str == "Uint" )
82 parameters->ConfigureAsUint( param_name );
83 else if( param_type_str == "Real" )
84 parameters->ConfigureAsReal( param_name );
85 else if( param_type_str == "Index" )
86 parameters->ConfigureAsIndex( param_name );
87 else if( param_type_str == "Point" )
88 parameters->ConfigureAsPoint( param_name );
89 else if( param_type_str == "Vector" )
90 parameters->ConfigureAsVector( param_name );
91 else if( param_type_str == "StringList" )
92 parameters->ConfigureAsStringList( param_name );
93 else if( param_type_str == "BoolList" )
94 parameters->ConfigureAsBoolList( param_name );
95 else if( param_type_str == "IntList" )
96 parameters->ConfigureAsIntList( param_name );
97 else if( param_type_str == "UintList" )
98 parameters->ConfigureAsUintList( param_name );
99 else if( param_type_str == "RealList" )
100 parameters->ConfigureAsRealList( param_name );
101 else if( param_type_str == "IndexList" )
102 parameters->ConfigureAsIndexList( param_name );
103 else if( param_type_str == "PointList" )
104 parameters->ConfigureAsPointList( param_name );
105 else if( param_type_str == "VectorList" )
106 parameters->ConfigureAsVectorList( param_name );
107 else if( param_type_str == "Choices" )
108 parameters->ConfigureAsChoices( param_name );
109 parameters->SetString( param_name, value, false );
114 param = param->NextSiblingElement( "parameter" );
120 << "No valid class \"" << class_value << "\" with name \""
121 << name_value << "\"" << std::endl;
124 err << "Incomplete data." << std::endl;
125 filter = filter->NextSiblingElement( "filter" );
130 TiXmlElement* connection = root->FirstChildElement( "connection" );
131 while( connection != NULL )
133 TiXmlElement* orig = connection->FirstChildElement( "origin" );
134 TiXmlElement* dest = connection->FirstChildElement( "destination" );
135 if( orig != NULL && dest != NULL )
137 const char* orig_filter = orig->Attribute( "filter" );
138 const char* dest_filter = dest->Attribute( "filter" );
139 const char* orig_name = orig->Attribute( "name" );
140 const char* dest_name = dest->Attribute( "name" );
142 orig_filter != NULL && dest_filter != NULL &&
143 orig_name != NULL && dest_name != NULL
145 this->Connect( orig_filter, dest_filter, orig_name, dest_name );
148 connection = connection->NextSiblingElement( "connection" );
154 return( err.str( ) );
157 // -------------------------------------------------------------------------
158 std::string cpPlugins::Interface::Workspace::
159 SaveWorkspace( const std::string& fname ) const
161 std::stringstream err;
162 TiXmlDocument* doc = new TiXmlDocument( );
163 TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" );
167 auto plugIt = this->m_LoadedPlugins.begin( );
168 plugIt != this->m_LoadedPlugins.end( );
172 TiXmlElement* plugin = new TiXmlElement( "plugins" );
173 plugin->SetAttribute( "filename", plugIt->c_str( ) );
174 root->LinkEndChild( plugin );
179 auto vIt = this->m_Graph->BeginVertices( );
180 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
182 auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
183 auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) );
186 TiXmlElement* e = new TiXmlElement( "filter" );
187 e->SetAttribute( "class", filter->GetClassName( ).c_str( ) );
188 e->SetAttribute( "name", filter->GetName( ) );
190 const TParameters* params = filter->GetParameters( );
191 std::vector< std::string > names;
192 params->GetNames( names );
193 for( auto nIt = names.begin( ); nIt != names.end( ); ++nIt )
195 TiXmlElement* p = new TiXmlElement( "parameter" );
196 p->SetAttribute( "name", nIt->c_str( ) );
197 if( params->HasString( *nIt ) )
198 p->SetAttribute( "type", "String" );
199 else if( params->HasBool( *nIt ) )
200 p->SetAttribute( "type", "Bool" );
201 else if( params->HasInt( *nIt ) )
202 p->SetAttribute( "type", "Int" );
203 else if( params->HasUint( *nIt ) )
204 p->SetAttribute( "type", "Uint" );
205 else if( params->HasReal( *nIt ) )
206 p->SetAttribute( "type", "Real" );
207 else if( params->HasIndex( *nIt ) )
208 p->SetAttribute( "type", "Index" );
209 else if( params->HasPoint( *nIt ) )
210 p->SetAttribute( "type", "Point" );
211 else if( params->HasVector( *nIt ) )
212 p->SetAttribute( "type", "Vector" );
213 else if( params->HasStringList( *nIt ) )
214 p->SetAttribute( "type", "StringList" );
215 else if( params->HasBoolList( *nIt ) )
216 p->SetAttribute( "type", "BoolList" );
217 else if( params->HasIntList( *nIt ) )
218 p->SetAttribute( "type", "IntList" );
219 else if( params->HasUintList( *nIt ) )
220 p->SetAttribute( "type", "UintList" );
221 else if( params->HasRealList( *nIt ) )
222 p->SetAttribute( "type", "RealList" );
223 else if( params->HasIndexList( *nIt ) )
224 p->SetAttribute( "type", "IndexList" );
225 else if( params->HasPointList( *nIt ) )
226 p->SetAttribute( "type", "PointList" );
227 else if( params->HasVectorList( *nIt ) )
228 p->SetAttribute( "type", "VectorList" );
229 else if( params->HasChoices( *nIt ) )
230 p->SetAttribute( "type", "Choices" );
232 "value", params->GetString( *nIt, false ).c_str( )
234 e->LinkEndChild( p );
237 root->LinkEndChild( e );
239 else if( data != NULL )
247 auto mIt = this->m_Graph->BeginEdgesRows( );
248 for( ; mIt != this->m_Graph->EndEdgesRows( ); ++mIt )
250 auto rIt = mIt->second.begin( );
251 for( ; rIt != mIt->second.end( ); ++rIt )
253 auto eIt = rIt->second.begin( );
254 for( ; eIt != rIt->second.end( ); ++eIt )
256 TiXmlElement* conn = new TiXmlElement( "connection" );
257 TiXmlElement* orig = new TiXmlElement( "origin" );
258 TiXmlElement* dest = new TiXmlElement( "destination" );
259 orig->SetAttribute( "filter", mIt->first.c_str( ) );
260 orig->SetAttribute( "name", eIt->first.c_str( ) );
261 dest->SetAttribute( "filter", rIt->first.c_str( ) );
262 dest->SetAttribute( "name", eIt->second.c_str( ) );
264 conn->LinkEndChild( orig );
265 conn->LinkEndChild( dest );
266 root->LinkEndChild( conn );
274 // Physical write and return
275 doc->LinkEndChild( root );
276 doc->SaveFile( fname.c_str( ) );
278 return( err.str( ) );