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 );
203 this->SetSelectedChoice( name, "float" );
206 // -------------------------------------------------------------------------
207 void cpPlugins::BaseObjects::Parameters::
208 ConfigureAsIntTypesChoices( const std::string& name )
210 std::vector< std::string > choices;
211 choices.push_back( "char" );
212 choices.push_back( "uchar" );
213 choices.push_back( "short" );
214 choices.push_back( "ushort" );
215 choices.push_back( "int" );
216 choices.push_back( "uint" );
217 choices.push_back( "long" );
218 choices.push_back( "ulong" );
219 this->ConfigureAsChoices( name, choices );
220 this->SetSelectedChoice( name, "char" );
223 // -------------------------------------------------------------------------
224 void cpPlugins::BaseObjects::Parameters::
225 ConfigureAsScalarTypesChoices( const std::string& name )
227 std::vector< std::string > choices;
228 choices.push_back( "char" );
229 choices.push_back( "uchar" );
230 choices.push_back( "short" );
231 choices.push_back( "ushort" );
232 choices.push_back( "int" );
233 choices.push_back( "uint" );
234 choices.push_back( "long" );
235 choices.push_back( "ulong" );
236 choices.push_back( "float" );
237 choices.push_back( "double" );
238 this->ConfigureAsChoices( name, choices );
239 this->SetSelectedChoice( name, "char" );
242 // -------------------------------------------------------------------------
243 std::vector< std::string > cpPlugins::BaseObjects::Parameters::
244 GetChoices( const std::string& name ) const
246 std::vector< std::string > choices;
248 TParameters::const_iterator i = this->m_Parameters.find( name );
249 if( i != this->m_Parameters.end( ) )
251 if( i->second.first == Self::Choices )
253 std::istringstream str_choices( i->second.second );
254 std::string real_choices;
255 std::getline( str_choices, real_choices, '@' );
256 std::istringstream str( real_choices );
258 while( std::getline( str, token, '#' ) )
259 choices.push_back( token );
267 // -------------------------------------------------------------------------
268 std::string cpPlugins::BaseObjects::Parameters::
269 GetSelectedChoice( const std::string& name ) const
271 auto i = this->m_Parameters.find( name );
272 if( i != this->m_Parameters.end( ) )
274 if( i->second.first == Self::Choices )
276 std::istringstream str_choices( i->second.second );
277 std::string real_choice;
278 std::getline( str_choices, real_choice, '@' );
279 std::getline( str_choices, real_choice, '@' );
280 return( real_choice );
289 // -------------------------------------------------------------------------
290 bool cpPlugins::BaseObjects::Parameters::
291 SetSelectedChoice( const std::string& name, const std::string& choice )
293 auto i = this->m_Parameters.find( name );
294 if( i != this->m_Parameters.end( ) )
296 if( i->second.first == Self::Choices )
298 std::istringstream str_choices( i->second.second );
300 std::getline( str_choices, choices, '@' );
301 if( choices.find( choice ) != std::string::npos )
303 std::stringstream new_choices;
304 new_choices << choices << "@" << choice;
305 i->second.second = new_choices.str( );
319 // -------------------------------------------------------------------------
320 std::string cpPlugins::BaseObjects::Parameters::
321 GetAcceptedFileExtensions( const std::string& name ) const
323 auto i = this->m_AcceptedFileExtensions.find( name );
324 if( i != this->m_AcceptedFileExtensions.end( ) )
330 // -------------------------------------------------------------------------
331 void cpPlugins::BaseObjects::Parameters::
332 SetAcceptedFileExtensions(
333 const std::string& name, const std::string& extensions
336 auto i = this->m_Parameters.find( name );
337 if( i != this->m_Parameters.end( ) )
339 bool is_valid = ( i->second.first == Self::OpenFileName );
340 is_valid |= ( i->second.first == Self::SaveFileName );
341 is_valid |= ( i->second.first == Self::OpenFileNameList );
342 is_valid |= ( i->second.first == Self::SaveFileNameList );
344 this->m_AcceptedFileExtensions[ name ] = extensions;
349 // -------------------------------------------------------------------------
350 bool cpPlugins::BaseObjects::Parameters::
352 tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
355 if( parent_elem == NULL )
358 auto pIt = this->m_Parameters.begin( );
359 for( ; pIt != this->m_Parameters.end( ); ++pIt )
361 tinyxml2::XMLElement* p = doc->NewElement( "Parameter" );
362 p->SetAttribute( "Name", pIt->first.c_str( ) );
363 p->SetAttribute( "Value", pIt->second.second.c_str( ) );
364 p->SetAttribute( "Type", this->GetTypeAsString( pIt->first ).c_str( ) );
365 parent_elem->InsertEndChild( p );
371 // -------------------------------------------------------------------------
372 bool cpPlugins::BaseObjects::Parameters::
373 FromXML( const tinyxml2::XMLElement* filter_elem )
375 const tinyxml2::XMLElement* param =
376 filter_elem->FirstChildElement( "Parameter" );
378 while( param != NULL )
380 const char* param_name = param->Attribute( "Name" );
381 const char* param_type = param->Attribute( "Type" );
382 if( param_name != NULL && param_type != NULL )
385 value.second = param->Attribute( "Value" );
386 value.first = Self::GetTypeFromString( param_type );
387 this->m_Parameters[ param_name ] = value;
390 param = param->NextSiblingElement( "Parameter" );
398 // -------------------------------------------------------------------------
399 cpPlugins::BaseObjects::Parameters::
400 TParameters& cpPlugins::BaseObjects::Parameters::
403 return( this->m_Parameters );
406 // -------------------------------------------------------------------------
407 const cpPlugins::BaseObjects::Parameters::
408 TParameters& cpPlugins::BaseObjects::Parameters::
409 GetRawParameters( ) const
411 return( this->m_Parameters );
414 // -------------------------------------------------------------------------
415 #define cpPlugins_BaseObjects_Parameters_Configure_Code( Y ) \
416 void cpPlugins::BaseObjects::Parameters:: \
417 ConfigureAs##Y( const std::string& name, const T##Y& init ) \
419 this->_Configure< Y >( name ); \
420 this->Set##Y( name, init ); \
422 bool cpPlugins::BaseObjects::Parameters:: \
423 Has##Y( const std::string& name ) const \
424 { return( this->_Has< Y >( name ) ); }
426 // -------------------------------------------------------------------------
427 #define cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Y ) \
428 void cpPlugins::BaseObjects::Parameters:: \
429 ConfigureAs##Y##List( const std::string& name ) \
430 { this->_Configure< Y##List >( name ); } \
431 bool cpPlugins::BaseObjects::Parameters:: \
432 Has##Y##List( const std::string& name ) const \
433 { return( this->_Has< Y##List >( name ) ); }
435 // -------------------------------------------------------------------------
436 #define cpPlugins_BaseObjects_Parameters_GetSet_Code( Y ) \
437 cpPlugins::BaseObjects::Parameters::T##Y \
438 cpPlugins::BaseObjects::Parameters:: \
439 Get##Y( const std::string& name ) const \
440 { return( this->_Get< T##Y, Y >( name ) ); } \
441 void cpPlugins::BaseObjects::Parameters::Set##Y( \
442 const std::string& name, const T##Y& v \
444 { this->_Set< T##Y, Y >( name, v ); }
446 // -------------------------------------------------------------------------
447 #define cpPlugins_BaseObjects_Parameters_GetSetList_Code( Y ) \
448 std::vector< cpPlugins::BaseObjects::Parameters::T##Y > \
449 cpPlugins::BaseObjects::Parameters:: \
450 Get##Y##List( const std::string& name ) const \
451 { return( this->_GetList< T##Y, Y##List >( name ) ); } \
452 void cpPlugins::BaseObjects::Parameters::AddTo##Y##List( \
453 const std::string& name, \
454 const cpPlugins::BaseObjects::Parameters::T##Y& v \
456 { this->_AddToList< T##Y, Y##List >( name, v ); } \
457 void cpPlugins::BaseObjects::Parameters:: \
458 Clear##Y##List( const std::string& name ) \
459 { this->_ClearList< Y##List >( name ); }
461 // -------------------------------------------------------------------------
462 cpPlugins_BaseObjects_Parameters_Configure_Code( String );
463 cpPlugins_BaseObjects_Parameters_Configure_Code( Bool );
464 cpPlugins_BaseObjects_Parameters_Configure_Code( Int );
465 cpPlugins_BaseObjects_Parameters_Configure_Code( Uint );
466 cpPlugins_BaseObjects_Parameters_Configure_Code( Real );
467 cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileName );
468 cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileName );
469 cpPlugins_BaseObjects_Parameters_Configure_Code( PathName );
471 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( String );
472 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Bool );
473 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Int );
474 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Uint );
475 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Real );
476 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( OpenFileName );
477 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( SaveFileName );
478 cpPlugins_BaseObjects_Parameters_ConfigureList_Code( PathName );
480 cpPlugins_BaseObjects_Parameters_GetSet_Code( Bool );
481 cpPlugins_BaseObjects_Parameters_GetSet_Code( Int );
482 cpPlugins_BaseObjects_Parameters_GetSet_Code( Uint );
483 cpPlugins_BaseObjects_Parameters_GetSet_Code( Real );
484 cpPlugins_BaseObjects_Parameters_GetSet_Code( OpenFileName );
485 cpPlugins_BaseObjects_Parameters_GetSet_Code( SaveFileName );
486 cpPlugins_BaseObjects_Parameters_GetSet_Code( PathName );
488 cpPlugins_BaseObjects_Parameters_GetSetList_Code( String );
489 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Bool );
490 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Int );
491 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Uint );
492 cpPlugins_BaseObjects_Parameters_GetSetList_Code( Real );
493 cpPlugins_BaseObjects_Parameters_GetSetList_Code( PathName );
495 // -------------------------------------------------------------------------
496 std::vector< cpPlugins::BaseObjects::Parameters::TOpenFileName >
497 cpPlugins::BaseObjects::Parameters::
498 GetOpenFileNameList( const std::string& name ) const
500 return( this->_GetList< TOpenFileName, OpenFileNameList >( name ) );
503 // -------------------------------------------------------------------------
504 void cpPlugins::BaseObjects::Parameters::
505 AddToOpenFileNameList(
506 const std::string& name,
507 const cpPlugins::BaseObjects::Parameters::TOpenFileName& v
510 auto i = this->m_Parameters.find( name );
511 if( i != this->m_Parameters.end( ) )
513 if( i->second.first == OpenFileNameList )
515 auto pos = v.find_last_of( "/\\" );
516 if( i->second.second == "" )
517 i->second.second = v.substr( 0, pos );
518 i->second.second += std::string( "#" );
519 i->second.second += v.substr( pos + 1 );
527 // -------------------------------------------------------------------------
528 void cpPlugins::BaseObjects::Parameters::
529 ClearOpenFileNameList( const std::string& name )
531 this->_ClearList< OpenFileNameList >( name );
534 // -------------------------------------------------------------------------
535 std::vector< cpPlugins::BaseObjects::Parameters::TSaveFileName >
536 cpPlugins::BaseObjects::Parameters::
537 GetSaveFileNameList( const std::string& name ) const
539 return( this->_GetList< TSaveFileName, SaveFileNameList >( name ) );
542 // -------------------------------------------------------------------------
543 void cpPlugins::BaseObjects::Parameters::
544 AddToSaveFileNameList(
545 const std::string& name,
546 const cpPlugins::BaseObjects::Parameters::TSaveFileName& v
549 auto i = this->m_Parameters.find( name );
550 if( i != this->m_Parameters.end( ) )
552 if( i->second.first == SaveFileNameList )
554 auto pos = v.find_last_of( "/\\" );
555 if( i->second.second == "" )
556 i->second.second = v.substr( 0, pos );
557 i->second.second += std::string( "#" );
558 i->second.second += v.substr( pos + 1 );
566 // -------------------------------------------------------------------------
567 void cpPlugins::BaseObjects::Parameters::
568 ClearSaveFileNameList( const std::string& name )
570 this->_ClearList< SaveFileNameList >( name );
573 // -------------------------------------------------------------------------
574 template< unsigned int _Enum >
575 void cpPlugins::BaseObjects::Parameters::
576 _Configure( const std::string& name )
578 this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
582 // -------------------------------------------------------------------------
583 template< unsigned int _Enum >
584 bool cpPlugins::BaseObjects::Parameters::
585 _Has( const std::string& name ) const
587 auto i = this->m_Parameters.find( name );
588 if( i != this->m_Parameters.end( ) )
589 return( i->second.first == ( Self::Type )( _Enum ) );
594 // -------------------------------------------------------------------------
595 template< class _Type, unsigned int _Enum >
596 _Type cpPlugins::BaseObjects::Parameters::
597 _Get( const std::string& name ) const
599 auto i = this->m_Parameters.find( name );
600 if( i != this->m_Parameters.end( ) )
602 if( i->second.first == ( Self::Type )( _Enum ) )
604 if( typeid( _Type ) != typeid( std::string ) )
606 std::istringstream tok_str( i->second.second );
614 reinterpret_cast< const _Type* >( &( i->second.second ) );
622 return( _Type( 0 ) );
625 // -------------------------------------------------------------------------
626 template< class _Type, unsigned int _Enum >
627 void cpPlugins::BaseObjects::Parameters::
628 _Set( const std::string& name, const _Type& v )
630 auto i = this->m_Parameters.find( name );
631 if( i != this->m_Parameters.end( ) )
633 if( i->second.first == ( Self::Type )( _Enum ) )
635 if( typeid( _Type ) != typeid( std::string ) )
637 std::stringstream str;
639 if( i->second.second != str.str( ) )
641 i->second.second = str.str( );
648 const std::string* str = reinterpret_cast< const std::string* >( &v );
649 if( i->second.second != *str )
651 i->second.second = *str;
664 // -------------------------------------------------------------------------
665 template< class _Type, unsigned int _Enum >
666 std::vector< _Type > cpPlugins::BaseObjects::Parameters::
667 _GetList( const std::string& name ) const
669 std::vector< _Type > lst;
670 std::vector< std::string >* slst =
671 reinterpret_cast< std::vector< std::string >* >( &lst );
672 auto i = this->m_Parameters.find( name );
673 if( i != this->m_Parameters.end( ) )
675 if( i->second.first == ( Self::Type )( _Enum ) )
677 std::vector< std::string > tokens;
678 cpPlugins::Tokenize( tokens, i->second.second, "#" );
679 for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
681 if( typeid( _Type ) != typeid( std::string ) )
683 std::istringstream tok_str( *t );
689 slst->push_back( *t );
699 // -------------------------------------------------------------------------
700 template< class _Type, unsigned int _Enum >
701 void cpPlugins::BaseObjects::Parameters::
702 _AddToList( const std::string& name, const _Type& v )
704 auto i = this->m_Parameters.find( name );
705 if( i != this->m_Parameters.end( ) )
707 if( i->second.first == ( Self::Type )( _Enum ) )
709 std::stringstream str;
710 if( i->second.second != "" )
711 str << i->second.second << "#";
713 i->second.second = str.str( );
721 // -------------------------------------------------------------------------
722 template< unsigned int _Enum >
723 void cpPlugins::BaseObjects::Parameters::
724 _ClearList( const std::string& name )
726 auto i = this->m_Parameters.find( name );
727 if( i != this->m_Parameters.end( ) )
729 if( i->second.first == ( Self::Type )( _Enum ) )
731 if( i->second.second != "" )
733 i->second.second = "";