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