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