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