1 #include <cpPlugins/BaseObjects/Parameters.h>
2 #include <cpPlugins/BaseObjects/ProcessObject.h>
3 #include <cpPlugins/Utility.h>
4 #include <tinyxml2/tinyxml2.h>
6 // -------------------------------------------------------------------------
7 cpPlugins::BaseObjects::Parameters::
9 : m_ProcessObject( NULL )
14 // -------------------------------------------------------------------------
15 cpPlugins::BaseObjects::Parameters::
20 // -------------------------------------------------------------------------
21 cpPlugins::BaseObjects::ProcessObject* cpPlugins::BaseObjects::Parameters::
24 return( this->m_ProcessObject );
27 // -------------------------------------------------------------------------
29 cpPlugins::BaseObjects::ProcessObject* cpPlugins::BaseObjects::Parameters::
30 GetProcessObject( ) const
32 return( this->m_ProcessObject );
35 // -------------------------------------------------------------------------
36 void cpPlugins::BaseObjects::Parameters::
37 SetProcessObject( cpPlugins::BaseObjects::ProcessObject* po )
39 this->m_ProcessObject = po;
42 // -------------------------------------------------------------------------
43 void cpPlugins::BaseObjects::Parameters::
46 if( this->m_ProcessObject != NULL )
47 this->m_ProcessObject->Modified( );
50 // -------------------------------------------------------------------------
51 void cpPlugins::BaseObjects::Parameters::
54 this->m_Parameters.clear( );
58 // -------------------------------------------------------------------------
59 void cpPlugins::BaseObjects::Parameters::
60 GetNames( std::vector< std::string >& container ) const
63 TParameters::const_iterator i = this->m_Parameters.begin( );
64 for( ; i != this->m_Parameters.end( ); ++i )
65 container.push_back( i->first );
68 // -------------------------------------------------------------------------
69 cpPlugins::BaseObjects::Parameters::
70 Type cpPlugins::BaseObjects::Parameters::
71 GetType( const std::string& name ) const
73 auto i = this->m_Parameters.find( name );
74 if( i != this->m_Parameters.end( ) )
75 return( i->second.first );
77 return( Self::NoType );
80 // -------------------------------------------------------------------------
81 #define cpPlugins_BaseObjects_Parameters_TypeAsString( Y ) \
82 if( i->second.first == Self::Y ) \
85 std::string cpPlugins::BaseObjects::Parameters::
86 GetTypeAsString( const std::string& name ) const
88 auto i = this->m_Parameters.find( name );
89 cpPlugins_BaseObjects_Parameters_TypeAsString( String );
90 else cpPlugins_BaseObjects_Parameters_TypeAsString( Bool );
91 else cpPlugins_BaseObjects_Parameters_TypeAsString( Int );
92 else cpPlugins_BaseObjects_Parameters_TypeAsString( Uint );
93 else cpPlugins_BaseObjects_Parameters_TypeAsString( Real );
94 else cpPlugins_BaseObjects_Parameters_TypeAsString( OpenFileName );
95 else cpPlugins_BaseObjects_Parameters_TypeAsString( SaveFileName );
96 else cpPlugins_BaseObjects_Parameters_TypeAsString( PathName );
97 else cpPlugins_BaseObjects_Parameters_TypeAsString( StringList );
98 else cpPlugins_BaseObjects_Parameters_TypeAsString( BoolList );
99 else cpPlugins_BaseObjects_Parameters_TypeAsString( IntList );
100 else cpPlugins_BaseObjects_Parameters_TypeAsString( UintList );
101 else cpPlugins_BaseObjects_Parameters_TypeAsString( RealList );
102 else cpPlugins_BaseObjects_Parameters_TypeAsString( OpenFileNameList );
103 else cpPlugins_BaseObjects_Parameters_TypeAsString( SaveFileNameList );
104 else cpPlugins_BaseObjects_Parameters_TypeAsString( PathNameList );
105 else cpPlugins_BaseObjects_Parameters_TypeAsString( Choices );
106 else return( "NoType" );
109 // -------------------------------------------------------------------------
110 #define cpPlugins_BaseObjects_Parameters_TypeFromString( Y, str ) \
111 if( str == std::string( #Y ) ) \
114 cpPlugins::BaseObjects::Parameters::
115 Type cpPlugins::BaseObjects::Parameters::
116 GetTypeFromString( const std::string& t )
118 cpPlugins_BaseObjects_Parameters_TypeFromString( String, t );
119 else cpPlugins_BaseObjects_Parameters_TypeFromString( Bool, t );
120 else cpPlugins_BaseObjects_Parameters_TypeFromString( Int, t );
121 else cpPlugins_BaseObjects_Parameters_TypeFromString( Uint, t );
122 else cpPlugins_BaseObjects_Parameters_TypeFromString( Real, t );
123 else cpPlugins_BaseObjects_Parameters_TypeFromString( OpenFileName, t );
124 else cpPlugins_BaseObjects_Parameters_TypeFromString( SaveFileName, t );
125 else cpPlugins_BaseObjects_Parameters_TypeFromString( PathName, t );
126 else cpPlugins_BaseObjects_Parameters_TypeFromString( StringList, t );
127 else cpPlugins_BaseObjects_Parameters_TypeFromString( BoolList, t );
128 else cpPlugins_BaseObjects_Parameters_TypeFromString( IntList, t );
129 else cpPlugins_BaseObjects_Parameters_TypeFromString( UintList, t );
130 else cpPlugins_BaseObjects_Parameters_TypeFromString( RealList, t );
131 else cpPlugins_BaseObjects_Parameters_TypeFromString( OpenFileNameList, t );
132 else cpPlugins_BaseObjects_Parameters_TypeFromString( SaveFileNameList, t );
133 else cpPlugins_BaseObjects_Parameters_TypeFromString( PathNameList, t );
134 else cpPlugins_BaseObjects_Parameters_TypeFromString( Choices, t );
135 else return( Self::NoType );
138 // -------------------------------------------------------------------------
139 std::string cpPlugins::BaseObjects::Parameters::
140 GetString( const std::string& name, bool force ) const
142 auto i = this->m_Parameters.find( name );
143 if( i != this->m_Parameters.end( ) )
145 if( i->second.first == Self::String || force )
146 return( i->second.second );
154 // -------------------------------------------------------------------------
155 void cpPlugins::BaseObjects::Parameters::
156 SetString( const std::string& name, const std::string& v, bool force )
158 auto i = this->m_Parameters.find( name );
159 if( i != this->m_Parameters.end( ) )
161 if( i->second.first == Self::String || force )
163 if( i->second.second != v )
165 i->second.second = v;
175 // -------------------------------------------------------------------------
176 void cpPlugins::BaseObjects::Parameters::
178 const std::string& name, const std::vector< std::string >& choices
181 // It is invalid not to give choices when configuring
182 if( choices.size( ) == 0 )
185 std::stringstream str_choices;
186 str_choices << choices[ 0 ];
187 for( unsigned int i = 1; i < choices.size( ); ++i )
188 str_choices << "#" << choices[ i ];
190 this->m_Parameters[ name ] =
191 TParameter( Self::Choices, str_choices.str( ) );
195 // -------------------------------------------------------------------------
196 void cpPlugins::BaseObjects::Parameters::
197 ConfigureAsRealTypesChoices( const std::string& name )
199 std::vector< std::string > choices;
200 choices.push_back( "float" );
201 choices.push_back( "double" );
202 this->ConfigureAsChoices( name, choices );
205 // -------------------------------------------------------------------------
206 void cpPlugins::BaseObjects::Parameters::
207 ConfigureAsIntTypesChoices( const std::string& name )
209 std::vector< std::string > choices;
210 choices.push_back( "char" );
211 choices.push_back( "uchar" );
212 choices.push_back( "short" );
213 choices.push_back( "ushort" );
214 choices.push_back( "int" );
215 choices.push_back( "uint" );
216 choices.push_back( "long" );
217 choices.push_back( "ulong" );
218 this->ConfigureAsChoices( name, choices );
221 // -------------------------------------------------------------------------
222 void cpPlugins::BaseObjects::Parameters::
223 ConfigureAsScalarTypesChoices( const std::string& name )
225 std::vector< std::string > choices;
226 choices.push_back( "char" );
227 choices.push_back( "uchar" );
228 choices.push_back( "short" );
229 choices.push_back( "ushort" );
230 choices.push_back( "int" );
231 choices.push_back( "uint" );
232 choices.push_back( "long" );
233 choices.push_back( "ulong" );
234 choices.push_back( "float" );
235 choices.push_back( "double" );
236 this->ConfigureAsChoices( name, choices );
239 // -------------------------------------------------------------------------
240 std::vector< std::string > cpPlugins::BaseObjects::Parameters::
241 GetChoices( const std::string& name ) const
243 std::vector< std::string > choices;
245 TParameters::const_iterator i = this->m_Parameters.find( name );
246 if( i != this->m_Parameters.end( ) )
248 if( i->second.first == Self::Choices )
250 std::istringstream str_choices( i->second.second );
251 std::string real_choices;
252 std::getline( str_choices, real_choices, '@' );
253 std::istringstream str( real_choices );
255 while( std::getline( str, token, '#' ) )
256 choices.push_back( token );
264 // -------------------------------------------------------------------------
265 std::string cpPlugins::BaseObjects::Parameters::
266 GetSelectedChoice( const std::string& name ) const
268 auto i = this->m_Parameters.find( name );
269 if( i != this->m_Parameters.end( ) )
271 if( i->second.first == Self::Choices )
273 std::istringstream str_choices( i->second.second );
274 std::string real_choice;
275 std::getline( str_choices, real_choice, '@' );
276 std::getline( str_choices, real_choice, '@' );
277 return( real_choice );
286 // -------------------------------------------------------------------------
287 bool cpPlugins::BaseObjects::Parameters::
288 SetSelectedChoice( const std::string& name, const std::string& choice )
290 auto i = this->m_Parameters.find( name );
291 if( i != this->m_Parameters.end( ) )
293 if( i->second.first == Self::Choices )
295 std::istringstream str_choices( i->second.second );
297 std::getline( str_choices, choices, '@' );
298 if( choices.find( choice ) != std::string::npos )
300 std::stringstream new_choices;
301 new_choices << choices << "@" << choice;
302 i->second.second = new_choices.str( );
316 // -------------------------------------------------------------------------
317 std::string cpPlugins::BaseObjects::Parameters::
318 GetAcceptedFileExtensions( const std::string& name ) const
320 auto i = this->m_AcceptedFileExtensions.find( name );
321 if( i != this->m_AcceptedFileExtensions.end( ) )
327 // -------------------------------------------------------------------------
328 void cpPlugins::BaseObjects::Parameters::
329 SetAcceptedFileExtensions(
330 const std::string& name, const std::string& extensions
333 auto i = this->m_Parameters.find( name );
334 if( i != this->m_Parameters.end( ) )
336 bool is_valid = ( i->second.first == Self::OpenFileName );
337 is_valid |= ( i->second.first == Self::SaveFileName );
338 is_valid |= ( i->second.first == Self::OpenFileNameList );
339 is_valid |= ( i->second.first == Self::SaveFileNameList );
341 this->m_AcceptedFileExtensions[ name ] = extensions;
346 // -------------------------------------------------------------------------
347 bool cpPlugins::BaseObjects::Parameters::
349 tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
352 if( parent_elem == NULL )
355 auto pIt = this->m_Parameters.begin( );
356 for( ; pIt != this->m_Parameters.end( ); ++pIt )
358 tinyxml2::XMLElement* p = doc->NewElement( "Parameter" );
359 p->SetAttribute( "Name", pIt->first.c_str( ) );
360 p->SetAttribute( "Value", pIt->second.second.c_str( ) );
361 p->SetAttribute( "Type", this->GetTypeAsString( pIt->first ).c_str( ) );
362 parent_elem->InsertEndChild( p );
368 // -------------------------------------------------------------------------
369 bool cpPlugins::BaseObjects::Parameters::
370 FromXML( const tinyxml2::XMLElement* filter_elem )
372 const tinyxml2::XMLElement* param =
373 filter_elem->FirstChildElement( "Parameter" );
375 while( param != NULL )
377 const char* param_name = param->Attribute( "Name" );
378 const char* param_type = param->Attribute( "Type" );
379 if( param_name != NULL && param_type != NULL )
382 value.second = param->Attribute( "Value" );
383 value.first = Self::GetTypeFromString( param_type );
384 this->m_Parameters[ param_name ] = value;
387 param = param->NextSiblingElement( "Parameter" );
395 // -------------------------------------------------------------------------
396 cpPlugins::BaseObjects::Parameters::
397 TParameters& cpPlugins::BaseObjects::Parameters::
400 return( this->m_Parameters );
403 // -------------------------------------------------------------------------
404 const cpPlugins::BaseObjects::Parameters::
405 TParameters& cpPlugins::BaseObjects::Parameters::
406 GetRawParameters( ) const
408 return( this->m_Parameters );
411 // -------------------------------------------------------------------------
412 #define cpPlugins_BaseObjects_Parameters_Configure_Code( Y ) \
413 void cpPlugins::BaseObjects::Parameters:: \
414 ConfigureAs##Y( const std::string& name, const T##Y& init ) \
416 this->_Configure< Y >( name ); \
417 this->Set##Y( name, init ); \
419 bool cpPlugins::BaseObjects::Parameters:: \
420 Has##Y( const std::string& name ) const \
421 { return( this->_Has< Y >( name ) ); }
423 // -------------------------------------------------------------------------
424 #define cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Y ) \
425 void cpPlugins::BaseObjects::Parameters:: \
426 ConfigureAs##Y##List( const std::string& name ) \
427 { this->_Configure< Y##List >( name ); } \
428 bool cpPlugins::BaseObjects::Parameters:: \
429 Has##Y##List( const std::string& name ) const \
430 { return( this->_Has< Y##List >( name ) ); }
432 // -------------------------------------------------------------------------
433 #define cpPlugins_BaseObjects_Parameters_GetSet_Code( Y ) \
434 cpPlugins::BaseObjects::Parameters::T##Y \
435 cpPlugins::BaseObjects::Parameters:: \
436 Get##Y( const std::string& name ) const \
437 { return( this->_Get< T##Y, Y >( name ) ); } \
438 void cpPlugins::BaseObjects::Parameters::Set##Y( \
439 const std::string& name, const T##Y& v \
441 { this->_Set< T##Y, Y >( name, v ); }
443 // -------------------------------------------------------------------------
444 #define cpPlugins_BaseObjects_Parameters_GetSetList_Code( Y ) \
445 std::vector< cpPlugins::BaseObjects::Parameters::T##Y > \
446 cpPlugins::BaseObjects::Parameters:: \
447 Get##Y##List( const std::string& name ) const \
448 { return( this->_GetList< T##Y, Y##List >( name ) ); } \
449 void cpPlugins::BaseObjects::Parameters::AddTo##Y##List( \
450 const std::string& name, \
451 const cpPlugins::BaseObjects::Parameters::T##Y& v \
453 { this->_AddToList< T##Y, Y##List >( name, v ); } \
454 void cpPlugins::BaseObjects::Parameters:: \
455 Clear##Y##List( const std::string& name ) \
456 { this->_ClearList< Y##List >( name ); }
458 // -------------------------------------------------------------------------
459 cpPlugins_BaseObjects_Parameters_Configure_Code( String );
460 cpPlugins_BaseObjects_Parameters_Configure_Code( Bool );
461 cpPlugins_BaseObjects_Parameters_Configure_Code( Int );
462 cpPlugins_BaseObjects_Parameters_Configure_Code( Uint );
463 cpPlugins_BaseObjects_Parameters_Configure_Code( Real );
464 cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileName );
465 cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileName );
466 cpPlugins_BaseObjects_Parameters_Configure_Code( PathName );
468 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( String );
469 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Bool );
470 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Int );
471 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Uint );
472 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Real );
473 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( OpenFileName );
474 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( SaveFileName );
475 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( PathName );
477 cpPlugins_BaseObjects_Parameters_GetSet_Code( Bool );
478 cpPlugins_BaseObjects_Parameters_GetSet_Code( Int );
479 cpPlugins_BaseObjects_Parameters_GetSet_Code( Uint );
480 cpPlugins_BaseObjects_Parameters_GetSet_Code( Real );
481 cpPlugins_BaseObjects_Parameters_GetSet_Code( OpenFileName );
482 cpPlugins_BaseObjects_Parameters_GetSet_Code( SaveFileName );
483 cpPlugins_BaseObjects_Parameters_GetSet_Code( PathName );
485 cpPlugins_BaseObjects_Parameters_GetSetList_Code( String );
486 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Bool );
487 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Int );
488 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Uint );
489 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Real );
490 cpPlugins_BaseObjects_Parameters_GetSetList_Code( PathName );
492 // -------------------------------------------------------------------------
493 std::vector< cpPlugins::BaseObjects::Parameters::TOpenFileName >
494 cpPlugins::BaseObjects::Parameters::
495 GetOpenFileNameList( const std::string& name ) const
497 return( this->_GetList< TOpenFileName, OpenFileNameList >( name ) );
500 // -------------------------------------------------------------------------
501 void cpPlugins::BaseObjects::Parameters::
502 AddToOpenFileNameList(
503 const std::string& name,
504 const cpPlugins::BaseObjects::Parameters::TOpenFileName& v
507 auto i = this->m_Parameters.find( name );
508 if( i != this->m_Parameters.end( ) )
510 if( i->second.first == OpenFileNameList )
512 auto pos = v.find_last_of( "/\\" );
513 if( i->second.second == "" )
514 i->second.second = v.substr( 0, pos );
515 i->second.second += std::string( "#" );
516 i->second.second += v.substr( pos + 1 );
524 // -------------------------------------------------------------------------
525 void cpPlugins::BaseObjects::Parameters::
526 ClearOpenFileNameList( const std::string& name )
528 this->_ClearList< OpenFileNameList >( name );
531 // -------------------------------------------------------------------------
532 std::vector< cpPlugins::BaseObjects::Parameters::TSaveFileName >
533 cpPlugins::BaseObjects::Parameters::
534 GetSaveFileNameList( const std::string& name ) const
536 return( this->_GetList< TSaveFileName, SaveFileNameList >( name ) );
539 // -------------------------------------------------------------------------
540 void cpPlugins::BaseObjects::Parameters::
541 AddToSaveFileNameList(
542 const std::string& name,
543 const cpPlugins::BaseObjects::Parameters::TSaveFileName& v
546 auto i = this->m_Parameters.find( name );
547 if( i != this->m_Parameters.end( ) )
549 if( i->second.first == SaveFileNameList )
551 auto pos = v.find_last_of( "/\\" );
552 if( i->second.second == "" )
553 i->second.second = v.substr( 0, pos );
554 i->second.second += std::string( "#" );
555 i->second.second += v.substr( pos + 1 );
563 // -------------------------------------------------------------------------
564 void cpPlugins::BaseObjects::Parameters::
565 ClearSaveFileNameList( const std::string& name )
567 this->_ClearList< SaveFileNameList >( name );
570 // -------------------------------------------------------------------------
571 template< unsigned int _Enum >
572 void cpPlugins::BaseObjects::Parameters::
573 _Configure( const std::string& name )
575 this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
579 // -------------------------------------------------------------------------
580 template< unsigned int _Enum >
581 bool cpPlugins::BaseObjects::Parameters::
582 _Has( const std::string& name ) const
584 auto i = this->m_Parameters.find( name );
585 if( i != this->m_Parameters.end( ) )
586 return( i->second.first == ( Self::Type )( _Enum ) );
591 // -------------------------------------------------------------------------
592 template< class _Type, unsigned int _Enum >
593 _Type cpPlugins::BaseObjects::Parameters::
594 _Get( const std::string& name ) const
596 auto i = this->m_Parameters.find( name );
597 if( i != this->m_Parameters.end( ) )
599 if( i->second.first == ( Self::Type )( _Enum ) )
601 if( typeid( _Type ) != typeid( std::string ) )
603 std::istringstream tok_str( i->second.second );
611 reinterpret_cast< const _Type* >( &( i->second.second ) );
619 return( _Type( 0 ) );
622 // -------------------------------------------------------------------------
623 template< class _Type, unsigned int _Enum >
624 void cpPlugins::BaseObjects::Parameters::
625 _Set( const std::string& name, const _Type& v )
627 auto i = this->m_Parameters.find( name );
628 if( i != this->m_Parameters.end( ) )
630 if( i->second.first == ( Self::Type )( _Enum ) )
632 if( typeid( _Type ) != typeid( std::string ) )
634 std::stringstream str;
636 if( i->second.second != str.str( ) )
638 i->second.second = str.str( );
645 const std::string* str = reinterpret_cast< const std::string* >( &v );
646 if( i->second.second != *str )
648 i->second.second = *str;
661 // -------------------------------------------------------------------------
662 template< class _Type, unsigned int _Enum >
663 std::vector< _Type > cpPlugins::BaseObjects::Parameters::
664 _GetList( const std::string& name ) const
666 std::vector< _Type > lst;
667 std::vector< std::string >* slst =
668 reinterpret_cast< std::vector< std::string >* >( &lst );
669 auto i = this->m_Parameters.find( name );
670 if( i != this->m_Parameters.end( ) )
672 if( i->second.first == ( Self::Type )( _Enum ) )
674 std::vector< std::string > tokens;
675 cpPlugins::Tokenize( tokens, i->second.second, "#" );
676 for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
678 if( typeid( _Type ) != typeid( std::string ) )
680 std::istringstream tok_str( *t );
686 slst->push_back( *t );
696 // -------------------------------------------------------------------------
697 template< class _Type, unsigned int _Enum >
698 void cpPlugins::BaseObjects::Parameters::
699 _AddToList( const std::string& name, const _Type& v )
701 auto i = this->m_Parameters.find( name );
702 if( i != this->m_Parameters.end( ) )
704 if( i->second.first == ( Self::Type )( _Enum ) )
706 std::stringstream str;
707 if( i->second.second != "" )
708 str << i->second.second << "#";
710 i->second.second = str.str( );
718 // -------------------------------------------------------------------------
719 template< unsigned int _Enum >
720 void cpPlugins::BaseObjects::Parameters::
721 _ClearList( const std::string& name )
723 auto i = this->m_Parameters.find( name );
724 if( i != this->m_Parameters.end( ) )
726 if( i->second.first == ( Self::Type )( _Enum ) )
728 if( i->second.second != "" )
730 i->second.second = "";