// // std::map // Python implementation %include std_pair.i %include std_container.i %define %std_map_methods_common(map) %std_container_methods(SWIG_arg(map)); size_type erase(const key_type& x); size_type count(const key_type& x) const; #ifdef SWIG_EXPORT_ITERATOR_METHODS iterator insert(iterator position, const value_type& x); void erase(iterator position); void erase(iterator first, iterator last); iterator find(const key_type& x); const_iterator find(const key_type& x) const; iterator lower_bound(const key_type& x); const_iterator lower_bound(const key_type& x) const; iterator upper_bound(const key_type& x); const_iterator upper_bound(const key_type& x) const; #endif %enddef %define %std_map_methods(...) %std_map_methods_common(SWIG_arg(__VA_ARGS__)); #ifdef SWIG_EXPORT_ITERATOR_METHODS iterator insert(const value_type& x); #endif %enddef // ------------------------------------------------------------------------ // std::map // // The aim of all that follows would be to integrate std::map with // Python as much as possible, namely, to allow the user to pass and // be returned Python tuples or maps. // const declarations are used to guess the intent of the function being // exported; therefore, the following rationale is applied: // // -- f(std::map), f(const std::map&): // the parameter being read-only, either a Python sequence or a // previously wrapped std::map can be passed. // -- f(std::map&), f(std::map*): // the parameter may be modified; therefore, only a wrapped std::map // can be passed. // -- std::map f(), const std::map& f(): // the map is returned by copy; therefore, a Python sequence of T:s // is returned which is most easily used in other Python functions // -- std::map& f(), std::map* f(): // the map is returned by reference; therefore, a wrapped std::map // is returned // -- const std::map* f(), f(const std::map*): // for consistency, they expect and return a plain map pointer. // ------------------------------------------------------------------------ %{ #include #include #include %} // exported class %fragment("StdMapTraits","header",fragment="StdSequenceTraits") { namespace swigpy { template void assign(const PySeq& pyseq, std::map *map) { typedef typename std::map::value_type value_type; typename PySeq::const_iterator it = pyseq.begin(); for (;it != pyseq.end(); ++it) { map->insert(value_type(it->first, it->second)); } } template struct traits_asptr > { typedef std::map map_type; typedef K key_type; static int asptr(PyObject *obj, map_type **val) { if (PyDict_Check(obj)) { PyObject_var items = PyMapping_Items(obj); return traits_asptr_stdseq, std::pair > ::asptr(items, val); } if (val) { PyErr_SetString(PyExc_TypeError, "a dictionary is expected"); } return 0; } }; template struct traits_from > { typedef std::map map_type; typedef typename map_type::const_iterator const_iterator; typedef typename map_type::size_type size_type; static PyObject *from(const map_type& map) { size_type size = map.size(); int pysize = size <= INT_MAX ? (int) size : 0; if (!pysize) { PyErr_SetString(PyExc_OverflowError, "map size not valid in python"); Py_INCREF(Py_None); return Py_None; } PyObject *obj = PyDict_New(); for (const_iterator i= map.begin(); i!= map.end(); ++i) { PyDict_SetItem(obj, swigpy::from(i->first), swigpy::from(i->second)); } return obj; } }; } } namespace std { template class map { public: typedef size_t size_type; typedef ptrdiff_t difference_type; typedef K key_type; typedef T mapped_type; typedef std::pair value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typedef const value_type& const_reference; %traits_swigtype(K); %traits_swigtype(T); %fragment(SWIG_Traits_frag(std::map), "header", fragment=SWIG_Traits_frag(std::pair), fragment="StdMapTraits") { namespace swigpy { template <> struct traits > { typedef pointer_category category; static const char* type_name() { return "std::map<" #K "," #T " >"; } }; } } %typemap_traits_ptr(SWIG_CCode(MAP), std::map); %std_map_methods(std::map); %pydict_methods(SWIG_arg(std::map)); }; } %define %std_map_ptypen(...) %std_extcomp_2(map, __VA_ARGS__); %std_definst_2(map, __VA_ARGS__); %enddef #if defined(SWIG_STD_EXTEND_COMPARISON) || defined(SWIG_STD_DEFAULT_INSTANTIATION) %apply_cpptypes_2(%std_map_ptypen); #endif