1 #include <cpPlugins/Parameters.h>
2 #include <tinyxml2/tinyxml2.h>
4 // -------------------------------------------------------------------------
5 cpPlugins::Parameters::
11 // -------------------------------------------------------------------------
12 cpPlugins::Parameters::
17 // -------------------------------------------------------------------------
18 void cpPlugins::Parameters::
21 this->m_TimeStamp.Modified( );
24 // -------------------------------------------------------------------------
25 itk::ModifiedTimeType cpPlugins::Parameters::
28 return( this->m_TimeStamp.GetMTime( ) );
31 // -------------------------------------------------------------------------
32 void cpPlugins::Parameters::
35 this->m_Parameters.clear( );
39 // -------------------------------------------------------------------------
40 void cpPlugins::Parameters::
41 GetNames( std::vector< std::string >& container ) const
44 TParameters::const_iterator i = this->m_Parameters.begin( );
45 for( ; i != this->m_Parameters.end( ); ++i )
46 container.push_back( i->first );
49 // -------------------------------------------------------------------------
50 cpPlugins::Parameters::
51 Type cpPlugins::Parameters::
52 GetType( const std::string& name ) const
54 auto i = this->m_Parameters.find( name );
55 if( i != this->m_Parameters.end( ) )
56 return( i->second.first );
58 return( Self::NoType );
61 // -------------------------------------------------------------------------
62 #define cpPlugins_Parameters_TypeAsString( Y ) \
63 if( i->second.first == Self::Y ) \
66 std::string cpPlugins::Parameters::
67 GetTypeAsString( const std::string& name ) const
69 auto i = this->m_Parameters.find( name );
70 cpPlugins_Parameters_TypeAsString( String );
71 else cpPlugins_Parameters_TypeAsString( Bool );
72 else cpPlugins_Parameters_TypeAsString( Int );
73 else cpPlugins_Parameters_TypeAsString( Uint );
74 else cpPlugins_Parameters_TypeAsString( Real );
75 else cpPlugins_Parameters_TypeAsString( OpenFileName );
76 else cpPlugins_Parameters_TypeAsString( SaveFileName );
77 else cpPlugins_Parameters_TypeAsString( PathName );
78 else cpPlugins_Parameters_TypeAsString( StringList );
79 else cpPlugins_Parameters_TypeAsString( BoolList );
80 else cpPlugins_Parameters_TypeAsString( IntList );
81 else cpPlugins_Parameters_TypeAsString( UintList );
82 else cpPlugins_Parameters_TypeAsString( RealList );
83 else cpPlugins_Parameters_TypeAsString( OpenFileNameList );
84 else cpPlugins_Parameters_TypeAsString( SaveFileNameList );
85 else cpPlugins_Parameters_TypeAsString( PathNameList );
86 else cpPlugins_Parameters_TypeAsString( Choices );
87 else return( "NoType" );
90 // -------------------------------------------------------------------------
91 #define cpPlugins_Parameters_TypeFromString( Y, str ) \
92 if( str == std::string( #Y ) ) \
95 cpPlugins::Parameters::
96 Type cpPlugins::Parameters::
97 GetTypeFromString( const std::string& t )
99 cpPlugins_Parameters_TypeFromString( String, t );
100 else cpPlugins_Parameters_TypeFromString( Bool, t );
101 else cpPlugins_Parameters_TypeFromString( Int, t );
102 else cpPlugins_Parameters_TypeFromString( Uint, t );
103 else cpPlugins_Parameters_TypeFromString( Real, t );
104 else cpPlugins_Parameters_TypeFromString( OpenFileName, t );
105 else cpPlugins_Parameters_TypeFromString( SaveFileName, t );
106 else cpPlugins_Parameters_TypeFromString( PathName, t );
107 else cpPlugins_Parameters_TypeFromString( StringList, t );
108 else cpPlugins_Parameters_TypeFromString( BoolList, t );
109 else cpPlugins_Parameters_TypeFromString( IntList, t );
110 else cpPlugins_Parameters_TypeFromString( UintList, t );
111 else cpPlugins_Parameters_TypeFromString( RealList, t );
112 else cpPlugins_Parameters_TypeFromString( OpenFileNameList, t );
113 else cpPlugins_Parameters_TypeFromString( SaveFileNameList, t );
114 else cpPlugins_Parameters_TypeFromString( PathNameList, t );
115 else cpPlugins_Parameters_TypeFromString( Choices, t );
116 else return( Self::NoType );
119 // -------------------------------------------------------------------------
120 std::string cpPlugins::Parameters::
121 GetString( const std::string& name, bool force ) const
123 auto i = this->m_Parameters.find( name );
124 if( i != this->m_Parameters.end( ) )
126 if( i->second.first == Self::String || force )
127 return( i->second.second );
135 // -------------------------------------------------------------------------
136 void cpPlugins::Parameters::
137 SetString( const std::string& name, const std::string& v, bool force )
139 auto i = this->m_Parameters.find( name );
140 if( i != this->m_Parameters.end( ) )
142 if( i->second.first == Self::String || force )
144 if( i->second.second != v )
146 i->second.second = v;
156 // -------------------------------------------------------------------------
157 void cpPlugins::Parameters::
159 const std::string& name, const std::vector< std::string >& choices
162 // It is invalid not to give choices when configuring
163 if( choices.size( ) == 0 )
166 std::stringstream str_choices;
167 str_choices << choices[ 0 ];
168 for( unsigned int i = 1; i < choices.size( ); ++i )
169 str_choices << "#" << choices[ i ];
171 this->m_Parameters[ name ] =
172 TParameter( Self::Choices, str_choices.str( ) );
176 // -------------------------------------------------------------------------
177 std::vector< std::string > cpPlugins::Parameters::
178 GetChoices( const std::string& name ) const
180 std::vector< std::string > choices;
182 TParameters::const_iterator i = this->m_Parameters.find( name );
183 if( i != this->m_Parameters.end( ) )
185 if( i->second.first == Self::Choices )
187 std::istringstream str_choices( i->second.second );
188 std::string real_choices;
189 std::getline( str_choices, real_choices, '@' );
190 std::istringstream str( real_choices );
192 while( std::getline( str, token, '#' ) )
193 choices.push_back( token );
201 // -------------------------------------------------------------------------
202 std::string cpPlugins::Parameters::
203 GetSelectedChoice( const std::string& name ) const
205 auto i = this->m_Parameters.find( name );
206 if( i != this->m_Parameters.end( ) )
208 if( i->second.first == Self::Choices )
210 std::istringstream str_choices( i->second.second );
211 std::string real_choice;
212 std::getline( str_choices, real_choice, '@' );
213 std::getline( str_choices, real_choice, '@' );
214 return( real_choice );
223 // -------------------------------------------------------------------------
224 bool cpPlugins::Parameters::
225 SetSelectedChoice( const std::string& name, const std::string& choice )
227 auto i = this->m_Parameters.find( name );
228 if( i != this->m_Parameters.end( ) )
230 if( i->second.first == Self::Choices )
232 std::istringstream str_choices( i->second.second );
234 std::getline( str_choices, choices, '@' );
235 if( choices.find( choice ) != std::string::npos )
237 std::stringstream new_choices;
238 new_choices << choices << "@" << choice;
239 i->second.second = new_choices.str( );
253 // -------------------------------------------------------------------------
254 std::string cpPlugins::Parameters::
255 GetAcceptedFileExtensions( const std::string& name ) const
257 auto i = this->m_AcceptedFileExtensions.find( name );
258 if( i != this->m_AcceptedFileExtensions.end( ) )
264 // -------------------------------------------------------------------------
265 void cpPlugins::Parameters::
266 SetAcceptedFileExtensions(
267 const std::string& name, const std::string& extensions
270 auto i = this->m_Parameters.find( name );
271 if( i != this->m_Parameters.end( ) )
273 bool is_valid = ( i->second.first == Self::OpenFileName );
274 is_valid |= ( i->second.first == Self::SaveFileName );
275 is_valid |= ( i->second.first == Self::OpenFileNameList );
276 is_valid |= ( i->second.first == Self::SaveFileNameList );
278 this->m_AcceptedFileExtensions[ name ] = extensions;
283 // -------------------------------------------------------------------------
284 bool cpPlugins::Parameters::
286 tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
289 if( parent_elem == NULL )
292 auto pIt = this->m_Parameters.begin( );
293 for( ; pIt != this->m_Parameters.end( ); ++pIt )
295 tinyxml2::XMLElement* p = doc->NewElement( "parameter" );
296 p->SetAttribute( "name", pIt->first.c_str( ) );
297 p->SetAttribute( "value", pIt->second.second.c_str( ) );
298 p->SetAttribute( "type", this->GetTypeAsString( pIt->first ).c_str( ) );
299 parent_elem->InsertEndChild( p );
305 // -------------------------------------------------------------------------
306 bool cpPlugins::Parameters::
307 FromXML( const tinyxml2::XMLElement* filter_elem )
309 const tinyxml2::XMLElement* param =
310 filter_elem->FirstChildElement( "parameter" );
312 while( param != NULL )
314 const char* param_name = param->Attribute( "name" );
315 const char* param_type = param->Attribute( "type" );
316 if( param_name != NULL && param_type != NULL )
319 value.second = param->Attribute( "value" );
320 value.first = Self::GetTypeFromString( param_type );
321 this->m_Parameters[ param_name ] = value;
324 param = param->NextSiblingElement( "parameter" );
332 // -------------------------------------------------------------------------
333 cpPlugins::Parameters::
334 TParameters& cpPlugins::Parameters::
337 return( this->m_Parameters );
340 // -------------------------------------------------------------------------
341 const cpPlugins::Parameters::
342 TParameters& cpPlugins::Parameters::
343 GetRawParameters( ) const
345 return( this->m_Parameters );
348 // -------------------------------------------------------------------------
349 #define cpPlugins_Parameters_Configure_Code( Y ) \
350 void cpPlugins::Parameters::ConfigureAs##Y( const std::string& name ) \
351 { this->_Configure< Y >( name ); } \
352 bool cpPlugins::Parameters::Has##Y( const std::string& name ) const \
353 { return( this->_Has< Y >( name ) ); }
355 // -------------------------------------------------------------------------
356 #define cpPlugins_Parameters_GetSet_Code( Y ) \
357 cpPlugins::Parameters::T##Y \
358 cpPlugins::Parameters::Get##Y( const std::string& name ) const \
359 { return( this->_Get< T##Y, Y >( name ) ); } \
360 void cpPlugins::Parameters::Set##Y( \
361 const std::string& name, const T##Y& v \
363 { this->_Set< T##Y, Y >( name, v ); }
365 // -------------------------------------------------------------------------
366 #define cpPlugins_Parameters_GetSetList_Code( Y ) \
367 std::vector< cpPlugins::Parameters::T##Y > \
368 cpPlugins::Parameters::Get##Y##List( const std::string& name ) const \
369 { return( this->_GetList< T##Y, Y##List >( name ) ); } \
370 void cpPlugins::Parameters::AddTo##Y##List( \
371 const std::string& name, const cpPlugins::Parameters::T##Y& v \
373 { this->_AddToList< T##Y, Y##List >( name, v ); } \
374 void cpPlugins::Parameters::Clear##Y##List( const std::string& name ) \
375 { this->_ClearList< Y##List >( name ); }
377 // -------------------------------------------------------------------------
378 cpPlugins_Parameters_Configure_Code( String );
379 cpPlugins_Parameters_Configure_Code( Bool );
380 cpPlugins_Parameters_Configure_Code( Int );
381 cpPlugins_Parameters_Configure_Code( Uint );
382 cpPlugins_Parameters_Configure_Code( Real );
383 cpPlugins_Parameters_Configure_Code( OpenFileName );
384 cpPlugins_Parameters_Configure_Code( SaveFileName );
385 cpPlugins_Parameters_Configure_Code( PathName );
386 cpPlugins_Parameters_Configure_Code( StringList );
387 cpPlugins_Parameters_Configure_Code( BoolList );
388 cpPlugins_Parameters_Configure_Code( IntList );
389 cpPlugins_Parameters_Configure_Code( UintList );
390 cpPlugins_Parameters_Configure_Code( RealList );
391 cpPlugins_Parameters_Configure_Code( OpenFileNameList );
392 cpPlugins_Parameters_Configure_Code( SaveFileNameList );
393 cpPlugins_Parameters_Configure_Code( PathNameList );
394 cpPlugins_Parameters_Configure_Code( Choices );
395 cpPlugins_Parameters_GetSet_Code( Bool );
396 cpPlugins_Parameters_GetSet_Code( Int );
397 cpPlugins_Parameters_GetSet_Code( Uint );
398 cpPlugins_Parameters_GetSet_Code( Real );
399 cpPlugins_Parameters_GetSet_Code( OpenFileName );
400 cpPlugins_Parameters_GetSet_Code( SaveFileName );
401 cpPlugins_Parameters_GetSet_Code( PathName );
402 cpPlugins_Parameters_GetSetList_Code( String );
403 cpPlugins_Parameters_GetSetList_Code( Bool );
404 cpPlugins_Parameters_GetSetList_Code( Int );
405 cpPlugins_Parameters_GetSetList_Code( Uint );
406 cpPlugins_Parameters_GetSetList_Code( Real );
407 cpPlugins_Parameters_GetSetList_Code( OpenFileName );
408 cpPlugins_Parameters_GetSetList_Code( SaveFileName );
409 cpPlugins_Parameters_GetSetList_Code( PathName );
411 // -------------------------------------------------------------------------
412 template< unsigned int _Enum >
413 void cpPlugins::Parameters::
414 _Configure( const std::string& name )
416 this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
420 // -------------------------------------------------------------------------
421 template< unsigned int _Enum >
422 bool cpPlugins::Parameters::
423 _Has( const std::string& name ) const
425 auto i = this->m_Parameters.find( name );
426 if( i != this->m_Parameters.end( ) )
427 return( i->second.first == ( Self::Type )( _Enum ) );
432 // -------------------------------------------------------------------------
433 template< class _Type, unsigned int _Enum >
434 _Type cpPlugins::Parameters::
435 _Get( const std::string& name ) const
437 auto i = this->m_Parameters.find( name );
438 if( i != this->m_Parameters.end( ) )
440 if( i->second.first == ( Self::Type )( _Enum ) )
442 if( typeid( _Type ) != typeid( std::string ) )
444 std::istringstream tok_str( i->second.second );
452 reinterpret_cast< const _Type* >( &( i->second.second ) );
460 return( _Type( 0 ) );
463 // -------------------------------------------------------------------------
464 template< class _Type, unsigned int _Enum >
465 void cpPlugins::Parameters::
466 _Set( const std::string& name, const _Type& v )
468 auto i = this->m_Parameters.find( name );
469 if( i != this->m_Parameters.end( ) )
471 if( i->second.first == ( Self::Type )( _Enum ) )
473 if( typeid( _Type ) != typeid( std::string ) )
475 std::stringstream str;
477 if( i->second.second != str.str( ) )
479 i->second.second = str.str( );
486 const std::string* str = reinterpret_cast< const std::string* >( &v );
487 if( i->second.second != *str )
489 i->second.second = *str;
502 // -------------------------------------------------------------------------
503 template< class _Type, unsigned int _Enum >
504 std::vector< _Type > cpPlugins::Parameters::
505 _GetList( const std::string& name ) const
507 std::vector< _Type > lst;
508 std::vector< std::string >* slst =
509 reinterpret_cast< std::vector< std::string >* >( &lst );
510 auto i = this->m_Parameters.find( name );
511 if( i != this->m_Parameters.end( ) )
513 if( i->second.first == ( Self::Type )( _Enum ) )
515 std::vector< std::string > tokens;
516 cpPlugins::TokenizeString( tokens, i->second.second, "#" );
517 for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
519 if( typeid( _Type ) != typeid( std::string ) )
521 std::istringstream tok_str( *t );
527 slst->push_back( *t );
537 // -------------------------------------------------------------------------
538 template< class _Type, unsigned int _Enum >
539 void cpPlugins::Parameters::
540 _AddToList( const std::string& name, const _Type& v )
542 auto i = this->m_Parameters.find( name );
543 if( i != this->m_Parameters.end( ) )
545 if( i->second.first == ( Self::Type )( _Enum ) )
547 std::stringstream str;
548 if( i->second.second != "" )
549 str << i->second.second << "#";
551 i->second.second = str.str( );
559 // -------------------------------------------------------------------------
560 template< unsigned int _Enum >
561 void cpPlugins::Parameters::
562 _ClearList( const std::string& name )
564 auto i = this->m_Parameters.find( name );
565 if( i != this->m_Parameters.end( ) )
567 if( i->second.first == ( Self::Type )( _Enum ) )
569 if( i->second.second != "" )
571 i->second.second = "";