Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / engine_swig / pypilot.i
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 %define PYPILOTDOCSTRING
21 "This module provides 2 classes PyObserver and PyCatalogLoader that can be subclassed in Python (uses director feature)"
22 %enddef
23
24 %module(directors="1",docstring=PYPILOTDOCSTRING) pypilot
25
26 %feature("autodoc", "1");
27
28 %include "engtypemaps.i"
29
30 %{
31 #include "Dispatcher.hxx"
32 #include "Catalog.hxx"
33 #include "TypeCode.hxx"
34
35 namespace YACS
36 {
37   namespace ENGINE
38   {
39     class PyObserver:public Observer
40     {
41     public:
42       virtual void notifyObserver(Node* object,const std::string& event)
43       {
44         //YACS engine processing is pure C++ : need to take the GIL
45         PyGILState_STATE gstate = PyGILState_Ensure();
46
47         try
48           {
49             pynotify(object,event);
50             if (PyErr_Occurred()) 
51               {
52                 // print the exception and clear it
53                 PyErr_Print();
54                 //do not propagate the python exception (ignore it)
55               }
56           }
57         catch (...)
58           {
59             //ignore this exception:probably Swig::DirectorException
60             if (PyErr_Occurred()) 
61                 PyErr_Print();
62           }
63         PyGILState_Release(gstate);
64       }
65       virtual void pynotify(Node* object,const std::string& event)
66       {
67         //To inherit (Python class)
68         std::cerr << "pynotify " << event << object << std::endl;
69       }
70     };
71
72     class PyCatalogLoader:public CatalogLoader
73     {
74     protected:
75       virtual CatalogLoader* newLoader(const std::string& path) {return 0;}
76     };
77   }
78 }
79
80 %}
81
82 %init
83 %{
84   // init section
85 #ifdef OMNIORB
86   PyObject* omnipy = PyImport_ImportModule((char*)"_omnipy");
87   if (!omnipy)
88   {
89     PyErr_SetString(PyExc_ImportError,(char*)"Cannot import _omnipy");
90     return;
91   }
92   PyObject* pyapi = PyObject_GetAttrString(omnipy, (char*)"API");
93   api = (omniORBPYAPI*)PyCObject_AsVoidPtr(pyapi);
94   Py_DECREF(pyapi);
95 #endif
96 %}
97
98 /*
99  * Director section : classes that can be subclassed in python
100  */
101 %feature("director") YACS::ENGINE::PyObserver;
102 %feature("nodirector") YACS::ENGINE::PyObserver::notifyObserver;
103 %feature("director") YACS::ENGINE::PyCatalogLoader;
104 %feature("nodirector") YACS::ENGINE::PyCatalogLoader::newLoader;
105
106 /*
107 %feature("director:except") {
108   if ($error != NULL) {
109     // print the exception and clear it
110     PyErr_Print();
111     //do not propagate the python exception (ignore it)
112     //throw Swig::DirectorMethodException();
113   }
114 }
115 */
116 /*
117  * End of Director section
118  */
119
120 %types(YACS::ENGINE::Node *,YACS::ENGINE::Proc *,YACS::ENGINE::ForLoop *,YACS::ENGINE::Bloc *, YACS::ENGINE::Switch *);
121 %types(YACS::ENGINE::WhileLoop *,YACS::ENGINE::ForEachLoop *,YACS::ENGINE::ComposedNode *,YACS::ENGINE::InlineNode *);
122 %types(YACS::ENGINE::InlineFuncNode *,YACS::ENGINE::ServiceInlineNode *,YACS::ENGINE::ServiceNode *);
123 %types(YACS::ENGINE::ElementaryNode *);
124 %types(YACS::ENGINE::Container *, YACS::ENGINE::HomogeneousPoolContainer *);
125
126 %types(YACS::ENGINE::InputPort *,YACS::ENGINE::OutputPort *,YACS::ENGINE::InPropertyPort *);
127 %types(YACS::ENGINE::InputDataStreamPort *,YACS::ENGINE::OutputDataStreamPort *);
128 %types(YACS::ENGINE::InGate *,YACS::ENGINE::OutGate *);
129 %types(YACS::ENGINE::InPort *,YACS::ENGINE::OutPort *,YACS::ENGINE::Port *);
130
131 %types(YACS::ENGINE::Observer *);
132
133 %import "pilot.i"
134 %import "Dispatcher.hxx"
135 %import "Catalog.hxx"
136
137 namespace YACS
138 {
139   namespace ENGINE
140   {
141     class PyObserver:public Observer
142     {
143     public:
144       virtual void pynotify(Node* object,const std::string& event);
145     };
146
147     class PyCatalogLoader:public CatalogLoader
148     {
149     protected:
150       virtual CatalogLoader* newLoader(const std::string& path);
151     };
152   }
153 }
154