]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/AnyInputPort.cxx
Salome HOME
Merge branch 'agy/ParallelContainerLaunch'
[modules/yacs.git] / src / engine / AnyInputPort.cxx
1 // Copyright (C) 2006-2014  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 <sstream>
30
31 using namespace YACS::ENGINE;
32 using namespace std;
33
34 AnyInputPort::AnyInputPort(const std::string& name, Node *node, TypeCode* type, bool canBeNull)
35   : InputPort(name, node, type, canBeNull),
36     DataPort(name, node, type),
37     Port(node),
38     _value(0)
39 {
40 }
41
42 AnyInputPort::AnyInputPort(const AnyInputPort& other, Node *newHelder)
43   : InputPort(other, newHelder),
44     DataPort(other, newHelder),
45     Port(other, newHelder),
46     _value(0)
47 {
48   if(other._value)
49     _value=other._value->clone();
50 }
51
52 AnyInputPort::~AnyInputPort()
53 {
54   if(_value)
55     {
56       DEBTRACE("_value ref count: " << _value->getRefCnt());
57       _value->decrRef();
58     }
59 }
60
61 //! Save the current data value for further reinitialization of the port
62 /*!
63  *
64  */
65 void AnyInputPort::exSaveInit()
66 {
67   if(_initValue) _initValue->decrRef();
68   _initValue=_value;
69   _initValue->incrRef();
70 }
71
72 //! Restore the saved data value to current data value
73 /*!
74  * If no data has been saved (_initValue == 0) don't restore
75  */
76 void AnyInputPort::exRestoreInit()
77 {
78   if(!_initValue)
79     return;
80   if(_value)
81     _value->decrRef();
82   _value=_initValue;
83   _value->incrRef();
84 }
85
86 void AnyInputPort::put(Any *data)
87 {
88   YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
89   if(_value)
90     _value->decrRef();
91   _value=data;
92   if (_value) {
93     _value->incrRef();
94     DEBTRACE("value ref count: " << _value->getRefCnt());
95   }
96 }
97
98 bool AnyInputPort::isEmpty()
99 {
100   return !_value;
101 }
102
103 void *AnyInputPort::get() const
104 {
105   return (void *)_value;
106 }
107
108 std::string AnyInputPort::getAsString()
109 {
110   YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
111   return getRuntime()->convertNeutralAsString(edGetType(),_value);
112 }
113
114 void AnyInputPort::put(const void *data) throw(ConversionException)
115 {
116   put((Any *)data);
117 }
118
119 InputPort *AnyInputPort::clone(Node *newHelder) const
120 {
121   return new AnyInputPort(*this,newHelder);
122 }
123
124 std::string AnyInputPort::dump()
125 {
126   if(!_value)
127     {
128       std::string what="AnyInputPort::get : no value currently in input port with name \""; what+=_name; what+="\"";
129       throw Exception(what);
130     }
131   stringstream xmldump;
132   switch (_value->getType()->kind())
133     {
134     case Double:
135       xmldump << "<value><double>" << _value->getDoubleValue() << "</double></value>" << endl;
136       break;
137     case Int:
138       xmldump << "<value><int>" << _value->getIntValue() << "</int></value>" << endl;
139       break;
140     case Bool:
141       xmldump << "<value><boolean>" << _value->getBoolValue() << "</boolean></value>" << endl;
142       break;
143     case String:
144       xmldump << "<value><string>" << _value->getStringValue() << "</string></value>" << endl;
145       break;
146     default:
147       xmldump << "<value><error> NO_SERIALISATION_AVAILABLE </error></value>" << endl;
148       break;
149     }
150   return xmldump.str();
151 }