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 std::vector< std::string > cpPlugins::BaseObjects::Parameters::
179 GetChoices( const std::string& name ) const
181 std::vector< std::string > choices;
183 TParameters::const_iterator i = this->m_Parameters.find( name );
184 if( i != this->m_Parameters.end( ) )
186 if( i->second.first == Self::Choices )
188 std::istringstream str_choices( i->second.second );
189 std::string real_choices;
190 std::getline( str_choices, real_choices, '@' );
191 std::istringstream str( real_choices );
193 while( std::getline( str, token, '#' ) )
194 choices.push_back( token );
202 // -------------------------------------------------------------------------
203 std::string cpPlugins::BaseObjects::Parameters::
204 GetSelectedChoice( const std::string& name ) const
206 auto i = this->m_Parameters.find( name );
207 if( i != this->m_Parameters.end( ) )
209 if( i->second.first == Self::Choices )
211 std::istringstream str_choices( i->second.second );
212 std::string real_choice;
213 std::getline( str_choices, real_choice, '@' );
214 std::getline( str_choices, real_choice, '@' );
215 return( real_choice );
224 // -------------------------------------------------------------------------
225 bool cpPlugins::BaseObjects::Parameters::
226 SetSelectedChoice( const std::string& name, const std::string& choice )
228 auto i = this->m_Parameters.find( name );
229 if( i != this->m_Parameters.end( ) )
231 if( i->second.first == Self::Choices )
233 std::istringstream str_choices( i->second.second );
235 std::getline( str_choices, choices, '@' );
236 if( choices.find( choice ) != std::string::npos )
238 std::stringstream new_choices;
239 new_choices << choices << "@" << choice;
240 i->second.second = new_choices.str( );
254 // -------------------------------------------------------------------------
255 std::string cpPlugins::BaseObjects::Parameters::
256 GetAcceptedFileExtensions( const std::string& name ) const
258 auto i = this->m_AcceptedFileExtensions.find( name );
259 if( i != this->m_AcceptedFileExtensions.end( ) )
265 // -------------------------------------------------------------------------
266 void cpPlugins::BaseObjects::Parameters::
267 SetAcceptedFileExtensions(
268 const std::string& name, const std::string& extensions
271 auto i = this->m_Parameters.find( name );
272 if( i != this->m_Parameters.end( ) )
274 bool is_valid = ( i->second.first == Self::OpenFileName );
275 is_valid |= ( i->second.first == Self::SaveFileName );
276 is_valid |= ( i->second.first == Self::OpenFileNameList );
277 is_valid |= ( i->second.first == Self::SaveFileNameList );
279 this->m_AcceptedFileExtensions[ name ] = extensions;
284 // -------------------------------------------------------------------------
285 bool cpPlugins::BaseObjects::Parameters::
287 tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
290 if( parent_elem == NULL )
293 auto pIt = this->m_Parameters.begin( );
294 for( ; pIt != this->m_Parameters.end( ); ++pIt )
296 tinyxml2::XMLElement* p = doc->NewElement( "Parameter" );
297 p->SetAttribute( "Name", pIt->first.c_str( ) );
298 p->SetAttribute( "Value", pIt->second.second.c_str( ) );
299 p->SetAttribute( "Type", this->GetTypeAsString( pIt->first ).c_str( ) );
300 parent_elem->InsertEndChild( p );
306 // -------------------------------------------------------------------------
307 bool cpPlugins::BaseObjects::Parameters::
308 FromXML( const tinyxml2::XMLElement* filter_elem )
310 const tinyxml2::XMLElement* param =
311 filter_elem->FirstChildElement( "Parameter" );
313 while( param != NULL )
315 const char* param_name = param->Attribute( "Name" );
316 const char* param_type = param->Attribute( "Type" );
317 if( param_name != NULL && param_type != NULL )
320 value.second = param->Attribute( "Value" );
321 value.first = Self::GetTypeFromString( param_type );
322 this->m_Parameters[ param_name ] = value;
325 param = param->NextSiblingElement( "Parameter" );
333 // -------------------------------------------------------------------------
334 cpPlugins::BaseObjects::Parameters::
335 TParameters& cpPlugins::BaseObjects::Parameters::
338 return( this->m_Parameters );
341 // -------------------------------------------------------------------------
342 const cpPlugins::BaseObjects::Parameters::
343 TParameters& cpPlugins::BaseObjects::Parameters::
344 GetRawParameters( ) const
346 return( this->m_Parameters );
349 // -------------------------------------------------------------------------
350 #define cpPlugins_BaseObjects_Parameters_Configure_Code( Y ) \
351 void cpPlugins::BaseObjects::Parameters:: \
352 ConfigureAs##Y( const std::string& name, const T##Y& init ) \
354 this->_Configure< Y >( name ); \
355 this->Set##Y( name, init ); \
357 bool cpPlugins::BaseObjects::Parameters:: \
358 Has##Y( const std::string& name ) const \
359 { return( this->_Has< Y >( name ) ); }
361 // -------------------------------------------------------------------------
362 #define cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Y ) \
363 void cpPlugins::BaseObjects::Parameters:: \
364 ConfigureAs##Y##List( const std::string& name ) \
365 { this->_Configure< Y##List >( name ); } \
366 bool cpPlugins::BaseObjects::Parameters:: \
367 Has##Y##List( const std::string& name ) const \
368 { return( this->_Has< Y##List >( name ) ); }
370 // -------------------------------------------------------------------------
371 #define cpPlugins_BaseObjects_Parameters_GetSet_Code( Y ) \
372 cpPlugins::BaseObjects::Parameters::T##Y \
373 cpPlugins::BaseObjects::Parameters:: \
374 Get##Y( const std::string& name ) const \
375 { return( this->_Get< T##Y, Y >( name ) ); } \
376 void cpPlugins::BaseObjects::Parameters::Set##Y( \
377 const std::string& name, const T##Y& v \
379 { this->_Set< T##Y, Y >( name, v ); }
381 // -------------------------------------------------------------------------
382 #define cpPlugins_BaseObjects_Parameters_GetSetList_Code( Y ) \
383 std::vector< cpPlugins::BaseObjects::Parameters::T##Y > \
384 cpPlugins::BaseObjects::Parameters:: \
385 Get##Y##List( const std::string& name ) const \
386 { return( this->_GetList< T##Y, Y##List >( name ) ); } \
387 void cpPlugins::BaseObjects::Parameters::AddTo##Y##List( \
388 const std::string& name, \
389 const cpPlugins::BaseObjects::Parameters::T##Y& v \
391 { this->_AddToList< T##Y, Y##List >( name, v ); } \
392 void cpPlugins::BaseObjects::Parameters:: \
393 Clear##Y##List( const std::string& name ) \
394 { this->_ClearList< Y##List >( name ); }
396 // -------------------------------------------------------------------------
397 cpPlugins_BaseObjects_Parameters_Configure_Code( String );
398 cpPlugins_BaseObjects_Parameters_Configure_Code( Bool );
399 cpPlugins_BaseObjects_Parameters_Configure_Code( Int );
400 cpPlugins_BaseObjects_Parameters_Configure_Code( Uint );
401 cpPlugins_BaseObjects_Parameters_Configure_Code( Real );
402 cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileName );
403 cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileName );
404 cpPlugins_BaseObjects_Parameters_Configure_Code( PathName );
406 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( String );
407 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Bool );
408 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Int );
409 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Uint );
410 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Real );
411 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( OpenFileName );
412 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( SaveFileName );
413 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( PathName );
415 cpPlugins_BaseObjects_Parameters_GetSet_Code( Bool );
416 cpPlugins_BaseObjects_Parameters_GetSet_Code( Int );
417 cpPlugins_BaseObjects_Parameters_GetSet_Code( Uint );
418 cpPlugins_BaseObjects_Parameters_GetSet_Code( Real );
419 cpPlugins_BaseObjects_Parameters_GetSet_Code( OpenFileName );
420 cpPlugins_BaseObjects_Parameters_GetSet_Code( SaveFileName );
421 cpPlugins_BaseObjects_Parameters_GetSet_Code( PathName );
423 cpPlugins_BaseObjects_Parameters_GetSetList_Code( String );
424 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Bool );
425 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Int );
426 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Uint );
427 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Real );
428 cpPlugins_BaseObjects_Parameters_GetSetList_Code( PathName );
430 // -------------------------------------------------------------------------
431 std::vector< cpPlugins::BaseObjects::Parameters::TOpenFileName >
432 cpPlugins::BaseObjects::Parameters::
433 GetOpenFileNameList( const std::string& name ) const
435 return( this->_GetList< TOpenFileName, OpenFileNameList >( name ) );
438 // -------------------------------------------------------------------------
439 void cpPlugins::BaseObjects::Parameters::
440 AddToOpenFileNameList(
441 const std::string& name,
442 const cpPlugins::BaseObjects::Parameters::TOpenFileName& v
445 auto i = this->m_Parameters.find( name );
446 if( i != this->m_Parameters.end( ) )
448 if( i->second.first == OpenFileNameList )
450 auto pos = v.find_last_of( "/\\" );
451 if( i->second.second == "" )
452 i->second.second = v.substr( 0, pos );
453 i->second.second += std::string( "#" );
454 i->second.second += v.substr( pos + 1 );
462 // -------------------------------------------------------------------------
463 void cpPlugins::BaseObjects::Parameters::
464 ClearOpenFileNameList( const std::string& name )
466 this->_ClearList< OpenFileNameList >( name );
469 // -------------------------------------------------------------------------
470 std::vector< cpPlugins::BaseObjects::Parameters::TSaveFileName >
471 cpPlugins::BaseObjects::Parameters::
472 GetSaveFileNameList( const std::string& name ) const
474 return( this->_GetList< TSaveFileName, SaveFileNameList >( name ) );
477 // -------------------------------------------------------------------------
478 void cpPlugins::BaseObjects::Parameters::
479 AddToSaveFileNameList(
480 const std::string& name,
481 const cpPlugins::BaseObjects::Parameters::TSaveFileName& v
484 auto i = this->m_Parameters.find( name );
485 if( i != this->m_Parameters.end( ) )
487 if( i->second.first == SaveFileNameList )
489 auto pos = v.find_last_of( "/\\" );
490 if( i->second.second == "" )
491 i->second.second = v.substr( 0, pos );
492 i->second.second += std::string( "#" );
493 i->second.second += v.substr( pos + 1 );
501 // -------------------------------------------------------------------------
502 void cpPlugins::BaseObjects::Parameters::
503 ClearSaveFileNameList( const std::string& name )
505 this->_ClearList< SaveFileNameList >( name );
508 // -------------------------------------------------------------------------
509 template< unsigned int _Enum >
510 void cpPlugins::BaseObjects::Parameters::
511 _Configure( const std::string& name )
513 this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
517 // -------------------------------------------------------------------------
518 template< unsigned int _Enum >
519 bool cpPlugins::BaseObjects::Parameters::
520 _Has( const std::string& name ) const
522 auto i = this->m_Parameters.find( name );
523 if( i != this->m_Parameters.end( ) )
524 return( i->second.first == ( Self::Type )( _Enum ) );
529 // -------------------------------------------------------------------------
530 template< class _Type, unsigned int _Enum >
531 _Type cpPlugins::BaseObjects::Parameters::
532 _Get( const std::string& name ) const
534 auto i = this->m_Parameters.find( name );
535 if( i != this->m_Parameters.end( ) )
537 if( i->second.first == ( Self::Type )( _Enum ) )
539 if( typeid( _Type ) != typeid( std::string ) )
541 std::istringstream tok_str( i->second.second );
549 reinterpret_cast< const _Type* >( &( i->second.second ) );
557 return( _Type( 0 ) );
560 // -------------------------------------------------------------------------
561 template< class _Type, unsigned int _Enum >
562 void cpPlugins::BaseObjects::Parameters::
563 _Set( const std::string& name, const _Type& v )
565 auto i = this->m_Parameters.find( name );
566 if( i != this->m_Parameters.end( ) )
568 if( i->second.first == ( Self::Type )( _Enum ) )
570 if( typeid( _Type ) != typeid( std::string ) )
572 std::stringstream str;
574 if( i->second.second != str.str( ) )
576 i->second.second = str.str( );
583 const std::string* str = reinterpret_cast< const std::string* >( &v );
584 if( i->second.second != *str )
586 i->second.second = *str;
599 // -------------------------------------------------------------------------
600 template< class _Type, unsigned int _Enum >
601 std::vector< _Type > cpPlugins::BaseObjects::Parameters::
602 _GetList( const std::string& name ) const
604 std::vector< _Type > lst;
605 std::vector< std::string >* slst =
606 reinterpret_cast< std::vector< std::string >* >( &lst );
607 auto i = this->m_Parameters.find( name );
608 if( i != this->m_Parameters.end( ) )
610 if( i->second.first == ( Self::Type )( _Enum ) )
612 std::vector< std::string > tokens;
613 cpExtensions::Tokenize( tokens, i->second.second, "#" );
614 for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
616 if( typeid( _Type ) != typeid( std::string ) )
618 std::istringstream tok_str( *t );
624 slst->push_back( *t );
634 // -------------------------------------------------------------------------
635 template< class _Type, unsigned int _Enum >
636 void cpPlugins::BaseObjects::Parameters::
637 _AddToList( const std::string& name, const _Type& v )
639 auto i = this->m_Parameters.find( name );
640 if( i != this->m_Parameters.end( ) )
642 if( i->second.first == ( Self::Type )( _Enum ) )
644 std::stringstream str;
645 if( i->second.second != "" )
646 str << i->second.second << "#";
648 i->second.second = str.str( );
656 // -------------------------------------------------------------------------
657 template< unsigned int _Enum >
658 void cpPlugins::BaseObjects::Parameters::
659 _ClearList( const std::string& name )
661 auto i = this->m_Parameters.find( name );
662 if( i != this->m_Parameters.end( ) )
664 if( i->second.first == ( Self::Type )( _Enum ) )
666 if( i->second.second != "" )
668 i->second.second = "";