Salome HOME
Added constants to represent job status uniformly. Fixed some errors under Windows...
[tools/libbatch.git] / src / Python / libBatch_Swig_typemap.i
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * _typemap.i : 
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Date   : Septembre 2003
27  * Projet : SALOME 2
28  *
29  */
30
31 %{
32 #include <string>
33 #include <list>
34 #include <map>
35 #include "Batch_Parametre.hxx"
36 #include "Batch_PyVersatile.hxx"
37 #include "Batch_JobId.hxx"
38 #include "Batch_FactBatchManager.hxx"
39 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
40 typedef int Py_ssize_t;
41 #define PY_SSIZE_T_MAX INT_MAX
42 #define PY_SSIZE_T_MIN INT_MIN
43 #endif
44 %}
45
46 # // supprime toutes les definitions par defaut => sert au debug
47 # %typemap(in) SWIGTYPE ;
48
49
50 # // construction d'un dictionnaire Python a partir d'un objet BatchManagerCatalog C++
51 %typemap(out) std::map<std::string, Batch::FactBatchManager *> *
52 {
53   $result = PyDict_New();
54
55   // on itere sur toutes les clefs de la map
56   for(std::map<std::string, Batch::FactBatchManager *>::const_iterator it=(* $1).begin(); it!=(* $1).end(); it++) {
57     std::string key = (*it).first;
58     PyObject * obj  = SWIG_NewPointerObj((void *) (*it).second, SWIGTYPE_p_Batch__FactBatchManager, 0);
59     PyDict_SetItem($result, PyString_FromString(key.c_str()), obj);
60   }
61 }
62
63 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) Batch::Parametre
64 {
65   $1 = PyDict_Check($input)? 1 : 0;
66 }
67
68 # // construction d'un dictionnaire Python a partir d'un objet Parametre C++
69 %typemap(out) Batch::Parametre
70 {
71   $result = PyDict_New();
72
73   // on itere sur toutes les clefs de la map, et on passe par la classe PyVersatile
74         // qui convertit un Versatile en PyObject et vice versa
75   for(Batch::Parametre::const_iterator it=$1.begin(); it!=$1.end(); it++) {
76     std::string key = (*it).first;
77     Batch::PyVersatile PyV = (*it).second;
78     PyDict_SetItem($result, PyString_FromString(key.c_str()), PyV);
79   }
80 }
81
82
83 # // construction d'un objet Parametre C++ a partir d'un dictionnaire Python
84 %typemap(in) Batch::Parametre & (Batch::Parametre PM)
85 {
86   if (!PyDict_Check($input)) {
87     PyErr_SetString(PyExc_ValueError,"Expected a dictionnary");
88     return NULL;
89   }
90
91   try { 
92   // on itere sur toutes les clefs du dictionnaire, et on passe par la classe PyVersatile
93         // qui convertit un Versatile en PyObject et vice versa
94         PyObject *key, *value;
95         Py_ssize_t pos = 0;
96         while (PyDict_Next($input, &pos, &key, &value)) {
97                 std::string mk = PyString_AsString(key);
98                 Batch::PyVersatile PyV = value;
99                 PyV.setName(mk);
100                 PM[mk] = PyV;
101         }
102
103   $1 = &PM; // $1 est une reference donc on lui passe une adresse
104   }
105   catch (Batch::GenericException & ex) {
106       std::string msg = ex.type + " : " + ex.message;
107       PyErr_SetString(PyExc_RuntimeWarning, msg.c_str());
108       return NULL;
109   }
110   catch (...) {
111       PyErr_SetString(PyExc_RuntimeWarning, "unknown exception");
112       return NULL;
113   }
114 }
115
116
117 # // construction d'un objet Parametre C++ a partir d'un dictionnaire Python
118 %typemap(in) Batch::Parametre (Batch::Parametre PM)
119 {
120   if (!PyDict_Check($input)) {
121     PyErr_SetString(PyExc_ValueError,"Expected a dictionnary");
122     return NULL;
123   }
124
125   try {
126   // on itere sur toutes les clefs du dictionnaire, et on passe par la classe PyVersatile
127         // qui convertit un Versatile en PyObject et vice versa
128         PyObject *key, *value;
129         Py_ssize_t pos = 0;
130         while (PyDict_Next($input, &pos, &key, &value)) {
131                 std::string mk = PyString_AsString(key);
132                 Batch::PyVersatile PyV = value;
133                 PyV.setName(mk);
134                 PM[mk] = PyV;
135         }
136
137   $1 = PM;
138   }
139   catch (Batch::GenericException & ex) {
140       std::string msg = ex.type + " : " + ex.message;
141       PyErr_SetString(PyExc_RuntimeWarning, msg.c_str());
142       return NULL;
143   }
144   catch (...) {
145       PyErr_SetString(PyExc_RuntimeWarning, "unknown exception");
146       return NULL;
147   }
148 }
149
150 %typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) Batch::Environnement
151 {
152   $1 = PyDict_Check($input)? 1 : 0;
153 }
154
155 # // construction d'un dictionnaire Python a partir d'un objet Environnement C++
156 %typemap(out) Batch::Environnement
157 {
158   $result = PyDict_New();
159
160   // on itere sur toutes les clefs de la map
161   for(Batch::Environnement::const_iterator it=$1.begin(); it!=$1.end(); it++) {
162     std::string key = (*it).first;
163     std::string val = (*it).second;
164     PyDict_SetItem($result, 
165                    PyString_FromString(key.c_str()),
166                    PyString_FromString(val.c_str()));
167   }
168 }
169
170
171 # // construction d'un objet Environnement C++ a partir d'un dictionnaire Python
172 %typemap(in) Batch::Environnement & (Batch::Environnement E)
173 {
174   if (!PyDict_Check($input)) {
175     PyErr_SetString(PyExc_ValueError,"Expected a dictionnary");
176     return NULL;
177   }
178
179         // on itere sur toutes les clefs du dictionnaire
180         PyObject *key, *value;
181         Py_ssize_t pos = 0;
182         while (PyDict_Next($input, &pos, &key, &value)) {
183                 std::string mk  = PyString_AsString(key);
184                 std::string val = PyString_AsString(value);
185                 E[mk] = val;
186         }
187   
188   $1 = &E; // $1 est une reference donc on lui passe une adresse
189 }
190
191
192
193 # // construction d'un objet Environnement C++ a partir d'un dictionnaire Python
194 %typemap(in) Batch::Environnement (Batch::Environnement E)
195 {
196   if (!PyDict_Check($input)) {
197     PyErr_SetString(PyExc_ValueError,"Expected a dictionnary");
198     return NULL;
199   }
200
201         // on itere sur toutes les clefs du dictionnaire
202         PyObject *key, *value;
203         Py_ssize_t pos = 0;
204         while (PyDict_Next($input, &pos, &key, &value)) {
205                 std::string mk  = PyString_AsString(key);
206                 std::string val = PyString_AsString(value);
207                 E[mk] = val;
208         }
209   
210   $1 = E;
211 }
212