]> SALOME platform Git repositories - modules/kernel.git/blob - src/Batch/Batch_PyVersatile.cxx
Salome HOME
BUG 0020024: a --gdb-session option to runSalome ...
[modules/kernel.git] / src / Batch / Batch_PyVersatile.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21  * PyVersatile.cxx : 
22  *
23  * Auteur : Ivan DUTKA-MALEN - EDF R&D
24  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
25  * Date   : Mon Oct 13 12:01:12 2003
26  * Projet : Salome 2
27  *
28  */
29
30 #include <Python.h>
31 #include "Batch_TypeMismatchException.hxx"
32 #include "Batch_ListIsFullException.hxx"
33 #include "Batch_InvalidArgumentException.hxx"
34 #include "Batch_PyVersatile.hxx"
35 #include <string>
36
37 using namespace std;
38
39 namespace Batch {
40
41   // Constructeur a partir d'un objet Versatile
42   PyVersatile::PyVersatile(const Versatile & V) : Versatile(V)
43   {
44     // Nothing to do
45   }
46
47
48   // Constructeur a partir d'un PyObject
49   // Les objets autorises sont les strings et les ints,
50   // ainsi que les listes de strings
51   PyVersatile::PyVersatile(const PyObject * PyO) throw(TypeMismatchException, ListIsFullException, InvalidArgumentException) : Versatile()
52   {
53     PyObject * _PyO = const_cast<PyObject *>(PyO);
54
55     if (PyList_Check(_PyO)) { // c'est une liste
56       _maxsize = PyList_Size(_PyO);
57       for(int i=0; i<_maxsize; i++) {
58         PyObject * val = PyList_GetItem(_PyO, i);
59         if (PyString_Check(val)) {
60           *this += PyString_AsString(val);
61                                         
62         } else if (PyTuple_Check(val) && 
63                    (PyTuple_Size(val) == 2) &&
64                    PyString_Check( PyTuple_GetItem(val,0) ) && 
65                    PyString_Check( PyTuple_GetItem(val,1) )   ) {
66           *this += Couple( PyString_AsString( PyTuple_GetItem(val,0) ),
67                            PyString_AsString( PyTuple_GetItem(val,1) )
68                            );
69                                         
70         } else {
71           PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
72         }
73       }
74
75     } else if (PyString_Check(_PyO)) { // c'est une string
76       const char * s = PyString_AsString(_PyO);
77       Versatile V = string(s);
78       *this = V;
79       
80     } else if (PyInt_Check(_PyO)) { // c'est un int
81       *this = PyInt_AsLong(_PyO);
82
83     } else { // erreur
84       PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
85     }
86   }
87
88
89
90   // Conversion de type vers un PyObject
91   PyVersatile::operator PyObject *() const
92   {
93     PyObject * obj;
94
95     if (_maxsize != 1) { // une liste
96       obj = PyList_New(0);
97       for(Versatile::const_iterator it=begin(); it!=end(); it++) {
98 //      char ch[2] = {0, 0};
99         string st;
100         Couple cp;
101 //      PyObject * tuple;
102         switch (_discriminator) {
103           //    case BOOL:
104           //      PyList_Append(obj, PyInt_FromLong(* static_cast<BoolType *>(*it)));
105           //      break;
106
107           //    case CHAR:
108           //      *ch = * static_cast<CharType *>(*it);
109           //      PyList_Append(obj, PyString_FromString(ch));
110           //      break;
111
112           //    case INT:
113           //      PyList_Append(obj, PyInt_FromLong(* static_cast<IntType *>(*it)));
114           //      break;
115
116         case LONG:
117           PyList_Append(obj, PyInt_FromLong(* static_cast<LongType *>(*it)));
118           break;
119
120         case STRING:
121           st = * static_cast<StringType *>(*it);
122           PyList_Append(obj, PyString_FromString(st.c_str()));
123           break;
124
125         case COUPLE:
126           cp = * static_cast<CoupleType *>(*it);
127 //        tuple = PyTuple_New(2);
128 //        PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str()  ) );
129 //        PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
130 //        PyList_Append(obj, tuple);
131           PyList_Append(obj, Py_BuildValue("(ss)", cp.getLocal().c_str(), cp.getRemote().c_str() ));
132           break;
133
134         case UNDEFINED:
135           PyList_Append(obj, Py_None);
136           break;
137         }
138
139       }
140
141     } else { // un scalaire
142 //      char ch[2] = {0, 0};
143       string st;
144       Couple cp;
145 //       PyObject * tuple;
146       switch (_discriminator) {
147         //       case BOOL:
148         //      obj = PyInt_FromLong(* static_cast<BoolType *>(front()));
149         //      break;
150
151         //       case CHAR:
152         //      *ch = * static_cast<CharType *>(front());
153         //      obj = PyString_FromString(ch);
154         //      break;
155
156         //       case INT:
157         //      obj = PyInt_FromLong(* static_cast<IntType *>(front()));
158         //      break;
159
160       case LONG:
161         obj = PyInt_FromLong(* static_cast<LongType *>(front()));
162         break;
163
164       case STRING:
165         st = * static_cast<StringType *>(front());
166         obj = PyString_FromString(st.c_str());
167         break;
168
169       case COUPLE:
170         cp = * static_cast<CoupleType *>(front());
171 //      tuple = PyTuple_New(2);
172 //      PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str()  ) );
173 //      PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
174 //      obj = PyList_New(0);
175 //      PyList_Append(obj, tuple);
176         obj = Py_BuildValue("[(ss)]", cp.getLocal().c_str(), cp.getRemote().c_str() );
177         break;
178
179       case UNDEFINED:
180         obj = Py_None;
181         break;
182       }
183     }
184
185     return obj;
186   }
187
188
189   // Operateur d'affectation a partir d'un objet Versatile
190   PyVersatile & PyVersatile::operator =(const Versatile & V)
191   {
192     Versatile * me = this;
193     *me = V;
194     return *this;
195   }
196
197 }
198
199
200 // COMMENTS