1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #define private public
23 #define protected public
24 #include <omniORB4/CORBA.h>
25 #include <omniORB4/internal/typecode.h>
28 #include "RuntimeSALOME.hxx"
29 #include "TypeConversions.hxx"
30 #include "TypeCode.hxx"
31 #include "CORBAPorts.hxx"
32 #include "PythonPorts.hxx"
33 #include "ServiceNode.hxx"
34 #include "ComponentInstance.hxx"
35 #include "SALOME_GenericObj.hh"
41 #include "YacsTrace.hxx"
43 using namespace YACS::ENGINE;
46 void releaseObj(CORBA::Any& data)
48 CORBA::Object_var obj;
49 if(data >>= CORBA::Any::to_object(obj))
51 SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obj);
52 if(!CORBA::is_nil(gobj))
54 DEBTRACE("It's a SALOME::GenericObj");
58 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
61 DEBTRACE("It's not a CORBA::Object");
64 void registerObj(CORBA::Any& data)
66 CORBA::Object_var obj;
67 if(data >>= CORBA::Any::to_object(obj))
69 SALOME::GenericObj_var gobj=SALOME::GenericObj::_narrow(obj);
70 if(!CORBA::is_nil(gobj))
72 DEBTRACE("It's a SALOME::GenericObj");
76 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
79 DEBTRACE("It's not a CORBA::Object");
82 InputCorbaPort::InputCorbaPort(const std::string& name,
85 : InputPort(name, node, type), DataPort(name, node, type), Port(node), _initData(0)
87 _orb = getSALOMERuntime()->getOrb();
90 InputCorbaPort::InputCorbaPort(const InputCorbaPort& other, Node *newHelder):InputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder),
93 _orb = getSALOMERuntime()->getOrb();
96 _initData=new CORBA::Any;
97 *_initData=*(other._initData);
102 InputCorbaPort::~InputCorbaPort()
105 // Release or not release : all GenericObj are deleted when the input port is deleted
109 bool InputCorbaPort::edIsManuallyInitialized() const
114 void InputCorbaPort::edRemoveManInit()
118 InputPort::edRemoveManInit();
121 void InputCorbaPort::put(const void *data) throw (ConversionException)
123 put((CORBA::Any *)data);
126 void display(CORBA::Any* data)
128 CORBA::TypeCode_var tc=data->type();
131 case CORBA::tk_double:
134 DEBTRACE( "Double: " << d );
139 DEBTRACE( "Int: " << l );
146 void InputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
149 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
157 // make a copy of the any (protect against deletion of any source)
164 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
168 InputPort *InputCorbaPort::clone(Node *newHelder) const
170 return new InputCorbaPort(*this,newHelder);
173 void *InputCorbaPort::get() const throw(Exception)
175 return (void *)&_data;
178 bool InputCorbaPort::isEmpty()
180 CORBA::TypeCode_var tc=_data.type();
181 return tc->equivalent(CORBA::_tc_null);
184 CORBA::Any * InputCorbaPort::getAny()
186 // --- return a pointer to internal any
190 PyObject * InputCorbaPort::getPyObj()
192 CORBA::TypeCode_var tc=getAny()->type();
193 if (!tc->equivalent(CORBA::_tc_null))
194 return convertCorbaPyObject(edGetType(),getAny());
202 std::string InputCorbaPort::getAsString()
204 InterpreterUnlocker loc;
205 PyObject* ob=getPyObj();
206 std::string s=convertPyObjectToString(ob);
212 //! Save the current data value for further reinitialization of the port
216 void InputCorbaPort::exSaveInit()
220 _initData=new CORBA::Any;
224 //! Restore the saved data value to current data value
226 * If no data has been saved (_initData == 0) don't restore
228 void InputCorbaPort::exRestoreInit()
230 if(!_initData)return;
234 std::string InputCorbaPort::dump()
236 CORBA::TypeCode_var tc=_data.type();
237 if (tc->equivalent(CORBA::_tc_null))
238 return "<value>nil</value>";
239 if (edGetType()->kind() != YACS::ENGINE::Objref)
240 return convertCorbaXml(edGetType(), &_data);
241 if (! _stringRef.empty())
244 return convertCorbaXml(edGetType(), &_data);
247 // msg << "Cannot retreive init string reference string for port " << _name
248 // << " on node " << _node->getName();
249 // throw Exception(msg.str());
253 std::string InputCorbaPort::valToStr()
255 int isString = PyString_Check(getPyObj());
256 PyObject *strPyObj = PyObject_Str(getPyObj());
257 string val = PyString_AsString(strPyObj);
259 val = "\"" + val + "\"";
264 void InputCorbaPort::valFromStr(std::string valstr)
268 OutputCorbaPort::OutputCorbaPort(const std::string& name,
271 : OutputPort(name, node, type), DataPort(name, node, type), Port(node)
273 _orb = getSALOMERuntime()->getOrb();
276 OutputCorbaPort::OutputCorbaPort(const OutputCorbaPort& other, Node *newHelder):OutputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder)
278 _orb = getSALOMERuntime()->getOrb();
281 OutputCorbaPort::~OutputCorbaPort()
284 // Release or not release : all GenericObj are deleted when the output port is deleted
287 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
288 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
292 void OutputCorbaPort::put(const void *data) throw (ConversionException)
294 put((CORBA::Any *)data);
297 void OutputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
301 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
304 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
311 //no registerObj : we steal the output reference of the node
314 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
316 OutputPort::put(data);
318 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
321 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
325 OutputPort *OutputCorbaPort::clone(Node *newHelder) const
327 return new OutputCorbaPort(*this,newHelder);
330 CORBA::Any * OutputCorbaPort::getAny()
332 // return a pointer to the internal any
336 CORBA::Any * OutputCorbaPort::getAnyOut()
338 CORBA::Any* a=new CORBA::Any;
339 DynType kind=edGetType()->kind();
340 CORBA::TypeCode_var t;
344 a->replace(CORBA::_tc_long, (void*) 0);
346 else if(kind == String)
348 a->replace(CORBA::_tc_string, (void*) 0);
350 else if(kind == Double)
352 a->replace(CORBA::_tc_double, (void*) 0);
354 else if(kind == Objref)
356 t = getCorbaTC(edGetType());
357 a->replace(t, (void*) 0);
359 else if(kind == Sequence)
361 t = getCorbaTC(edGetType());
362 a->replace(t, (void*) 0);
364 else if(kind == Struct)
366 t = getCorbaTC(edGetType());
368 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
370 a->replace(t, (void*) 0);
372 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
375 else if(kind == Bool)
377 a->replace(CORBA::_tc_boolean, (void*) 0);
379 else if(kind == NONE)
382 msg << "Cannot set Any Out for None" << __FILE__ << ":" << __LINE__;
383 throw Exception(msg.str());
388 msg << "Cannot set Any Out for unknown type" << __FILE__
390 throw Exception(msg.str());
393 DEBTRACE( "getAnyOut:a: " << a );
395 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
400 PyObject * OutputCorbaPort::getPyObj()
402 CORBA::TypeCode_var tc=getAny()->type();
403 if (!tc->equivalent(CORBA::_tc_null))
404 return convertCorbaPyObject(edGetType(),getAny());
412 std::string OutputCorbaPort::getAsString()
414 InterpreterUnlocker loc;
415 PyObject* ob=getPyObj();
416 std::string s=convertPyObjectToString(ob);
421 std::string OutputCorbaPort::dump()
423 CORBA::TypeCode_var tc=_data.type();
424 if (tc->equivalent(CORBA::_tc_null))
425 return "<value>nil</value>";
426 string xmldump = convertCorbaXml(edGetType(), &_data);
431 ostream& operator<<(ostream& os, const OutputCorbaPort& p)
435 os << p._name << " : " << l ;
441 std::string OutputCorbaPort::valToStr()
443 PyObject *strPyObj = PyObject_Str(getPyObj());
444 string val = PyString_AsString(strPyObj);
449 void OutputCorbaPort::valFromStr(std::string valstr)