1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
7 #include <cpPlugins/Value.h>
8 #include <cpPlugins/ProcessObject.h>
10 // -------------------------------------------------------------------------
18 m_Complex( TComplex( 0 ) ),
21 this->m_Source.reset( );
24 // -------------------------------------------------------------------------
26 Value( const std::type_info& i )
31 m_Complex( TComplex( 0 ) ),
34 if( i == typeid( bool ) )
35 this->m_TypeName = typeid( TBool ).name( );
38 if( i == typeid( {{n}} ) )
39 this->m_TypeName = typeid( TNatural ).name( );
43 if( i == typeid( {{z}} ) )
44 this->m_TypeName = typeid( TInteger ).name( );
48 if( i == typeid( {{r}} ) )
49 this->m_TypeName = typeid( TReal ).name( );
53 if( i == typeid( std::complex< {{r}} > ) )
54 this->m_TypeName = typeid( TComplex ).name( );
57 if( i == typeid( std::string ) )
58 this->m_TypeName = typeid( TString ).name( );
59 this->m_Source.reset( );
62 // -------------------------------------------------------------------------
64 Value( const Self& v )
65 : m_TypeName( v.m_TypeName ),
67 m_Natural( v.m_Natural ),
68 m_Integer( v.m_Integer ),
70 m_Complex( v.m_Complex ),
71 m_String( v.m_String ),
72 m_Source( v.m_Source )
76 // -------------------------------------------------------------------------
78 Value( const bool& v )
79 : m_TypeName( typeid( TBool ).name( ) ),
84 m_Complex( TComplex( 0 ) ),
87 this->m_Source.reset( );
91 // -------------------------------------------------------------------------
93 Value( const {{n}}& v )
94 : m_TypeName( typeid( TNatural ).name( ) ),
96 m_Natural( TNatural( v ) ),
99 m_Complex( TComplex( 0 ) ),
102 this->m_Source.reset( );
107 // -------------------------------------------------------------------------
109 Value( const {{z}}& v )
110 : m_TypeName( typeid( TInteger ).name( ) ),
113 m_Integer( TInteger( v ) ),
115 m_Complex( TComplex( 0 ) ),
118 this->m_Source.reset( );
123 // -------------------------------------------------------------------------
125 Value( const {{r}}& v )
126 : m_TypeName( typeid( TReal ).name( ) ),
130 m_Real( TReal( v ) ),
131 m_Complex( TComplex( 0 ) ),
134 this->m_Source.reset( );
137 // -------------------------------------------------------------------------
139 Value( const std::complex< {{r}} >& v )
140 : m_TypeName( typeid( TComplex ).name( ) ),
145 m_Complex( TComplex( std::real( v ), std::imag( v ) ) ),
148 this->m_Source.reset( );
152 // -------------------------------------------------------------------------
154 Value( const std::string& v )
155 : m_TypeName( typeid( TString ).name( ) ),
160 m_Complex( TComplex( 0 ) ),
163 this->m_Source.reset( );
166 // -------------------------------------------------------------------------
168 operator bool( ) const
170 if( typeid( TBool ).name( ) == this->m_TypeName )
171 return( this->m_Bool );
172 else if( typeid( TNatural ).name( ) == this->m_TypeName )
173 return( this->m_Natural != 0 );
174 else if( typeid( TInteger ).name( ) == this->m_TypeName )
175 return( this->m_Integer != 0 );
176 else if( typeid( TReal ).name( ) == this->m_TypeName )
177 return( this->m_Real != 0 );
178 else if( typeid( TComplex ).name( ) == this->m_TypeName )
179 return( std::norm( this->m_Complex ) != 0 );
180 else if( typeid( TString ).name( ) == this->m_TypeName )
181 return( this->m_String.size( ) > 0 );
187 // -------------------------------------------------------------------------
189 operator {{n}}( ) const
191 if( typeid( TBool ).name( ) == this->m_TypeName )
192 return( ( {{n}} )( this->m_Bool ) );
193 else if( typeid( TNatural ).name( ) == this->m_TypeName )
194 return( ( {{n}} )( this->m_Natural ) );
195 else if( typeid( TInteger ).name( ) == this->m_TypeName )
196 return( ( {{n}} )( this->m_Integer ) );
197 else if( typeid( TReal ).name( ) == this->m_TypeName )
198 return( ( {{n}} )( this->m_Real ) );
199 else if( typeid( TComplex ).name( ) == this->m_TypeName )
200 return( ( {{n}} )( std::real( this->m_Complex ) ) );
201 else if( typeid( TString ).name( ) == this->m_TypeName )
204 std::istringstream s( this->m_String );
214 // -------------------------------------------------------------------------
216 operator {{z}}( ) const
218 if( typeid( TBool ).name( ) == this->m_TypeName )
219 return( ( {{z}} )( this->m_Bool ) );
220 else if( typeid( TNatural ).name( ) == this->m_TypeName )
221 return( ( {{z}} )( this->m_Natural ) );
222 else if( typeid( TInteger ).name( ) == this->m_TypeName )
223 return( ( {{z}} )( this->m_Integer ) );
224 else if( typeid( TReal ).name( ) == this->m_TypeName )
225 return( ( {{z}} )( this->m_Real ) );
226 else if( typeid( TComplex ).name( ) == this->m_TypeName )
227 return( ( {{z}} )( std::real( this->m_Complex ) ) );
228 else if( typeid( TString ).name( ) == this->m_TypeName )
231 std::istringstream s( this->m_String );
241 // -------------------------------------------------------------------------
243 operator {{r}}( ) const
245 if( typeid( TBool ).name( ) == this->m_TypeName )
246 return( ( {{r}} )( this->m_Bool ) );
247 else if( typeid( TNatural ).name( ) == this->m_TypeName )
248 return( ( {{r}} )( this->m_Natural ) );
249 else if( typeid( TInteger ).name( ) == this->m_TypeName )
250 return( ( {{r}} )( this->m_Integer ) );
251 else if( typeid( TReal ).name( ) == this->m_TypeName )
252 return( ( {{r}} )( this->m_Real ) );
253 else if( typeid( TComplex ).name( ) == this->m_TypeName )
254 return( ( {{r}} )( std::real( this->m_Complex ) ) );
255 else if( typeid( TString ).name( ) == this->m_TypeName )
258 std::istringstream s( this->m_String );
266 // -------------------------------------------------------------------------
268 operator std::complex< {{r}} >( ) const
270 std::complex< {{r}} > c;
271 if( typeid( TBool ).name( ) == this->m_TypeName )
272 c = std::complex< {{r}} >( ( {{r}} )( this->m_Bool ) );
273 else if( typeid( TNatural ).name( ) == this->m_TypeName )
274 c = std::complex< {{r}} >( ( {{r}} )( this->m_Natural ) );
275 else if( typeid( TInteger ).name( ) == this->m_TypeName )
276 c = std::complex< {{r}} >( ( {{r}} )( this->m_Integer ) );
277 else if( typeid( TReal ).name( ) == this->m_TypeName )
278 c = std::complex< {{r}} >( ( {{r}} )( this->m_Real ) );
279 else if( typeid( TComplex ).name( ) == this->m_TypeName )
280 c = std::complex< {{r}} >(
281 ( {{r}} )( std::real( this->m_Complex ) ),
282 ( {{r}} )( std::imag( this->m_Complex ) )
284 else if( typeid( TString ).name( ) == this->m_TypeName )
288 std::istringstream s( this->m_String );
290 c = std::complex< {{r}} >( r, i );
296 // -------------------------------------------------------------------------
298 operator std::string( ) const
300 if( this->m_TypeName != typeid( TString ).name( ) )
302 std::stringstream ss;
303 if( typeid( TBool ).name( ) == this->m_TypeName )
305 else if( typeid( TNatural ).name( ) == this->m_TypeName )
306 ss << this->m_Natural;
307 else if( typeid( TInteger ).name( ) == this->m_TypeName )
308 ss << this->m_Integer;
309 else if( typeid( TReal ).name( ) == this->m_TypeName )
311 else if( typeid( TComplex ).name( ) == this->m_TypeName )
313 << std::real( this->m_Complex ) << "+"
314 << std::imag( this->m_Complex ) << "i";
318 return( this->m_String );
321 // -------------------------------------------------------------------------
323 Self& cpPlugins::Value::
324 operator=( const Self& v )
326 if( this->m_TypeName == "" )
327 this->m_TypeName = v.m_TypeName;
330 if( v.m_TypeName == typeid( T{{w}} ).name( ) )
331 this->operator=( v.m_{{w}} );
334 this->m_Source = v.m_Source;
338 // -------------------------------------------------------------------------
340 Self& cpPlugins::Value::
341 operator=( const bool& v )
344 if( typeid( TBool ).name( ) == this->m_TypeName )
346 else if( typeid( TNatural ).name( ) == this->m_TypeName )
347 this->m_Natural = ( v )? 1: 0;
348 else if( typeid( TInteger ).name( ) == this->m_TypeName )
349 this->m_Integer = ( v )? 1: 0;
350 else if( typeid( TReal ).name( ) == this->m_TypeName )
351 this->m_Real = ( v )? 1: 0;
352 else if( typeid( TComplex ).name( ) == this->m_TypeName )
353 this->m_Complex = TComplex( ( v )? 1: 0, 0 );
354 else if( typeid( TString ).name( ) == this->m_TypeName )
356 std::stringstream ss;
358 this->m_String = ss.str( );
364 // -------------------------------------------------------------------------
366 Self& cpPlugins::Value::
367 operator=( const {{x}}& v )
370 if( typeid( TBool ).name( ) == this->m_TypeName )
371 this->m_Bool = ( v != 0 );
372 else if( typeid( TNatural ).name( ) == this->m_TypeName )
373 this->m_Natural = TNatural( v );
374 else if( typeid( TInteger ).name( ) == this->m_TypeName )
375 this->m_Integer = TInteger( v );
376 else if( typeid( TReal ).name( ) == this->m_TypeName )
377 this->m_Real = TReal( v );
378 else if( typeid( TComplex ).name( ) == this->m_TypeName )
379 this->m_Complex = TComplex( v );
380 else if( typeid( TString ).name( ) == this->m_TypeName )
382 std::stringstream ss;
384 this->m_String = ss.str( );
391 // -------------------------------------------------------------------------
393 Self& cpPlugins::Value::
394 operator=( const std::complex< {{r}} >& v )
397 if( typeid( TBool ).name( ) == this->m_TypeName )
398 this->m_Bool = ( std::real( v ) != 0 );
399 else if( typeid( TNatural ).name( ) == this->m_TypeName )
400 this->m_Natural = TNatural( std::real( v ) );
401 else if( typeid( TInteger ).name( ) == this->m_TypeName )
402 this->m_Integer = TInteger( std::real( v ) );
403 else if( typeid( TReal ).name( ) == this->m_TypeName )
404 this->m_Real = TReal( std::real( v ) );
405 else if( typeid( TComplex ).name( ) == this->m_TypeName )
406 this->m_Complex = TComplex( std::real( v ), std::imag( v ) );
407 else if( typeid( TString ).name( ) == this->m_TypeName )
409 std::stringstream ss;
410 ss << std::real( v ) << "+" << std::imag( v ) << "i";
411 this->m_String = ss.str( );
417 // -------------------------------------------------------------------------
419 Self& cpPlugins::Value::
420 operator=( const std::string& v )
422 std::istringstream s( v );
424 if( typeid( TBool ).name( ) == this->m_TypeName )
426 else if( typeid( TNatural ).name( ) == this->m_TypeName )
427 s >> this->m_Natural;
428 else if( typeid( TInteger ).name( ) == this->m_TypeName )
429 s >> this->m_Integer;
430 else if( typeid( TReal ).name( ) == this->m_TypeName )
432 else if( typeid( TComplex ).name( ) == this->m_TypeName )
434 // TODO: s >> this->m_Bool;
436 else if( typeid( TString ).name( ) == this->m_TypeName )
441 // -------------------------------------------------------------------------
442 bool cpPlugins::Value::
443 operator<( const Self& v ) const
445 if( this->m_TypeName == v.m_TypeName )
447 if( this->m_TypeName == typeid( TBool ).name( ) )
448 return( this->m_Bool < v.m_Bool );
449 else if( this->m_TypeName == typeid( TNatural ).name( ) )
450 return( this->m_Natural < v.m_Natural );
451 else if( this->m_TypeName == typeid( TInteger ).name( ) )
452 return( this->m_Integer < v.m_Integer );
453 else if( this->m_TypeName == typeid( TReal ).name( ) )
454 return( this->m_Real < v.m_Real );
455 else if( this->m_TypeName == typeid( TString ).name( ) )
456 return( this->m_String < v.m_String );
457 else if( this->m_TypeName == typeid( TComplex ).name( ) )
459 if( std::real( this->m_Complex ) == std::real( v.m_Complex ) )
461 std::imag( this->m_Complex ) < std::imag( v.m_Complex )
465 std::real( this->m_Complex ) < std::real( v.m_Complex )
473 // -------------------------------------------------------------------------
474 const std::string& cpPlugins::Value::
477 return( this->m_TypeName );
480 // -------------------------------------------------------------------------
481 cpPlugins::ProcessObject* cpPlugins::Value::
484 if( !( this->m_Source.expired( ) ) )
485 return( this->m_Source.lock( ).get( ) );
490 // -------------------------------------------------------------------------
491 const cpPlugins::ProcessObject* cpPlugins::Value::
494 if( !( this->m_Source.expired( ) ) )
495 return( this->m_Source.lock( ).get( ) );
500 // -------------------------------------------------------------------------
502 void cpPlugins::Value::
503 SetSource( cpPlugins::ProcessObject* source )
506 this->m_Source = source->CastWeakPtr< cpPlugins::ProcessObject >( );
508 this->m_Source.reset( );
511 // -------------------------------------------------------------------------
513 void cpPlugins::Value::
514 SetSource( cpPlugins::Parameters* source )
516 this->SetSource( dynamic_cast< cpPlugins::ProcessObject* >( source ) );
519 // -------------------------------------------------------------------------
520 void cpPlugins::Value::
521 Print( std::ostream& o ) const
523 o << std::string( *this );