Salome HOME
Merge from V6_main 01/04/2013
[modules/yacs.git] / src / runtime / PresetPorts.cxx
1 // Copyright (C) 2006-2013  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.
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 "PresetPorts.hxx"
22 #include "PythonPorts.hxx"
23 #include "TypeCode.hxx"
24 #include <iostream>
25
26 //#define _DEVDEBUG_
27 #include "YacsTrace.hxx"
28
29 using namespace YACS::ENGINE;
30 using namespace std;
31
32 /*! \class YACS::ENGINE::OutputPresetPort
33  *  \brief Class for PRESET output Ports
34  *
35  * \ingroup Ports
36  *
37  * \see PresetNode
38  */
39
40 OutputPresetPort::OutputPresetPort(const std::string& name,  Node* node, TypeCode* type)
41   : OutputXmlPort(name, node, type),
42     DataPort(name, node, type),
43     Port(node)
44 {
45 }
46
47 OutputPresetPort::OutputPresetPort(const OutputPresetPort& other, Node *newHelder)
48   : OutputXmlPort(other,newHelder),
49     DataPort(other,newHelder),
50     Port(other,newHelder),_storeData(other._storeData)
51 {
52 }
53
54 OutputPort* OutputPresetPort::clone(Node *newHelder) const
55 {
56   return new OutputPresetPort(*this,newHelder);
57 }
58
59 void OutputPresetPort::setData(std::string data)
60 {
61   _storeData = data;
62   DEBTRACE( "OutputPresetPort::setData " << _storeData );
63   modified();
64 }
65
66 void OutputPresetPort::checkBasicConsistency() const throw(YACS::Exception)
67 {
68   DEBTRACE("OutputPresetPort::checkBasicConsistency " << _storeData);
69   if (_storeData.empty())
70     {
71       std::string what("OutputPresetPort: ");
72       what += getName();
73       what += " is not initialised";
74       throw Exception(what);
75     }
76 }
77
78 std::string OutputPresetPort::getData()
79 {
80   DEBTRACE("OutputPresetPort::getData " << _storeData);
81   if(_storeData.substr(0,7) == "<value>")
82     {
83       return _storeData;
84     }
85   else
86     {
87       std::string value;
88       switch(edGetType()->kind())
89         {
90         case Double:
91           value="<value><double>"+_storeData+"</double></value>";
92           break;
93         case Int:
94           value="<value><int>"+_storeData+"</int></value>";
95           break;
96         case String:
97           value="<value><string>"+_storeData+"</string></value>";
98           break;
99         case Bool:
100           value="<value><boolean>"+_storeData+"</boolean></value>";
101           break;
102         case Objref:
103           value="<value><objref>"+_storeData+"</objref></value>";
104           break;
105         case Sequence:
106         case Array:
107           value="<value><array><data>"+_storeData+"</data></array></value>";
108           break;
109         case Struct:
110           value="<value><struct><data>"+_storeData+"</data></struct></value>";
111           break;
112         default:
113           break;
114         }
115       return value;
116     }
117 }
118
119 std::string OutputPresetPort::dump()
120 {
121   return getData();
122 }
123
124 PyObject * OutputPresetPort::getPyObj()
125 {
126   DEBTRACE(getData());
127   if(_storeData=="")
128     {
129       Py_INCREF(Py_None);
130       return Py_None;
131     }
132   else
133     return convertXmlStrPyObject(edGetType(),getData());
134 }
135
136 std::string OutputPresetPort::getAsString()
137 {
138   InterpreterUnlocker loc;
139   PyObject* ob=getPyObj();
140   DEBTRACE(PyObject_Str(ob));
141   std::string s=convertPyObjectToString(ob);
142   DEBTRACE(s);
143   Py_DECREF(ob);
144   return s;
145 }
146
147 /*! \class YACS::ENGINE::InputPresetPort
148  *  \brief Class for PRESET input Ports
149  *
150  * \ingroup Ports
151  *
152  * \see OutNode
153  */
154
155 InputPresetPort::InputPresetPort(const std::string& name,  Node* node, TypeCode* type)
156   : InputXmlPort(name, node, type),
157     DataPort(name, node, type),
158     Port(node)
159 {
160 }
161
162 InputPresetPort::InputPresetPort(const InputPresetPort& other, Node *newHelder)
163   : InputXmlPort(other,newHelder),
164     DataPort(other,newHelder),
165     Port(other,newHelder),_storeData(other._storeData)
166 {
167 }
168
169 InputPort* InputPresetPort::clone(Node *newHelder) const
170 {
171   return new InputPresetPort(*this,newHelder);
172 }
173
174 void InputPresetPort::setData(std::string data)
175 {
176   _storeData = data;
177   DEBTRACE( "InputPresetPort::setData " << _storeData );
178   modified();
179 }
180
181 std::string InputPresetPort::getData()
182 {
183   DEBTRACE("InputPresetPort::getData " << _storeData);
184   return _storeData;
185 }
186
187 std::string InputPresetPort::dump()
188 {
189   return _data;
190 }
191
192 PyObject * InputPresetPort::getPyObj()
193 {
194   DEBTRACE(dump());
195   if(_data=="")
196     {
197       Py_INCREF(Py_None);
198       return Py_None;
199     }
200   else
201     return convertXmlStrPyObject(edGetType(),dump());
202 }
203
204 std::string InputPresetPort::getAsString()
205 {
206   InterpreterUnlocker loc;
207   PyObject* ob=getPyObj();
208   DEBTRACE(PyObject_Str(ob));
209   std::string s=convertPyObjectToString(ob);
210   DEBTRACE(s);
211   Py_DECREF(ob);
212   return s;
213 }
214