1 #include <cpPlugins/BaseObjects/Parameters.h>
2 #include <cpExtensions/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 std::vector< std::string > cpPlugins::BaseObjects::Parameters::
193 GetChoices( const std::string& name ) const
195 std::vector< std::string > choices;
197 TParameters::const_iterator i = this->m_Parameters.find( name );
198 if( i != this->m_Parameters.end( ) )
200 if( i->second.first == Self::Choices )
202 std::istringstream str_choices( i->second.second );
203 std::string real_choices;
204 std::getline( str_choices, real_choices, '@' );
205 std::istringstream str( real_choices );
207 while( std::getline( str, token, '#' ) )
208 choices.push_back( token );
216 // -------------------------------------------------------------------------
217 std::string cpPlugins::BaseObjects::Parameters::
218 GetSelectedChoice( const std::string& name ) const
220 auto i = this->m_Parameters.find( name );
221 if( i != this->m_Parameters.end( ) )
223 if( i->second.first == Self::Choices )
225 std::istringstream str_choices( i->second.second );
226 std::string real_choice;
227 std::getline( str_choices, real_choice, '@' );
228 std::getline( str_choices, real_choice, '@' );
229 return( real_choice );
238 // -------------------------------------------------------------------------
239 bool cpPlugins::BaseObjects::Parameters::
240 SetSelectedChoice( const std::string& name, const std::string& choice )
242 auto i = this->m_Parameters.find( name );
243 if( i != this->m_Parameters.end( ) )
245 if( i->second.first == Self::Choices )
247 std::istringstream str_choices( i->second.second );
249 std::getline( str_choices, choices, '@' );
250 if( choices.find( choice ) != std::string::npos )
252 std::stringstream new_choices;
253 new_choices << choices << "@" << choice;
254 i->second.second = new_choices.str( );
268 // -------------------------------------------------------------------------
269 std::string cpPlugins::BaseObjects::Parameters::
270 GetAcceptedFileExtensions( const std::string& name ) const
272 auto i = this->m_AcceptedFileExtensions.find( name );
273 if( i != this->m_AcceptedFileExtensions.end( ) )
279 // -------------------------------------------------------------------------
280 void cpPlugins::BaseObjects::Parameters::
281 SetAcceptedFileExtensions(
282 const std::string& name, const std::string& extensions
285 auto i = this->m_Parameters.find( name );
286 if( i != this->m_Parameters.end( ) )
288 bool is_valid = ( i->second.first == Self::OpenFileName );
289 is_valid |= ( i->second.first == Self::SaveFileName );
290 is_valid |= ( i->second.first == Self::OpenFileNameList );
291 is_valid |= ( i->second.first == Self::SaveFileNameList );
293 this->m_AcceptedFileExtensions[ name ] = extensions;
298 // -------------------------------------------------------------------------
299 bool cpPlugins::BaseObjects::Parameters::
301 tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
304 if( parent_elem == NULL )
307 auto pIt = this->m_Parameters.begin( );
308 for( ; pIt != this->m_Parameters.end( ); ++pIt )
310 tinyxml2::XMLElement* p = doc->NewElement( "Parameter" );
311 p->SetAttribute( "Name", pIt->first.c_str( ) );
312 p->SetAttribute( "Value", pIt->second.second.c_str( ) );
313 p->SetAttribute( "Type", this->GetTypeAsString( pIt->first ).c_str( ) );
314 parent_elem->InsertEndChild( p );
320 // -------------------------------------------------------------------------
321 bool cpPlugins::BaseObjects::Parameters::
322 FromXML( const tinyxml2::XMLElement* filter_elem )
324 const tinyxml2::XMLElement* param =
325 filter_elem->FirstChildElement( "Parameter" );
327 while( param != NULL )
329 const char* param_name = param->Attribute( "Name" );
330 const char* param_type = param->Attribute( "Type" );
331 if( param_name != NULL && param_type != NULL )
334 value.second = param->Attribute( "Value" );
335 value.first = Self::GetTypeFromString( param_type );
336 this->m_Parameters[ param_name ] = value;
339 param = param->NextSiblingElement( "Parameter" );
347 // -------------------------------------------------------------------------
348 cpPlugins::BaseObjects::Parameters::
349 TParameters& cpPlugins::BaseObjects::Parameters::
352 return( this->m_Parameters );
355 // -------------------------------------------------------------------------
356 const cpPlugins::BaseObjects::Parameters::
357 TParameters& cpPlugins::BaseObjects::Parameters::
358 GetRawParameters( ) const
360 return( this->m_Parameters );
363 // -------------------------------------------------------------------------
364 #define cpPlugins_BaseObjects_Parameters_Configure_Code( Y ) \
365 void cpPlugins::BaseObjects::Parameters:: \
366 ConfigureAs##Y( const std::string& name, const T##Y& init ) \
368 this->_Configure< Y >( name ); \
369 this->Set##Y( name, init ); \
371 bool cpPlugins::BaseObjects::Parameters:: \
372 Has##Y( const std::string& name ) const \
373 { return( this->_Has< Y >( name ) ); }
375 // -------------------------------------------------------------------------
376 #define cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Y ) \
377 void cpPlugins::BaseObjects::Parameters:: \
378 ConfigureAs##Y##List( const std::string& name ) \
379 { this->_Configure< Y##List >( name ); } \
380 bool cpPlugins::BaseObjects::Parameters:: \
381 Has##Y##List( const std::string& name ) const \
382 { return( this->_Has< Y##List >( name ) ); }
384 // -------------------------------------------------------------------------
385 #define cpPlugins_BaseObjects_Parameters_GetSet_Code( Y ) \
386 cpPlugins::BaseObjects::Parameters::T##Y \
387 cpPlugins::BaseObjects::Parameters:: \
388 Get##Y( const std::string& name ) const \
389 { return( this->_Get< T##Y, Y >( name ) ); } \
390 void cpPlugins::BaseObjects::Parameters::Set##Y( \
391 const std::string& name, const T##Y& v \
393 { this->_Set< T##Y, Y >( name, v ); }
395 // -------------------------------------------------------------------------
396 #define cpPlugins_BaseObjects_Parameters_GetSetList_Code( Y ) \
397 std::vector< cpPlugins::BaseObjects::Parameters::T##Y > \
398 cpPlugins::BaseObjects::Parameters:: \
399 Get##Y##List( const std::string& name ) const \
400 { return( this->_GetList< T##Y, Y##List >( name ) ); } \
401 void cpPlugins::BaseObjects::Parameters::AddTo##Y##List( \
402 const std::string& name, \
403 const cpPlugins::BaseObjects::Parameters::T##Y& v \
405 { this->_AddToList< T##Y, Y##List >( name, v ); } \
406 void cpPlugins::BaseObjects::Parameters:: \
407 Clear##Y##List( const std::string& name ) \
408 { this->_ClearList< Y##List >( name ); }
410 // -------------------------------------------------------------------------
411 cpPlugins_BaseObjects_Parameters_Configure_Code( String );
412 cpPlugins_BaseObjects_Parameters_Configure_Code( Bool );
413 cpPlugins_BaseObjects_Parameters_Configure_Code( Int );
414 cpPlugins_BaseObjects_Parameters_Configure_Code( Uint );
415 cpPlugins_BaseObjects_Parameters_Configure_Code( Real );
416 cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileName );
417 cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileName );
418 cpPlugins_BaseObjects_Parameters_Configure_Code( PathName );
420 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( String );
421 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Bool );
422 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Int );
423 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Uint );
424 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Real );
425 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( OpenFileName );
426 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( SaveFileName );
427 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( PathName );
429 cpPlugins_BaseObjects_Parameters_GetSet_Code( Bool );
430 cpPlugins_BaseObjects_Parameters_GetSet_Code( Int );
431 cpPlugins_BaseObjects_Parameters_GetSet_Code( Uint );
432 cpPlugins_BaseObjects_Parameters_GetSet_Code( Real );
433 cpPlugins_BaseObjects_Parameters_GetSet_Code( OpenFileName );
434 cpPlugins_BaseObjects_Parameters_GetSet_Code( SaveFileName );
435 cpPlugins_BaseObjects_Parameters_GetSet_Code( PathName );
437 cpPlugins_BaseObjects_Parameters_GetSetList_Code( String );
438 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Bool );
439 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Int );
440 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Uint );
441 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Real );
442 cpPlugins_BaseObjects_Parameters_GetSetList_Code( PathName );
444 // -------------------------------------------------------------------------
445 std::vector< cpPlugins::BaseObjects::Parameters::TOpenFileName >
446 cpPlugins::BaseObjects::Parameters::
447 GetOpenFileNameList( const std::string& name ) const
449 return( this->_GetList< TOpenFileName, OpenFileNameList >( name ) );
452 // -------------------------------------------------------------------------
453 void cpPlugins::BaseObjects::Parameters::
454 AddToOpenFileNameList(
455 const std::string& name,
456 const cpPlugins::BaseObjects::Parameters::TOpenFileName& v
459 auto i = this->m_Parameters.find( name );
460 if( i != this->m_Parameters.end( ) )
462 if( i->second.first == OpenFileNameList )
464 auto pos = v.find_last_of( "/\\" );
465 if( i->second.second == "" )
466 i->second.second = v.substr( 0, pos );
467 i->second.second += std::string( "#" );
468 i->second.second += v.substr( pos + 1 );
476 // -------------------------------------------------------------------------
477 void cpPlugins::BaseObjects::Parameters::
478 ClearOpenFileNameList( const std::string& name )
480 this->_ClearList< OpenFileNameList >( name );
483 // -------------------------------------------------------------------------
484 std::vector< cpPlugins::BaseObjects::Parameters::TSaveFileName >
485 cpPlugins::BaseObjects::Parameters::
486 GetSaveFileNameList( const std::string& name ) const
488 return( this->_GetList< TSaveFileName, SaveFileNameList >( name ) );
491 // -------------------------------------------------------------------------
492 void cpPlugins::BaseObjects::Parameters::
493 AddToSaveFileNameList(
494 const std::string& name,
495 const cpPlugins::BaseObjects::Parameters::TSaveFileName& v
498 auto i = this->m_Parameters.find( name );
499 if( i != this->m_Parameters.end( ) )
501 if( i->second.first == SaveFileNameList )
503 auto pos = v.find_last_of( "/\\" );
504 if( i->second.second == "" )
505 i->second.second = v.substr( 0, pos );
506 i->second.second += std::string( "#" );
507 i->second.second += v.substr( pos + 1 );
515 // -------------------------------------------------------------------------
516 void cpPlugins::BaseObjects::Parameters::
517 ClearSaveFileNameList( const std::string& name )
519 this->_ClearList< SaveFileNameList >( name );
522 // -------------------------------------------------------------------------
523 template< unsigned int _Enum >
524 void cpPlugins::BaseObjects::Parameters::
525 _Configure( const std::string& name )
527 this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
531 // -------------------------------------------------------------------------
532 template< unsigned int _Enum >
533 bool cpPlugins::BaseObjects::Parameters::
534 _Has( const std::string& name ) const
536 auto i = this->m_Parameters.find( name );
537 if( i != this->m_Parameters.end( ) )
538 return( i->second.first == ( Self::Type )( _Enum ) );
543 // -------------------------------------------------------------------------
544 template< class _Type, unsigned int _Enum >
545 _Type cpPlugins::BaseObjects::Parameters::
546 _Get( const std::string& name ) const
548 auto i = this->m_Parameters.find( name );
549 if( i != this->m_Parameters.end( ) )
551 if( i->second.first == ( Self::Type )( _Enum ) )
553 if( typeid( _Type ) != typeid( std::string ) )
555 std::istringstream tok_str( i->second.second );
563 reinterpret_cast< const _Type* >( &( i->second.second ) );
571 return( _Type( 0 ) );
574 // -------------------------------------------------------------------------
575 template< class _Type, unsigned int _Enum >
576 void cpPlugins::BaseObjects::Parameters::
577 _Set( const std::string& name, const _Type& v )
579 auto i = this->m_Parameters.find( name );
580 if( i != this->m_Parameters.end( ) )
582 if( i->second.first == ( Self::Type )( _Enum ) )
584 if( typeid( _Type ) != typeid( std::string ) )
586 std::stringstream str;
588 if( i->second.second != str.str( ) )
590 i->second.second = str.str( );
597 const std::string* str = reinterpret_cast< const std::string* >( &v );
598 if( i->second.second != *str )
600 i->second.second = *str;
613 // -------------------------------------------------------------------------
614 template< class _Type, unsigned int _Enum >
615 std::vector< _Type > cpPlugins::BaseObjects::Parameters::
616 _GetList( const std::string& name ) const
618 std::vector< _Type > lst;
619 std::vector< std::string >* slst =
620 reinterpret_cast< std::vector< std::string >* >( &lst );
621 auto i = this->m_Parameters.find( name );
622 if( i != this->m_Parameters.end( ) )
624 if( i->second.first == ( Self::Type )( _Enum ) )
626 std::vector< std::string > tokens;
627 cpExtensions::Tokenize( tokens, i->second.second, "#" );
628 for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
630 if( typeid( _Type ) != typeid( std::string ) )
632 std::istringstream tok_str( *t );
638 slst->push_back( *t );
648 // -------------------------------------------------------------------------
649 template< class _Type, unsigned int _Enum >
650 void cpPlugins::BaseObjects::Parameters::
651 _AddToList( const std::string& name, const _Type& v )
653 auto i = this->m_Parameters.find( name );
654 if( i != this->m_Parameters.end( ) )
656 if( i->second.first == ( Self::Type )( _Enum ) )
658 std::stringstream str;
659 if( i->second.second != "" )
660 str << i->second.second << "#";
662 i->second.second = str.str( );
670 // -------------------------------------------------------------------------
671 template< unsigned int _Enum >
672 void cpPlugins::BaseObjects::Parameters::
673 _ClearList( const std::string& name )
675 auto i = this->m_Parameters.find( name );
676 if( i != this->m_Parameters.end( ) )
678 if( i->second.first == ( Self::Type )( _Enum ) )
680 if( i->second.second != "" )
682 i->second.second = "";