/* Pairs */ %include //#define SWIG_STD_PAIR_ASVAL %fragment("StdPairTraits","header",fragment="StdTraits") { namespace swig { #ifdef SWIG_STD_PAIR_ASVAL template struct traits_asval > { typedef std::pair value_type; static int get_pair(PyObject* first, PyObject* second, std::pair *val) { if (val) { T *pfirst = &(val->first); U *psecond = &(val->second); if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK) && (swig::asval((PyObject*)second, psecond) == SWIG_OK)) { return SWIG_OK; } else { return SWIG_ERROR; } } else { T *pfirst = 0; U *psecond = 0; if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK) && (swig::asval((PyObject*)second, psecond) == SWIG_OK)) { return SWIG_OK; } else { return SWIG_ERROR; } } } static int asval(PyObject *obj, std::pair *val) { int res = SWIG_ERROR; if (PyTuple_Check(obj)) { if (PyTuple_GET_SIZE(obj) == 2) { res = get_pair(PyTuple_GET_ITEM(obj,0),PyTuple_GET_ITEM(obj,1), val); } } else if (PySequence_Check(obj)) { if (PySequence_Size(obj) == 2) { swig::PyObject_var first = PySequence_GetItem(obj,0); swig::PyObject_var second = PySequence_GetItem(obj,1); res = get_pair(first, second, val); } } else { value_type *p; if (SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0) == SWIG_OK) { if (val) *val = *p; res = SWIG_OK; } } if ((res == SWIG_ERROR) && val) { PyErr_Format(PyExc_TypeError, "a %s is expected", swig::type_name()); } return res; } }; #else template struct traits_asptr > { typedef std::pair value_type; static int get_pair(PyObject* first, PyObject* second, std::pair **val) { if (val) { value_type *vp = %new_instance(std::pair); T *pfirst = &(vp->first); U *psecond = &(vp->second); if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK) && (swig::asval((PyObject*)second, psecond) == SWIG_OK)) { *val = vp; return SWIG_NEWOBJ; } else { %delete(vp); return SWIG_BADOBJ; } } else { T *pfirst = 0; U *psecond = 0; if ((swig::asval((PyObject*)first, pfirst) == SWIG_OK) && (swig::asval((PyObject*)second, psecond) == SWIG_OK)) { return SWIG_NEWOBJ; } else { return SWIG_BADOBJ; } } } static int asptr(PyObject *obj, std::pair **val) { int res = SWIG_BADOBJ; if (PyTuple_Check(obj)) { if (PyTuple_GET_SIZE(obj) == 2) { res = get_pair(PyTuple_GET_ITEM(obj,0),PyTuple_GET_ITEM(obj,1), val); } } else if (PySequence_Check(obj)) { if (PySequence_Size(obj) == 2) { swig::PyObject_var first = PySequence_GetItem(obj,0); swig::PyObject_var second = PySequence_GetItem(obj,1); res = get_pair(first, second, val); } } else { value_type *p; if (SWIG_ConvertPtr(obj,(void**)&p,swig::type_info(),0) == SWIG_OK) { if (val) *val = p; res = SWIG_OLDOBJ; } } if ((res == SWIG_BADOBJ) && val) { PyErr_Format(PyExc_TypeError, "a %s is expected", swig::type_name()); } return res; } }; #endif template struct traits_from > { static PyObject *from(const std::pair& val) { PyObject* obj = PyTuple_New(2); PyTuple_SetItem(obj,0,swig::from(val.first)); PyTuple_SetItem(obj,1,swig::from(val.second)); return obj; } }; } } %define %swig_pair_methods(pair...) %extend { %pythoncode { def __len__(self): return 2 def __getitem__(self, index): if not (index % 2): return self.first else: return self.second def __setitem__(self, index, val): if not (index % 2): self.first = val else: self.second = val def __repr__(self): return str((self.first, self.second)) } } %enddef %include