Salome HOME
Copyright update 2020
[modules/yacs.git] / src / runtime / CppPorts.cxx
1 // Copyright (C) 2006-2020  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 "TypeConversions.hxx"
21 #include "CppPorts.hxx"
22 #include "Node.hxx"
23 #include "TypeCode.hxx"
24
25 #include <iostream>
26 #include <sstream>
27
28 //#define _DEVDEBUG_
29 #include "YacsTrace.hxx"
30
31 using namespace YACS::ENGINE;
32 using namespace std;
33
34 InputCppPort::InputCppPort(const std::string& name, Node *node, TypeCode * type)
35   : InputPort(name, node, type), DataPort(name, node, type), Port(node), _data(NULL),_initData(NULL)
36 {
37 }
38
39 InputCppPort::~InputCppPort()
40 {
41   if(_data)
42     {
43       DEBTRACE("_data ref count: " << _data->getRefCnt());
44       _data->decrRef();
45     }
46 }
47
48 InputCppPort::InputCppPort(const InputCppPort& other, Node *newHelder):InputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder)
49 {
50   _initData=other._initData;
51   _data=other._data;
52 }
53
54 bool InputCppPort::edIsManuallyInitialized() const
55 {
56   return _initData!= NULL;
57 }
58
59 void InputCppPort::edRemoveManInit()
60 {
61   _initData=NULL;
62   InputPort::edRemoveManInit();
63 }
64
65 void InputCppPort::put(const void *data) throw(ConversionException)
66 {
67   put((YACS::ENGINE::Any *)data);
68 }
69
70 void InputCppPort::releaseData()
71 {
72   if(_data)
73     _data->decrRef();
74   _data=nullptr;
75 }
76
77 void InputCppPort::put(YACS::ENGINE::Any *data) throw(ConversionException)
78 {
79   releaseData();
80   _data=data;
81   _data->incrRef();
82   DEBTRACE("value ref count: " << _data->getRefCnt());
83 }
84
85 InputPort *InputCppPort::clone(Node *newHelder) const
86 {
87   return new InputCppPort(*this,newHelder);
88 }
89
90 YACS::ENGINE::Any * InputCppPort::getCppObj() const
91 {
92   return _data;
93 }
94
95 void *InputCppPort::get() const throw(YACS::Exception)
96 {
97   return (void*) _data;
98 }
99
100 bool InputCppPort::isEmpty()
101 {
102   return _data == NULL;
103 }
104
105 //! Save the current data value for further reinitialization of the port
106 /*!
107  *
108  */
109 void InputCppPort::exSaveInit()
110 {
111   _initData=_data;
112   //DEBTRACE("_initData.ob refcnt: " << _initData->ob_refcnt);
113   //DEBTRACE("_data.ob refcnt: " << _data->ob_refcnt);
114 }
115
116 //! Restore the saved data value to current data value
117 /*!
118  * If no data has been saved (_initData == 0) don't restore
119  */
120 void InputCppPort::exRestoreInit()
121 {
122   if(!_initData)return;
123   _data=_initData;
124   //DEBTRACE("_initData.ob refcnt: " << _initData->ob_refcnt);
125   //DEBTRACE("_data.ob refcnt: " << _data->ob_refcnt);
126 }
127
128 std::string InputCppPort::dump()
129 {
130   if( _data == NULL)
131     return "<value>None</value>";
132
133   if (edGetType()->kind() != YACS::ENGINE::Objref)
134     return convertNeutralXml(edGetType(), _data);
135     //return convertCppXml(edGetType(), _data);
136   if (! _stringRef.empty())
137     return _stringRef;
138   else 
139     return convertNeutralXml(edGetType(), _data);
140 //     {
141 //       stringstream msg;
142 //       msg << "Cannot retreive init reference string for port " << _name
143 //           << " on node " << _node->getName();
144 //       throw Exception(msg.str());      
145 //     }
146 }
147
148
149 OutputCppPort::OutputCppPort(const std::string& name, Node *node, TypeCode * type)
150   : OutputPort(name, node, type), DataPort(name, node, type), Port(node)
151 {
152   _data = NULL;
153 }
154
155 OutputCppPort::~OutputCppPort()
156 {
157   if(_data)
158     {
159       DEBTRACE("_data ref count: " << _data->getRefCnt());
160       _data->decrRef();
161     }
162 }
163
164 OutputCppPort::OutputCppPort(const OutputCppPort& other, Node *newHelder):OutputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder),
165                                                                        _data(NULL)
166 {
167 }
168
169 void OutputCppPort::put(const void *data) throw(ConversionException)
170 {
171   put((YACS::ENGINE::Any *)data);
172 }
173
174 void OutputCppPort::put(YACS::ENGINE::Any *data) throw(ConversionException)
175 {
176   InputPort *p;
177   if(_data)
178     _data->decrRef();
179   _data = data;
180   if(_data)
181     _data->incrRef();
182   OutputPort::put(data);
183 }
184
185 OutputPort *OutputCppPort::clone(Node *newHelder) const
186 {
187   return new OutputCppPort(*this,newHelder);
188 }
189
190 YACS::ENGINE::Any * OutputCppPort::get() const
191 {
192   return _data;
193 }
194
195 std::string OutputCppPort::dump()
196 {
197   if( _data == NULL)
198     return "<value>None</value>";
199   string xmldump = convertNeutralXml(edGetType(), _data);
200   return xmldump;
201 }
202