1 // Copyright (C) 2006-2014 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, or (at your option) any later version.
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
23 #define private public
24 #define protected public
25 #include <omniORB4/CORBA.h>
26 #include <omniORB4/internal/typecode.h>
29 #include "RuntimeSALOME.hxx"
30 #include "TypeConversions.hxx"
31 #include "TypeCode.hxx"
32 #include "CORBAPorts.hxx"
33 #include "PythonPorts.hxx"
34 #include "ServiceNode.hxx"
35 #include "ComponentInstance.hxx"
36 #include "SALOME_GenericObj.hh"
42 #include "YacsTrace.hxx"
44 using namespace YACS::ENGINE;
47 void releaseObj(CORBA::Any& data)
49 CORBA::Object_var obj;
50 if(data >>= CORBA::Any::to_object(obj))
52 SALOME::GenericObj_var gobj;
55 gobj=SALOME::GenericObj::_narrow(obj);
57 catch(const CORBA::SystemException& )
61 if(!CORBA::is_nil(gobj))
63 DEBTRACE("It's a SALOME::GenericObj");
67 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
70 DEBTRACE("It's not a CORBA::Object");
73 void registerObj(CORBA::Any& data)
75 CORBA::Object_var obj;
76 if(data >>= CORBA::Any::to_object(obj))
78 SALOME::GenericObj_var gobj;
81 gobj=SALOME::GenericObj::_narrow(obj);
83 catch(const CORBA::SystemException& )
87 if(!CORBA::is_nil(gobj))
89 DEBTRACE("It's a SALOME::GenericObj");
93 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
96 DEBTRACE("It's not a CORBA::Object");
99 InputCorbaPort::InputCorbaPort(const std::string& name,
102 : InputPort(name, node, type), DataPort(name, node, type), Port(node), _initData(0)
104 _orb = getSALOMERuntime()->getOrb();
107 InputCorbaPort::InputCorbaPort(const InputCorbaPort& other, Node *newHelder):InputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder),
110 _orb = getSALOMERuntime()->getOrb();
113 _initData=new CORBA::Any;
114 *_initData=*(other._initData);
119 InputCorbaPort::~InputCorbaPort()
122 // Release or not release : all GenericObj are deleted when the input port is deleted
126 bool InputCorbaPort::edIsManuallyInitialized() const
131 void InputCorbaPort::edRemoveManInit()
135 InputPort::edRemoveManInit();
138 void InputCorbaPort::put(const void *data) throw (ConversionException)
140 put((CORBA::Any *)data);
143 void display(CORBA::Any* data)
145 CORBA::TypeCode_var tc=data->type();
148 case CORBA::tk_double:
151 DEBTRACE( "Double: " << d );
156 DEBTRACE( "Int: " << l );
163 void InputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
166 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
168 YACS::BASES::Lock lock(&_mutex);
175 // make a copy of the any (protect against deletion of any source)
182 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
186 InputPort *InputCorbaPort::clone(Node *newHelder) const
188 return new InputCorbaPort(*this,newHelder);
191 void *InputCorbaPort::get() const throw(YACS::Exception)
193 return (void *)&_data;
196 bool InputCorbaPort::isEmpty()
198 CORBA::TypeCode_var tc=_data.type();
199 return tc->equivalent(CORBA::_tc_null);
202 CORBA::Any * InputCorbaPort::getAny()
204 // --- return a pointer to internal any
208 PyObject * InputCorbaPort::getPyObj()
210 YACS::BASES::Lock lock(&_mutex);
211 CORBA::TypeCode_var tc=getAny()->type();
212 if (!tc->equivalent(CORBA::_tc_null))
213 return convertCorbaPyObject(edGetType(),getAny());
221 std::string InputCorbaPort::getAsString()
223 InterpreterUnlocker loc;
224 PyObject* ob=getPyObj();
225 std::string s=convertPyObjectToString(ob);
231 //! Save the current data value for further reinitialization of the port
235 void InputCorbaPort::exSaveInit()
239 _initData=new CORBA::Any;
243 //! Restore the saved data value to current data value
245 * If no data has been saved (_initData == 0) don't restore
247 void InputCorbaPort::exRestoreInit()
249 if(!_initData)return;
253 std::string InputCorbaPort::dump()
255 CORBA::TypeCode_var tc=_data.type();
256 if (tc->equivalent(CORBA::_tc_null))
257 return "<value>nil</value>";
258 if (edGetType()->kind() != YACS::ENGINE::Objref)
259 return convertCorbaXml(edGetType(), &_data);
260 if (! _stringRef.empty())
263 return convertCorbaXml(edGetType(), &_data);
266 // msg << "Cannot retreive init string reference string for port " << _name
267 // << " on node " << _node->getName();
268 // throw Exception(msg.str());
272 std::string InputCorbaPort::valToStr()
274 int isString = PyString_Check(getPyObj());
275 PyObject *strPyObj = PyObject_Str(getPyObj());
276 string val = PyString_AsString(strPyObj);
278 val = "\"" + val + "\"";
283 void InputCorbaPort::valFromStr(std::string valstr)
287 OutputCorbaPort::OutputCorbaPort(const std::string& name,
290 : OutputPort(name, node, type), DataPort(name, node, type), Port(node)
292 _orb = getSALOMERuntime()->getOrb();
295 OutputCorbaPort::OutputCorbaPort(const OutputCorbaPort& other, Node *newHelder):OutputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder)
297 _orb = getSALOMERuntime()->getOrb();
300 OutputCorbaPort::~OutputCorbaPort()
303 // Release or not release : all GenericObj are deleted when the output port is deleted
306 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
307 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
311 void OutputCorbaPort::put(const void *data) throw (ConversionException)
313 put((CORBA::Any *)data);
316 void OutputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
321 YACS::BASES::Lock lock(&_mutex);
323 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
326 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
333 //no registerObj : we steal the output reference of the node
337 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
339 OutputPort::put(data);
341 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
344 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
348 OutputPort *OutputCorbaPort::clone(Node *newHelder) const
350 return new OutputCorbaPort(*this,newHelder);
353 CORBA::Any * OutputCorbaPort::getAny()
355 // return a pointer to the internal any
359 CORBA::Any * OutputCorbaPort::getAnyOut()
361 CORBA::Any* a=new CORBA::Any;
362 DynType kind=edGetType()->kind();
363 CORBA::TypeCode_var t;
367 a->replace(CORBA::_tc_long, (void*) 0);
369 else if(kind == String)
371 a->replace(CORBA::_tc_string, (void*) 0);
373 else if(kind == Double)
375 a->replace(CORBA::_tc_double, (void*) 0);
377 else if(kind == Objref)
379 t = getCorbaTC(edGetType());
380 a->replace(t, (void*) 0);
382 else if(kind == Sequence)
384 t = getCorbaTC(edGetType());
385 a->replace(t, (void*) 0);
387 else if(kind == Struct)
389 t = getCorbaTC(edGetType());
391 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
393 a->replace(t, (void*) 0);
395 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
398 else if(kind == Bool)
400 a->replace(CORBA::_tc_boolean, (void*) 0);
402 else if(kind == NONE)
405 msg << "Cannot set Any Out for None" << __FILE__ << ":" << __LINE__;
406 throw Exception(msg.str());
411 msg << "Cannot set Any Out for unknown type" << __FILE__
413 throw Exception(msg.str());
416 DEBTRACE( "getAnyOut:a: " << a );
418 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
423 PyObject * OutputCorbaPort::getPyObj()
425 YACS::BASES::Lock lock(&_mutex);
426 CORBA::TypeCode_var tc=getAny()->type();
427 if (!tc->equivalent(CORBA::_tc_null))
428 return convertCorbaPyObject(edGetType(),getAny());
436 std::string OutputCorbaPort::getAsString()
438 InterpreterUnlocker loc;
439 PyObject* ob=getPyObj();
440 std::string s=convertPyObjectToString(ob);
445 std::string OutputCorbaPort::dump()
447 CORBA::TypeCode_var tc=_data.type();
448 if (tc->equivalent(CORBA::_tc_null))
449 return "<value>nil</value>";
450 string xmldump = convertCorbaXml(edGetType(), &_data);
455 ostream& operator<<(ostream& os, const OutputCorbaPort& p)
459 os << p._name << " : " << l ;
465 std::string OutputCorbaPort::valToStr()
467 PyObject *strPyObj = PyObject_Str(getPyObj());
468 string val = PyString_AsString(strPyObj);
473 void OutputCorbaPort::valFromStr(std::string valstr)