Salome HOME
merge from branch DEV tag mergeto_trunk_04apr08
[modules/yacs.git] / src / engine / AnyInputPort.cxx
1 #include "AnyInputPort.hxx"
2 #include "TypeCode.hxx"
3 #include <iostream>
4 #include <sstream>
5
6 //#define _DEVDEBUG_
7 #include "YacsTrace.hxx"
8
9 using namespace YACS::ENGINE;
10 using namespace std;
11
12 AnyInputPort::AnyInputPort(const std::string& name, Node *node, TypeCode* type):InputPort(name,node,type),DataPort(name,node,type),Port(node),_value(0)
13 {
14 }
15
16 AnyInputPort::AnyInputPort(const  AnyInputPort& other, Node *newHelder):InputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder),_value(0)
17 {
18   if(other._value)
19     _value=other._value->clone();
20 }
21
22 AnyInputPort::~AnyInputPort()
23 {
24   if(_value)
25     {
26       DEBTRACE("_value ref count: " << _value->getRefCnt());
27       _value->decrRef();
28     }
29 }
30
31 //! Save the current data value for further reinitialization of the port
32 /*!
33  *
34  */
35 void AnyInputPort::exSaveInit()
36 {
37   if(_initValue) _initValue->decrRef();
38   _initValue=_value;
39   _initValue->incrRef();
40 }
41
42 //! Restore the saved data value to current data value
43 /*!
44  * If no data has been saved (_initValue == 0) don't restore
45  */
46 void AnyInputPort::exRestoreInit()
47 {
48   if(!_initValue)
49     return;
50   if(_value)
51     _value->decrRef();
52   _value=_initValue;
53   _value->incrRef();
54 }
55
56 void AnyInputPort::put(Any *data)
57 {
58    if(_value)
59     _value->decrRef();
60   _value=data;
61   _value->incrRef();
62   DEBTRACE("value ref count: " << _value->getRefCnt());
63 }
64
65 bool AnyInputPort::isEmpty()
66 {
67   return !_value;
68 }
69
70 void *AnyInputPort::get() const
71 {
72   return (void *)_value;
73 }
74
75 void AnyInputPort::put(const void *data) throw(ConversionException)
76 {
77   put((Any *)data);
78 }
79
80 InputPort *AnyInputPort::clone(Node *newHelder) const
81 {
82   return new AnyInputPort(*this,newHelder);
83 }
84
85 std::string AnyInputPort::dump()
86 {
87   if(!_value)
88     {
89       std::string what="AnyInputPort::get : no value currently in input whith name \""; what+=_name; what+="\"";
90       throw Exception(what);
91     }
92   stringstream xmldump;
93   switch (_value->getType()->kind())
94     {
95     case Double:
96       xmldump << "<value><double>" << _value->getDoubleValue() << "</double></value>" << endl;
97       break;
98     case Int:
99       xmldump << "<value><int>" << _value->getIntValue() << "</int></value>" << endl;
100       break;
101     case Bool:
102       xmldump << "<value><boolean>" << _value->getBoolValue() << "</boolean></value>" << endl;
103       break;
104     case String:
105       xmldump << "<value><string>" << _value->getStringValue() << "</string></value>" << endl;
106       break;
107     default:
108       xmldump << "<value><error> NO_SERIALISATION_AVAILABLE </error></value>" << endl;
109       break;
110     }
111   return xmldump.str();
112 }