1 /* 2 Multimaps 3 */ 4 %include <std_map.i> 5 6 %fragment("StdMultimapTraits","header",fragment="StdMapCommonTraits") 7 { 8 namespace swig { 9 template <class SwigPySeq, class K, class T > 10 inline void 11 assign(const SwigPySeq& swigpyseq, std::multimap<K,T > *multimap) { 12 typedef typename std::multimap<K,T>::value_type value_type; 13 typename SwigPySeq::const_iterator it = swigpyseq.begin(); 14 for (;it != swigpyseq.end(); ++it) { 15 multimap->insert(value_type(it->first, it->second)); 16 } 17 } 18 19 template <class K, class T> 20 struct traits_asptr<std::multimap<K,T> > { 21 typedef std::multimap<K,T> multimap_type; 22 static int asptr(PyObject *obj, std::multimap<K,T> **val) { 23 int res = SWIG_ERROR; 24 if (PyDict_Check(obj)) { 25 SwigVar_PyObject items = PyObject_CallMethod(obj,(char *)"items",NULL); 26 return traits_asptr_stdseq<std::multimap<K,T>, std::pair<K, T> >::asptr(items, val); 27 } else { 28 multimap_type *p; 29 res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<multimap_type>(),0); 30 if (SWIG_IsOK(res) && val) *val = p; 31 } 32 return res; 33 } 34 }; 35 36 template <class K, class T > 37 struct traits_from<std::multimap<K,T> > { 38 typedef std::multimap<K,T> multimap_type; 39 typedef typename multimap_type::const_iterator const_iterator; 40 typedef typename multimap_type::size_type size_type; 41 42 static PyObject *from(const multimap_type& multimap) { 43 swig_type_info *desc = swig::type_info<multimap_type>(); 44 if (desc && desc->clientdata) { 45 return SWIG_InternalNewPointerObj(new multimap_type(multimap), desc, SWIG_POINTER_OWN); 46 } else { 47 size_type size = multimap.size(); 48 int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1; 49 if (pysize < 0) { 50 SWIG_PYTHON_THREAD_BEGIN_BLOCK; 51 PyErr_SetString(PyExc_OverflowError, 52 "multimap size not valid in python"); 53 SWIG_PYTHON_THREAD_END_BLOCK; 54 return NULL; 55 } 56 PyObject *obj = PyDict_New(); 57 for (const_iterator i= multimap.begin(); i!= multimap.end(); ++i) { 58 swig::SwigVar_PyObject key = swig::from(i->first); 59 swig::SwigVar_PyObject val = swig::from(i->second); 60 PyDict_SetItem(obj, key, val); 61 } 62 return obj; 63 } 64 } 65 }; 66 } 67 } 68 69 %define %swig_multimap_methods(Type...) 70 %swig_map_common(Type); 71 %extend { 72 void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) { 73 self->insert(Type::value_type(key,x)); 74 } 75 } 76 %enddef 77 78 %include <std/std_multimap.i> 79 80