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 #include <omniORBpy.h>
38 #define OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS \
39 catch (const CORBA::SystemException& ex) { \
40 return api->handleCxxSystemException(ex); \
43 #define OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
47 #include "InlineNode.hxx"
48 #include "ComposedNode.hxx"
49 #include "ServiceNode.hxx"
50 #include "ServiceInlineNode.hxx"
51 #include "ServerNode.hxx"
54 #include "ForLoop.hxx"
55 #include "WhileLoop.hxx"
56 #include "ForEachLoop.hxx"
58 #include "InputPort.hxx"
59 #include "OutputPort.hxx"
60 #include "InPropertyPort.hxx"
61 #include "InputDataStreamPort.hxx"
62 #include "OutputDataStreamPort.hxx"
63 #include "OptimizerLoop.hxx"
64 #include "HomogeneousPoolContainer.hxx"
65 #include "IteratorPy3.hxx"
69 class InterpreterUnlocker
74 _save = PyEval_SaveThread(); // allow Python threads to run
76 ~InterpreterUnlocker()
78 PyEval_RestoreThread(_save); // restore the thread state
84 static PyObject* convertNode(YACS::ENGINE::Node* node,int owner=0)
87 return SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Node,owner);
89 //should use $descriptor(YACS::ENGINE::Bloc *) and so on but $descriptor is not defined here
90 // It is better to define a helper function to avoid code bloat
91 // First try to find a swig type info by its mangled name
92 std::string swigtypename="_p_"+node->typeName();
93 swig_type_info *ret = SWIG_MangledTypeQuery(swigtypename.c_str());
95 ob=SWIG_NewPointerObj((void*)node,ret,owner);
98 //typeName not known by swig. Try dynamic_cast on known classes
99 //You must respect inheritance order in casting : Bloc before ComposedNode and so on
100 if(dynamic_cast<YACS::ENGINE::Proc *>(node))
101 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Proc,owner);
102 else if(dynamic_cast<YACS::ENGINE::Bloc *>(node))
103 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Bloc,owner);
104 else if(dynamic_cast<YACS::ENGINE::ForLoop *>(node))
105 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ForLoop,owner);
106 else if(dynamic_cast<YACS::ENGINE::WhileLoop *>(node))
107 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__WhileLoop,owner);
108 else if(dynamic_cast<YACS::ENGINE::ForEachLoop *>(node))
109 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ForEachLoop,owner);
110 else if(dynamic_cast<YACS::ENGINE::Switch *>(node))
111 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Switch,owner);
112 else if(dynamic_cast<YACS::ENGINE::ComposedNode *>(node))
113 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ComposedNode,owner);
114 else if(dynamic_cast<YACS::ENGINE::InlineFuncNode *>(node))
115 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__InlineFuncNode,owner);
116 else if(dynamic_cast<YACS::ENGINE::InlineNode *>(node))
117 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__InlineNode,owner);
118 else if(dynamic_cast<YACS::ENGINE::ServiceInlineNode *>(node))
119 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ServiceInlineNode,owner);
120 else if(dynamic_cast<YACS::ENGINE::ServiceNode *>(node))
121 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ServiceNode,owner);
122 else if(dynamic_cast<YACS::ENGINE::ServerNode *>(node))
123 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ServerNode,owner);
124 else if(dynamic_cast<YACS::ENGINE::ElementaryNode *>(node))
125 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__ElementaryNode,owner);
127 ob=SWIG_NewPointerObj((void*)node,SWIGTYPE_p_YACS__ENGINE__Node,owner);
132 static PyObject* convertPort(YACS::ENGINE::Port* port,int owner=0)
135 return SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__Port, owner);
137 std::string swigtypename="_p_"+port->typeName();
138 swig_type_info *ret = SWIG_MangledTypeQuery(swigtypename.c_str());
141 YACS::ENGINE::InPropertyPort *inpropertyport = dynamic_cast<YACS::ENGINE::InPropertyPort*>(port);
143 return SWIG_NewPointerObj((void*)inpropertyport,ret,owner);
145 YACS::ENGINE::InputPort *inport = dynamic_cast<YACS::ENGINE::InputPort *>(port);
147 return SWIG_NewPointerObj((void*)inport,ret,owner);
149 YACS::ENGINE::OutputPort *outport = dynamic_cast<YACS::ENGINE::OutputPort *>(port);
151 return SWIG_NewPointerObj((void*)outport,ret,owner);
153 YACS::ENGINE::InputDataStreamPort *indsport = dynamic_cast<YACS::ENGINE::InputDataStreamPort *>(port);
155 return SWIG_NewPointerObj((void*)indsport,ret,owner);
157 YACS::ENGINE::OutputDataStreamPort *outdsport = dynamic_cast<YACS::ENGINE::OutputDataStreamPort *>(port);
159 return SWIG_NewPointerObj((void*)outdsport,ret,owner);
161 return SWIG_NewPointerObj((void*)port,ret,owner);
165 if(YACS::ENGINE::AnyInputPort *cport =dynamic_cast<YACS::ENGINE::AnyInputPort *>(port))
166 ob=SWIG_NewPointerObj((void*)cport,SWIGTYPE_p_YACS__ENGINE__AnyInputPort,owner);
167 else if(YACS::ENGINE::AnyOutputPort *cport =dynamic_cast<YACS::ENGINE::AnyOutputPort *>(port))
168 ob=SWIG_NewPointerObj((void*)cport,SWIGTYPE_p_YACS__ENGINE__AnyOutputPort,owner);
169 else if(dynamic_cast<YACS::ENGINE::InPropertyPort*>(port))
170 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InPropertyPort,owner);
171 else if(dynamic_cast<YACS::ENGINE::InputPort *>(port))
172 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InputPort,owner);
173 else if(dynamic_cast<YACS::ENGINE::OutputPort *>(port))
174 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutputPort,owner);
175 else if(dynamic_cast<YACS::ENGINE::InputDataStreamPort *>(port))
176 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InputDataStreamPort, owner);
177 else if(dynamic_cast<YACS::ENGINE::OutputDataStreamPort *>(port))
178 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutputDataStreamPort, owner);
179 else if(dynamic_cast<YACS::ENGINE::InPort *>(port))
180 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InPort, owner);
181 else if(dynamic_cast<YACS::ENGINE::OutPort *>(port))
182 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutPort, owner);
183 else if(dynamic_cast<YACS::ENGINE::InGate *>(port))
184 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__InGate, owner);
185 else if(dynamic_cast<YACS::ENGINE::OutGate *>(port))
186 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__OutGate, owner);
188 ob=SWIG_NewPointerObj((void*)port,SWIGTYPE_p_YACS__ENGINE__Port, owner);
193 static PyObject *convertContainer(YACS::ENGINE::Container *cont, int owner=0)
196 return SWIG_NewPointerObj((void*)cont,SWIGTYPE_p_YACS__ENGINE__Container, owner);
197 if(dynamic_cast<YACS::ENGINE::HomogeneousPoolContainer *>(cont))
199 return SWIG_NewPointerObj((void*)dynamic_cast<YACS::ENGINE::HomogeneousPoolContainer *>(cont),SWIGTYPE_p_YACS__ENGINE__HomogeneousPoolContainer, owner);
201 return SWIG_NewPointerObj((void*)cont,SWIGTYPE_p_YACS__ENGINE__Container, owner);
204 //convertFromPyObjVectorOfObj<YACS::ENGINE::SequenceAny *>(pyLi,SWIGTYPE_p_YACS__ENGINE__SequenceAny,"SequenceAny")
206 static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, const char *typeStr, typename std::vector<T>& ret)
209 if(PyList_Check(pyLi))
211 int size=PyList_Size(pyLi);
213 for(int i=0;i<size;i++)
215 PyObject *obj=PyList_GetItem(pyLi,i);
216 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
217 if(!SWIG_IsOK(status))
219 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : list is excepted to contain only " << typeStr << " instances !";
220 throw YACS::Exception(oss.str());
222 T arg=reinterpret_cast< T >(argp);
226 else if(PyTuple_Check(pyLi))
228 int size=PyTuple_Size(pyLi);
230 for(int i=0;i<size;i++)
232 PyObject *obj=PyTuple_GetItem(pyLi,i);
233 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
234 if(!SWIG_IsOK(status))
236 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : tuple is excepted to contain only " << typeStr << " instances !";
237 throw YACS::Exception(oss.str());
239 T arg=reinterpret_cast< T >(argp);
243 else if(SWIG_IsOK(SWIG_ConvertPtr(pyLi,&argp,ty,0|0)))
246 T arg=reinterpret_cast< T >(argp);
250 throw YACS::Exception("convertFromPyObjVectorOfObj : not a list nor a tuple");
255 // ----------------------------------------------------------------------------
257 #if SWIG_VERSION >= 0x010329
258 %template() std::list<int>;
259 %template() std::list<std::string>;
263 %typemap(out) std::list<int>
266 std::list<int>::iterator iL;
268 $result = PyList_New($1.size());
269 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
270 PyList_SetItem($result,i,PyLong_FromLong((*iL)));
273 %typemap(out) std::list<std::string>
276 std::list<std::string>::iterator iL;
278 $result = PyList_New($1.size());
279 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
280 PyList_SetItem($result,i,PyUnicode_FromString((*iL).c_str()));
283 %typemap(in) std::list<std::string>
285 /* Check if input is a list */
286 if (PyList_Check($input))
288 int size = PyList_Size($input);
290 std::list<std::string> myList;
292 for (i = 0; i < size; i++)
294 PyObject *o = PyList_GetItem($input,i);
295 if (PyUnicode_Check(o))
296 $1.push_back(std::string(PyUnicode_AsUTF8(PyList_GetItem($input,i))));
299 PyErr_SetString(PyExc_TypeError,"list must contain strings");
306 PyErr_SetString(PyExc_TypeError,"not a list");
313 // ----------------------------------------------------------------------------
317 %typecheck(SWIG_TYPECHECK_POINTER) YACS::ENGINE::Any*
320 if (SWIG_ConvertPtr($input, (void **) &ptr, $1_descriptor, 0) == 0)
322 else if (PyLong_Check($input))
324 else if(PyFloat_Check($input))
326 else if (PyUnicode_Check($input))
328 else if (PyBytes_Check($input))
334 %typemap(in) YACS::ENGINE::Any* (int is_new_object)
336 if ((SWIG_ConvertPtr($input,(void **) &$1, $1_descriptor,SWIG_POINTER_EXCEPTION)) == 0)
338 // It is an Any : it is converted by SWIG_ConvertPtr $input -> $1
341 else if (PyLong_Check($input))
344 $1=YACS::ENGINE::AtomAny::New((int)PyLong_AsLong($input));
347 else if(PyFloat_Check($input))
350 $1=YACS::ENGINE::AtomAny::New(PyFloat_AsDouble($input));
353 else if(PyUnicode_Check($input))
356 $1=YACS::ENGINE::AtomAny::New(PyUnicode_AsUTF8($input));
359 else if(PyBytes_Check($input))
362 $1=YACS::ENGINE::AtomAny::New(PyBytes_AsString($input));
368 PyErr_SetString(PyExc_TypeError,"not a yacs any or a convertible type");
373 %typemap(directorout) YACS::ENGINE::Any*
375 if ((SWIG_ConvertPtr($1,(void **) &$result, $1_descriptor,SWIG_POINTER_EXCEPTION)) == 0)
377 // It is an Any : it is converted by SWIG_ConvertPtr $input -> $1
379 else if (PyInt_Check($1))
382 $result=YACS::ENGINE::AtomAny::New((int)PyLong_AsLong($1));
384 else if(PyFloat_Check($1))
387 $result=YACS::ENGINE::AtomAny::New(PyFloat_AsDouble($1));
389 else if(PyUnicode_Check($1))
392 $result=YACS::ENGINE::AtomAny::New(PyUnicode_AsUTF8($1));
394 else if(PyBytes_Check($1))
397 $result=YACS::ENGINE::AtomAny::New(PyBytes_AsString($1));
402 PyErr_SetString(PyExc_TypeError,"not a yacs any or a convertible type");
407 %typemap(freearg) YACS::ENGINE::Any *inSample
409 //a reference is taken by the routine called
410 if (!is_new_object$argnum) $1->incrRef();
413 %typemap(freearg) YACS::ENGINE::Any*
415 //no reference taken by the routine called
416 if (is_new_object$argnum) $1->decrRef();
419 %typemap(out) YACS::ENGINE::Any*
421 if(dynamic_cast<YACS::ENGINE::SequenceAny *>($1))
422 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__SequenceAny,$owner);
423 else if(dynamic_cast<YACS::ENGINE::ArrayAny *>($1))
424 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__ArrayAny,$owner);
425 else if(dynamic_cast<YACS::ENGINE::StructAny *>($1))
426 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__StructAny,$owner);
428 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__Any,$owner);
431 %typemap(out) YACS::ENGINE::TypeCode*
433 if(dynamic_cast<YACS::ENGINE::TypeCodeStruct *>($1))
434 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCodeStruct,$owner);
435 else if(dynamic_cast<YACS::ENGINE::TypeCodeSeq *>($1))
436 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCodeSeq,$owner);
437 else if(dynamic_cast<YACS::ENGINE::TypeCodeObjref *>($1))
438 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCodeObjref,$owner);
440 $result=SWIG_NewPointerObj((void*)$1,SWIGTYPE_p_YACS__ENGINE__TypeCode,$owner);
443 %typemap(in) std::list<YACS::ENGINE::TypeCodeObjref*>
445 // Check if input is a list
446 if (PyList_Check($input))
448 int size = PyList_Size($input);
450 std::list<YACS::ENGINE::TypeCodeObjref*> myList;
452 for (i = 0; i < size; i++)
454 PyObject *o = PyList_GetItem($input,i);
455 YACS::ENGINE::TypeCode* temp;
456 if ((SWIG_ConvertPtr(o, (void **) &temp, $descriptor(YACS::ENGINE::TypeCode*),0)) == -1)
458 PyErr_SetString(PyExc_TypeError,"not a YACS::ENGINE::TypeCode*");
463 if(temp->kind() == YACS::ENGINE::Objref)
464 $1.push_back((YACS::ENGINE::TypeCodeObjref*)temp);
467 PyErr_SetString(PyExc_TypeError,"not a YACS::ENGINE::TypeCodeObjref*");
475 PyErr_SetString(PyExc_TypeError,"not a list");
480 %typemap(in) const std::list<YACS::ENGINE::Node *>&
482 if(!PyList_Check($input))
484 PyErr_SetString(PyExc_TypeError,"not a list");
487 $1=new std::list<YACS::ENGINE::Node *>;
488 int size(PyList_Size($input));
489 for(int i=0;i<size;i++)
491 PyObject *o=PyList_GetItem($input,i);
492 YACS::ENGINE::Node *temp(nullptr);
493 if((SWIG_ConvertPtr(o,(void **)&temp, $descriptor(YACS::ENGINE::Node*),0)) == -1)
495 PyErr_SetString(PyExc_TypeError,"not a YACS::ENGINE::TypeCode*");
502 %typemap(out) YACS::ENGINE::Node*
504 $result=convertNode($1,$owner);
507 %typemap(out) YACS::ENGINE::ServiceNode*
509 $result=convertNode($1,$owner);
512 %typemap(out) YACS::ENGINE::InlineNode*
514 $result=convertNode($1,$owner);
517 %typemap(out) YACS::ENGINE::InlineFuncNode*
519 $result=convertNode($1,$owner);
522 %typemap(out) YACS::ENGINE::ComposedNode*
524 $result=convertNode($1,$owner);
527 %typemap(out) YACS::ENGINE::OptimizerLoop*
529 $result=convertNode($1,$owner);
532 %typemap(out) YACS::ENGINE::Proc*
534 $result=convertNode($1,$owner);
537 %typemap(out) std::set<YACS::ENGINE::Node *>
540 std::set<YACS::ENGINE::Node *>::iterator iL;
542 $result = PyList_New($1.size());
544 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
547 PyList_SetItem($result,i,ob);
551 %typemap(out) std::list<YACS::ENGINE::Node *>
554 std::list<YACS::ENGINE::Node *>::iterator iL;
556 $result = PyList_New($1.size());
558 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
561 PyList_SetItem($result,i,ob);
565 %typemap(out) YACS::ENGINE::InputPort*,YACS::ENGINE::OutputPort*,YACS::ENGINE::InPort*,YACS::ENGINE::OutPort*,YACS::ENGINE::InPropertyPort*
567 $result=convertPort($1,$owner);
570 %typemap(out) std::set<YACS::ENGINE::InGate *>
573 std::set<YACS::ENGINE::InGate *>::iterator iL;
574 $result = PyList_New($1.size());
576 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
579 PyList_SetItem($result,i,ob);
583 %typemap(out) std::set<YACS::ENGINE::OutGate *>
586 std::set<YACS::ENGINE::OutGate *>::iterator iL;
587 $result = PyList_New($1.size());
589 for (i=0, iL=$1.begin(); iL!=$1.end(); i++, iL++)
592 PyList_SetItem($result,i,ob);
596 %typemap(out) std::set<YACS::ENGINE::InPort *>
598 std::set<YACS::ENGINE::InPort *>::iterator iL;
599 $result = PyList_New(0);
602 for (iL=$1.begin(); iL!=$1.end(); iL++)
605 status=PyList_Append($result,ob);
609 PyErr_SetString(PyExc_TypeError,"cannot build the inport list");
615 %typemap(out) std::set<YACS::ENGINE::OutPort *>
617 std::set<YACS::ENGINE::OutPort *>::iterator iL;
618 $result = PyList_New(0);
621 for (iL=$1.begin(); iL!=$1.end(); iL++)
624 status=PyList_Append($result,ob);
628 PyErr_SetString(PyExc_TypeError,"cannot build the outport list");
634 %typemap(out) std::list<YACS::ENGINE::OutPort *>
636 std::list<YACS::ENGINE::OutPort *>::const_iterator it;
637 $result = PyTuple_New($1.size());
639 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
640 PyTuple_SetItem($result,i,convertPort(*it));
643 %typemap(out) std::list<YACS::ENGINE::InPort *>
645 std::list<YACS::ENGINE::InPort *>::const_iterator it;
646 $result = PyTuple_New($1.size());
648 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
649 PyTuple_SetItem($result,i,convertPort(*it));
652 %typemap(out) std::list<YACS::ENGINE::OutputPort *>
654 std::list<YACS::ENGINE::OutputPort *>::const_iterator it;
655 $result = PyTuple_New($1.size());
657 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
658 PyTuple_SetItem($result,i,convertPort(*it));
661 %typemap(out) std::list<YACS::ENGINE::InputPort *>
663 std::list<YACS::ENGINE::InputPort *>::const_iterator it;
664 $result = PyTuple_New($1.size());
666 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
667 PyTuple_SetItem($result,i,convertPort(*it));
670 %typemap(out) std::list<YACS::ENGINE::InPropertyPort*>
672 std::list<YACS::ENGINE::InPropertyPort *>::const_iterator it;
673 $result = PyTuple_New($1.size());
675 for (it = $1.begin(); it != $1.end(); ++it, ++i) {
676 PyTuple_SetItem($result,i,convertPort(*it));
680 %typemap(out) std::vector< std::list<YACS::ENGINE::Node *> >
682 std::vector< std::list<YACS::ENGINE::Node *> >::const_iterator it;
683 $result = PyList_New($1.size());
685 for (it = $1.begin(); it != $1.end(); ++it, ++i)
687 const std::list<YACS::ENGINE::Node *>& elt(*it);
688 PyObject *tmp(PyList_New(elt.size()));
690 for (auto it2=elt.begin() ; it2!= elt.end() ; ++it2, ++j)
691 PyList_SetItem(tmp,j,convertNode(*it2));
692 PyList_SetItem($result,i,tmp);
698 // ----------------------------------------------------------------------------
703 // a general exception handler
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");
740 // a specific exception handler = generic + release lock
741 %define PYEXCEPTION(name)
745 InterpreterUnlocker _l;
748 catch(YACS::Exception& _e)
750 PyErr_SetString(PyExc_ValueError,_e.what());
753 catch(std::invalid_argument& _e)
755 PyErr_SetString(PyExc_IOError ,_e.what());
757 } catch (std::domain_error& e) {
758 SWIG_exception(SWIG_ValueError, e.what() );
759 } catch (std::overflow_error& e) {
760 SWIG_exception(SWIG_OverflowError, e.what() );
761 } catch (std::out_of_range& e) {
762 PyErr_SetString(PyExc_KeyError,e.what());
764 } catch (std::length_error& e) {
765 SWIG_exception(SWIG_IndexError, e.what() );
766 } catch (std::runtime_error& e) {
767 SWIG_exception(SWIG_RuntimeError, e.what() );
769 OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
770 catch (std::exception& e) {
771 SWIG_exception(SWIG_SystemError, e.what() );
775 SWIG_exception(SWIG_UnknownError, "Unknown exception");
780 %define EXCEPTION(name)
786 catch(YACS::Exception& _e)
788 PyErr_SetString(PyExc_ValueError,_e.what());
791 catch(std::invalid_argument& _e)
793 PyErr_SetString(PyExc_IOError ,_e.what());
795 } catch (std::domain_error& e) {
796 SWIG_exception(SWIG_ValueError, e.what() );
797 } catch (std::overflow_error& e) {
798 SWIG_exception(SWIG_OverflowError, e.what() );
799 } catch (std::out_of_range& e) {
800 PyErr_SetString(PyExc_KeyError,e.what());
802 } catch (std::length_error& e) {
803 SWIG_exception(SWIG_IndexError, e.what() );
804 } catch (std::runtime_error& e) {
805 SWIG_exception(SWIG_RuntimeError, e.what() );
807 OMNIPY_CATCH_AND_HANDLE_SYSTEM_EXCEPTIONS
808 catch (std::exception& e) {
809 SWIG_exception(SWIG_SystemError, e.what() );
813 SWIG_exception(SWIG_UnknownError, "Unknown exception");
818 * End of Exception section
821 // ----------------------------------------------------------------------------
826 //Release ownership : transfer it to C++
827 %apply SWIGTYPE *DISOWN { YACS::ENGINE::CatalogLoader* factory};
828 %apply SWIGTYPE *DISOWN { YACS::ENGINE::Node *DISOWNnode };
829 %apply SWIGTYPE *DISOWN { Node *DISOWNnode };
831 * End of ownership section
835 * Reference counting section
836 * reference counted objects are created with a count of 1 so we do not incrRef them on wrapping creation
837 * we only decrRef them on wrapping destruction.
838 * Do not forget to declare them new (%newobject) when they are not returned from a constructor
839 * unless they will not be decrRef on wrapping destruction
841 %feature("ref") YACS::ENGINE::RefCounter ""
842 %feature("unref") YACS::ENGINE::RefCounter "$this->decrRef();"
844 // Unfortunately, class ComponentInstance inherits from RefCounter AND PropertyInterface. Thus the ref and
845 // unref features are ambiguous and with swig 2.0.7 at least, we must re-specify those features for class
846 // ComponentInstance unless the instances are destroyed when the Swig object is unref'ed.
847 %feature("ref") YACS::ENGINE::ComponentInstance ""
848 %feature("unref") YACS::ENGINE::ComponentInstance "$this->decrRef();"
850 * End of Reference counting section
853 // ----------------------------------------------------------------------------
858 template <> struct traits_from<YACS::ENGINE::InPort *> {
859 static PyObject *from(YACS::ENGINE::InPort* val){
860 return convertPort(val);
863 template <> struct traits_from<YACS::ENGINE::OutPort *> {
864 static PyObject *from(YACS::ENGINE::OutPort* val) {
865 return convertPort(val);
868 template <> struct traits_from<YACS::ENGINE::InputPort *> {
869 static PyObject *from(YACS::ENGINE::InPort* val){
870 return convertPort(val);
873 template <> struct traits_from<YACS::ENGINE::OutputPort *> {
874 static PyObject *from(YACS::ENGINE::OutPort* val) {
875 return convertPort(val);
882 // ----------------------------------------------------------------------------
884 %include "IteratorPy3.hxx"
885 %include "exception.i"
887 %define REFCOUNT_TEMPLATE(tname, T)
889 This macro is a special wrapping for map with value type which derives from RefCounter.
890 To overload standard SWIG wrapping we define a full specialization of std::map
891 with %extend for 5 basic methods : getitem, setitem, delitem, keys and iter.
892 We also provide an iterator and %extend with the __next__ method : required in python 3.
893 (see https://docs.python.org/3/library/stdtypes.html#iterator-types)
894 Then we complete the interface by deriving the shadow wrapper from
895 the python mixin class (UserDict.DictMixin / collections.MutableMapping with Python 3).
896 Do not forget to declare the new shadow class to SWIG with tname_swigregister(tname).
897 Objects returned by __getitem__ are declared new (%newobject) so that when destroyed they
898 call decrRef (see feature("unref") for RefCounter).
901 %exception IteratorPy3<T>::__next__
905 $action // calls %extend function next() below
907 catch (StopIteratorPy3<T>)
909 PyErr_SetString(PyExc_StopIteration, "End of iterator");
914 %extend IteratorPy3<T>
916 // std::pair<const std::string,T*>& __next__()
917 std::string __next__()
919 if ($self->cur != $self->end)
921 // dereference the iterator and return reference to the object,
922 // after that it increments the iterator
923 //return *self->cur++;
924 std::string key= self->cur->first;
928 throw StopIteratorPy3<T>();
933 class std::map<std::string,T*>
938 void __setitem__(const std::string& name, T* c)
940 std::map<std::string, T* >::iterator i = self->find(name);
941 if (i != self->end())
945 i->second->decrRef();
950 T* __getitem__(std::string name)
952 std::map<std::string, T* >::iterator i = self->find(name);
953 if (i != self->end())
955 i->second->incrRef();
959 throw std::out_of_range("key not found");
961 void __delitem__(std::string name)
963 std::map<std::string, T* >::iterator i = self->find(name);
964 if (i != self->end())
966 i->second->decrRef();
970 throw std::out_of_range("key not found");
974 int pysize = self->size();
975 PyObject* keyList = PyList_New(pysize);
976 std::map<std::string, T* >::const_iterator i = self->begin();
977 for (int j = 0; j < pysize; ++i, ++j)
979 PyList_SET_ITEM(keyList, j, PyUnicode_FromString(i->first.c_str()));
983 IteratorPy3<T> __iter__()
985 // return a constructed IteratorPy3 object
986 return IteratorPy3<T>($self->begin(), $self->end());
990 int pysize = self->size();
995 %newobject std::map<std::string,T* >::__getitem__;
996 %newobject std::map<std::string,T* >::__iter__;
997 %template(tname##it) IteratorPy3<T >;
998 %template() std::pair<std::string, T* >;
999 %template(tname) std::map<std::string, T* >;
1002 from collections import MutableMapping
1003 class tname(tname,MutableMapping):pass
1004 tname##_swigregister(tname)