1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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
32 #include "Batch_TypeMismatchException.hxx"
33 #include "Batch_ListIsFullException.hxx"
34 #include "Batch_InvalidArgumentException.hxx"
35 #include "Batch_PyVersatile.hxx"
39 // Constructeur a partir d'un objet Versatile
40 PyVersatile::PyVersatile(const Versatile & V) : Versatile(V)
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()
51 PyObject * _PyO = const_cast<PyObject *>(PyO);
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);
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) )
69 PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
73 } else if (PyString_Check(_PyO)) { // c'est une string
74 const char * s = PyString_AsString(_PyO);
75 Versatile V = string(s);
78 } else if (PyInt_Check(_PyO)) { // c'est un int
79 *this = PyInt_AsLong(_PyO);
82 PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
88 // Conversion de type vers un PyObject
89 PyVersatile::operator PyObject *() const
93 if (_maxsize != 1) { // une liste
95 for(Versatile::const_iterator it=begin(); it!=end(); it++) {
100 switch (_discriminator) {
102 // PyList_Append(obj, PyInt_FromLong(* static_cast<BoolType *>(*it)));
106 // *ch = * static_cast<CharType *>(*it);
107 // PyList_Append(obj, PyString_FromString(ch));
111 // PyList_Append(obj, PyInt_FromLong(* static_cast<IntType *>(*it)));
115 PyList_Append(obj, PyInt_FromLong(* static_cast<LongType *>(*it)));
119 st = * static_cast<StringType *>(*it);
120 PyList_Append(obj, PyString_FromString(st.c_str()));
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() ));
133 PyList_Append(obj, Py_None);
139 } else { // un scalaire
144 switch (_discriminator) {
146 // obj = PyInt_FromLong(* static_cast<BoolType *>(front()));
150 // *ch = * static_cast<CharType *>(front());
151 // obj = PyString_FromString(ch);
155 // obj = PyInt_FromLong(* static_cast<IntType *>(front()));
159 obj = PyInt_FromLong(* static_cast<LongType *>(front()));
163 st = * static_cast<StringType *>(front());
164 obj = PyString_FromString(st.c_str());
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() );
187 // Operateur d'affectation a partir d'un objet Versatile
188 PyVersatile & PyVersatile::operator =(const Versatile & V)
190 Versatile * me = this;