]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Parameters.cxx.d
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Parameters.cxx.d
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 $include "Value.e"
6
7 #include <cpPlugins/Parameters.h>
8 #include <cpPlugins/ProcessObject.h>
9 #include <boost/property_tree/ptree.hpp>
10 #include <boost/tokenizer.hpp>
11
12 // -------------------------------------------------------------------------
13 #define cpPlugins_Parameters_SetInsert( _set_, _value_ )        \
14   _set_.insert( _value_ )
15
16 #define cpPlugins_Parameters_SequenceInsert( _sequence_, _value_ )      \
17   _sequence_.push_back( _value_ )
18
19 // -------------------------------------------------------------------------
20 cpPlugins::Parameters::
21 Parameters( )
22 {
23 }
24
25 // -------------------------------------------------------------------------
26 cpPlugins::Parameters::
27 ~Parameters( )
28 {
29 }
30
31 // -------------------------------------------------------------------------
32 void cpPlugins::Parameters::
33 Print( std::ostream& o ) const
34 {
35   {{#t}}
36   if( this->m_{{t}}ValueParams.size( ) > 0 )
37   {
38     o << "+ {{t}} scalar parameters:" << std::endl;
39     for( const TValueParams::value_type& p: this->m_{{t}}ValueParams )
40       o << p.first << " = " << *( p.second.second.get( ) ) << std::endl;
41   } // end if
42   {{#c}}
43   if( this->m_{{t}}{{c}}Params.size( ) > 0 )
44   {
45     o << "+ {{t}} {{c}} parameters:" << std::endl;
46     for( const T{{c}}Params::value_type& p: this->m_{{t}}{{c}}Params )
47     {
48       o << p.first;
49       for( const TValuePtr& s: p.second.second )
50         o << " : " << *( s.get( ) );
51       o << std::endl;
52
53     } // end for
54   } // end if
55   {{/c}}{{/t}}
56 }
57
58 {{#t}}{{#a}}
59 // -------------------------------------------------------------------------
60 void cpPlugins::Parameters::
61 Configure{{t}}{{a}}( const TString& name, const TValue& templ )
62 {
63   {{#w}}
64   if( templ.GetTypeName( ) == typeid( T{{w}} ).name( ) )
65   {
66     this->Configure{{t}}{{a}}< T{{w}} >( name );
67   }
68   else
69   {{/w}};
70 }
71 {{/a}}{{/t}}
72
73 // -------------------------------------------------------------------------
74 template< class _TValue >
75 void cpPlugins::Parameters::
76 ConfigureInValue( const TString& name )
77 {
78   typedef TValuePair               _TPair;
79   typedef TValueParams::value_type _TValuePair;
80
81   if( this->m_InValueParams.find( name ) == this->m_InValueParams.end( ) )
82   {
83     this->m_InValueParams.insert(
84       _TValuePair(
85         name, _TPair( TValue( typeid( _TValue ) ), TValuePtr( ) )
86         )
87       );
88   } // end if
89 }
90
91 // -------------------------------------------------------------------------
92 template< class _TValue >
93 void cpPlugins::Parameters::
94 ConfigureOutValue( const TString& name )
95 {
96   typedef TValuePair               _TPair;
97   typedef TValueParams::value_type _TValuePair;
98
99   if( this->m_OutValueParams.find( name ) == this->m_OutValueParams.end( ) )
100   {
101     TValue t( typeid( _TValue ) );
102     TValuePtr p = std::make_shared< TValue >( );
103     t.SetSource( this );
104     p->SetSource( this );
105     this->m_OutValueParams.insert( _TValuePair( name, _TPair( t, p ) ) );
106   } // end if
107 }
108
109 {{#t}}{{#c}}
110 // -------------------------------------------------------------------------
111 template< class _TValue >
112 void cpPlugins::Parameters::
113 Configure{{t}}{{c}}( const TString& name )
114 {
115   typedef T{{c}}Pair               _TPair;
116   typedef T{{c}}Params::value_type _T{{c}}Pair;
117
118   if(
119     this->m_{{t}}{{c}}Params.find( name ) == this->m_{{t}}{{c}}Params.end( )
120     )
121   {
122     this->m_{{t}}{{c}}Params.insert(
123       _T{{c}}Pair(
124         name, _TPair( TValue( typeid( _TValue ) ), T{{c}}( ) )
125         )
126       );
127   } // end if
128 }
129 {{/c}}{{/t}}
130
131 {{#t}}
132 // -------------------------------------------------------------------------
133 const cpPlugins::Parameters::
134 TValue& cpPlugins::Parameters::
135 Get{{t}}Value( const TString& name ) const
136 {
137   TValueParams::const_iterator i = this->m_{{t}}ValueParams.find( name );
138   if( i != this->m_{{t}}ValueParams.end( ) )
139   {
140     if( i->second.second )
141       return( *( i->second.second.get( ) ) );
142     else
143       cpPluginsErrorMacro(
144         << "{{t}}ValueParameter \"" << name << "\" is not yet assigned." );
145   }
146   else
147     cpPluginsErrorMacro(
148       << "{{t}}ValueParameter \"" << name << "\" not found." );
149 }
150 {{/t}}
151
152 {{#t}}{{#c}}
153 // -------------------------------------------------------------------------
154 const cpPlugins::Parameters::
155 T{{c}}& cpPlugins::Parameters::
156 Get{{t}}{{c}}( const TString& name ) const
157 {
158   T{{c}}Params::const_iterator i = this->m_{{t}}{{c}}Params.find( name );
159   if( i != this->m_{{t}}{{c}}Params.end( ) )
160     return( i->second.second );
161   else
162     cpPluginsErrorMacro(
163       << "{{t}}ValueParameter \"" << name << "\" not found." );
164 }
165 {{/c}}{{/t}}
166
167 // -------------------------------------------------------------------------
168 cpPlugins::Parameters::
169 TValuePtr& cpPlugins::Parameters::
170 GetOutValuePtr( const TString& name )
171 {
172   TValueParams::iterator i = this->m_OutValueParams.find( name );
173   if( i != this->m_OutValueParams.end( ) )
174     return( i->second.second );
175   else
176     cpPluginsErrorMacro(
177       << "OutValueParameter \"" << name << "\" not found." );
178 }
179
180 // -------------------------------------------------------------------------
181 const cpPlugins::Parameters::
182 TValuePtr& cpPlugins::Parameters::
183 GetOutValuePtr( const TString& name ) const
184 {
185   TValueParams::const_iterator i = this->m_OutValueParams.find( name );
186   if( i != this->m_OutValueParams.end( ) )
187     return( i->second.second );
188   else
189     cpPluginsErrorMacro(
190       << "OutValueParameter \"" << name << "\" not found." );
191 }
192
193 {{#t}}{{#a}}
194 // -------------------------------------------------------------------------
195 std::set< std::string > cpPlugins::Parameters::
196 Get{{t}}{{a}}Names( ) const
197 {
198   std::set< std::string > names;
199   for( const T{{a}}Params::value_type& v: this->m_{{t}}{{a}}Params )
200     names.insert( v.first );
201   return( names );
202 }
203 {{/a}}{{/t}}
204
205 {{#t}}{{#a}}
206 // -------------------------------------------------------------------------
207 const cpPlugins::Parameters::
208 TValue& cpPlugins::Parameters::
209 Get{{t}}{{a}}Template( const TString& name ) const
210 {
211   T{{a}}Params::const_iterator i = this->m_{{t}}{{a}}Params.find( name );
212   if( i != this->m_{{t}}{{a}}Params.end( ) )
213     return( i->second.first );
214   else
215     cpPluginsErrorMacro( << "Parameter \"" << name << "\" not found." );
216 }
217 {{/a}}{{/t}}
218
219 {{#t}}{{#a}}
220 // -------------------------------------------------------------------------
221 bool cpPlugins::Parameters::
222 Has{{t}}{{a}}( const TString& name ) const
223 {
224   T{{a}}Params::const_iterator i = this->m_{{t}}{{a}}Params.find( name );
225   return( i != this->m_{{t}}{{a}}Params.end( ) );
226 }
227 {{/a}}{{/t}}
228
229 {{#t}}
230 // -------------------------------------------------------------------------
231 template< class _TValue >
232 void cpPlugins::Parameters::
233 Set{{t}}Value( const TString& name, const _TValue& value )
234 {
235   TValueParams::iterator i = this->m_{{t}}ValueParams.find( name );
236   if( i != this->m_{{t}}ValueParams.end( ) )
237   {
238     // Copy configured type and value
239     if( !bool( i->second.second ) )
240       i->second.second = std::make_shared< TValue >( );
241     *( i->second.second.get( ) ) = i->second.first;
242     *( i->second.second.get( ) ) = value;
243
244     // Inform the modification
245     this->Modified{{t}}Value( name );
246   } // end if
247 }
248 {{/t}}
249
250 // -------------------------------------------------------------------------
251 void cpPlugins::Parameters::
252 SetInValuePtr( const TString& name, TValuePtr& value )
253 {
254   TValueParams::iterator i = this->m_InValueParams.find( name );
255   if( i != this->m_InValueParams.end( ) )
256   {
257     i->second.second = value;
258     this->ModifiedInValue( name );
259   } // end if
260 }
261
262 {{#t}}{{#c}}
263 // -------------------------------------------------------------------------
264 template< class _TValue >
265 void cpPlugins::Parameters::
266 AddTo{{t}}{{c}}( const TString& name, const _TValue& value )
267 {
268   T{{c}}Params::iterator i = this->m_{{t}}{{c}}Params.find( name );
269   if( i != this->m_{{t}}{{c}}Params.end( ) )
270   {
271     // Copy configured type and value
272     TValuePtr nv = std::make_shared< TValue >( i->second.first );
273     *( nv.get( ) ) = value;
274
275     // Add to container and inform
276     cpPlugins_Parameters_{{c}}Insert( i->second.second, nv );
277     this->Modified{{t}}{{c}}( name );
278   } // end if
279 }
280 {{/c}}{{/t}}
281
282 {{#t}}{{#c}}
283 // -------------------------------------------------------------------------
284 void cpPlugins::Parameters::
285 Clear{{t}}{{c}}( const TString& name )
286 {
287   T{{c}}Params::iterator i = this->m_{{t}}{{c}}Params.find( name );
288   if( i != this->m_{{t}}{{c}}Params.end( ) )
289   {
290     i->second.second.clear( );
291     this->Modified{{t}}{{c}}( name );
292   } // end if
293 }
294 {{/c}}{{/t}}
295
296 // -------------------------------------------------------------------------
297 template< class _TValue >
298 void cpPlugins::Parameters::
299 AddChoice( const TString& name, const _TValue& v )
300 {
301   TChoiceParams::iterator i = this->m_ChoiceParams.find( name );
302   if( i != this->m_ChoiceParams.end( ) )
303   {
304     TValuePtr nv = std::make_shared< TValue >( i->second.first.first );
305     *( nv.get( ) ) = v;
306     i->second.second = i->second.first.second.insert( nv ).first;
307     this->ModifiedChoice( name );
308   } // end if
309 }
310
311 // -------------------------------------------------------------------------
312 void cpPlugins::Parameters::
313 ClearChoice( const TString& name )
314 {
315   TChoiceParams::iterator i = this->m_ChoiceParams.find( name );
316   if( i != this->m_ChoiceParams.end( ) )
317   {
318     i->second.second = i->second.first.second.end( );
319     this->ModifiedChoice( name );
320   } // end if
321 }
322
323 // -------------------------------------------------------------------------
324 template< class _TValue >
325 void cpPlugins::Parameters::
326 ConfigureChoices( const TString& name )
327 {
328   if( this->m_ChoiceParams.find( name ) == this->m_ChoiceParams.end( ) )
329   {
330     TSetPair s( TValue( typeid( _TValue ) ), TSet( ) );
331     this->m_ChoiceParams.insert(
332       TChoiceParams::value_type( name, TChoicePair( s, s.second.end( ) ) )
333       );
334   } // end if
335 }
336
337 // -------------------------------------------------------------------------
338 const cpPlugins::Parameters::
339 TValue& cpPlugins::Parameters::
340 GetChoice( const TString& name ) const
341 {
342   static const TValue zero;
343   TChoiceParams::const_iterator i = this->m_ChoiceParams.find( name );
344   if( i != this->m_ChoiceParams.end( ) )
345   {
346     if( i->second.second != i->second.first.second.end( ) )
347       return( *( i->second.second->get( ) ) );
348     else
349       return( zero );
350   }
351   else
352     return( zero );
353 }
354
355 {{#a}}{{#t}}
356 // -------------------------------------------------------------------------
357 void cpPlugins::Parameters::
358 Modified{{t}}{{a}}( const std::string& name )
359 {
360   // Do nothing at this level
361 }
362 {{/t}}{{/a}}
363
364 // -------------------------------------------------------------------------
365 void cpPlugins::Parameters::
366 ModifiedChoice( const std::string& name )
367 {
368   // Do nothing at this level
369 }
370
371 // -------------------------------------------------------------------------
372 bool cpPlugins::Parameters::
373 Update( )
374 {
375   bool updated = true;
376   for( TValueParams::value_type& v: this->m_InValueParams )
377   {
378     if( v.second.second )
379     {
380       cpPlugins::ProcessObject* po = v.second.second->GetSource( );
381       if( po != NULL )
382       {
383         if( !( po->IsUpdated( ) ) )
384         {
385           po->Update( );
386           updated = false;
387
388         } // end if
389       } // end if
390     } // end if
391   } // end for
392   return( updated );
393 }
394
395 // -------------------------------------------------------------------------
396 template<>
397 void cpPlugins::Parameters::
398 LoadXML( const boost::property_tree::ptree::value_type& node )
399 {
400   typedef boost::char_separator< char > _TSep;
401   typedef boost::tokenizer< _TSep > _TTok;
402
403   for( const boost::property_tree::ptree::value_type& p: node.second )
404   {
405     {{#t}}
406     if( p.first == "{{t}}ValueParameter" )
407     {
408       std::string pn = p.second.get< std::string >( "<xmlattr>.name" );
409       std::string pv = p.second.get< std::string >( "<xmlattr>.value" );
410       this->Set{{t}}Value( pn, pv );
411     }
412     else
413     {{/t}}
414     {{#t}}{{#c}}
415     if( p.first == "{{t}}{{c}}Parameter" )
416     {
417       std::string pn = p.second.get< std::string >( "<xmlattr>.name" );
418       std::string pv = p.second.get< std::string >( "<xmlattr>.value" );
419       this->Clear{{t}}{{c}}( pn );
420       _TTok tok( pv, _TSep( "@" ) );
421       for( _TTok::const_iterator tIt = tok.begin( ); tIt != tok.end( ); ++tIt )
422         this->AddTo{{t}}{{c}}( pn, *tIt );
423     }
424     else
425     {{/c}}{{/t}};
426     // TODO: Set and Sequence
427   } // end for
428 }
429
430 // -------------------------------------------------------------------------
431 template<>
432 void cpPlugins::Parameters::
433 SaveXML( boost::property_tree::ptree* node ) const
434 {
435   {{#t}}
436   if( this->m_{{t}}ValueParams.size( ) > 0 )
437   {
438     for( const TValueParams::value_type& p: this->m_{{t}}ValueParams )
439     {
440       if( p.second.second )
441       {
442         boost::property_tree::ptree param;
443         param.put( "<xmlattr>.name", p.first );
444         param.put(
445         "<xmlattr>.value", std::string( *( p.second.second.get( ) ) )
446           );
447         node->add_child( "{{t}}ValueParameter", param );
448       } // end if
449     } // end for
450   } // end if
451   {{#c}}
452   if( this->m_{{t}}{{c}}Params.size( ) > 0 )
453   {
454     std::stringstream str;
455     for( const T{{c}}Params::value_type& p: this->m_{{t}}{{c}}Params )
456     {
457       for( const TValuePtr& s: p.second.second )
458         str << *( s.get( ) ) << "@";
459       boost::property_tree::ptree param;
460       param.put( "<xmlattr>.name", p.first );
461       param.put( "<xmlattr>.value", str.str( ) );
462       node->add_child( "{{t}}{{c}}Parameter", param );
463     } // end for
464   } // end if
465   {{/c}}{{/t}}
466 }
467
468 // -------------------------------------------------------------------------
469 $nm=cpPlugins::Parameters;
470 $q=$s$;cpPlugins::Value;
471
472 template $ex$ void $nm$::Configure$t$$a$< $s$ >( const TString& );
473 template $ex$ void $nm$::Set$t$Value< $q$ >( const TString&, const $q$& );
474 template $ex$ void $nm$::AddTo$t$$c$< $q$ >( const TString&, const $q$& );
475 template $ex$ void $nm$::AddChoice< $q$ >( const TString&, const $q$& );
476 template $ex$ void $nm$::ConfigureChoices< $s$ >( const TString& );
477
478 // eof - $RCSfile$