1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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
33 #include "Batch_TypeMismatchException.hxx"
34 #include "Batch_ListIsFullException.hxx"
35 #include "Batch_InvalidArgumentException.hxx"
36 #include "Batch_PyVersatile.hxx"
43 // Constructeur a partir d'un objet Versatile
44 PyVersatile::PyVersatile(const Versatile & V) : Versatile(V)
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()
55 PyObject * _PyO = const_cast<PyObject *>(PyO);
57 if (PyList_Check(_PyO)) { // c'est une liste
58 _maxsize = PyList_Size(_PyO);
59 for(int i=0; i<_maxsize; i++) {
60 PyObject * val = PyList_GetItem(_PyO, i);
61 if (PyString_Check(val)) {
62 *this += PyString_AsString(val);
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) )
73 PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
77 } else if (PyString_Check(_PyO)) { // c'est une string
78 const char * s = PyString_AsString(_PyO);
79 Versatile V = string(s);
82 } else if (PyInt_Check(_PyO)) { // c'est un int
83 *this = PyInt_AsLong(_PyO);
86 PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
92 // Conversion de type vers un PyObject
93 PyVersatile::operator PyObject *() const
97 if (_maxsize != 1) { // une liste
99 for(Versatile::const_iterator it=begin(); it!=end(); it++) {
100 // char ch[2] = {0, 0};
104 switch (_discriminator) {
106 // PyList_Append(obj, PyInt_FromLong(* static_cast<BoolType *>(*it)));
110 // *ch = * static_cast<CharType *>(*it);
111 // PyList_Append(obj, PyString_FromString(ch));
115 // PyList_Append(obj, PyInt_FromLong(* static_cast<IntType *>(*it)));
119 PyList_Append(obj, PyInt_FromLong(* static_cast<LongType *>(*it)));
123 st = * static_cast<StringType *>(*it);
124 PyList_Append(obj, PyString_FromString(st.c_str()));
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() ));
137 PyList_Append(obj, Py_None);
143 } else { // un scalaire
144 // char ch[2] = {0, 0};
148 switch (_discriminator) {
150 // obj = PyInt_FromLong(* static_cast<BoolType *>(front()));
154 // *ch = * static_cast<CharType *>(front());
155 // obj = PyString_FromString(ch);
159 // obj = PyInt_FromLong(* static_cast<IntType *>(front()));
163 obj = PyInt_FromLong(* static_cast<LongType *>(front()));
167 st = * static_cast<StringType *>(front());
168 obj = PyString_FromString(st.c_str());
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() );
191 // Operateur d'affectation a partir d'un objet Versatile
192 PyVersatile & PyVersatile::operator =(const Versatile & V)
194 Versatile * me = this;