]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Value.cxx.d
Moved to version 1.0
[cpPlugins.git] / lib / cpPlugins / Value.cxx.d
1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
4
5 $include "Value.e"
6
7 #include <cpPlugins/Value.h>
8 #include <cpPlugins/ProcessObject.h>
9
10 // -------------------------------------------------------------------------
11 cpPlugins::Value::
12 Value( )
13   : m_TypeName( "" ),
14     m_Bool( false ),
15     m_Natural( 0 ),
16     m_Integer( 0 ),
17     m_Real( 0 ),
18     m_Complex( TComplex( 0 ) ),
19     m_String( "" )
20 {
21   this->m_Source.reset( );
22 }
23
24 // -------------------------------------------------------------------------
25 cpPlugins::Value::
26 Value( const std::type_info& i )
27   : m_Bool( false ),
28     m_Natural( 0 ),
29     m_Integer( 0 ),
30     m_Real( 0 ),
31     m_Complex( TComplex( 0 ) ),
32     m_String( "" )
33 {
34   if( i == typeid( bool ) )
35     this->m_TypeName = typeid( TBool ).name( );
36   else
37   {{#n}}
38   if( i == typeid( {{n}} ) )
39     this->m_TypeName = typeid( TNatural ).name( );
40   else
41   {{/n}}
42   {{#z}}
43   if( i == typeid( {{z}} ) )
44     this->m_TypeName = typeid( TInteger ).name( );
45   else
46   {{/z}}
47   {{#r}}
48   if( i == typeid( {{r}} ) )
49     this->m_TypeName = typeid( TReal ).name( );
50   else
51   {{/r}}
52   {{#r}}
53   if( i == typeid( std::complex< {{r}} > ) )
54     this->m_TypeName = typeid( TComplex ).name( );
55   else
56   {{/r}}
57   if( i == typeid( std::string ) )
58     this->m_TypeName = typeid( TString ).name( );
59   this->m_Source.reset( );
60 }
61
62 // -------------------------------------------------------------------------
63 cpPlugins::Value::
64 Value( const Self& v )
65   : m_TypeName( v.m_TypeName ),
66     m_Bool( v.m_Bool ),
67     m_Natural( v.m_Natural ),
68     m_Integer( v.m_Integer ),
69     m_Real( v.m_Real ),
70     m_Complex( v.m_Complex ),
71     m_String( v.m_String ),
72     m_Source( v.m_Source )
73 {
74 }
75
76 // -------------------------------------------------------------------------
77 cpPlugins::Value::
78 Value( const bool& v )
79   : m_TypeName( typeid( TBool ).name( ) ),
80     m_Bool( v ),
81     m_Natural( 0 ),
82     m_Integer( 0 ),
83     m_Real( 0 ),
84     m_Complex( TComplex( 0 ) ),
85     m_String( "" )
86 {
87   this->m_Source.reset( );
88 }
89
90 {{#n}}
91 // -------------------------------------------------------------------------
92 cpPlugins::Value::
93 Value( const {{n}}& v )
94   : m_TypeName( typeid( TNatural ).name( ) ),
95     m_Bool( false ),
96     m_Natural( TNatural( v ) ),
97     m_Integer( 0 ),
98     m_Real( 0 ),
99     m_Complex( TComplex( 0 ) ),
100     m_String( "" )
101 {
102   this->m_Source.reset( );
103 }
104 {{/n}}
105
106 {{#z}}
107 // -------------------------------------------------------------------------
108 cpPlugins::Value::
109 Value( const {{z}}& v )
110   : m_TypeName( typeid( TInteger ).name( ) ),
111     m_Bool( false ),
112     m_Natural( 0 ),
113     m_Integer( TInteger( v ) ),
114     m_Real( 0 ),
115     m_Complex( TComplex( 0 ) ),
116     m_String( "" )
117 {
118   this->m_Source.reset( );
119 }
120 {{/z}}
121
122 {{#r}}
123 // -------------------------------------------------------------------------
124 cpPlugins::Value::
125 Value( const {{r}}& v )
126   : m_TypeName( typeid( TReal ).name( ) ),
127     m_Bool( false ),
128     m_Natural( 0 ),
129     m_Integer( 0 ),
130     m_Real( TReal( v ) ),
131     m_Complex( TComplex( 0 ) ),
132     m_String( "" )
133 {
134   this->m_Source.reset( );
135 }
136
137 // -------------------------------------------------------------------------
138 cpPlugins::Value::
139 Value( const std::complex< {{r}} >& v )
140   : m_TypeName( typeid( TComplex ).name( ) ),
141     m_Bool( false ),
142     m_Natural( 0 ),
143     m_Integer( 0 ),
144     m_Real( 0 ),
145     m_Complex( TComplex( std::real( v ), std::imag( v ) ) ),
146     m_String( "" )
147 {
148   this->m_Source.reset( );
149 }
150 {{/r}}
151
152 // -------------------------------------------------------------------------
153 cpPlugins::Value::
154 Value( const std::string& v )
155   : m_TypeName( typeid( TString ).name( ) ),
156     m_Bool( false ),
157     m_Natural( 0 ),
158     m_Integer( 0 ),
159     m_Real( 0 ),
160     m_Complex( TComplex( 0 ) ),
161     m_String( v )
162 {
163   this->m_Source.reset( );
164 }
165
166 // -------------------------------------------------------------------------
167 cpPlugins::Value::
168 operator bool( ) const
169 {
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 );
182   else
183     return( false );
184 }
185
186 {{#n}}
187 // -------------------------------------------------------------------------
188 cpPlugins::Value::
189 operator {{n}}( ) const
190 {
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 )
202   {
203     {{n}} v;
204     std::istringstream s( this->m_String );
205     s >> v;
206     return( v );
207   }
208   else
209     return( 0 );
210 }
211 {{/n}}
212
213 {{#z}}
214 // -------------------------------------------------------------------------
215 cpPlugins::Value::
216 operator {{z}}( ) const
217 {
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 )
229   {
230     {{z}} v;
231     std::istringstream s( this->m_String );
232     s >> v;
233     return( v );
234   }
235   else
236     return( 0 );
237 }
238 {{/z}}
239
240 {{#r}}
241 // -------------------------------------------------------------------------
242 cpPlugins::Value::
243 operator {{r}}( ) const
244 {
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 )
256   {
257     {{r}} v;
258     std::istringstream s( this->m_String );
259     s >> v;
260     return( v );
261   }
262   else
263     return( 0 );
264 }
265
266 // -------------------------------------------------------------------------
267 cpPlugins::Value::
268 operator std::complex< {{r}} >( ) const
269 {
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 ) )
283       );
284   else if( typeid( TString ).name( ) == this->m_TypeName )
285   {
286     // TODO: improve
287     {{r}} r, i;
288     std::istringstream s( this->m_String );
289     s >> r >> i;
290     c = std::complex< {{r}} >( r, i );
291   }
292   return( c );
293 }
294 {{/r}}
295
296 // -------------------------------------------------------------------------
297 cpPlugins::Value::
298 operator std::string( ) const
299 {
300   if( this->m_TypeName != typeid( TString ).name( ) )
301   {
302     std::stringstream ss;
303     if( typeid( TBool ).name( ) == this->m_TypeName )
304       ss << this->m_Bool;
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 )
310       ss << this->m_Real;
311     else if( typeid( TComplex ).name( ) == this->m_TypeName )
312       ss
313         << std::real( this->m_Complex ) << "+"
314         << std::imag( this->m_Complex ) << "i";
315     return( ss.str( ) );
316   }
317   else
318     return( this->m_String );
319 }
320
321 // -------------------------------------------------------------------------
322 cpPlugins::Value::
323 Self& cpPlugins::Value::
324 operator=( const Self& v )
325 {
326   if( this->m_TypeName == "" )
327     this->m_TypeName = v.m_TypeName;
328   this->m_String = "";
329   {{#w}}
330   if( v.m_TypeName == typeid( T{{w}} ).name( ) )
331     this->operator=( v.m_{{w}} );
332   else
333   {{/w}};
334   this->m_Source = v.m_Source;
335   return( *this );
336 }
337
338 // -------------------------------------------------------------------------
339 cpPlugins::Value::
340 Self& cpPlugins::Value::
341 operator=( const bool& v )
342 {
343   this->m_String = "";
344   if( typeid( TBool ).name( ) == this->m_TypeName )
345     this->m_Bool = v;
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 )
355   {
356     std::stringstream ss;
357     ss << v;
358     this->m_String = ss.str( );
359   } // end if
360   return( *this );
361 }
362
363 {{#x}}
364 // -------------------------------------------------------------------------
365 cpPlugins::Value::
366 Self& cpPlugins::Value::
367 operator=( const {{x}}& v )
368 {
369   this->m_String = "";
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 )
381   {
382     std::stringstream ss;
383     ss << v;
384     this->m_String = ss.str( );
385   } // end if
386   return( *this );
387 }
388 {{/x}}
389
390 {{#r}}
391 // -------------------------------------------------------------------------
392 cpPlugins::Value::
393 Self& cpPlugins::Value::
394 operator=( const std::complex< {{r}} >& v )
395 {
396   this->m_String = "";
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 )
408   {
409     std::stringstream ss;
410     ss << std::real( v ) << "+" << std::imag( v ) << "i";
411     this->m_String = ss.str( );
412   } // end if
413   return( *this );
414 }
415 {{/r}}
416
417 // -------------------------------------------------------------------------
418 cpPlugins::Value::
419 Self& cpPlugins::Value::
420 operator=( const std::string& v )
421 {
422   std::istringstream s( v );
423   this->m_String = "";
424   if( typeid( TBool ).name( ) == this->m_TypeName )
425     s >> this->m_Bool;
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 )
431     s >> this->m_Real;
432   else if( typeid( TComplex ).name( ) == this->m_TypeName )
433   {
434     // TODO: s >> this->m_Bool;
435   }
436   else if( typeid( TString ).name( ) == this->m_TypeName )
437     this->m_String = v;
438   return( *this );
439 }
440
441 // -------------------------------------------------------------------------
442 bool cpPlugins::Value::
443 operator<( const Self& v ) const
444 {
445   if( this->m_TypeName == v.m_TypeName )
446   {
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( ) )
458     {
459       if( std::real( this->m_Complex ) == std::real( v.m_Complex ) )
460         return(
461           std::imag( this->m_Complex ) < std::imag( v.m_Complex )
462           );
463       else
464         return(
465           std::real( this->m_Complex ) < std::real( v.m_Complex )
466           );
467     } // end if
468   }
469   else
470     return( false );
471 }
472
473 // -------------------------------------------------------------------------
474 const std::string& cpPlugins::Value::
475 GetTypeName( ) const
476 {
477   return( this->m_TypeName );
478 }
479
480 // -------------------------------------------------------------------------
481 cpPlugins::ProcessObject* cpPlugins::Value::
482 GetSource( )
483 {
484   if( !( this->m_Source.expired( ) ) )
485     return( this->m_Source.lock( ).get( ) );
486   else
487     return( NULL );
488 }
489
490 // -------------------------------------------------------------------------
491 const cpPlugins::ProcessObject* cpPlugins::Value::
492 GetSource( ) const
493 {
494   if( !( this->m_Source.expired( ) ) )
495     return( this->m_Source.lock( ).get( ) );
496   else
497     return( NULL );
498 }
499
500 // -------------------------------------------------------------------------
501 template<>
502 void cpPlugins::Value::
503 SetSource( cpPlugins::ProcessObject* source )
504 {
505   if( source != NULL )
506     this->m_Source = source->CastWeakPtr< cpPlugins::ProcessObject >( );
507   else
508     this->m_Source.reset( );
509 }
510
511 // -------------------------------------------------------------------------
512 template<>
513 void cpPlugins::Value::
514 SetSource( cpPlugins::Parameters* source )
515 {
516   this->SetSource( dynamic_cast< cpPlugins::ProcessObject* >( source ) );
517 }
518
519 // -------------------------------------------------------------------------
520 void cpPlugins::Value::
521 Print( std::ostream& o ) const
522 {
523   o << std::string( *this );
524 }
525
526 // eof - $RCSfile$