1 #include <cpPlugins/BaseObjects/Parameters.h>
2 #include <cpPlugins/Utility.h>
3 #include <tinyxml2/tinyxml2.h>
5 // -------------------------------------------------------------------------
6 cpPlugins::BaseObjects::Parameters::
12 // -------------------------------------------------------------------------
13 cpPlugins::BaseObjects::Parameters::
18 // -------------------------------------------------------------------------
19 void cpPlugins::BaseObjects::Parameters::
22 this->m_TimeStamp.Modified( );
25 // -------------------------------------------------------------------------
26 itk::ModifiedTimeType cpPlugins::BaseObjects::Parameters::
29 return( this->m_TimeStamp.GetMTime( ) );
32 // -------------------------------------------------------------------------
33 void cpPlugins::BaseObjects::Parameters::
36 this->m_Parameters.clear( );
40 // -------------------------------------------------------------------------
41 void cpPlugins::BaseObjects::Parameters::
42 GetNames( std::vector< std::string >& container ) const
45 TParameters::const_iterator i = this->m_Parameters.begin( );
46 for( ; i != this->m_Parameters.end( ); ++i )
47 container.push_back( i->first );
50 // -------------------------------------------------------------------------
51 cpPlugins::BaseObjects::Parameters::
52 Type cpPlugins::BaseObjects::Parameters::
53 GetType( const std::string& name ) const
55 auto i = this->m_Parameters.find( name );
56 if( i != this->m_Parameters.end( ) )
57 return( i->second.first );
59 return( Self::NoType );
62 // -------------------------------------------------------------------------
63 #define cpPlugins_BaseObjects_Parameters_TypeAsString( Y ) \
64 if( i->second.first == Self::Y ) \
67 std::string cpPlugins::BaseObjects::Parameters::
68 GetTypeAsString( const std::string& name ) const
70 auto i = this->m_Parameters.find( name );
71 cpPlugins_BaseObjects_Parameters_TypeAsString( String );
72 else cpPlugins_BaseObjects_Parameters_TypeAsString( Bool );
73 else cpPlugins_BaseObjects_Parameters_TypeAsString( Int );
74 else cpPlugins_BaseObjects_Parameters_TypeAsString( Uint );
75 else cpPlugins_BaseObjects_Parameters_TypeAsString( Real );
76 else cpPlugins_BaseObjects_Parameters_TypeAsString( OpenFileName );
77 else cpPlugins_BaseObjects_Parameters_TypeAsString( SaveFileName );
78 else cpPlugins_BaseObjects_Parameters_TypeAsString( PathName );
79 else cpPlugins_BaseObjects_Parameters_TypeAsString( StringList );
80 else cpPlugins_BaseObjects_Parameters_TypeAsString( BoolList );
81 else cpPlugins_BaseObjects_Parameters_TypeAsString( IntList );
82 else cpPlugins_BaseObjects_Parameters_TypeAsString( UintList );
83 else cpPlugins_BaseObjects_Parameters_TypeAsString( RealList );
84 else cpPlugins_BaseObjects_Parameters_TypeAsString( OpenFileNameList );
85 else cpPlugins_BaseObjects_Parameters_TypeAsString( SaveFileNameList );
86 else cpPlugins_BaseObjects_Parameters_TypeAsString( PathNameList );
87 else cpPlugins_BaseObjects_Parameters_TypeAsString( Choices );
88 else return( "NoType" );
91 // -------------------------------------------------------------------------
92 #define cpPlugins_BaseObjects_Parameters_TypeFromString( Y, str ) \
93 if( str == std::string( #Y ) ) \
96 cpPlugins::BaseObjects::Parameters::
97 Type cpPlugins::BaseObjects::Parameters::
98 GetTypeFromString( const std::string& t )
100 cpPlugins_BaseObjects_Parameters_TypeFromString( String, t );
101 else cpPlugins_BaseObjects_Parameters_TypeFromString( Bool, t );
102 else cpPlugins_BaseObjects_Parameters_TypeFromString( Int, t );
103 else cpPlugins_BaseObjects_Parameters_TypeFromString( Uint, t );
104 else cpPlugins_BaseObjects_Parameters_TypeFromString( Real, t );
105 else cpPlugins_BaseObjects_Parameters_TypeFromString( OpenFileName, t );
106 else cpPlugins_BaseObjects_Parameters_TypeFromString( SaveFileName, t );
107 else cpPlugins_BaseObjects_Parameters_TypeFromString( PathName, t );
108 else cpPlugins_BaseObjects_Parameters_TypeFromString( StringList, t );
109 else cpPlugins_BaseObjects_Parameters_TypeFromString( BoolList, t );
110 else cpPlugins_BaseObjects_Parameters_TypeFromString( IntList, t );
111 else cpPlugins_BaseObjects_Parameters_TypeFromString( UintList, t );
112 else cpPlugins_BaseObjects_Parameters_TypeFromString( RealList, t );
113 else cpPlugins_BaseObjects_Parameters_TypeFromString( OpenFileNameList, t );
114 else cpPlugins_BaseObjects_Parameters_TypeFromString( SaveFileNameList, t );
115 else cpPlugins_BaseObjects_Parameters_TypeFromString( PathNameList, t );
116 else cpPlugins_BaseObjects_Parameters_TypeFromString( Choices, t );
117 else return( Self::NoType );
120 // -------------------------------------------------------------------------
121 std::string cpPlugins::BaseObjects::Parameters::
122 GetString( const std::string& name, bool force ) const
124 auto i = this->m_Parameters.find( name );
125 if( i != this->m_Parameters.end( ) )
127 if( i->second.first == Self::String || force )
128 return( i->second.second );
136 // -------------------------------------------------------------------------
137 void cpPlugins::BaseObjects::Parameters::
138 SetString( const std::string& name, const std::string& v, bool force )
140 auto i = this->m_Parameters.find( name );
141 if( i != this->m_Parameters.end( ) )
143 if( i->second.first == Self::String || force )
145 if( i->second.second != v )
147 i->second.second = v;
157 // -------------------------------------------------------------------------
158 void cpPlugins::BaseObjects::Parameters::
160 const std::string& name, const std::vector< std::string >& choices
163 // It is invalid not to give choices when configuring
164 if( choices.size( ) == 0 )
167 std::stringstream str_choices;
168 str_choices << choices[ 0 ];
169 for( unsigned int i = 1; i < choices.size( ); ++i )
170 str_choices << "#" << choices[ i ];
172 this->m_Parameters[ name ] =
173 TParameter( Self::Choices, str_choices.str( ) );
177 // -------------------------------------------------------------------------
178 void cpPlugins::BaseObjects::Parameters::
179 ConfigureAsRealTypesChoices( const std::string& name )
181 std::vector< std::string > choices;
182 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
183 choices.push_back( "float" );
184 #endif // cpPlugins_CONFIG_REAL_TYPES_float
185 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
186 choices.push_back( "double" );
187 #endif // cpPlugins_CONFIG_REAL_TYPES_double
188 this->ConfigureAsChoices( name, choices );
191 // -------------------------------------------------------------------------
192 void cpPlugins::BaseObjects::Parameters::
193 ConfigureAsIntTypesChoices( const std::string& name )
195 std::vector< std::string > choices;
196 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
197 choices.push_back( "char" );
198 choices.push_back( "uchar" );
199 #endif // cpPlugins_CONFIG_INTEGER_TYPES_char
200 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
201 choices.push_back( "short" );
202 choices.push_back( "ushort" );
203 #endif // cpPlugins_CONFIG_INTEGER_TYPES_short
204 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
205 choices.push_back( "int" );
206 choices.push_back( "uint" );
207 #endif // cpPlugins_CONFIG_INTEGER_TYPES_int
208 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
209 choices.push_back( "long" );
210 choices.push_back( "ulong" );
211 #endif // cpPlugins_CONFIG_INTEGER_TYPES_long
212 this->ConfigureAsChoices( name, choices );
215 // -------------------------------------------------------------------------
216 void cpPlugins::BaseObjects::Parameters::
217 ConfigureAsScalarTypesChoices( const std::string& name )
219 std::vector< std::string > choices;
220 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char
221 choices.push_back( "char" );
222 choices.push_back( "uchar" );
223 #endif // cpPlugins_CONFIG_INTEGER_TYPES_char
224 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short
225 choices.push_back( "short" );
226 choices.push_back( "ushort" );
227 #endif // cpPlugins_CONFIG_INTEGER_TYPES_short
228 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int
229 choices.push_back( "int" );
230 choices.push_back( "uint" );
231 #endif // cpPlugins_CONFIG_INTEGER_TYPES_int
232 #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long
233 choices.push_back( "long" );
234 choices.push_back( "ulong" );
235 #endif // cpPlugins_CONFIG_INTEGER_TYPES_long
236 #ifdef cpPlugins_CONFIG_REAL_TYPES_float
237 choices.push_back( "float" );
238 #endif // cpPlugins_CONFIG_REAL_TYPES_float
239 #ifdef cpPlugins_CONFIG_REAL_TYPES_double
240 choices.push_back( "double" );
241 #endif // cpPlugins_CONFIG_REAL_TYPES_double
242 this->ConfigureAsChoices( name, choices );
245 // -------------------------------------------------------------------------
246 std::vector< std::string > cpPlugins::BaseObjects::Parameters::
247 GetChoices( const std::string& name ) const
249 std::vector< std::string > choices;
251 TParameters::const_iterator i = this->m_Parameters.find( name );
252 if( i != this->m_Parameters.end( ) )
254 if( i->second.first == Self::Choices )
256 std::istringstream str_choices( i->second.second );
257 std::string real_choices;
258 std::getline( str_choices, real_choices, '@' );
259 std::istringstream str( real_choices );
261 while( std::getline( str, token, '#' ) )
262 choices.push_back( token );
270 // -------------------------------------------------------------------------
271 std::string cpPlugins::BaseObjects::Parameters::
272 GetSelectedChoice( const std::string& name ) const
274 auto i = this->m_Parameters.find( name );
275 if( i != this->m_Parameters.end( ) )
277 if( i->second.first == Self::Choices )
279 std::istringstream str_choices( i->second.second );
280 std::string real_choice;
281 std::getline( str_choices, real_choice, '@' );
282 std::getline( str_choices, real_choice, '@' );
283 return( real_choice );
292 // -------------------------------------------------------------------------
293 bool cpPlugins::BaseObjects::Parameters::
294 SetSelectedChoice( const std::string& name, const std::string& choice )
296 auto i = this->m_Parameters.find( name );
297 if( i != this->m_Parameters.end( ) )
299 if( i->second.first == Self::Choices )
301 std::istringstream str_choices( i->second.second );
303 std::getline( str_choices, choices, '@' );
304 if( choices.find( choice ) != std::string::npos )
306 std::stringstream new_choices;
307 new_choices << choices << "@" << choice;
308 i->second.second = new_choices.str( );
322 // -------------------------------------------------------------------------
323 std::string cpPlugins::BaseObjects::Parameters::
324 GetAcceptedFileExtensions( const std::string& name ) const
326 auto i = this->m_AcceptedFileExtensions.find( name );
327 if( i != this->m_AcceptedFileExtensions.end( ) )
333 // -------------------------------------------------------------------------
334 void cpPlugins::BaseObjects::Parameters::
335 SetAcceptedFileExtensions(
336 const std::string& name, const std::string& extensions
339 auto i = this->m_Parameters.find( name );
340 if( i != this->m_Parameters.end( ) )
342 bool is_valid = ( i->second.first == Self::OpenFileName );
343 is_valid |= ( i->second.first == Self::SaveFileName );
344 is_valid |= ( i->second.first == Self::OpenFileNameList );
345 is_valid |= ( i->second.first == Self::SaveFileNameList );
347 this->m_AcceptedFileExtensions[ name ] = extensions;
352 // -------------------------------------------------------------------------
353 bool cpPlugins::BaseObjects::Parameters::
355 tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
358 if( parent_elem == NULL )
361 auto pIt = this->m_Parameters.begin( );
362 for( ; pIt != this->m_Parameters.end( ); ++pIt )
364 tinyxml2::XMLElement* p = doc->NewElement( "Parameter" );
365 p->SetAttribute( "Name", pIt->first.c_str( ) );
366 p->SetAttribute( "Value", pIt->second.second.c_str( ) );
367 p->SetAttribute( "Type", this->GetTypeAsString( pIt->first ).c_str( ) );
368 parent_elem->InsertEndChild( p );
374 // -------------------------------------------------------------------------
375 bool cpPlugins::BaseObjects::Parameters::
376 FromXML( const tinyxml2::XMLElement* filter_elem )
378 const tinyxml2::XMLElement* param =
379 filter_elem->FirstChildElement( "Parameter" );
381 while( param != NULL )
383 const char* param_name = param->Attribute( "Name" );
384 const char* param_type = param->Attribute( "Type" );
385 if( param_name != NULL && param_type != NULL )
388 value.second = param->Attribute( "Value" );
389 value.first = Self::GetTypeFromString( param_type );
390 this->m_Parameters[ param_name ] = value;
393 param = param->NextSiblingElement( "Parameter" );
401 // -------------------------------------------------------------------------
402 cpPlugins::BaseObjects::Parameters::
403 TParameters& cpPlugins::BaseObjects::Parameters::
406 return( this->m_Parameters );
409 // -------------------------------------------------------------------------
410 const cpPlugins::BaseObjects::Parameters::
411 TParameters& cpPlugins::BaseObjects::Parameters::
412 GetRawParameters( ) const
414 return( this->m_Parameters );
417 // -------------------------------------------------------------------------
418 #define cpPlugins_BaseObjects_Parameters_Configure_Code( Y ) \
419 void cpPlugins::BaseObjects::Parameters:: \
420 ConfigureAs##Y( const std::string& name, const T##Y& init ) \
422 this->_Configure< Y >( name ); \
423 this->Set##Y( name, init ); \
425 bool cpPlugins::BaseObjects::Parameters:: \
426 Has##Y( const std::string& name ) const \
427 { return( this->_Has< Y >( name ) ); }
429 // -------------------------------------------------------------------------
430 #define cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Y ) \
431 void cpPlugins::BaseObjects::Parameters:: \
432 ConfigureAs##Y##List( const std::string& name ) \
433 { this->_Configure< Y##List >( name ); } \
434 bool cpPlugins::BaseObjects::Parameters:: \
435 Has##Y##List( const std::string& name ) const \
436 { return( this->_Has< Y##List >( name ) ); }
438 // -------------------------------------------------------------------------
439 #define cpPlugins_BaseObjects_Parameters_GetSet_Code( Y ) \
440 cpPlugins::BaseObjects::Parameters::T##Y \
441 cpPlugins::BaseObjects::Parameters:: \
442 Get##Y( const std::string& name ) const \
443 { return( this->_Get< T##Y, Y >( name ) ); } \
444 void cpPlugins::BaseObjects::Parameters::Set##Y( \
445 const std::string& name, const T##Y& v \
447 { this->_Set< T##Y, Y >( name, v ); }
449 // -------------------------------------------------------------------------
450 #define cpPlugins_BaseObjects_Parameters_GetSetList_Code( Y ) \
451 std::vector< cpPlugins::BaseObjects::Parameters::T##Y > \
452 cpPlugins::BaseObjects::Parameters:: \
453 Get##Y##List( const std::string& name ) const \
454 { return( this->_GetList< T##Y, Y##List >( name ) ); } \
455 void cpPlugins::BaseObjects::Parameters::AddTo##Y##List( \
456 const std::string& name, \
457 const cpPlugins::BaseObjects::Parameters::T##Y& v \
459 { this->_AddToList< T##Y, Y##List >( name, v ); } \
460 void cpPlugins::BaseObjects::Parameters:: \
461 Clear##Y##List( const std::string& name ) \
462 { this->_ClearList< Y##List >( name ); }
464 // -------------------------------------------------------------------------
465 cpPlugins_BaseObjects_Parameters_Configure_Code( String );
466 cpPlugins_BaseObjects_Parameters_Configure_Code( Bool );
467 cpPlugins_BaseObjects_Parameters_Configure_Code( Int );
468 cpPlugins_BaseObjects_Parameters_Configure_Code( Uint );
469 cpPlugins_BaseObjects_Parameters_Configure_Code( Real );
470 cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileName );
471 cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileName );
472 cpPlugins_BaseObjects_Parameters_Configure_Code( PathName );
474 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( String );
475 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Bool );
476 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Int );
477 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Uint );
478 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Real );
479 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( OpenFileName );
480 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( SaveFileName );
481 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( PathName );
483 cpPlugins_BaseObjects_Parameters_GetSet_Code( Bool );
484 cpPlugins_BaseObjects_Parameters_GetSet_Code( Int );
485 cpPlugins_BaseObjects_Parameters_GetSet_Code( Uint );
486 cpPlugins_BaseObjects_Parameters_GetSet_Code( Real );
487 cpPlugins_BaseObjects_Parameters_GetSet_Code( OpenFileName );
488 cpPlugins_BaseObjects_Parameters_GetSet_Code( SaveFileName );
489 cpPlugins_BaseObjects_Parameters_GetSet_Code( PathName );
491 cpPlugins_BaseObjects_Parameters_GetSetList_Code( String );
492 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Bool );
493 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Int );
494 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Uint );
495 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Real );
496 cpPlugins_BaseObjects_Parameters_GetSetList_Code( PathName );
498 // -------------------------------------------------------------------------
499 std::vector< cpPlugins::BaseObjects::Parameters::TOpenFileName >
500 cpPlugins::BaseObjects::Parameters::
501 GetOpenFileNameList( const std::string& name ) const
503 return( this->_GetList< TOpenFileName, OpenFileNameList >( name ) );
506 // -------------------------------------------------------------------------
507 void cpPlugins::BaseObjects::Parameters::
508 AddToOpenFileNameList(
509 const std::string& name,
510 const cpPlugins::BaseObjects::Parameters::TOpenFileName& v
513 auto i = this->m_Parameters.find( name );
514 if( i != this->m_Parameters.end( ) )
516 if( i->second.first == OpenFileNameList )
518 auto pos = v.find_last_of( "/\\" );
519 if( i->second.second == "" )
520 i->second.second = v.substr( 0, pos );
521 i->second.second += std::string( "#" );
522 i->second.second += v.substr( pos + 1 );
530 // -------------------------------------------------------------------------
531 void cpPlugins::BaseObjects::Parameters::
532 ClearOpenFileNameList( const std::string& name )
534 this->_ClearList< OpenFileNameList >( name );
537 // -------------------------------------------------------------------------
538 std::vector< cpPlugins::BaseObjects::Parameters::TSaveFileName >
539 cpPlugins::BaseObjects::Parameters::
540 GetSaveFileNameList( const std::string& name ) const
542 return( this->_GetList< TSaveFileName, SaveFileNameList >( name ) );
545 // -------------------------------------------------------------------------
546 void cpPlugins::BaseObjects::Parameters::
547 AddToSaveFileNameList(
548 const std::string& name,
549 const cpPlugins::BaseObjects::Parameters::TSaveFileName& v
552 auto i = this->m_Parameters.find( name );
553 if( i != this->m_Parameters.end( ) )
555 if( i->second.first == SaveFileNameList )
557 auto pos = v.find_last_of( "/\\" );
558 if( i->second.second == "" )
559 i->second.second = v.substr( 0, pos );
560 i->second.second += std::string( "#" );
561 i->second.second += v.substr( pos + 1 );
569 // -------------------------------------------------------------------------
570 void cpPlugins::BaseObjects::Parameters::
571 ClearSaveFileNameList( const std::string& name )
573 this->_ClearList< SaveFileNameList >( name );
576 // -------------------------------------------------------------------------
577 template< unsigned int _Enum >
578 void cpPlugins::BaseObjects::Parameters::
579 _Configure( const std::string& name )
581 this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
585 // -------------------------------------------------------------------------
586 template< unsigned int _Enum >
587 bool cpPlugins::BaseObjects::Parameters::
588 _Has( const std::string& name ) const
590 auto i = this->m_Parameters.find( name );
591 if( i != this->m_Parameters.end( ) )
592 return( i->second.first == ( Self::Type )( _Enum ) );
597 // -------------------------------------------------------------------------
598 template< class _Type, unsigned int _Enum >
599 _Type cpPlugins::BaseObjects::Parameters::
600 _Get( const std::string& name ) const
602 auto i = this->m_Parameters.find( name );
603 if( i != this->m_Parameters.end( ) )
605 if( i->second.first == ( Self::Type )( _Enum ) )
607 if( typeid( _Type ) != typeid( std::string ) )
609 std::istringstream tok_str( i->second.second );
617 reinterpret_cast< const _Type* >( &( i->second.second ) );
625 return( _Type( 0 ) );
628 // -------------------------------------------------------------------------
629 template< class _Type, unsigned int _Enum >
630 void cpPlugins::BaseObjects::Parameters::
631 _Set( const std::string& name, const _Type& v )
633 auto i = this->m_Parameters.find( name );
634 if( i != this->m_Parameters.end( ) )
636 if( i->second.first == ( Self::Type )( _Enum ) )
638 if( typeid( _Type ) != typeid( std::string ) )
640 std::stringstream str;
642 if( i->second.second != str.str( ) )
644 i->second.second = str.str( );
651 const std::string* str = reinterpret_cast< const std::string* >( &v );
652 if( i->second.second != *str )
654 i->second.second = *str;
667 // -------------------------------------------------------------------------
668 template< class _Type, unsigned int _Enum >
669 std::vector< _Type > cpPlugins::BaseObjects::Parameters::
670 _GetList( const std::string& name ) const
672 std::vector< _Type > lst;
673 std::vector< std::string >* slst =
674 reinterpret_cast< std::vector< std::string >* >( &lst );
675 auto i = this->m_Parameters.find( name );
676 if( i != this->m_Parameters.end( ) )
678 if( i->second.first == ( Self::Type )( _Enum ) )
680 std::vector< std::string > tokens;
681 cpPlugins::Tokenize( tokens, i->second.second, "#" );
682 for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
684 if( typeid( _Type ) != typeid( std::string ) )
686 std::istringstream tok_str( *t );
692 slst->push_back( *t );
702 // -------------------------------------------------------------------------
703 template< class _Type, unsigned int _Enum >
704 void cpPlugins::BaseObjects::Parameters::
705 _AddToList( const std::string& name, const _Type& v )
707 auto i = this->m_Parameters.find( name );
708 if( i != this->m_Parameters.end( ) )
710 if( i->second.first == ( Self::Type )( _Enum ) )
712 std::stringstream str;
713 if( i->second.second != "" )
714 str << i->second.second << "#";
716 i->second.second = str.str( );
724 // -------------------------------------------------------------------------
725 template< unsigned int _Enum >
726 void cpPlugins::BaseObjects::Parameters::
727 _ClearList( const std::string& name )
729 auto i = this->m_Parameters.find( name );
730 if( i != this->m_Parameters.end( ) )
732 if( i->second.first == ( Self::Type )( _Enum ) )
734 if( i->second.second != "" )
736 i->second.second = "";