1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
7 #include <cpPlugins/Parameters.h>
8 #include <cpPlugins/ProcessObject.h>
9 #include <boost/property_tree/ptree.hpp>
10 #include <boost/tokenizer.hpp>
12 // -------------------------------------------------------------------------
13 #define cpPlugins_Parameters_SetInsert( _set_, _value_ ) \
14 _set_.insert( _value_ )
16 #define cpPlugins_Parameters_SequenceInsert( _sequence_, _value_ ) \
17 _sequence_.push_back( _value_ )
19 // -------------------------------------------------------------------------
20 cpPlugins::Parameters::
25 // -------------------------------------------------------------------------
26 cpPlugins::Parameters::
31 // -------------------------------------------------------------------------
32 void cpPlugins::Parameters::
33 Print( std::ostream& o ) const
36 if( this->m_{{t}}ValueParams.size( ) > 0 )
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;
43 if( this->m_{{t}}{{c}}Params.size( ) > 0 )
45 o << "+ {{t}} {{c}} parameters:" << std::endl;
46 for( const T{{c}}Params::value_type& p: this->m_{{t}}{{c}}Params )
49 for( const TValuePtr& s: p.second.second )
50 o << " : " << *( s.get( ) );
59 // -------------------------------------------------------------------------
60 void cpPlugins::Parameters::
61 Configure{{t}}{{a}}( const TString& name, const TValue& templ )
64 if( templ.GetTypeName( ) == typeid( T{{w}} ).name( ) )
66 this->Configure{{t}}{{a}}< T{{w}} >( name );
73 // -------------------------------------------------------------------------
74 template< class _TValue >
75 void cpPlugins::Parameters::
76 ConfigureInValue( const TString& name )
78 typedef TValuePair _TPair;
79 typedef TValueParams::value_type _TValuePair;
81 if( this->m_InValueParams.find( name ) == this->m_InValueParams.end( ) )
83 this->m_InValueParams.insert(
85 name, _TPair( TValue( typeid( _TValue ) ), TValuePtr( ) )
91 // -------------------------------------------------------------------------
92 template< class _TValue >
93 void cpPlugins::Parameters::
94 ConfigureOutValue( const TString& name )
96 typedef TValuePair _TPair;
97 typedef TValueParams::value_type _TValuePair;
99 if( this->m_OutValueParams.find( name ) == this->m_OutValueParams.end( ) )
101 TValue t( typeid( _TValue ) );
102 TValuePtr p = std::make_shared< TValue >( );
104 p->SetSource( this );
105 this->m_OutValueParams.insert( _TValuePair( name, _TPair( t, p ) ) );
110 // -------------------------------------------------------------------------
111 template< class _TValue >
112 void cpPlugins::Parameters::
113 Configure{{t}}{{c}}( const TString& name )
115 typedef T{{c}}Pair _TPair;
116 typedef T{{c}}Params::value_type _T{{c}}Pair;
119 this->m_{{t}}{{c}}Params.find( name ) == this->m_{{t}}{{c}}Params.end( )
122 this->m_{{t}}{{c}}Params.insert(
124 name, _TPair( TValue( typeid( _TValue ) ), T{{c}}( ) )
132 // -------------------------------------------------------------------------
133 const cpPlugins::Parameters::
134 TValue& cpPlugins::Parameters::
135 Get{{t}}Value( const TString& name ) const
137 TValueParams::const_iterator i = this->m_{{t}}ValueParams.find( name );
138 if( i != this->m_{{t}}ValueParams.end( ) )
140 if( i->second.second )
141 return( *( i->second.second.get( ) ) );
144 << "{{t}}ValueParameter \"" << name << "\" is not yet assigned." );
148 << "{{t}}ValueParameter \"" << name << "\" not found." );
153 // -------------------------------------------------------------------------
154 const cpPlugins::Parameters::
155 T{{c}}& cpPlugins::Parameters::
156 Get{{t}}{{c}}( const TString& name ) const
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 );
163 << "{{t}}ValueParameter \"" << name << "\" not found." );
167 // -------------------------------------------------------------------------
168 cpPlugins::Parameters::
169 TValuePtr& cpPlugins::Parameters::
170 GetOutValuePtr( const TString& name )
172 TValueParams::iterator i = this->m_OutValueParams.find( name );
173 if( i != this->m_OutValueParams.end( ) )
174 return( i->second.second );
177 << "OutValueParameter \"" << name << "\" not found." );
180 // -------------------------------------------------------------------------
181 const cpPlugins::Parameters::
182 TValuePtr& cpPlugins::Parameters::
183 GetOutValuePtr( const TString& name ) const
185 TValueParams::const_iterator i = this->m_OutValueParams.find( name );
186 if( i != this->m_OutValueParams.end( ) )
187 return( i->second.second );
190 << "OutValueParameter \"" << name << "\" not found." );
194 // -------------------------------------------------------------------------
195 std::set< std::string > cpPlugins::Parameters::
196 Get{{t}}{{a}}Names( ) const
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 );
206 // -------------------------------------------------------------------------
207 const cpPlugins::Parameters::
208 TValue& cpPlugins::Parameters::
209 Get{{t}}{{a}}Template( const TString& name ) const
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 );
215 cpPluginsErrorMacro( << "Parameter \"" << name << "\" not found." );
220 // -------------------------------------------------------------------------
221 bool cpPlugins::Parameters::
222 Has{{t}}{{a}}( const TString& name ) const
224 T{{a}}Params::const_iterator i = this->m_{{t}}{{a}}Params.find( name );
225 return( i != this->m_{{t}}{{a}}Params.end( ) );
230 // -------------------------------------------------------------------------
231 template< class _TValue >
232 void cpPlugins::Parameters::
233 Set{{t}}Value( const TString& name, const _TValue& value )
235 TValueParams::iterator i = this->m_{{t}}ValueParams.find( name );
236 if( i != this->m_{{t}}ValueParams.end( ) )
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;
244 // Inform the modification
245 this->Modified{{t}}Value( name );
250 // -------------------------------------------------------------------------
251 void cpPlugins::Parameters::
252 SetInValuePtr( const TString& name, TValuePtr& value )
254 TValueParams::iterator i = this->m_InValueParams.find( name );
255 if( i != this->m_InValueParams.end( ) )
257 i->second.second = value;
258 this->ModifiedInValue( name );
263 // -------------------------------------------------------------------------
264 template< class _TValue >
265 void cpPlugins::Parameters::
266 AddTo{{t}}{{c}}( const TString& name, const _TValue& value )
268 T{{c}}Params::iterator i = this->m_{{t}}{{c}}Params.find( name );
269 if( i != this->m_{{t}}{{c}}Params.end( ) )
271 // Copy configured type and value
272 TValuePtr nv = std::make_shared< TValue >( i->second.first );
273 *( nv.get( ) ) = value;
275 // Add to container and inform
276 cpPlugins_Parameters_{{c}}Insert( i->second.second, nv );
277 this->Modified{{t}}{{c}}( name );
283 // -------------------------------------------------------------------------
284 void cpPlugins::Parameters::
285 Clear{{t}}{{c}}( const TString& name )
287 T{{c}}Params::iterator i = this->m_{{t}}{{c}}Params.find( name );
288 if( i != this->m_{{t}}{{c}}Params.end( ) )
290 i->second.second.clear( );
291 this->Modified{{t}}{{c}}( name );
296 // -------------------------------------------------------------------------
297 template< class _TValue >
298 void cpPlugins::Parameters::
299 AddChoice( const TString& name, const _TValue& v )
301 TChoiceParams::iterator i = this->m_ChoiceParams.find( name );
302 if( i != this->m_ChoiceParams.end( ) )
304 TValuePtr nv = std::make_shared< TValue >( i->second.first.first );
306 i->second.second = i->second.first.second.insert( nv ).first;
307 this->ModifiedChoice( name );
311 // -------------------------------------------------------------------------
312 void cpPlugins::Parameters::
313 ClearChoice( const TString& name )
315 TChoiceParams::iterator i = this->m_ChoiceParams.find( name );
316 if( i != this->m_ChoiceParams.end( ) )
318 i->second.second = i->second.first.second.end( );
319 this->ModifiedChoice( name );
323 // -------------------------------------------------------------------------
324 template< class _TValue >
325 void cpPlugins::Parameters::
326 ConfigureChoices( const TString& name )
328 if( this->m_ChoiceParams.find( name ) == this->m_ChoiceParams.end( ) )
330 TSetPair s( TValue( typeid( _TValue ) ), TSet( ) );
331 this->m_ChoiceParams.insert(
332 TChoiceParams::value_type( name, TChoicePair( s, s.second.end( ) ) )
337 // -------------------------------------------------------------------------
338 const cpPlugins::Parameters::
339 TValue& cpPlugins::Parameters::
340 GetChoice( const TString& name ) const
342 static const TValue zero;
343 TChoiceParams::const_iterator i = this->m_ChoiceParams.find( name );
344 if( i != this->m_ChoiceParams.end( ) )
346 if( i->second.second != i->second.first.second.end( ) )
347 return( *( i->second.second->get( ) ) );
356 // -------------------------------------------------------------------------
357 void cpPlugins::Parameters::
358 Modified{{t}}{{a}}( const std::string& name )
360 // Do nothing at this level
364 // -------------------------------------------------------------------------
365 void cpPlugins::Parameters::
366 ModifiedChoice( const std::string& name )
368 // Do nothing at this level
371 // -------------------------------------------------------------------------
372 bool cpPlugins::Parameters::
376 for( TValueParams::value_type& v: this->m_InValueParams )
378 if( v.second.second )
380 cpPlugins::ProcessObject* po = v.second.second->GetSource( );
383 if( !( po->IsUpdated( ) ) )
395 // -------------------------------------------------------------------------
397 void cpPlugins::Parameters::
398 LoadXML( const boost::property_tree::ptree::value_type& node )
400 typedef boost::char_separator< char > _TSep;
401 typedef boost::tokenizer< _TSep > _TTok;
403 for( const boost::property_tree::ptree::value_type& p: node.second )
406 if( p.first == "{{t}}ValueParameter" )
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 );
415 if( p.first == "{{t}}{{c}}Parameter" )
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 );
426 // TODO: Set and Sequence
430 // -------------------------------------------------------------------------
432 void cpPlugins::Parameters::
433 SaveXML( boost::property_tree::ptree* node ) const
436 if( this->m_{{t}}ValueParams.size( ) > 0 )
438 for( const TValueParams::value_type& p: this->m_{{t}}ValueParams )
440 if( p.second.second )
442 boost::property_tree::ptree param;
443 param.put( "<xmlattr>.name", p.first );
445 "<xmlattr>.value", std::string( *( p.second.second.get( ) ) )
447 node->add_child( "{{t}}ValueParameter", param );
452 if( this->m_{{t}}{{c}}Params.size( ) > 0 )
454 std::stringstream str;
455 for( const T{{c}}Params::value_type& p: this->m_{{t}}{{c}}Params )
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 );
468 // -------------------------------------------------------------------------
469 $nm=cpPlugins::Parameters;
470 $q=$s$;cpPlugins::Value;
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& );