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 ) \
353 { this->_Configure< Y >( name ); } \
354 bool cpPlugins::BaseObjects::Parameters:: \
355 Has##Y( const std::string& name ) const \
356 { return( this->_Has< Y >( name ) ); }
358 // -------------------------------------------------------------------------
359 #define cpPlugins_BaseObjects_Parameters_GetSet_Code( Y ) \
360 cpPlugins::BaseObjects::Parameters::T##Y \
361 cpPlugins::BaseObjects::Parameters:: \
362 Get##Y( const std::string& name ) const \
363 { return( this->_Get< T##Y, Y >( name ) ); } \
364 void cpPlugins::BaseObjects::Parameters::Set##Y( \
365 const std::string& name, const T##Y& v \
367 { this->_Set< T##Y, Y >( name, v ); }
369 // -------------------------------------------------------------------------
370 #define cpPlugins_BaseObjects_Parameters_GetSetList_Code( Y ) \
371 std::vector< cpPlugins::BaseObjects::Parameters::T##Y > \
372 cpPlugins::BaseObjects::Parameters:: \
373 Get##Y##List( const std::string& name ) const \
374 { return( this->_GetList< T##Y, Y##List >( name ) ); } \
375 void cpPlugins::BaseObjects::Parameters::AddTo##Y##List( \
376 const std::string& name, \
377 const cpPlugins::BaseObjects::Parameters::T##Y& v \
379 { this->_AddToList< T##Y, Y##List >( name, v ); } \
380 void cpPlugins::BaseObjects::Parameters:: \
381 Clear##Y##List( const std::string& name ) \
382 { this->_ClearList< Y##List >( name ); }
384 // -------------------------------------------------------------------------
385 cpPlugins_BaseObjects_Parameters_Configure_Code( String );
386 cpPlugins_BaseObjects_Parameters_Configure_Code( Bool );
387 cpPlugins_BaseObjects_Parameters_Configure_Code( Int );
388 cpPlugins_BaseObjects_Parameters_Configure_Code( Uint );
389 cpPlugins_BaseObjects_Parameters_Configure_Code( Real );
390 cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileName );
391 cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileName );
392 cpPlugins_BaseObjects_Parameters_Configure_Code( PathName );
393 cpPlugins_BaseObjects_Parameters_Configure_Code( StringList );
394 cpPlugins_BaseObjects_Parameters_Configure_Code( BoolList );
395 cpPlugins_BaseObjects_Parameters_Configure_Code( IntList );
396 cpPlugins_BaseObjects_Parameters_Configure_Code( UintList );
397 cpPlugins_BaseObjects_Parameters_Configure_Code( RealList );
398 cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileNameList );
399 cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileNameList );
400 cpPlugins_BaseObjects_Parameters_Configure_Code( PathNameList );
401 cpPlugins_BaseObjects_Parameters_Configure_Code( Choices );
402 cpPlugins_BaseObjects_Parameters_GetSet_Code( Bool );
403 cpPlugins_BaseObjects_Parameters_GetSet_Code( Int );
404 cpPlugins_BaseObjects_Parameters_GetSet_Code( Uint );
405 cpPlugins_BaseObjects_Parameters_GetSet_Code( Real );
406 cpPlugins_BaseObjects_Parameters_GetSet_Code( OpenFileName );
407 cpPlugins_BaseObjects_Parameters_GetSet_Code( SaveFileName );
408 cpPlugins_BaseObjects_Parameters_GetSet_Code( PathName );
409 cpPlugins_BaseObjects_Parameters_GetSetList_Code( String );
410 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Bool );
411 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Int );
412 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Uint );
413 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Real );
414 cpPlugins_BaseObjects_Parameters_GetSetList_Code( OpenFileName );
415 cpPlugins_BaseObjects_Parameters_GetSetList_Code( SaveFileName );
416 cpPlugins_BaseObjects_Parameters_GetSetList_Code( PathName );
418 // -------------------------------------------------------------------------
419 template< unsigned int _Enum >
420 void cpPlugins::BaseObjects::Parameters::
421 _Configure( const std::string& name )
423 this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
427 // -------------------------------------------------------------------------
428 template< unsigned int _Enum >
429 bool cpPlugins::BaseObjects::Parameters::
430 _Has( const std::string& name ) const
432 auto i = this->m_Parameters.find( name );
433 if( i != this->m_Parameters.end( ) )
434 return( i->second.first == ( Self::Type )( _Enum ) );
439 // -------------------------------------------------------------------------
440 template< class _Type, unsigned int _Enum >
441 _Type cpPlugins::BaseObjects::Parameters::
442 _Get( const std::string& name ) const
444 auto i = this->m_Parameters.find( name );
445 if( i != this->m_Parameters.end( ) )
447 if( i->second.first == ( Self::Type )( _Enum ) )
449 if( typeid( _Type ) != typeid( std::string ) )
451 std::istringstream tok_str( i->second.second );
459 reinterpret_cast< const _Type* >( &( i->second.second ) );
467 return( _Type( 0 ) );
470 // -------------------------------------------------------------------------
471 template< class _Type, unsigned int _Enum >
472 void cpPlugins::BaseObjects::Parameters::
473 _Set( const std::string& name, const _Type& v )
475 auto i = this->m_Parameters.find( name );
476 if( i != this->m_Parameters.end( ) )
478 if( i->second.first == ( Self::Type )( _Enum ) )
480 if( typeid( _Type ) != typeid( std::string ) )
482 std::stringstream str;
484 if( i->second.second != str.str( ) )
486 i->second.second = str.str( );
493 const std::string* str = reinterpret_cast< const std::string* >( &v );
494 if( i->second.second != *str )
496 i->second.second = *str;
509 // -------------------------------------------------------------------------
510 template< class _Type, unsigned int _Enum >
511 std::vector< _Type > cpPlugins::BaseObjects::Parameters::
512 _GetList( const std::string& name ) const
514 std::vector< _Type > lst;
515 std::vector< std::string >* slst =
516 reinterpret_cast< std::vector< std::string >* >( &lst );
517 auto i = this->m_Parameters.find( name );
518 if( i != this->m_Parameters.end( ) )
520 if( i->second.first == ( Self::Type )( _Enum ) )
522 std::vector< std::string > tokens;
523 cpExtensions::Tokenize( tokens, i->second.second, "#" );
524 for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
526 if( typeid( _Type ) != typeid( std::string ) )
528 std::istringstream tok_str( *t );
534 slst->push_back( *t );
544 // -------------------------------------------------------------------------
545 template< class _Type, unsigned int _Enum >
546 void cpPlugins::BaseObjects::Parameters::
547 _AddToList( const std::string& name, const _Type& v )
549 auto i = this->m_Parameters.find( name );
550 if( i != this->m_Parameters.end( ) )
552 if( i->second.first == ( Self::Type )( _Enum ) )
554 std::stringstream str;
555 if( i->second.second != "" )
556 str << i->second.second << "#";
558 i->second.second = str.str( );
566 // -------------------------------------------------------------------------
567 template< unsigned int _Enum >
568 void cpPlugins::BaseObjects::Parameters::
569 _ClearList( const std::string& name )
571 auto i = this->m_Parameters.find( name );
572 if( i != this->m_Parameters.end( ) )
574 if( i->second.first == ( Self::Type )( _Enum ) )
576 if( i->second.second != "" )
578 i->second.second = "";