1 // Copyright (C) 2006-2015 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 "AutoLocker.hxx"
33 #include "CORBAPorts.hxx"
34 #include "PythonPorts.hxx"
35 #include "ServiceNode.hxx"
36 #include "ComponentInstance.hxx"
37 #include "SALOME_GenericObj.hh"
43 #include "YacsTrace.hxx"
45 using namespace YACS::ENGINE;
48 void releaseObj(CORBA::Any& data)
50 CORBA::Object_var obj;
51 if(data >>= CORBA::Any::to_object(obj))
53 SALOME::GenericObj_var gobj;
56 gobj=SALOME::GenericObj::_narrow(obj);
58 catch(const CORBA::SystemException& )
62 if(!CORBA::is_nil(gobj))
64 DEBTRACE("It's a SALOME::GenericObj");
68 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
71 DEBTRACE("It's not a CORBA::Object");
74 void registerObj(CORBA::Any& data)
76 CORBA::Object_var obj;
77 if(data >>= CORBA::Any::to_object(obj))
79 SALOME::GenericObj_var gobj;
82 gobj=SALOME::GenericObj::_narrow(obj);
84 catch(const CORBA::SystemException& )
88 if(!CORBA::is_nil(gobj))
90 DEBTRACE("It's a SALOME::GenericObj");
94 DEBTRACE("It's a CORBA::Object but not a SALOME::GenericObj");
97 DEBTRACE("It's not a CORBA::Object");
100 InputCorbaPort::InputCorbaPort(const std::string& name,
103 : InputPort(name, node, type), DataPort(name, node, type), Port(node), _initData(0)
105 _orb = getSALOMERuntime()->getOrb();
108 InputCorbaPort::InputCorbaPort(const InputCorbaPort& other, Node *newHelder):InputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder),
111 _orb = getSALOMERuntime()->getOrb();
114 _initData=new CORBA::Any;
115 *_initData=*(other._initData);
120 InputCorbaPort::~InputCorbaPort()
123 // Release or not release : all GenericObj are deleted when the input port is deleted
127 bool InputCorbaPort::edIsManuallyInitialized() const
132 void InputCorbaPort::edRemoveManInit()
136 InputPort::edRemoveManInit();
139 void InputCorbaPort::put(const void *data) throw (ConversionException)
141 put((CORBA::Any *)data);
144 void display(CORBA::Any* data)
146 CORBA::TypeCode_var tc=data->type();
149 case CORBA::tk_double:
152 DEBTRACE( "Double: " << d );
157 DEBTRACE( "Int: " << l );
164 void InputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
167 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
169 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
176 // make a copy of the any (protect against deletion of any source)
183 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
187 InputPort *InputCorbaPort::clone(Node *newHelder) const
189 return new InputCorbaPort(*this,newHelder);
192 void *InputCorbaPort::get() const throw(YACS::Exception)
194 return (void *)&_data;
197 bool InputCorbaPort::isEmpty()
199 CORBA::TypeCode_var tc=_data.type();
200 return tc->equivalent(CORBA::_tc_null);
203 CORBA::Any * InputCorbaPort::getAny()
205 // --- return a pointer to internal any
209 PyObject * InputCorbaPort::getPyObj()
211 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
212 CORBA::TypeCode_var tc=getAny()->type();
213 if (!tc->equivalent(CORBA::_tc_null))
214 return convertCorbaPyObject(edGetType(),getAny());
222 std::string InputCorbaPort::getAsString()
224 InterpreterUnlocker loc;
225 PyObject* ob=getPyObj();
226 std::string s=convertPyObjectToString(ob);
232 //! Save the current data value for further reinitialization of the port
236 void InputCorbaPort::exSaveInit()
240 _initData=new CORBA::Any;
244 //! Restore the saved data value to current data value
246 * If no data has been saved (_initData == 0) don't restore
248 void InputCorbaPort::exRestoreInit()
250 if(!_initData)return;
254 std::string InputCorbaPort::dump()
256 CORBA::TypeCode_var tc=_data.type();
257 if (tc->equivalent(CORBA::_tc_null))
258 return "<value>nil</value>";
259 if (edGetType()->kind() != YACS::ENGINE::Objref)
260 return convertCorbaXml(edGetType(), &_data);
261 if (! _stringRef.empty())
264 return convertCorbaXml(edGetType(), &_data);
267 // msg << "Cannot retreive init string reference string for port " << _name
268 // << " on node " << _node->getName();
269 // throw Exception(msg.str());
273 std::string InputCorbaPort::valToStr()
275 int isString = PyString_Check(getPyObj());
276 PyObject *strPyObj = PyObject_Str(getPyObj());
277 string val = PyString_AsString(strPyObj);
279 val = "\"" + val + "\"";
284 void InputCorbaPort::valFromStr(std::string valstr)
288 OutputCorbaPort::OutputCorbaPort(const std::string& name,
291 : OutputPort(name, node, type), DataPort(name, node, type), Port(node)
293 _orb = getSALOMERuntime()->getOrb();
296 OutputCorbaPort::OutputCorbaPort(const OutputCorbaPort& other, Node *newHelder):OutputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder)
298 _orb = getSALOMERuntime()->getOrb();
301 OutputCorbaPort::~OutputCorbaPort()
304 // Release or not release : all GenericObj are deleted when the output port is deleted
307 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
308 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
312 void OutputCorbaPort::put(const void *data) throw (ConversionException)
314 put((CORBA::Any *)data);
317 void OutputCorbaPort::put(CORBA::Any *data) throw (ConversionException)
322 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
324 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
327 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
334 //no registerObj : we steal the output reference of the node
338 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
340 OutputPort::put(data);
342 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
345 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
349 OutputPort *OutputCorbaPort::clone(Node *newHelder) const
351 return new OutputCorbaPort(*this,newHelder);
354 CORBA::Any * OutputCorbaPort::getAny()
356 // return a pointer to the internal any
360 CORBA::Any * OutputCorbaPort::getAnyOut()
362 CORBA::Any* a=new CORBA::Any;
363 DynType kind=edGetType()->kind();
364 CORBA::TypeCode_var t;
368 a->replace(CORBA::_tc_long, (void*) 0);
370 else if(kind == String)
372 a->replace(CORBA::_tc_string, (void*) 0);
374 else if(kind == Double)
376 a->replace(CORBA::_tc_double, (void*) 0);
378 else if(kind == Objref)
380 t = getCorbaTC(edGetType());
381 a->replace(t, (void*) 0);
383 else if(kind == Sequence)
385 t = getCorbaTC(edGetType());
386 a->replace(t, (void*) 0);
388 else if(kind == Struct)
390 t = getCorbaTC(edGetType());
392 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
394 a->replace(t, (void*) 0);
396 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
399 else if(kind == Bool)
401 a->replace(CORBA::_tc_boolean, (void*) 0);
403 else if(kind == NONE)
406 msg << "Cannot set Any Out for None" << __FILE__ << ":" << __LINE__;
407 throw Exception(msg.str());
412 msg << "Cannot set Any Out for unknown type" << __FILE__
414 throw Exception(msg.str());
417 DEBTRACE( "getAnyOut:a: " << a );
419 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
424 PyObject * OutputCorbaPort::getPyObj()
426 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
427 CORBA::TypeCode_var tc=getAny()->type();
428 if (!tc->equivalent(CORBA::_tc_null))
429 return convertCorbaPyObject(edGetType(),getAny());
437 std::string OutputCorbaPort::getAsString()
439 InterpreterUnlocker loc;
440 PyObject* ob=getPyObj();
441 std::string s=convertPyObjectToString(ob);
446 std::string OutputCorbaPort::dump()
448 CORBA::TypeCode_var tc=_data.type();
449 if (tc->equivalent(CORBA::_tc_null))
450 return "<value>nil</value>";
451 string xmldump = convertCorbaXml(edGetType(), &_data);
456 ostream& operator<<(ostream& os, const OutputCorbaPort& p)
460 os << p._name << " : " << l ;
466 std::string OutputCorbaPort::valToStr()
468 PyObject *strPyObj = PyObject_Str(getPyObj());
469 string val = PyString_AsString(strPyObj);
474 void OutputCorbaPort::valFromStr(std::string valstr)