Salome HOME
Copyrights update
[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/
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 <string>
31 #include <Python.h>
32 #include "Batch_TypeMismatchException.hxx"
33 #include "Batch_ListIsFullException.hxx"
34 #include "Batch_InvalidArgumentException.hxx"
35 #include "Batch_PyVersatile.hxx"
36
37 namespace Batch {
38
39   // Constructeur a partir d'un objet Versatile
40   PyVersatile::PyVersatile(const Versatile & V) : Versatile(V)
41   {
42     // Nothing to do
43   }
44
45
46   // Constructeur a partir d'un PyObject
47   // Les objets autorises sont les strings et les ints,
48   // ainsi que les listes de strings
49   PyVersatile::PyVersatile(const PyObject * PyO) throw(TypeMismatchException, ListIsFullException, InvalidArgumentException) : Versatile()
50   {
51     PyObject * _PyO = const_cast<PyObject *>(PyO);
52
53     if (PyList_Check(_PyO)) { // c'est une liste
54       _maxsize = PyList_Size(_PyO);
55       for(int i=0; i<_maxsize; i++) {
56         PyObject * val = PyList_GetItem(_PyO, i);
57         if (PyString_Check(val)) {
58           *this += PyString_AsString(val);
59                                         
60         } else if (PyTuple_Check(val) && 
61                    (PyTuple_Size(val) == 2) &&
62                    PyString_Check( PyTuple_GetItem(val,0) ) && 
63                    PyString_Check( PyTuple_GetItem(val,1) )   ) {
64           *this += Couple( PyString_AsString( PyTuple_GetItem(val,0) ),
65                            PyString_AsString( PyTuple_GetItem(val,1) )
66                            );
67                                         
68         } else {
69           PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
70         }
71       }
72
73     } else if (PyString_Check(_PyO)) { // c'est une string
74       const char * s = PyString_AsString(_PyO);
75       Versatile V = string(s);
76       *this = V;
77       
78     } else if (PyInt_Check(_PyO)) { // c'est un int
79       *this = PyInt_AsLong(_PyO);
80
81     } else { // erreur
82       PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
83     }
84   }
85
86
87
88   // Conversion de type vers un PyObject
89   PyVersatile::operator PyObject *() const
90   {
91     PyObject * obj;
92
93     if (_maxsize != 1) { // une liste
94       obj = PyList_New(0);
95       for(Versatile::const_iterator it=begin(); it!=end(); it++) {
96         char ch[2] = {0, 0};
97         string st;
98         Couple cp;
99 //      PyObject * tuple;
100         switch (_discriminator) {
101           //    case BOOL:
102           //      PyList_Append(obj, PyInt_FromLong(* static_cast<BoolType *>(*it)));
103           //      break;
104
105           //    case CHAR:
106           //      *ch = * static_cast<CharType *>(*it);
107           //      PyList_Append(obj, PyString_FromString(ch));
108           //      break;
109
110           //    case INT:
111           //      PyList_Append(obj, PyInt_FromLong(* static_cast<IntType *>(*it)));
112           //      break;
113
114         case LONG:
115           PyList_Append(obj, PyInt_FromLong(* static_cast<LongType *>(*it)));
116           break;
117
118         case STRING:
119           st = * static_cast<StringType *>(*it);
120           PyList_Append(obj, PyString_FromString(st.c_str()));
121           break;
122
123         case COUPLE:
124           cp = * static_cast<CoupleType *>(*it);
125 //        tuple = PyTuple_New(2);
126 //        PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str()  ) );
127 //        PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
128 //        PyList_Append(obj, tuple);
129           PyList_Append(obj, Py_BuildValue("(ss)", cp.getLocal().c_str(), cp.getRemote().c_str() ));
130           break;
131
132         case UNDEFINED:
133           PyList_Append(obj, Py_None);
134           break;
135         }
136
137       }
138
139     } else { // un scalaire
140       char ch[2] = {0, 0};
141       string st;
142       Couple cp;
143 //       PyObject * tuple;
144       switch (_discriminator) {
145         //       case BOOL:
146         //      obj = PyInt_FromLong(* static_cast<BoolType *>(front()));
147         //      break;
148
149         //       case CHAR:
150         //      *ch = * static_cast<CharType *>(front());
151         //      obj = PyString_FromString(ch);
152         //      break;
153
154         //       case INT:
155         //      obj = PyInt_FromLong(* static_cast<IntType *>(front()));
156         //      break;
157
158       case LONG:
159         obj = PyInt_FromLong(* static_cast<LongType *>(front()));
160         break;
161
162       case STRING:
163         st = * static_cast<StringType *>(front());
164         obj = PyString_FromString(st.c_str());
165         break;
166
167       case COUPLE:
168         cp = * static_cast<CoupleType *>(front());
169 //      tuple = PyTuple_New(2);
170 //      PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str()  ) );
171 //      PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
172 //      obj = PyList_New(0);
173 //      PyList_Append(obj, tuple);
174         obj = Py_BuildValue("[(ss)]", cp.getLocal().c_str(), cp.getRemote().c_str() );
175         break;
176
177       case UNDEFINED:
178         obj = Py_None;
179         break;
180       }
181     }
182
183     return obj;
184   }
185
186
187   // Operateur d'affectation a partir d'un objet Versatile
188   PyVersatile & PyVersatile::operator =(const Versatile & V)
189   {
190     Versatile * me = this;
191     *me = V;
192     return *this;
193   }
194
195 }
196
197
198 // COMMENTS