1 // Copyright (C) 2006-2016 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
27 // ----------------------------------------------------------------------------
30 #include "yacsconfig.h"
33 #include <omniORB4/CORBA.h>
35 //--- from omniORBpy.h (not present on Debian Sarge packages)
38 PyObject* (*cxxObjRefToPyObjRef)(const CORBA::Object_ptr cxx_obj,
39 CORBA::Boolean hold_lock);
40 // Convert a C++ object reference to a Python object reference.
41 // If <hold_lock> is true, caller holds the Python interpreter lock.
43 CORBA::Object_ptr (*pyObjRefToCxxObjRef)(PyObject* py_obj,
44 CORBA::Boolean hold_lock);
45 // Convert a Python object reference to a C++ object reference.
46 // Raises BAD_PARAM if the Python object is not an object reference.
47 // If <hold_lock> is true, caller holds the Python interpreter lock.
49 PyObject* (*handleCxxSystemException)(const CORBA::SystemException& ex);
50 // Sets the Python exception state to reflect the given C++ system
51 // exception. Always returns NULL. The caller must hold the Python
57 #define OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS \
58 catch (const CORBA::SystemException& ex) { \
59 return api->handleCxxSystemException(ex); \
62 #define OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
66 #include "InlineNode.hxx"
67 #include "ComposedNode.hxx"
68 #include "ServiceNode.hxx"
69 #include "ServiceInlineNode.hxx"
70 #include "ServerNode.hxx"
73 #include "ForLoop.hxx"
74 #include "WhileLoop.hxx"
75 #include "ForEachLoop.hxx"
77 #include "InputPort.hxx"
78 #include "OutputPort.hxx"
79 #include "InPropertyPort.hxx"
80 #include "InputDataStreamPort.hxx"
81 #include "OutputDataStreamPort.hxx"
82 #include "OptimizerLoop.hxx"
83 #include "HomogeneousPoolContainer.hxx"
87 class InterpreterUnlocker
92 _save = PyEval_SaveThread(); // allow Python threads to run
94 ~InterpreterUnlocker()
96 PyEval_RestoreThread(_save); // restore the thread state
102 static PyObject* convertNode(YACS::ENGINE::Node* node,int owner=0)
105 return SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Node,owner);
107 //should use $descriptor(YACS::ENGINE::Bloc *) and so on but $descriptor is not defined here
108 // It is better to define a helper function to avoid code bloat
109 // First try to find a swig type info by its mangled name
110 std::string swigtypename="_p_"+node->typeName();
111 swig_type_info *ret = SWIG_MangledTypeQuery(swigtypename.c_str());
113 ob=SWIG_NewPointerObj((void*)node,ret,owner);
116 //typeName not known by swig. Try dynamic_cast on known classes
117 //You must respect inheritance order in casting : Bloc before ComposedNode and so on
118 if(dynamic_cast<YACS::ENGINE::Proc *>(node))
119 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Proc,owner);
120 else if(dynamic_cast<YACS::ENGINE::Bloc *>(node))
121 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Bloc,owner);
122 else if(dynamic_cast<YACS::ENGINE::ForLoop *>(node))
123 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ForLoop,owner);
124 else if(dynamic_cast<YACS::ENGINE::WhileLoop *>(node))
125 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__WhileLoop,owner);
126 else if(dynamic_cast<YACS::ENGINE::ForEachLoop *>(node))
127 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ForEachLoop,owner);
128 else if(dynamic_cast<YACS::ENGINE::Switch *>(node))
129 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Switch,owner);
130 else if(dynamic_cast<YACS::ENGINE::ComposedNode *>(node))
131 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ComposedNode,owner);
132 else if(dynamic_cast<YACS::ENGINE::InlineFuncNode *>(node))
133 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__InlineFuncNode,owner);
134 else if(dynamic_cast<YACS::ENGINE::InlineNode *>(node))
135 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__InlineNode,owner);
136 else if(dynamic_cast<YACS::ENGINE::ServiceInlineNode *>(node))
137 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ServiceInlineNode,owner);
138 else if(dynamic_cast<YACS::ENGINE::ServiceNode *>(node))
139 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ServiceNode,owner);
140 else if(dynamic_cast<YACS::ENGINE::ServerNode *>(node))
141 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ServerNode,owner);
142 else if(dynamic_cast<YACS::ENGINE::ElementaryNode *>(node))
143 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ElementaryNode,owner);
145 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Node,owner);
150 static PyObject* convertPort(YACS::ENGINE::Port* port,int owner=0)
153 return SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__Port, owner);
155 std::string swigtypename="_p_"+port->typeName();
156 swig_type_info *ret = SWIG_MangledTypeQuery(swigtypename.c_str());
159 YACS::ENGINE::InPropertyPort *inpropertyport = dynamic_cast<YACS::ENGINE::InPropertyPort*>(port);
161 return SWIG_NewPointerObj((void*)inpropertyport,ret,owner);
163 YACS::ENGINE::InputPort *inport = dynamic_cast<YACS::ENGINE::InputPort *>(port);
165 return SWIG_NewPointerObj((void*)inport,ret,owner);
167 YACS::ENGINE::OutputPort *outport = dynamic_cast<YACS::ENGINE::OutputPort *>(port);
169 return SWIG_NewPointerObj((void*)outport,ret,owner);
171 YACS::ENGINE::InputDataStreamPort *indsport = dynamic_cast<YACS::ENGINE::InputDataStreamPort *>(port);
173 return SWIG_NewPointerObj((void*)indsport,ret,owner);
175 YACS::ENGINE::OutputDataStreamPort *outdsport = dynamic_cast<YACS::ENGINE::OutputDataStreamPort *>(port);
177 return SWIG_NewPointerObj((void*)outdsport,ret,owner);
179 return SWIG_NewPointerObj((void*)port,ret,owner);
183 if(YACS::ENGINE::AnyInputPort *cport =dynamic_cast<YACS::ENGINE::AnyInputPort *>(port))
184 ob=SWIG_NewPointerObj((void*)cport,SWIGTYPE_p_YACS__ENGINE__AnyInputPort,owner);
185 else if(YACS::ENGINE::AnyOutputPort *cport =dynamic_cast<YACS::ENGINE::AnyOutputPort *>(port))
186 ob=SWIG_NewPointerObj((void*)cport,SWIGTYPE_p_YACS__ENGINE__AnyOutputPort,owner);
187 else if(dynamic_cast<YACS::ENGINE::InPropertyPort*>(port))
188 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InPropertyPort,owner);
189 else if(dynamic_cast<YACS::ENGINE::InputPort *>(port))
190 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InputPort,owner);
191 else if(dynamic_cast<YACS::ENGINE::OutputPort *>(port))
192 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutputPort,owner);
193 else if(dynamic_cast<YACS::ENGINE::InputDataStreamPort *>(port))
194 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InputDataStreamPort, owner);
195 else if(dynamic_cast<YACS::ENGINE::OutputDataStreamPort *>(port))
196 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutputDataStreamPort, owner);
197 else if(dynamic_cast<YACS::ENGINE::InPort *>(port))
198 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InPort, owner);
199 else if(dynamic_cast<YACS::ENGINE::OutPort *>(port))
200 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutPort, owner);
201 else if(dynamic_cast<YACS::ENGINE::InGate *>(port))
202 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InGate, owner);
203 else if(dynamic_cast<YACS::ENGINE::OutGate *>(port))
204 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutGate, owner);
206 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__Port, owner);
211 static PyObject *convertContainer(YACS::ENGINE::Container *cont, int owner=0)
214 return SWIG_NewPointerObj((void*)cont,SWIGTYPE_p_YACS__ENGINE__Container, owner);
215 if(dynamic_cast<YACS::ENGINE::HomogeneousPoolContainer *>(cont))
217 return SWIG_NewPointerObj((void*)dynamic_cast<YACS::ENGINE::HomogeneousPoolContainer *>(cont),SWIGTYPE_p_YACS__ENGINE__HomogeneousPoolContainer, owner);
219 return SWIG_NewPointerObj((void*)cont,SWIGTYPE_p_YACS__ENGINE__Container, owner);
222 //convertFromPyObjVectorOfObj<YACS::ENGINE::SequenceAny *>(pyLi,SWIGTYPE_p_YACS__ENGINE__SequenceAny,"SequenceAny")
224 static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, const char *typeStr, typename std::vector<T>& ret)
227 if(PyList_Check(pyLi))
229 int size=PyList_Size(pyLi);
231 for(int i=0;i<size;i++)
233 PyObject *obj=PyList_GetItem(pyLi,i);
234 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
235 if(!SWIG_IsOK(status))
237 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : list is excepted to contain only " << typeStr << " instances !";
238 throw YACS::Exception(oss.str());
240 T arg=reinterpret_cast< T >(argp);
244 else if(PyTuple_Check(pyLi))
246 int size=PyTuple_Size(pyLi);
248 for(int i=0;i<size;i++)
250 PyObject *obj=PyTuple_GetItem(pyLi,i);
251 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
252 if(!SWIG_IsOK(status))
254 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : tuple is excepted to contain only " << typeStr << " instances !";
255 throw YACS::Exception(oss.str());
257 T arg=reinterpret_cast< T >(argp);
261 else if(SWIG_IsOK(SWIG_ConvertPtr(pyLi,&argp,ty,0|0)))
264 T arg=reinterpret_cast< T >(argp);
268 throw YACS::Exception("convertFromPyObjVectorOfObj : not a list nor a tuple");
273 #if SWIG_VERSION >= 0x010329
274 %template() std::list<int>;
275 %template() std::list<std::string>;
279 %typemap(out) std::list<int>
282 std::list<int>::iterator iL;
284 $result = PyList_New($1.size());
285 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
286 PyList_SetItem($result,i,PyLong_FromLong((*iL)));
289 %typemap(out) std::list<std::string>
292 std::list<std::string>::iterator iL;
294 $result = PyList_New($1.size());
295 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
296 PyList_SetItem($result,i,PyString_FromString((*iL).c_str()));
299 %typemap(in) std::list<std::string>
301 /* Check if input is a list */
302 if (PyList_Check($input))
304 int size = PyList_Size($input);
306 std::list<std::string> myList;
308 for (i = 0; i < size; i++)
310 PyObject *o = PyList_GetItem($input,i);
311 if (PyString_Check(o))
312 $1.push_back(std::string(PyString_AsString(PyList_GetItem($input,i))));
315 PyErr_SetString(PyExc_TypeError,"list must contain strings");
322 PyErr_SetString(PyExc_TypeError,"not a list");
331 %typecheck(SWIG_TYPECHECK_POINTER) YACS::ENGINE::Any*
334 if (SWIG_ConvertPtr($input, (void **) &ptr, $1_descriptor, 0) == 0)
336 else if (PyInt_Check($input))
338 else if(PyFloat_Check($input))
340 else if (PyString_Check($input))
346 %typemap(in) YACS::ENGINE::Any* (int is_new_object)
348 if ((SWIG_ConvertPtr($input,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION)) == 0)
350 // It is an Any : it is converted by SWIG_ConvertPtr $input -> $1
353 else if (PyInt_Check($input))
356 $1=YACS::ENGINE::AtomAny::New((int)PyInt_AsLong($input));
359 else if(PyFloat_Check($input))
362 $1=YACS::ENGINE::AtomAny::New(PyFloat_AsDouble($input));
365 else if(PyString_Check($input))
368 $1=YACS::ENGINE::AtomAny::New(PyString_AsString($input));
374 PyErr_SetString(PyExc_TypeError,"not a yacs any or a convertible type");
379 %typemap(directorout) YACS::ENGINE::Any*
381 if ((SWIG_ConvertPtr($1,(void **) &$result, $1_descriptor,SWIG_POINTER_EXCEPTION)) == 0)
383 // It is an Any : it is converted by SWIG_ConvertPtr $input -> $1
385 else if (PyInt_Check($1))
388 $result=YACS::ENGINE::AtomAny::New((int)PyInt_AsLong($1));
390 else if(PyFloat_Check($1))
393 $result=YACS::ENGINE::AtomAny::New(PyFloat_AsDouble($1));
395 else if(PyString_Check($1))
398 $result=YACS::ENGINE::AtomAny::New(PyString_AsString($1));
403 PyErr_SetString(PyExc_TypeError,"not a yacs any or a convertible type");
408 %typemap(freearg) YACS::ENGINE::Any *inSample
410 //a reference is taken by the routine called
411 if (!is_new_object$argnum) $1->incrRef();
414 %typemap(freearg) YACS::ENGINE::Any*
416 //no reference taken by the routine called
417 if (is_new_object$argnum) $1->decrRef();
420 %typemap(out) YACS::ENGINE::Any*
422 if(dynamic_cast<YACS::ENGINE::SequenceAny *>($1))
423 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__SequenceAny,$owner);
424 else if(dynamic_cast<YACS::ENGINE::ArrayAny *>($1))
425 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__ArrayAny,$owner);
426 else if(dynamic_cast<YACS::ENGINE::StructAny *>($1))
427 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__StructAny,$owner);
429 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__Any,$owner);
432 %typemap(out) YACS::ENGINE::TypeCode*
434 if(dynamic_cast<YACS::ENGINE::TypeCodeStruct *>($1))
435 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCodeStruct,$owner);
436 else if(dynamic_cast<YACS::ENGINE::TypeCodeSeq *>($1))
437 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCodeSeq,$owner);
438 else if(dynamic_cast<YACS::ENGINE::TypeCodeObjref *>($1))
439 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCodeObjref,$owner);
441 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCode,$owner);
444 %typemap(in) std::list<YACS::ENGINE::TypeCodeObjref*>
446 // Check if input is a list
447 if (PyList_Check($input))
449 int size = PyList_Size($input);
451 std::list<YACS::ENGINE::TypeCodeObjref*> myList;
453 for (i = 0; i < size; i++)
455 PyObject *o = PyList_GetItem($input,i);
456 YACS::ENGINE::TypeCode* temp;
457 if ((SWIG_ConvertPtr(o, (void **) &temp, $descriptor(YACS::ENGINE::TypeCode*),0)) == -1)
459 PyErr_SetString(PyExc_TypeError,"not a YACS::ENGINE::TypeCode*");
464 if(temp->kind() == YACS::ENGINE::Objref)
465 $1.push_back((YACS::ENGINE::TypeCodeObjref*)temp);
468 PyErr_SetString(PyExc_TypeError,"not a YACS::ENGINE::TypeCodeObjref*");
476 PyErr_SetString(PyExc_TypeError,"not a list");
481 %typemap(out) YACS::ENGINE::Node*
483 $result=convertNode($1,$owner);
486 %typemap(out) YACS::ENGINE::ServiceNode*
488 $result=convertNode($1,$owner);
491 %typemap(out) YACS::ENGINE::InlineNode*
493 $result=convertNode($1,$owner);
496 %typemap(out) YACS::ENGINE::InlineFuncNode*
498 $result=convertNode($1,$owner);
501 %typemap(out) YACS::ENGINE::ComposedNode*
503 $result=convertNode($1,$owner);
506 %typemap(out) YACS::ENGINE::OptimizerLoop*
508 $result=convertNode($1,$owner);
511 %typemap(out) YACS::ENGINE::Proc*
513 $result=convertNode($1,$owner);
516 %typemap(out) std::set<YACS::ENGINE::Node *>
519 std::set<YACS::ENGINE::Node *>::iterator iL;
521 $result = PyList_New($1.size());
523 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
526 PyList_SetItem($result,i,ob);
530 %typemap(out) std::list<YACS::ENGINE::Node *>
533 std::list<YACS::ENGINE::Node *>::iterator iL;
535 $result = PyList_New($1.size());
537 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
540 PyList_SetItem($result,i,ob);
544 %typemap(out) YACS::ENGINE::InputPort*,YACS::ENGINE::OutputPort*,YACS::ENGINE::InPort*,YACS::ENGINE::OutPort*,YACS::ENGINE::InPropertyPort*
546 $result=convertPort($1,$owner);
549 %typemap(out) std::set<YACS::ENGINE::InGate *>
552 std::set<YACS::ENGINE::InGate *>::iterator iL;
553 $result = PyList_New($1.size());
555 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
558 PyList_SetItem($result,i,ob);
562 %typemap(out) std::set<YACS::ENGINE::OutGate *>
565 std::set<YACS::ENGINE::OutGate *>::iterator iL;
566 $result = PyList_New($1.size());
568 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
571 PyList_SetItem($result,i,ob);
575 %typemap(out) std::set<YACS::ENGINE::InPort *>
577 std::set<YACS::ENGINE::InPort *>::iterator iL;
578 $result = PyList_New(0);
581 for (iL=$1.begin(); iL!=$1.end(); iL++)
584 status=PyList_Append($result,ob);
588 PyErr_SetString(PyExc_TypeError,"cannot build the inport list");
594 %typemap(out) std::set<YACS::ENGINE::OutPort *>
596 std::set<YACS::ENGINE::OutPort *>::iterator iL;
597 $result = PyList_New(0);
600 for (iL=$1.begin(); iL!=$1.end(); iL++)
603 status=PyList_Append($result,ob);
607 PyErr_SetString(PyExc_TypeError,"cannot build the outport list");
613 %typemap(out) std::list<YACS::ENGINE::OutPort *>
615 std::list<YACS::ENGINE::OutPort *>::const_iterator it;
616 $result = PyTuple_New($1.size());
618 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
619 PyTuple_SetItem($result,i,convertPort(*it));
622 %typemap(out) std::list<YACS::ENGINE::InPort *>
624 std::list<YACS::ENGINE::InPort *>::const_iterator it;
625 $result = PyTuple_New($1.size());
627 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
628 PyTuple_SetItem($result,i,convertPort(*it));
631 %typemap(out) std::list<YACS::ENGINE::OutputPort *>
633 std::list<YACS::ENGINE::OutputPort *>::const_iterator it;
634 $result = PyTuple_New($1.size());
636 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
637 PyTuple_SetItem($result,i,convertPort(*it));
640 %typemap(out) std::list<YACS::ENGINE::InputPort *>
642 std::list<YACS::ENGINE::InputPort *>::const_iterator it;
643 $result = PyTuple_New($1.size());
645 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
646 PyTuple_SetItem($result,i,convertPort(*it));
649 %typemap(out) std::list<YACS::ENGINE::InPropertyPort*>
651 std::list<YACS::ENGINE::InPropertyPort *>::const_iterator it;
652 $result = PyTuple_New($1.size());
654 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
655 PyTuple_SetItem($result,i,convertPort(*it));
664 // a general exception handler
670 catch(YACS::Exception& _e)
672 PyErr_SetString(PyExc_ValueError,_e.what());
675 catch(std::invalid_argument& _e)
677 PyErr_SetString(PyExc_IOError ,_e.what());
679 } catch (std::domain_error& e) {
680 SWIG_exception(SWIG_ValueError, e.what() );
681 } catch (std::overflow_error& e) {
682 SWIG_exception(SWIG_OverflowError, e.what() );
683 } catch (std::out_of_range& e) {
684 PyErr_SetString(PyExc_KeyError,e.what());
686 } catch (std::length_error& e) {
687 SWIG_exception(SWIG_IndexError, e.what() );
688 } catch (std::runtime_error& e) {
689 SWIG_exception(SWIG_RuntimeError, e.what() );
691 OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
692 catch (std::exception& e) {
693 SWIG_exception(SWIG_SystemError, e.what() );
697 SWIG_exception(SWIG_UnknownError, "Unknown exception");
701 // a specific exception handler = generic + release lock
702 %define PYEXCEPTION(name)
706 InterpreterUnlocker _l;
709 catch(YACS::Exception& _e)
711 PyErr_SetString(PyExc_ValueError,_e.what());
714 catch(std::invalid_argument& _e)
716 PyErr_SetString(PyExc_IOError ,_e.what());
718 } catch (std::domain_error& e) {
719 SWIG_exception(SWIG_ValueError, e.what() );
720 } catch (std::overflow_error& e) {
721 SWIG_exception(SWIG_OverflowError, e.what() );
722 } catch (std::out_of_range& e) {
723 PyErr_SetString(PyExc_KeyError,e.what());
725 } catch (std::length_error& e) {
726 SWIG_exception(SWIG_IndexError, e.what() );
727 } catch (std::runtime_error& e) {
728 SWIG_exception(SWIG_RuntimeError, e.what() );
730 OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
731 catch (std::exception& e) {
732 SWIG_exception(SWIG_SystemError, e.what() );
736 SWIG_exception(SWIG_UnknownError, "Unknown exception");
741 %define EXCEPTION(name)
747 catch(YACS::Exception& _e)
749 PyErr_SetString(PyExc_ValueError,_e.what());
752 catch(std::invalid_argument& _e)
754 PyErr_SetString(PyExc_IOError ,_e.what());
756 } catch (std::domain_error& e) {
757 SWIG_exception(SWIG_ValueError, e.what() );
758 } catch (std::overflow_error& e) {
759 SWIG_exception(SWIG_OverflowError, e.what() );
760 } catch (std::out_of_range& e) {
761 PyErr_SetString(PyExc_KeyError,e.what());
763 } catch (std::length_error& e) {
764 SWIG_exception(SWIG_IndexError, e.what() );
765 } catch (std::runtime_error& e) {
766 SWIG_exception(SWIG_RuntimeError, e.what() );
768 OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
769 catch (std::exception& e) {
770 SWIG_exception(SWIG_SystemError, e.what() );
774 SWIG_exception(SWIG_UnknownError, "Unknown exception");
779 * End of Exception section
785 //Release ownership : transfer it to C++
786 %apply SWIGTYPE *DISOWN { YACS::ENGINE::CatalogLoader* factory};
787 %apply SWIGTYPE *DISOWN { YACS::ENGINE::Node *DISOWNnode };
788 %apply SWIGTYPE *DISOWN { Node *DISOWNnode };
790 * End of ownership section
794 * Reference counting section
795 * reference counted objects are created with a count of 1 so we do not incrRef them on wrapping creation
796 * we only decrRef them on wrapping destruction.
797 * Do not forget to declare them new (%newobject) when they are not returned from a constructor
798 * unless they will not be decrRef on wrapping destruction
800 %feature("ref") YACS::ENGINE::RefCounter ""
801 %feature("unref") YACS::ENGINE::RefCounter "$this->decrRef();"
803 // Unfortunately, class ComponentInstance inherits from RefCounter AND PropertyInterface. Thus the ref and
804 // unref features are ambiguous and with swig 2.0.7 at least, we must re-specify those features for class
805 // ComponentInstance unless the instances are destroyed when the Swig object is unref'ed.
806 %feature("ref") YACS::ENGINE::ComponentInstance ""
807 %feature("unref") YACS::ENGINE::ComponentInstance "$this->decrRef();"
809 * End of Reference counting section
815 template <> struct traits_from<YACS::ENGINE::InPort *> {
816 static PyObject *from(YACS::ENGINE::InPort* val){
817 return convertPort(val);
820 template <> struct traits_from<YACS::ENGINE::OutPort *> {
821 static PyObject *from(YACS::ENGINE::OutPort* val) {
822 return convertPort(val);
825 template <> struct traits_from<YACS::ENGINE::InputPort *> {
826 static PyObject *from(YACS::ENGINE::InPort* val){
827 return convertPort(val);
830 template <> struct traits_from<YACS::ENGINE::OutputPort *> {
831 static PyObject *from(YACS::ENGINE::OutPort* val) {
832 return convertPort(val);
839 %define REFCOUNT_TEMPLATE(tname, T...)
841 This macro is a special wrapping for map with value type which derives from RefCounter.
842 To overload standard SWIG wrapping we define a full specialization of std::map
843 with %extend for 4 basic methods : getitem, setitem, delitem and keys.
844 Then we complete the interface by deriving the shadow wrapper from
845 the python mixin class (UserDict.DictMixin).
846 Do not forget to declare the new shadow class to SWIG with tname_swigregister(tname).
847 Objects returned by __getitem__ are declared new (%newobject) so that when destroyed they
848 call decrRef (see feature("unref") for RefCounter).
851 class std::map<std::string,T*>
856 void __setitem__(const std::string& name, T* c)
858 std::map<std::string, T* >::iterator i = self->find(name);
859 if (i != self->end())
863 i->second->decrRef();
868 T* __getitem__(std::string name)
870 std::map<std::string, T* >::iterator i = self->find(name);
871 if (i != self->end())
873 i->second->incrRef();
877 throw std::out_of_range("key not found");
879 void __delitem__(std::string name)
881 std::map<std::string, T* >::iterator i = self->find(name);
882 if (i != self->end()){
883 i->second->decrRef();
887 throw std::out_of_range("key not found");
890 int pysize = self->size();
891 PyObject* keyList = PyList_New(pysize);
892 std::map<std::string, T* >::const_iterator i = self->begin();
893 for (int j = 0; j < pysize; ++i, ++j) {
894 PyList_SET_ITEM(keyList, j, PyString_FromString(i->first.c_str()));
901 %newobject std::map<std::string,T* >::__getitem__;
902 %template() std::pair<std::string, T* >;
903 %template(tname) std::map<std::string, T* >;
905 from UserDict import DictMixin
906 class tname(tname,DictMixin):pass
907 tname##_swigregister(tname)