1 #include <cpPlugins/Interface/Parameters.h>
2 #include <cpPlugins/Interface/ProcessObject.h>
3 #include <third_party/tinyxml/tinyxml.h>
7 // -------------------------------------------------------------------------
9 ProcessObject* cpPlugins::Interface::Parameters::
12 return( this->m_Process );
15 // -------------------------------------------------------------------------
16 const cpPlugins::Interface::
17 ProcessObject* cpPlugins::Interface::Parameters::
18 GetProcessObject( ) const
20 return( this->m_Process );
23 // -------------------------------------------------------------------------
24 void cpPlugins::Interface::Parameters::
25 SetProcessObject( ProcessObject* v )
30 // -------------------------------------------------------------------------
31 void cpPlugins::Interface::Parameters::
34 this->Superclass::Modified( );
35 if( this->m_Process != NULL )
36 this->m_Process->Modified( );
39 // -------------------------------------------------------------------------
40 void cpPlugins::Interface::Parameters::
43 this->m_Parameters.clear( );
47 // -------------------------------------------------------------------------
48 #define cpPlugins_Parameters_Configure( Y ) \
49 void cpPlugins::Interface::Parameters:: \
50 ConfigureAs##Y( const TString& name ) \
52 this->m_Parameters[ name ] = TParameter( Self::Y, "" ); \
56 cpPlugins_Parameters_Configure( String );
57 cpPlugins_Parameters_Configure( Bool );
58 cpPlugins_Parameters_Configure( Int );
59 cpPlugins_Parameters_Configure( Uint );
60 cpPlugins_Parameters_Configure( Real );
61 cpPlugins_Parameters_Configure( Index );
62 cpPlugins_Parameters_Configure( Point );
63 cpPlugins_Parameters_Configure( Vector );
64 cpPlugins_Parameters_Configure( StringList );
65 cpPlugins_Parameters_Configure( BoolList );
66 cpPlugins_Parameters_Configure( IntList );
67 cpPlugins_Parameters_Configure( UintList );
68 cpPlugins_Parameters_Configure( RealList );
69 cpPlugins_Parameters_Configure( IndexList );
70 cpPlugins_Parameters_Configure( PointList );
71 cpPlugins_Parameters_Configure( VectorList );
72 cpPlugins_Parameters_Configure( Choices );
74 // -------------------------------------------------------------------------
75 void cpPlugins::Interface::Parameters::
77 const TString& name, const std::vector< TString >& choices
80 // It is invalid not to give choices when configuring
81 if( choices.size( ) == 0 )
84 std::stringstream str_choices;
85 str_choices << choices[ 0 ];
86 for( unsigned int i = 1; i < choices.size( ); ++i )
87 str_choices << "#" << choices[ i ];
89 this->m_Parameters[ name ] =
90 TParameter( Self::Choices, str_choices.str( ) );
94 // -------------------------------------------------------------------------
95 void cpPlugins::Interface::Parameters::
96 GetNames( std::vector< TString >& container ) const
99 TParameters::const_iterator i = this->m_Parameters.begin( );
100 for( ; i != this->m_Parameters.end( ); ++i )
101 container.push_back( i->first );
104 // -------------------------------------------------------------------------
105 cpPlugins::Interface::Parameters::
106 Type cpPlugins::Interface::Parameters::
107 GetType( const TString& name ) const
109 TParameters::const_iterator i = this->m_Parameters.find( name );
110 if( i != this->m_Parameters.end( ) )
111 return( i->second.first );
113 return( Self::NoType );
116 // -------------------------------------------------------------------------
117 #define cpPlugins_Parameters_Has( Y ) \
118 bool cpPlugins::Interface::Parameters:: \
119 Has##Y( const TString& name ) const \
121 TParameters::const_iterator i = this->m_Parameters.find( name ); \
122 if( i != this->m_Parameters.end( ) ) \
123 return( i->second.first == Self::Y ); \
128 cpPlugins_Parameters_Has( String );
129 cpPlugins_Parameters_Has( Bool );
130 cpPlugins_Parameters_Has( Int );
131 cpPlugins_Parameters_Has( Uint );
132 cpPlugins_Parameters_Has( Real );
133 cpPlugins_Parameters_Has( Index );
134 cpPlugins_Parameters_Has( Point );
135 cpPlugins_Parameters_Has( Vector );
136 cpPlugins_Parameters_Has( StringList );
137 cpPlugins_Parameters_Has( BoolList );
138 cpPlugins_Parameters_Has( IntList );
139 cpPlugins_Parameters_Has( UintList );
140 cpPlugins_Parameters_Has( RealList );
141 cpPlugins_Parameters_Has( IndexList );
142 cpPlugins_Parameters_Has( PointList );
143 cpPlugins_Parameters_Has( VectorList );
144 cpPlugins_Parameters_Has( Choices );
146 // -------------------------------------------------------------------------
147 cpPlugins::Interface::Parameters::
148 TString cpPlugins::Interface::Parameters::
149 GetString( const TString& name, bool force ) const
151 TParameters::const_iterator i = this->m_Parameters.find( name );
152 if( i != this->m_Parameters.end( ) )
154 if( i->second.first == Self::String || !force )
155 return( i->second.second );
161 // -------------------------------------------------------------------------
162 cpPlugins::Interface::Parameters::
163 TBool cpPlugins::Interface::Parameters::
164 GetBool( const TString& name ) const
166 TParameters::const_iterator i = this->m_Parameters.find( name );
167 if( i != this->m_Parameters.end( ) )
169 if( i->second.first == Self::Bool )
170 return( std::atoi( i->second.second.c_str( ) ) == 1 );
176 // -------------------------------------------------------------------------
177 cpPlugins::Interface::Parameters::
178 TInt cpPlugins::Interface::Parameters::
179 GetInt( const TString& name ) const
181 TParameters::const_iterator i = this->m_Parameters.find( name );
182 if( i != this->m_Parameters.end( ) )
184 if( i->second.first == Self::Int )
185 return( TInt( std::atoi( i->second.second.c_str( ) ) ) );
191 // -------------------------------------------------------------------------
192 cpPlugins::Interface::Parameters::
193 TUint cpPlugins::Interface::Parameters::
194 GetUint( const TString& name ) const
196 TParameters::const_iterator i = this->m_Parameters.find( name );
197 if( i != this->m_Parameters.end( ) )
199 if( i->second.first == Self::Uint )
200 return( TUint( std::atoi( i->second.second.c_str( ) ) ) );
203 return( TUint( 0 ) );
206 // -------------------------------------------------------------------------
207 cpPlugins::Interface::Parameters::
208 TReal cpPlugins::Interface::Parameters::
209 GetReal( const TString& name ) const
211 TParameters::const_iterator i = this->m_Parameters.find( name );
212 if( i != this->m_Parameters.end( ) )
214 if( i->second.first == Self::Real )
216 std::istringstream tok_str( i->second.second );
219 return( TReal( v ) );
224 return( TReal( 0 ) );
227 // -------------------------------------------------------------------------
228 void cpPlugins::Interface::Parameters::
229 GetStringList( std::vector< TString >& lst, const TString& name ) const
232 TParameters::const_iterator i = this->m_Parameters.find( name );
233 if( i == this->m_Parameters.end( ) )
235 if( i->second.first != Self::StringList )
238 std::istringstream str( i->second.second );
240 while( std::getline( str, token, '#' ) )
241 lst.push_back( token );
244 // -------------------------------------------------------------------------
245 void cpPlugins::Interface::Parameters::
246 GetBoolList( std::vector< TBool >& lst, const TString& name ) const
249 TParameters::const_iterator i = this->m_Parameters.find( name );
250 if( i == this->m_Parameters.end( ) )
252 if( i->second.first != Self::BoolList )
255 std::istringstream str( i->second.second );
257 while( std::getline( str, token, '#' ) )
258 lst.push_back( std::atoi( token.c_str( ) ) == 1 );
261 // -------------------------------------------------------------------------
262 void cpPlugins::Interface::Parameters::
263 GetIntList( std::vector< TInt >& lst, const TString& name ) const
266 TParameters::const_iterator i = this->m_Parameters.find( name );
267 if( i == this->m_Parameters.end( ) )
269 if( i->second.first != Self::IntList )
272 std::istringstream str( i->second.second );
274 while( std::getline( str, token, '#' ) )
275 lst.push_back( TInt( std::atoi( token.c_str( ) ) ) );
278 // -------------------------------------------------------------------------
279 void cpPlugins::Interface::Parameters::
280 GetUintList( std::vector< TUint >& lst, const TString& name ) const
283 TParameters::const_iterator i = this->m_Parameters.find( name );
284 if( i == this->m_Parameters.end( ) )
286 if( i->second.first != Self::UintList )
289 std::istringstream str( i->second.second );
291 while( std::getline( str, token, '#' ) )
292 lst.push_back( TUint( std::atoi( token.c_str( ) ) ) );
295 // -------------------------------------------------------------------------
296 void cpPlugins::Interface::Parameters::
297 GetRealList( std::vector< TReal >& lst, const TString& name ) const
300 TParameters::const_iterator i = this->m_Parameters.find( name );
301 if( i == this->m_Parameters.end( ) )
303 if( i->second.first != Self::RealList )
306 std::istringstream str( i->second.second );
308 while( std::getline( str, token, '#' ) )
310 std::istringstream tok_str( token );
313 lst.push_back( TReal( v ) );
318 // -------------------------------------------------------------------------
319 void cpPlugins::Interface::Parameters::
320 GetChoices( std::vector< TString >& choices, const TString& name ) const
323 TParameters::const_iterator i = this->m_Parameters.find( name );
324 if( i == this->m_Parameters.end( ) )
326 if( i->second.first != Self::Choices )
329 std::istringstream str_choices( i->second.second );
330 std::string real_choices;
331 std::getline( str_choices, real_choices, '@' );
332 std::istringstream str( real_choices );
334 while( std::getline( str, token, '#' ) )
335 choices.push_back( token );
338 // -------------------------------------------------------------------------
339 cpPlugins::Interface::Parameters::
340 TString cpPlugins::Interface::Parameters::
341 GetSelectedChoice( const TString& name ) const
343 TParameters::const_iterator i = this->m_Parameters.find( name );
344 if( i == this->m_Parameters.end( ) )
346 if( i->second.first != Self::Choices )
349 std::istringstream str_choices( i->second.second );
350 std::string real_choice;
351 std::getline( str_choices, real_choice, '@' );
352 std::getline( str_choices, real_choice, '@' );
353 return( real_choice );
356 // -------------------------------------------------------------------------
357 void cpPlugins::Interface::Parameters::
358 SetString( const TString& name, const TString& v, bool force )
360 TParameters::iterator i = this->m_Parameters.find( name );
361 if( i == this->m_Parameters.end( ) )
363 if( i->second.first != Self::String && force )
365 i->second.second = v;
369 // -------------------------------------------------------------------------
370 #define cpPlugins_Parameters_Set( Y ) \
371 void cpPlugins::Interface::Parameters:: \
372 Set##Y( const TString& name, const T##Y& v ) \
374 TParameters::iterator i = this->m_Parameters.find( name ); \
375 if( i == this->m_Parameters.end( ) ) \
377 if( i->second.first != Self::Y ) \
379 std::stringstream str; \
381 i->second.second = str.str( ); \
385 cpPlugins_Parameters_Set( Bool );
386 cpPlugins_Parameters_Set( Int );
387 cpPlugins_Parameters_Set( Uint );
388 cpPlugins_Parameters_Set( Real );
390 // -------------------------------------------------------------------------
391 #define cpPlugins_Parameters_Add( Y ) \
392 void cpPlugins::Interface::Parameters:: \
393 AddTo##Y##List( const TString& name, const T##Y& v ) \
395 TParameters::iterator i = this->m_Parameters.find( name ); \
396 if( i == this->m_Parameters.end( ) ) \
398 if( i->second.first != Self::Y##List ) \
400 std::stringstream str; \
401 if( i->second.second == "" ) \
405 i->second.second += str.str( ); \
409 cpPlugins_Parameters_Add( String );
410 cpPlugins_Parameters_Add( Bool );
411 cpPlugins_Parameters_Add( Int );
412 cpPlugins_Parameters_Add( Uint );
413 cpPlugins_Parameters_Add( Real );
415 // -------------------------------------------------------------------------
416 #define cpPlugins_Parameters_Clear( Y ) \
417 void cpPlugins::Interface::Parameters:: \
418 Clear##Y##List( const TString& name ) \
420 TParameters::iterator i = this->m_Parameters.find( name ); \
421 if( i == this->m_Parameters.end( ) ) \
423 if( i->second.first != Self::Y##List ) \
425 i->second.second = ""; \
429 cpPlugins_Parameters_Clear( String );
430 cpPlugins_Parameters_Clear( Bool );
431 cpPlugins_Parameters_Clear( Int );
432 cpPlugins_Parameters_Clear( Uint );
433 cpPlugins_Parameters_Clear( Real );
434 cpPlugins_Parameters_Clear( Index );
435 cpPlugins_Parameters_Clear( Point );
436 cpPlugins_Parameters_Clear( Vector );
438 // -------------------------------------------------------------------------
439 bool cpPlugins::Interface::Parameters::
440 SetSelectedChoice( const TString& name, const TString& choice )
442 TParameters::iterator i = this->m_Parameters.find( name );
443 if( i == this->m_Parameters.end( ) )
445 if( i->second.first != Self::Choices )
448 std::vector< TString > c;
449 this->GetChoices( c, name );
450 if( std::find( c.begin( ), c.end( ), choice ) != c.end( ) )
452 std::istringstream str_choices( i->second.second );
454 std::getline( str_choices, choices, '@' );
455 std::stringstream new_choices;
456 new_choices << choices << "@" << choice;
457 i->second.second = new_choices.str( );
464 // -------------------------------------------------------------------------
465 bool cpPlugins::Interface::Parameters::
466 ToXML( TiXmlElement* parent_elem ) const
468 if( parent_elem == NULL )
471 auto pIt = this->m_Parameters.begin( );
472 for( ; pIt != this->m_Parameters.end( ); ++pIt )
474 TiXmlElement* p = new TiXmlElement( "parameter" );
475 p->SetAttribute( "name", pIt->first.c_str( ) );
476 p->SetAttribute( "value", pIt->second.second.c_str( ) );
477 if( pIt->second.first == Self::String )
478 p->SetAttribute( "type", "String" );
479 else if( pIt->second.first == Self::Bool )
480 p->SetAttribute( "type", "Bool" );
481 else if( pIt->second.first == Self::Int )
482 p->SetAttribute( "type", "Int" );
483 else if( pIt->second.first == Self::Uint )
484 p->SetAttribute( "type", "Uint" );
485 else if( pIt->second.first == Self::Real )
486 p->SetAttribute( "type", "Real" );
487 else if( pIt->second.first == Self::Index )
488 p->SetAttribute( "type", "Index" );
489 else if( pIt->second.first == Self::Point )
490 p->SetAttribute( "type", "Point" );
491 else if( pIt->second.first == Self::Vector )
492 p->SetAttribute( "type", "Vector" );
493 else if( pIt->second.first == Self::StringList )
494 p->SetAttribute( "type", "StringList" );
495 else if( pIt->second.first == Self::BoolList )
496 p->SetAttribute( "type", "BoolList" );
497 else if( pIt->second.first == Self::IntList )
498 p->SetAttribute( "type", "IntList" );
499 else if( pIt->second.first == Self::UintList )
500 p->SetAttribute( "type", "UintList" );
501 else if( pIt->second.first == Self::RealList )
502 p->SetAttribute( "type", "RealList" );
503 else if( pIt->second.first == Self::IndexList )
504 p->SetAttribute( "type", "IndexList" );
505 else if( pIt->second.first == Self::PointList )
506 p->SetAttribute( "type", "PointList" );
507 else if( pIt->second.first == Self::VectorList )
508 p->SetAttribute( "type", "VectorList" );
509 else if( pIt->second.first == Self::Choices )
510 p->SetAttribute( "type", "Choices" );
511 parent_elem->LinkEndChild( p );
517 // -------------------------------------------------------------------------
518 bool cpPlugins::Interface::Parameters::
519 FromXML( const TiXmlElement* filter_elem )
521 this->m_Parameters.clear( );
523 const TiXmlElement* param = filter_elem->FirstChildElement( "parameter" );
525 while( param != NULL )
527 const char* param_name = param->Attribute( "name" );
528 const char* param_type = param->Attribute( "type" );
529 if( param_name != NULL && param_type != NULL )
532 value.second = param->Attribute( "value" );
534 std::string param_type_str( param_type );
535 if( param_type_str == "Bool" )
536 value.first = Self::Bool;
537 else if( param_type_str == "Int" )
538 value.first = Self::Int;
539 else if( param_type_str == "Uint" )
540 value.first = Self::Uint;
541 else if( param_type_str == "Real" )
542 value.first = Self::Real;
543 else if( param_type_str == "Index" )
544 value.first = Self::Index;
545 else if( param_type_str == "Point" )
546 value.first = Self::Point;
547 else if( param_type_str == "Vector" )
548 value.first = Self::Vector;
549 else if( param_type_str == "StringList" )
550 value.first = Self::StringList;
551 else if( param_type_str == "BoolList" )
552 value.first = Self::BoolList;
553 else if( param_type_str == "IntList" )
554 value.first = Self::IntList;
555 else if( param_type_str == "UintList" )
556 value.first = Self::UintList;
557 else if( param_type_str == "RealList" )
558 value.first = Self::RealList;
559 else if( param_type_str == "IndexList" )
560 value.first = Self::IndexList;
561 else if( param_type_str == "PointList" )
562 value.first = Self::PointList;
563 else if( param_type_str == "VectorList" )
564 value.first = Self::VectorList;
565 else if( param_type_str == "Choices" )
566 value.first = Self::Choices;
568 value.first = Self::String;
570 this->m_Parameters[ param_name ] = value;
573 param = param->NextSiblingElement( "parameter" );
580 // -------------------------------------------------------------------------
581 cpPlugins::Interface::Parameters::
589 // -------------------------------------------------------------------------
590 cpPlugins::Interface::Parameters::
595 // -------------------------------------------------------------------------
596 void cpPlugins::Interface::Parameters::
597 PrintSelf( std::ostream& os, itk::Indent indent ) const
599 TParameters::const_iterator i = this->m_Parameters.begin( );
600 for( ; i != this->m_Parameters.end( ); ++i )
603 << i->second.first << " | "
604 << i->second.second << ")"