]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/BaseObjects/Parameters.cxx
e77c2cc16475cd0d77e68d320544ad8919130e34
[cpPlugins.git] / lib / cpPlugins / BaseObjects / Parameters.cxx
1 #include <cpPlugins/BaseObjects/Parameters.h>
2 #include <cpExtensions/Utility.h>
3 #include <tinyxml2/tinyxml2.h>
4
5 // -------------------------------------------------------------------------
6 cpPlugins::BaseObjects::Parameters::
7 Parameters( )
8 {
9   this->Clear( );
10 }
11
12 // -------------------------------------------------------------------------
13 cpPlugins::BaseObjects::Parameters::
14 ~Parameters( )
15 {
16 }
17
18 // -------------------------------------------------------------------------
19 void cpPlugins::BaseObjects::Parameters::
20 Modified( ) const
21 {
22   this->m_TimeStamp.Modified( );
23 }
24
25 // -------------------------------------------------------------------------
26 itk::ModifiedTimeType cpPlugins::BaseObjects::Parameters::
27 GetMTime( ) const
28 {
29   return( this->m_TimeStamp.GetMTime( ) );
30 }
31
32 // -------------------------------------------------------------------------
33 void cpPlugins::BaseObjects::Parameters::
34 Clear( )
35 {
36   this->m_Parameters.clear( );
37   this->Modified( );
38 }
39
40 // -------------------------------------------------------------------------
41 void cpPlugins::BaseObjects::Parameters::
42 GetNames( std::vector< std::string >& container ) const
43 {
44   container.clear( );
45   TParameters::const_iterator i = this->m_Parameters.begin( );
46   for( ; i != this->m_Parameters.end( ); ++i )
47     container.push_back( i->first );
48 }
49
50 // -------------------------------------------------------------------------
51 cpPlugins::BaseObjects::Parameters::
52 Type cpPlugins::BaseObjects::Parameters::
53 GetType( const std::string& name ) const
54 {
55   auto i = this->m_Parameters.find( name );
56   if( i != this->m_Parameters.end( ) )
57     return( i->second.first );
58   else
59     return( Self::NoType );
60 }
61
62 // -------------------------------------------------------------------------
63 #define cpPlugins_BaseObjects_Parameters_TypeAsString( Y )      \
64   if( i->second.first == Self::Y )                              \
65     return( #Y )
66
67 std::string cpPlugins::BaseObjects::Parameters::
68 GetTypeAsString( const std::string& name ) const
69 {
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" );
89 }
90
91 // -------------------------------------------------------------------------
92 #define cpPlugins_BaseObjects_Parameters_TypeFromString( Y, str )   \
93   if( str == std::string( #Y ) )                                    \
94     return( Self::Y )
95
96 cpPlugins::BaseObjects::Parameters::
97 Type cpPlugins::BaseObjects::Parameters::
98 GetTypeFromString( const std::string& t )
99 {
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 );
118 }
119
120 // -------------------------------------------------------------------------
121 std::string cpPlugins::BaseObjects::Parameters::
122 GetString( const std::string& name, bool force ) const
123 {
124   auto i = this->m_Parameters.find( name );
125   if( i != this->m_Parameters.end( ) )
126   {
127     if( i->second.first == Self::String || force )
128       return( i->second.second );
129     else
130       return( "" );
131   }
132   else
133     return( "" );
134 }
135
136 // -------------------------------------------------------------------------
137 void cpPlugins::BaseObjects::Parameters::
138 SetString( const std::string& name, const std::string& v, bool force )
139 {
140   auto i = this->m_Parameters.find( name );
141   if( i != this->m_Parameters.end( ) )
142   {
143     if( i->second.first == Self::String || force )
144     {
145       if( i->second.second != v )
146       {
147         i->second.second = v;
148         this->Modified( );
149
150       } // fi
151
152     } // fi
153
154   } // fi
155 }
156
157 // -------------------------------------------------------------------------
158 void cpPlugins::BaseObjects::Parameters::
159 ConfigureAsChoices(
160   const std::string& name, const std::vector< std::string >& choices
161   )
162 {
163   // It is invalid not to give choices when configuring
164   if( choices.size( ) == 0 )
165     return;
166
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 ];
171   str_choices << "@";
172   this->m_Parameters[ name ] =
173     TParameter( Self::Choices, str_choices.str( ) );
174   this->Modified( );
175 }
176
177 // -------------------------------------------------------------------------
178 std::vector< std::string > cpPlugins::BaseObjects::Parameters::
179 GetChoices( const std::string& name ) const
180 {
181   std::vector< std::string > choices;
182
183   TParameters::const_iterator i = this->m_Parameters.find( name );
184   if( i != this->m_Parameters.end( ) )
185   {
186     if( i->second.first == Self::Choices )
187     {
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 );
192       std::string token;
193       while( std::getline( str, token, '#' ) )
194         choices.push_back( token );
195
196     } // fi
197
198   } // fi
199   return( choices );
200 }
201
202 // -------------------------------------------------------------------------
203 std::string cpPlugins::BaseObjects::Parameters::
204 GetSelectedChoice( const std::string& name ) const
205 {
206   auto i = this->m_Parameters.find( name );
207   if( i != this->m_Parameters.end( ) )
208   {
209     if( i->second.first == Self::Choices )
210     {
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 );
216     }
217     else
218       return( "" );
219   }
220   else
221     return( "" );
222 }
223
224 // -------------------------------------------------------------------------
225 bool cpPlugins::BaseObjects::Parameters::
226 SetSelectedChoice( const std::string& name, const std::string& choice )
227 {
228   auto i = this->m_Parameters.find( name );
229   if( i != this->m_Parameters.end( ) )
230   {
231     if( i->second.first == Self::Choices )
232     {
233       std::istringstream str_choices( i->second.second );
234       std::string choices;
235       std::getline( str_choices, choices, '@' );
236       if( choices.find( choice ) != std::string::npos )
237       {
238         std::stringstream new_choices;
239         new_choices << choices << "@" << choice;
240         i->second.second = new_choices.str( );
241         this->Modified( );
242         return( true );
243       }
244       else
245         return( false );
246     }
247     else
248       return( false );
249   }
250   else
251     return( false );
252 }
253
254 // -------------------------------------------------------------------------
255 std::string cpPlugins::BaseObjects::Parameters::
256 GetAcceptedFileExtensions( const std::string& name ) const
257 {
258   auto i = this->m_AcceptedFileExtensions.find( name );
259   if( i != this->m_AcceptedFileExtensions.end( ) )
260     return( i->second );
261   else
262     return( "" );
263 }
264
265 // -------------------------------------------------------------------------
266 void cpPlugins::BaseObjects::Parameters::
267 SetAcceptedFileExtensions(
268   const std::string& name, const std::string& extensions
269   )
270 {
271   auto i = this->m_Parameters.find( name );
272   if( i != this->m_Parameters.end( ) )
273   {
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 );
278     if( is_valid )
279       this->m_AcceptedFileExtensions[ name ] = extensions;
280
281   } // fi
282 }
283
284 // -------------------------------------------------------------------------
285 bool cpPlugins::BaseObjects::Parameters::
286 ToXML(
287   tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem
288   ) const
289 {
290   if( parent_elem == NULL )
291     return( false );
292
293   auto pIt = this->m_Parameters.begin( );
294   for( ; pIt != this->m_Parameters.end( ); ++pIt )
295   {
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 );
301
302   } // rof
303   return( true );
304 }
305
306 // -------------------------------------------------------------------------
307 bool cpPlugins::BaseObjects::Parameters::
308 FromXML( const tinyxml2::XMLElement* filter_elem )
309 {
310   const tinyxml2::XMLElement* param =
311     filter_elem->FirstChildElement( "Parameter" );
312   bool ret = false;
313   while( param != NULL )
314   {
315     const char* param_name = param->Attribute( "Name" );
316     const char* param_type = param->Attribute( "Type" );
317     if( param_name != NULL && param_type != NULL )
318     {
319       TParameter value;
320       value.second = param->Attribute( "Value" );
321       value.first = Self::GetTypeFromString( param_type );
322       this->m_Parameters[ param_name ] = value;
323
324     } // fi
325     param = param->NextSiblingElement( "Parameter" );
326     ret = true;
327
328   } // elihw
329   this->Modified( );
330   return( ret );
331 }
332
333 // -------------------------------------------------------------------------
334 cpPlugins::BaseObjects::Parameters::
335 TParameters& cpPlugins::BaseObjects::Parameters::
336 GetRawParameters( )
337 {
338   return( this->m_Parameters );
339 }
340
341 // -------------------------------------------------------------------------
342 const cpPlugins::BaseObjects::Parameters::
343 TParameters& cpPlugins::BaseObjects::Parameters::
344 GetRawParameters( ) const
345 {
346   return( this->m_Parameters );
347 }
348
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 ) ); }
357
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                              \
366     )                                                                   \
367   { this->_Set< T##Y, Y >( name, v ); }
368
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                   \
378     )                                                                   \
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 ); }
383
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 );
417
418 // -------------------------------------------------------------------------
419 template< unsigned int _Enum >
420 void cpPlugins::BaseObjects::Parameters::
421 _Configure( const std::string& name )
422 {
423   this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" );
424   this->Modified( );
425 }
426
427 // -------------------------------------------------------------------------
428 template< unsigned int _Enum >
429 bool cpPlugins::BaseObjects::Parameters::
430 _Has( const std::string& name ) const
431 {
432   auto i = this->m_Parameters.find( name );
433   if( i != this->m_Parameters.end( ) )
434     return( i->second.first == ( Self::Type )( _Enum ) );
435   else
436     return( false );
437 }
438
439 // -------------------------------------------------------------------------
440 template< class _Type, unsigned int _Enum >
441 _Type cpPlugins::BaseObjects::Parameters::
442 _Get( const std::string& name ) const
443 {
444   auto i = this->m_Parameters.find( name );
445   if( i != this->m_Parameters.end( ) )
446   {
447     if( i->second.first == ( Self::Type )( _Enum ) )
448     {
449       if( typeid( _Type ) != typeid( std::string ) )
450       {
451         std::istringstream tok_str( i->second.second );
452         _Type v;
453         tok_str >> v;
454         return( v );
455       }
456       else
457       {
458         const _Type* ptr =
459           reinterpret_cast< const _Type* >( &( i->second.second ) );
460         return( *ptr );
461
462       } // fi
463
464     } // fi
465
466   } // fi
467   return( _Type( 0 ) );
468 }
469
470 // -------------------------------------------------------------------------
471 template< class _Type, unsigned int _Enum >
472 void cpPlugins::BaseObjects::Parameters::
473 _Set( const std::string& name, const _Type& v )
474 {
475   auto i = this->m_Parameters.find( name );
476   if( i != this->m_Parameters.end( ) )
477   {
478     if( i->second.first == ( Self::Type )( _Enum ) )
479     {
480       if( typeid( _Type ) != typeid( std::string ) )
481       {
482         std::stringstream str;
483         str << v;
484         if( i->second.second != str.str( ) )
485         {
486           i->second.second = str.str( );
487           this->Modified( );
488
489         } // fi
490       }
491       else
492       {
493         const std::string* str = reinterpret_cast< const std::string* >( &v );
494         if( i->second.second != *str )
495         {
496           i->second.second = *str;
497           this->Modified( );
498
499         } // fi
500
501       } // fi
502
503     } // fi
504
505   } // fi
506
507 }
508
509 // -------------------------------------------------------------------------
510 template< class _Type, unsigned int _Enum >
511 std::vector< _Type > cpPlugins::BaseObjects::Parameters::
512 _GetList( const std::string& name ) const
513 {
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( ) )
519   {
520     if( i->second.first == ( Self::Type )( _Enum ) )
521     {
522       std::vector< std::string > tokens;
523       cpExtensions::Tokenize( tokens, i->second.second, "#" );
524       for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
525       {
526         if( typeid( _Type ) != typeid( std::string ) )
527         {
528           std::istringstream tok_str( *t );
529           _Type v;
530           tok_str >> v;
531           lst.push_back( v );
532         }
533         else
534           slst->push_back( *t );
535
536       } // rof
537
538     } // fi
539
540   } // fi
541   return( lst );
542 }
543
544 // -------------------------------------------------------------------------
545 template< class _Type, unsigned int _Enum >
546 void cpPlugins::BaseObjects::Parameters::
547 _AddToList( const std::string& name, const _Type& v )
548 {
549   auto i = this->m_Parameters.find( name );
550   if( i != this->m_Parameters.end( ) )
551   {
552     if( i->second.first == ( Self::Type )( _Enum ) )
553     {
554       std::stringstream str;
555       if( i->second.second != "" )
556         str << i->second.second << "#";
557       str << v;
558       i->second.second = str.str( );
559       this->Modified( );
560
561     } // fi
562
563   } // fi
564 }
565
566 // -------------------------------------------------------------------------
567 template< unsigned int _Enum >
568 void cpPlugins::BaseObjects::Parameters::
569 _ClearList( const std::string& name )
570 {
571   auto i = this->m_Parameters.find( name );
572   if( i != this->m_Parameters.end( ) )
573   {
574     if( i->second.first == ( Self::Type )( _Enum ) )
575     {
576       if( i->second.second != "" )
577       {
578         i->second.second = "";
579         this->Modified( );
580
581       } // fi
582
583     } // fi
584
585   } // fi
586 }
587
588 // eof - $RCSfile$