| 1 | /* |
| 2 | Pairs |
| 3 | */ |
| 4 | %include <pycontainer.swg> |
| 5 | |
| 6 | %fragment("StdPairTraits","header", |
| 7 | fragment="StdTraits",fragment="PyObject_var") { |
| 8 | namespace swig { |
| 9 | template <class T, class U > |
| 10 | struct traits_asptr<std::pair<T,U> > { |
| 11 | static int asptr(PyObject *obj, std::pair<T,U> **val) { |
| 12 | typedef std::pair<T,U> value_type; |
| 13 | if (PySequence_Check(obj) && (PySequence_Size(obj) == 2)) { |
| 14 | swig::PyObject_var first = PySequence_GetItem(obj,0); |
| 15 | swig::PyObject_var second = PySequence_GetItem(obj,1); |
| 16 | T *pfirst = 0; |
| 17 | U *psecond = 0; |
| 18 | if (val) { |
| 19 | *val = new std::pair<T,U>; |
| 20 | pfirst = &((*val)->first); |
| 21 | psecond = &((*val)->second); |
| 22 | } |
| 23 | if (swig::asval(first,pfirst) && swig::asval(second,psecond)) { |
| 24 | return SWIG_NEWOBJ; |
| 25 | } else { |
| 26 | delete *val; |
| 27 | } |
| 28 | } else { |
| 29 | value_type *p; |
| 30 | if (SWIG_ConvertPtr(obj,(void**)&p, |
| 31 | swig::type_info<value_type>(),0) != -1) { |
| 32 | if (val) *val = p; |
| 33 | return SWIG_OLDOBJ; |
| 34 | } |
| 35 | } |
| 36 | if (val) { |
| 37 | PyErr_Format(PyExc_TypeError, "a %s is expected", |
| 38 | swig::type_name<value_type>()); |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | template <class T, class U > |
| 45 | struct traits_from<std::pair<T,U> > { |
| 46 | static PyObject *from(const std::pair<T,U>& val) { |
| 47 | PyObject* obj = PyTuple_New(2); |
| 48 | PyTuple_SetItem(obj,0,swig::from(val.first)); |
| 49 | PyTuple_SetItem(obj,1,swig::from(val.second)); |
| 50 | return obj; |
| 51 | } |
| 52 | }; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | %define %swig_pair_methods(pair...) |
| 57 | %extend { |
| 58 | %pythoncode { |
| 59 | def __len__(self): |
| 60 | return 2 |
| 61 | def __getitem__(self, index): |
| 62 | if not (index % 2): |
| 63 | return self.first |
| 64 | else: |
| 65 | return self.second |
| 66 | def __setitem__(self, index, val): |
| 67 | if not (index % 2): |
| 68 | self.first = val |
| 69 | else: |
| 70 | self.second = val |
| 71 | def __repr__(self): |
| 72 | return str((self.first, self.second)) |
| 73 | } |
| 74 | } |
| 75 | %enddef |
| 76 | |
| 77 | |
| 78 | %include <std/std_pair.i> |
| 79 | |