1 /* 2 Pairs 3 */ 4 %include <pystdcommon.swg> 5 6 //#define SWIG_STD_PAIR_ASVAL 7 8 %fragment("StdPairTraits","header",fragment="StdTraits") { 9 namespace swig { 10 #ifdef SWIG_STD_PAIR_ASVAL 11 template <class T, class U > 12 struct traits_asval<std::pair<T,U> > { 13 typedef std::pair<T,U> value_type; 14 15 static int get_pair(PyObject* first, PyObject* second, 16 std::pair<T,U> *val) 17 { 18 if (val) { 19 T *pfirst = &(val->first); 20 int res1 = swig::asval((PyObject*)first, pfirst); 21 if (!SWIG_IsOK(res1)) return res1; 22 U *psecond = &(val->second); 23 int res2 = swig::asval((PyObject*)second, psecond); 24 if (!SWIG_IsOK(res2)) return res2; 25 return res1 > res2 ? res1 : res2; 26 } else { 27 T *pfirst = 0; 28 int res1 = swig::asval((PyObject*)first, 0); 29 if (!SWIG_IsOK(res1)) return res1; 30 U *psecond = 0; 31 int res2 = swig::asval((PyObject*)second, psecond); 32 if (!SWIG_IsOK(res2)) return res2; 33 return res1 > res2 ? res1 : res2; 34 } 35 } 36 37 static int asval(PyObject *obj, std::pair<T,U> *val) { 38 int res = SWIG_ERROR; 39 if (PyTuple_Check(obj)) { 40 if (PyTuple_GET_SIZE(obj) == 2) { 41 res = get_pair(PyTuple_GET_ITEM(obj,0),PyTuple_GET_ITEM(obj,1), val); 42 } 43 } else if (PySequence_Check(obj)) { 44 if (PySequence_Size(obj) == 2) { 45 swig::SwigVar_PyObject first = PySequence_GetItem(obj,0); 46 swig::SwigVar_PyObject second = PySequence_GetItem(obj,1); 47 res = get_pair(first, second, val); 48 } 49 } else { 50 value_type *p; 51 res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0); 52 if (SWIG_IsOK(res) && val) *val = *p; 53 } 54 return res; 55 } 56 }; 57 58 #else 59 template <class T, class U > 60 struct traits_asptr<std::pair<T,U> > { 61 typedef std::pair<T,U> value_type; 62 63 static int get_pair(PyObject* first, PyObject* second, 64 std::pair<T,U> **val) 65 { 66 if (val) { 67 value_type *vp = %new_instance(std::pair<T,U>); 68 T *pfirst = &(vp->first); 69 int res1 = swig::asval((PyObject*)first, pfirst); 70 if (!SWIG_IsOK(res1)) return res1; 71 U *psecond = &(vp->second); 72 int res2 = swig::asval((PyObject*)second, psecond); 73 if (!SWIG_IsOK(res2)) return res2; 74 *val = vp; 75 return SWIG_AddNewMask(res1 > res2 ? res1 : res2); 76 } else { 77 T *pfirst = 0; 78 int res1 = swig::asval((PyObject*)first, pfirst); 79 if (!SWIG_IsOK(res1)) return res1; 80 U *psecond = 0; 81 int res2 = swig::asval((PyObject*)second, psecond); 82 if (!SWIG_IsOK(res2)) return res2; 83 return res1 > res2 ? res1 : res2; 84 } 85 } 86 87 static int asptr(PyObject *obj, std::pair<T,U> **val) { 88 int res = SWIG_ERROR; 89 if (PyTuple_Check(obj)) { 90 if (PyTuple_GET_SIZE(obj) == 2) { 91 res = get_pair(PyTuple_GET_ITEM(obj,0),PyTuple_GET_ITEM(obj,1), val); 92 } 93 } else if (PySequence_Check(obj)) { 94 if (PySequence_Size(obj) == 2) { 95 swig::SwigVar_PyObject first = PySequence_GetItem(obj,0); 96 swig::SwigVar_PyObject second = PySequence_GetItem(obj,1); 97 res = get_pair(first, second, val); 98 } 99 } else { 100 value_type *p; 101 res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0); 102 if (SWIG_IsOK(res) && val) *val = p; 103 } 104 return res; 105 } 106 }; 107 108 #endif 109 template <class T, class U > 110 struct traits_from<std::pair<T,U> > { 111 static PyObject *from(const std::pair<T,U>& val) { 112 PyObject* obj = PyTuple_New(2); 113 PyTuple_SetItem(obj,0,swig::from(val.first)); 114 PyTuple_SetItem(obj,1,swig::from(val.second)); 115 return obj; 116 } 117 }; 118 } 119 120 #if defined(SWIGPYTHON_BUILTIN) 121 SWIGINTERN Py_ssize_t 122 SwigPython_std_pair_len (PyObject *a) 123 { 124 return 2; 125 } 126 127 SWIGINTERN PyObject* 128 SwigPython_std_pair_repr (PyObject *o) 129 { 130 PyObject *tuple = PyTuple_New(2); 131 assert(tuple); 132 PyTuple_SET_ITEM(tuple, 0, PyObject_GetAttrString(o, (char*) "first")); 133 PyTuple_SET_ITEM(tuple, 1, PyObject_GetAttrString(o, (char*) "second")); 134 PyObject *result = PyObject_Repr(tuple); 135 Py_DECREF(tuple); 136 return result; 137 } 138 139 SWIGINTERN PyObject* 140 SwigPython_std_pair_getitem (PyObject *a, Py_ssize_t b) 141 { 142 PyObject *result = PyObject_GetAttrString(a, b % 2 ? (char*) "second" : (char*) "first"); 143 return result; 144 } 145 146 SWIGINTERN int 147 SwigPython_std_pair_setitem (PyObject *a, Py_ssize_t b, PyObject *c) 148 { 149 int result = PyObject_SetAttrString(a, b % 2 ? (char*) "second" : (char*) "first", c); 150 return result; 151 } 152 #endif 153 154 } 155 156 %feature("python:sq_length") std::pair "SwigPython_std_pair_len"; 157 %feature("python:sq_length") std::pair<T*,U> "SwigPython_std_pair_len"; 158 %feature("python:sq_length") std::pair<T,U*> "SwigPython_std_pair_len"; 159 %feature("python:sq_length") std::pair<T*,U*> "SwigPython_std_pair_len"; 160 161 %feature("python:tp_repr") std::pair "SwigPython_std_pair_repr"; 162 %feature("python:tp_repr") std::pair<T*,U> "SwigPython_std_pair_repr"; 163 %feature("python:tp_repr") std::pair<T,U*> "SwigPython_std_pair_repr"; 164 %feature("python:tp_repr") std::pair<T*,U*> "SwigPython_std_pair_repr"; 165 166 %feature("python:sq_item") std::pair "SwigPython_std_pair_getitem"; 167 %feature("python:sq_item") std::pair<T*,U> "SwigPython_std_pair_getitem"; 168 %feature("python:sq_item") std::pair<T,U*> "SwigPython_std_pair_getitem"; 169 %feature("python:sq_item") std::pair<T*,U*> "SwigPython_std_pair_getitem"; 170 171 %feature("python:sq_ass_item") std::pair "SwigPython_std_pair_setitem"; 172 %feature("python:sq_ass_item") std::pair<T*,U> "SwigPython_std_pair_setitem"; 173 %feature("python:sq_ass_item") std::pair<T,U*> "SwigPython_std_pair_setitem"; 174 %feature("python:sq_ass_item") std::pair<T*,U*> "SwigPython_std_pair_setitem"; 175 176 %define %swig_pair_methods(pair...) 177 #if !defined(SWIGPYTHON_BUILTIN) 178 %extend { 179 %pythoncode {def __len__(self): return 2 180 def __repr__(self): return str((self.first, self.second)) 181 def __getitem__(self, index): 182 if not (index % 2): 183 return self.first 184 else: 185 return self.second 186 def __setitem__(self, index, val): 187 if not (index % 2): 188 self.first = val 189 else: 190 self.second = val} 191 } 192 #endif 193 %enddef 194 195 %include <std/std_pair.i> 196 197