4 * Auteur : Ivan DUTKA-MALEN - EDF R&D
5 * Mail : mailto:ivan.dutka-malen@der.edf.fr
6 * Date : Mon Oct 13 12:01:12 2003
13 #include "Batch_TypeMismatchException.hxx"
14 #include "Batch_ListIsFullException.hxx"
15 #include "Batch_InvalidArgumentException.hxx"
16 #include "Batch_PyVersatile.hxx"
20 // Constructeur a partir d'un objet Versatile
21 PyVersatile::PyVersatile(const Versatile & V) : Versatile(V)
27 // Constructeur a partir d'un PyObject
28 // Les objets autorises sont les strings et les ints,
29 // ainsi que les listes de strings
30 PyVersatile::PyVersatile(const PyObject * PyO) throw(TypeMismatchException, ListIsFullException, InvalidArgumentException) : Versatile()
32 PyObject * _PyO = const_cast<PyObject *>(PyO);
34 if (PyList_Check(_PyO)) { // c'est une liste
35 _maxsize = PyList_Size(_PyO);
36 for(int i=0; i<_maxsize; i++) {
37 PyObject * val = PyList_GetItem(_PyO, i);
38 if (PyString_Check(val)) {
39 *this += PyString_AsString(val);
41 } else if (PyTuple_Check(val) &&
42 (PyTuple_Size(val) == 2) &&
43 PyString_Check( PyTuple_GetItem(val,0) ) &&
44 PyString_Check( PyTuple_GetItem(val,1) ) ) {
45 *this += Couple( PyString_AsString( PyTuple_GetItem(val,0) ),
46 PyString_AsString( PyTuple_GetItem(val,1) )
50 PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
54 } else if (PyString_Check(_PyO)) { // c'est une string
55 const char * s = PyString_AsString(_PyO);
56 Versatile V = string(s);
59 } else if (PyInt_Check(_PyO)) { // c'est un int
60 *this = PyInt_AsLong(_PyO);
63 PyErr_SetString(PyExc_RuntimeWarning, "PyVersatile::PyVersatile(const PyObject * PyO) : invalid PyObject");
69 // Conversion de type vers un PyObject
70 PyVersatile::operator PyObject *() const
74 if (_maxsize != 1) { // une liste
76 for(Versatile::const_iterator it=begin(); it!=end(); it++) {
81 switch (_discriminator) {
83 // PyList_Append(obj, PyInt_FromLong(* static_cast<BoolType *>(*it)));
87 // *ch = * static_cast<CharType *>(*it);
88 // PyList_Append(obj, PyString_FromString(ch));
92 // PyList_Append(obj, PyInt_FromLong(* static_cast<IntType *>(*it)));
96 PyList_Append(obj, PyInt_FromLong(* static_cast<LongType *>(*it)));
100 st = * static_cast<StringType *>(*it);
101 PyList_Append(obj, PyString_FromString(st.c_str()));
105 cp = * static_cast<CoupleType *>(*it);
106 // tuple = PyTuple_New(2);
107 // PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str() ) );
108 // PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
109 // PyList_Append(obj, tuple);
110 PyList_Append(obj, Py_BuildValue("(ss)", cp.getLocal().c_str(), cp.getRemote().c_str() ));
114 PyList_Append(obj, Py_None);
120 } else { // un scalaire
125 switch (_discriminator) {
127 // obj = PyInt_FromLong(* static_cast<BoolType *>(front()));
131 // *ch = * static_cast<CharType *>(front());
132 // obj = PyString_FromString(ch);
136 // obj = PyInt_FromLong(* static_cast<IntType *>(front()));
140 obj = PyInt_FromLong(* static_cast<LongType *>(front()));
144 st = * static_cast<StringType *>(front());
145 obj = PyString_FromString(st.c_str());
149 cp = * static_cast<CoupleType *>(front());
150 // tuple = PyTuple_New(2);
151 // PyTuple_SetItem(tuple, 0, PyString_FromString( cp.getLocal().c_str() ) );
152 // PyTuple_SetItem(tuple, 1, PyString_FromString( cp.getRemote().c_str() ) );
153 // obj = PyList_New(0);
154 // PyList_Append(obj, tuple);
155 obj = Py_BuildValue("[(ss)]", cp.getLocal().c_str(), cp.getRemote().c_str() );
168 // Operateur d'affectation a partir d'un objet Versatile
169 PyVersatile & PyVersatile::operator =(const Versatile & V)
171 Versatile * me = this;