#include #include // ------------------------------------------------------------------------- std::string cpPlugins::Interface::Workspace:: LoadWorkspace( const std::string& fname ) { TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) ); doc->LoadFile( ); TiXmlElement* root = doc->RootElement( ); if( root == NULL ) return( "cpPlugins::Interface::Workspace: No valid file" ); if( std::string( root->Value( ) ) != "cpPlugins_Workspace" ) return( "cpPlugins::Interface::Workspace: No valid workspace" ); std::stringstream err; // Read filters TiXmlElement* filter = root->FirstChildElement( "filter" ); while( filter != NULL ) { const char* class_value = filter->Attribute( "class" ); const char* name_value = filter->Attribute( "name" ); float viewX = float( 0 ); float viewY = float( 0 ); filter->QueryFloatAttribute( "ViewX", &viewX ); filter->QueryFloatAttribute( "ViewY", &viewY ); if( class_value != NULL && name_value != NULL ) { if( this->CreateFilter( class_value, name_value ) ) { TFilter* new_filter = this->GetFilter( name_value ); new_filter->SetViewCoords( viewX, viewY ); // Read parameters TParameters* parameters = new_filter->GetParameters( ); parameters->Clear( ); TiXmlElement* param = filter->FirstChildElement( "parameter" ); while( param != NULL ) { const char* param_name = param->Attribute( "name" ); const char* param_type = param->Attribute( "type" ); if( param_name != NULL && param_type != NULL ) { std::string param_type_str( param_type ); const char* value = param->Attribute( "value" ); if( value != NULL ) { if( param_type_str == "String" ) parameters->ConfigureAsString( param_name ); else if( param_type_str == "Bool" ) parameters->ConfigureAsBool( param_name ); else if( param_type_str == "Int" ) parameters->ConfigureAsInt( param_name ); else if( param_type_str == "Uint" ) parameters->ConfigureAsUint( param_name ); else if( param_type_str == "Real" ) parameters->ConfigureAsReal( param_name ); else if( param_type_str == "Index" ) parameters->ConfigureAsIndex( param_name ); else if( param_type_str == "Point" ) parameters->ConfigureAsPoint( param_name ); else if( param_type_str == "Vector" ) parameters->ConfigureAsVector( param_name ); else if( param_type_str == "StringList" ) parameters->ConfigureAsStringList( param_name ); else if( param_type_str == "BoolList" ) parameters->ConfigureAsBoolList( param_name ); else if( param_type_str == "IntList" ) parameters->ConfigureAsIntList( param_name ); else if( param_type_str == "UintList" ) parameters->ConfigureAsUintList( param_name ); else if( param_type_str == "RealList" ) parameters->ConfigureAsRealList( param_name ); else if( param_type_str == "IndexList" ) parameters->ConfigureAsIndexList( param_name ); else if( param_type_str == "PointList" ) parameters->ConfigureAsPointList( param_name ); else if( param_type_str == "VectorList" ) parameters->ConfigureAsVectorList( param_name ); else if( param_type_str == "Choices" ) parameters->ConfigureAsChoices( param_name ); parameters->SetString( param_name, value, false ); } // fi } // fi param = param->NextSiblingElement( "parameter" ); } // elihw } else err << "No valid class \"" << class_value << "\" with name \"" << name_value << "\"" << std::endl; } else err << "Incomplete data." << std::endl; filter = filter->NextSiblingElement( "filter" ); } // elihw // Read connections TiXmlElement* connection = root->FirstChildElement( "connection" ); while( connection != NULL ) { TiXmlElement* orig = connection->FirstChildElement( "origin" ); TiXmlElement* dest = connection->FirstChildElement( "destination" ); if( orig != NULL && dest != NULL ) { const char* orig_filter = orig->Attribute( "filter" ); const char* dest_filter = dest->Attribute( "filter" ); const char* orig_name = orig->Attribute( "name" ); const char* dest_name = dest->Attribute( "name" ); if( orig_filter != NULL && dest_filter != NULL && orig_name != NULL && dest_name != NULL ) this->Connect( orig_filter, dest_filter, orig_name, dest_name ); } // fi connection = connection->NextSiblingElement( "connection" ); } // elihw // Finish and return delete doc; return( err.str( ) ); } // ------------------------------------------------------------------------- std::string cpPlugins::Interface::Workspace:: SaveWorkspace( const std::string& fname ) const { std::stringstream err; TiXmlDocument* doc = new TiXmlDocument( ); TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" ); // Save vertices auto vIt = this->m_Graph->BeginVertices( ); for( ; vIt != this->m_Graph->EndVertices( ); ++vIt ) { auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) ); auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) ); if( filter != NULL ) { TiXmlElement* e = new TiXmlElement( "filter" ); e->SetAttribute( "class", filter->GetClassName( ) ); e->SetAttribute( "name", filter->GetName( ) ); e->SetAttribute( "ViewX", filter->GetViewX( ) ); e->SetAttribute( "ViewY", filter->GetViewY( ) ); const TParameters* params = filter->GetParameters( ); std::vector< std::string > names; params->GetNames( names ); for( auto nIt = names.begin( ); nIt != names.end( ); ++nIt ) { TiXmlElement* p = new TiXmlElement( "parameter" ); p->SetAttribute( "name", nIt->c_str( ) ); if( params->HasString( *nIt ) ) p->SetAttribute( "type", "String" ); else if( params->HasBool( *nIt ) ) p->SetAttribute( "type", "Bool" ); else if( params->HasInt( *nIt ) ) p->SetAttribute( "type", "Int" ); else if( params->HasUint( *nIt ) ) p->SetAttribute( "type", "Uint" ); else if( params->HasReal( *nIt ) ) p->SetAttribute( "type", "Real" ); else if( params->HasIndex( *nIt ) ) p->SetAttribute( "type", "Index" ); else if( params->HasPoint( *nIt ) ) p->SetAttribute( "type", "Point" ); else if( params->HasVector( *nIt ) ) p->SetAttribute( "type", "Vector" ); else if( params->HasStringList( *nIt ) ) p->SetAttribute( "type", "StringList" ); else if( params->HasBoolList( *nIt ) ) p->SetAttribute( "type", "BoolList" ); else if( params->HasIntList( *nIt ) ) p->SetAttribute( "type", "IntList" ); else if( params->HasUintList( *nIt ) ) p->SetAttribute( "type", "UintList" ); else if( params->HasRealList( *nIt ) ) p->SetAttribute( "type", "RealList" ); else if( params->HasIndexList( *nIt ) ) p->SetAttribute( "type", "IndexList" ); else if( params->HasPointList( *nIt ) ) p->SetAttribute( "type", "PointList" ); else if( params->HasVectorList( *nIt ) ) p->SetAttribute( "type", "VectorList" ); else if( params->HasChoices( *nIt ) ) p->SetAttribute( "type", "Choices" ); p->SetAttribute( "value", params->GetString( *nIt, false ).c_str( ) ); e->LinkEndChild( p ); } // rof root->LinkEndChild( e ); } else if( data != NULL ) { // TODO } // fi } // rof // Save connections auto mIt = this->m_Graph->BeginEdgesRows( ); for( ; mIt != this->m_Graph->EndEdgesRows( ); ++mIt ) { auto rIt = mIt->second.begin( ); for( ; rIt != mIt->second.end( ); ++rIt ) { auto eIt = rIt->second.begin( ); for( ; eIt != rIt->second.end( ); ++eIt ) { TiXmlElement* conn = new TiXmlElement( "connection" ); TiXmlElement* orig = new TiXmlElement( "origin" ); TiXmlElement* dest = new TiXmlElement( "destination" ); orig->SetAttribute( "filter", mIt->first.c_str( ) ); orig->SetAttribute( "name", eIt->first.c_str( ) ); dest->SetAttribute( "filter", rIt->first.c_str( ) ); dest->SetAttribute( "name", eIt->second.c_str( ) ); conn->LinkEndChild( orig ); conn->LinkEndChild( dest ); root->LinkEndChild( conn ); } // rof } // rof } // rof // Physical write and return doc->LinkEndChild( root ); doc->SaveFile( fname.c_str( ) ); delete doc; return( err.str( ) ); } // eof - $RCSfile$