Salome HOME
2c914bbe1cf2d8648e798c7f0942f33cbfd913e1
[modules/yacs.git] / src / engine / AnyInputPort.cxx
1 // Copyright (C) 2006-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "AnyInputPort.hxx"
21 #include "TypeCode.hxx"
22 #include "Mutex.hxx"
23 #include "AutoLocker.hxx"
24
25 //#define _DEVDEBUG_
26 #include "YacsTrace.hxx"
27
28 #include <iostream>
29 #include <iomanip>
30 #include <sstream>
31
32 using namespace YACS::ENGINE;
33 using namespace std;
34
35 AnyInputPort::AnyInputPort(const std::string& name, Node *node, TypeCode* type, bool canBeNull)
36   : InputPort(name, node, type, canBeNull),
37     DataPort(name, node, type),
38     Port(node),
39     _value(0)
40 {
41 }
42
43 AnyInputPort::AnyInputPort(const AnyInputPort& other, Node *newHelder)
44   : InputPort(other, newHelder),
45     DataPort(other, newHelder),
46     Port(other, newHelder),
47     _value(0)
48 {
49   if(other._value)
50     _value=other._value->clone();
51 }
52
53 AnyInputPort::~AnyInputPort()
54 {
55   if(_value)
56     {
57       DEBTRACE("_value ref count: " << _value->getRefCnt());
58       _value->decrRef();
59     }
60 }
61
62 //! Save the current data value for further reinitialization of the port
63 /*!
64  *
65  */
66 void AnyInputPort::exSaveInit()
67 {
68   if(_initValue) _initValue->decrRef();
69   _initValue=_value;
70   _initValue->incrRef();
71 }
72
73 //! Restore the saved data value to current data value
74 /*!
75  * If no data has been saved (_initValue == 0) don't restore
76  */
77 void AnyInputPort::exRestoreInit()
78 {
79   if(!_initValue)
80     return;
81   if(_value)
82     _value->decrRef();
83   _value=_initValue;
84   _value->incrRef();
85 }
86
87 void AnyInputPort::put(Any *data)
88 {
89   YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
90   if(_value)
91     _value->decrRef();
92   _value=data;
93   if (_value) {
94     _value->incrRef();
95     DEBTRACE("value ref count: " << _value->getRefCnt());
96   }
97 }
98
99 bool AnyInputPort::isEmpty()
100 {
101   return !_value;
102 }
103
104 void *AnyInputPort::get() const
105 {
106   return (void *)_value;
107 }
108
109 std::string AnyInputPort::getAsString()
110 {
111   YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
112   return getRuntime()->convertNeutralAsString(edGetType(),_value);
113 }
114
115 void AnyInputPort::put(const void *data) throw(ConversionException)
116 {
117   put((Any *)data);
118 }
119
120 InputPort *AnyInputPort::clone(Node *newHelder) const
121 {
122   return new AnyInputPort(*this,newHelder);
123 }
124
125 std::string AnyInputPort::dump()
126 {
127   if(!_value)
128     {
129       std::string what="AnyInputPort::get : no value currently in input port with name \""; what+=_name; what+="\"";
130       throw Exception(what);
131     }
132   stringstream xmldump;
133   switch (_value->getType()->kind())
134     {
135     case Double:
136       xmldump << "<value><double>" << setprecision(16) << _value->getDoubleValue() << "</double></value>" << endl;
137       break;
138     case Int:
139       xmldump << "<value><int>" << _value->getIntValue() << "</int></value>" << endl;
140       break;
141     case Bool:
142       xmldump << "<value><boolean>" << _value->getBoolValue() << "</boolean></value>" << endl;
143       break;
144     case String:
145       xmldump << "<value><string>" << _value->getStringValue() << "</string></value>" << endl;
146       break;
147     default:
148       xmldump << "<value><error> NO_SERIALISATION_AVAILABLE </error></value>" << endl;
149       break;
150     }
151   return xmldump.str();
152 }