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