1 // Copyright (C) 2006-2024 CEA, EDF
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)
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::releaseData()
165 {//do nothing - to be implemented
168 void InputCorbaPort::put(CORBA::Any *data)
171 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
173 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
180 // make a copy of the any (protect against deletion of any source)
187 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
191 InputPort *InputCorbaPort::clone(Node *newHelder) const
193 return new InputCorbaPort(*this,newHelder);
196 void *InputCorbaPort::get() const
198 return (void *)&_data;
201 bool InputCorbaPort::isEmpty()
203 CORBA::TypeCode_var tc=_data.type();
204 return tc->equivalent(CORBA::_tc_null);
207 CORBA::Any * InputCorbaPort::getAny()
209 // --- return a pointer to internal any
213 PyObject * InputCorbaPort::getPyObj()
215 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
216 CORBA::TypeCode_var tc=getAny()->type();
217 if (!tc->equivalent(CORBA::_tc_null))
218 return convertCorbaPyObject(edGetType(),getAny());
226 std::string InputCorbaPort::getAsString()
228 InterpreterUnlocker loc;
229 PyObject* ob=getPyObj();
230 std::string s=convertPyObjectToString(ob);
236 //! Save the current data value for further reinitialization of the port
240 void InputCorbaPort::exSaveInit()
244 _initData=new CORBA::Any;
248 //! Restore the saved data value to current data value
250 * If no data has been saved (_initData == 0) don't restore
252 void InputCorbaPort::exRestoreInit()
254 if(!_initData)return;
258 std::string InputCorbaPort::dump()
260 CORBA::TypeCode_var tc=_data.type();
261 if (tc->equivalent(CORBA::_tc_null))
262 return "<value>nil</value>";
263 if (edGetType()->kind() != YACS::ENGINE::Objref)
264 return convertCorbaXml(edGetType(), &_data);
265 if (! _stringRef.empty())
268 return convertCorbaXml(edGetType(), &_data);
271 // msg << "Cannot retreive init string reference string for port " << _name
272 // << " on node " << _node->getName();
273 // throw Exception(msg.str());
277 std::string InputCorbaPort::valToStr()
279 int isString = PyBytes_Check(getPyObj());
280 PyObject *strPyObj = PyObject_Str(getPyObj());
281 string val = PyBytes_AsString(strPyObj);
283 val = "\"" + val + "\"";
288 void InputCorbaPort::valFromStr(std::string valstr)
292 OutputCorbaPort::OutputCorbaPort(const std::string& name,
295 : OutputPort(name, node, type), DataPort(name, node, type), Port(node)
297 _orb = getSALOMERuntime()->getOrb();
300 OutputCorbaPort::OutputCorbaPort(const OutputCorbaPort& other, Node *newHelder):OutputPort(other,newHelder),DataPort(other,newHelder),Port(other,newHelder)
302 _orb = getSALOMERuntime()->getOrb();
305 OutputCorbaPort::~OutputCorbaPort()
308 // Release or not release : all GenericObj are deleted when the output port is deleted
311 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
312 DEBTRACE("refcount CORBA tc_double: " << ((omni::TypeCode_base*)CORBA::_tc_double)->pd_ref_count);
316 void OutputCorbaPort::put(const void *data)
318 put((CORBA::Any *)data);
321 void OutputCorbaPort::put(CORBA::Any *data)
326 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
328 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
331 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
338 //no registerObj : we steal the output reference of the node
342 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
344 OutputPort::put(data);
346 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)data->pd_tc.in())->pd_ref_count);
349 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)_data.pd_tc.in())->pd_ref_count);
353 OutputPort *OutputCorbaPort::clone(Node *newHelder) const
355 return new OutputCorbaPort(*this,newHelder);
358 CORBA::Any * OutputCorbaPort::getAny()
360 // return a pointer to the internal any
364 CORBA::Any * OutputCorbaPort::getAnyOut()
366 CORBA::Any* a=new CORBA::Any;
367 DynType kind=edGetType()->kind();
368 CORBA::TypeCode_var t;
372 a->replace(CORBA::_tc_long, (void*) 0);
374 else if(kind == String)
376 a->replace(CORBA::_tc_string, (void*) 0);
378 else if(kind == Double)
380 a->replace(CORBA::_tc_double, (void*) 0);
382 else if(kind == Objref)
384 t = getCorbaTC(edGetType());
385 a->replace(t, (void*) 0);
387 else if(kind == Sequence)
389 t = getCorbaTC(edGetType());
390 a->replace(t, (void*) 0);
392 else if(kind == Struct)
394 t = getCorbaTC(edGetType());
396 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
398 a->replace(t, (void*) 0);
400 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)t.in())->pd_ref_count);
403 else if(kind == Bool)
405 a->replace(CORBA::_tc_boolean, (void*) 0);
407 else if(kind == NONE)
410 msg << "Cannot set Any Out for None" << __FILE__ << ":" << __LINE__;
411 throw Exception(msg.str());
416 msg << "Cannot set Any Out for unknown type" << __FILE__
418 throw Exception(msg.str());
421 DEBTRACE( "getAnyOut:a: " << a );
423 DEBTRACE("refcount CORBA : " << ((omni::TypeCode_base*)a->pd_tc.in())->pd_ref_count);
428 PyObject * OutputCorbaPort::getPyObj()
430 YACS::BASES::AutoLocker<YACS::BASES::Mutex> lock(&_mutex);
431 CORBA::TypeCode_var tc=getAny()->type();
432 if (!tc->equivalent(CORBA::_tc_null))
433 return convertCorbaPyObject(edGetType(),getAny());
441 std::string OutputCorbaPort::getAsString()
443 InterpreterUnlocker loc;
444 PyObject* ob=getPyObj();
445 std::string s=convertPyObjectToString(ob);
450 std::string OutputCorbaPort::dump()
452 CORBA::TypeCode_var tc=_data.type();
453 if (tc->equivalent(CORBA::_tc_null))
454 return "<value>nil</value>";
455 string xmldump = convertCorbaXml(edGetType(), &_data);
460 ostream& operator<<(ostream& os, const OutputCorbaPort& p)
464 os << p._name << " : " << l ;
470 std::string OutputCorbaPort::valToStr()
472 PyObject *strPyObj = PyObject_Str(getPyObj());
473 string val = PyBytes_AsString(strPyObj);
478 void OutputCorbaPort::valFromStr(std::string valstr)