diff --git a/Lib/python/pycontainer.i b/Lib/python/pycontainer.i index b2364397e..6ed8d0dcb 100644 --- a/Lib/python/pycontainer.i +++ b/Lib/python/pycontainer.i @@ -255,42 +255,37 @@ namespace swigpy // __getitem__ is required to raise an IndexError for for-loops to work // other methods which can raise are made to throw an IndexError as well %exception __getitem__ { - try { - $action; - } catch (std::out_of_range& e) { + try { $action } + catch (std::out_of_range& e) { SWIG_exception(SWIG_IndexError,const_cast(e.what())); } } %exception __setitem__ { - try { - $action; - } catch (std::out_of_range& e) { + try { $action } + catch (std::out_of_range& e) { SWIG_exception(SWIG_IndexError,const_cast(e.what())); } } %exception __setslice__ { - try { - $action; - } catch (std::invalid_argument& e) { + try { $action } + catch (std::invalid_argument& e) { SWIG_exception(SWIG_TypeError,const_cast(e.what())); } } %exception __delitem__ { - try { - $action; - } catch (std::out_of_range& e) { + try { $action } + catch (std::out_of_range& e) { SWIG_exception(SWIG_IndexError,const_cast(e.what())); } } %exception pop { - try { - $action; - } catch (std::out_of_range& e) { - SWIG_exception(SWIG_IndexError,const_cast(e.what())); + try { $action } + catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast(e.what())); } } @@ -301,8 +296,7 @@ namespace swigpy return !(self->empty()); } - size_type __len__() const - { + size_type __len__() const { return self->size(); } } diff --git a/Lib/python/std_container.i b/Lib/python/std_container.i index 1da599e77..7f3b3fe7e 100644 --- a/Lib/python/std_container.i +++ b/Lib/python/std_container.i @@ -175,3 +175,15 @@ } %} + +// +// Ignore member methods for Type with no default constructor +// +%define %std_nodefconst_type(...) +%feature("ignore") std::vector<__VA_ARGS__ >::vector(size_type size); +%feature("ignore") std::vector<__VA_ARGS__ >::resize(size_type size); +%feature("ignore") std::deque<__VA_ARGS__ >::deque(size_type size); +%feature("ignore") std::deque<__VA_ARGS__ >::resize(size_type size); +%feature("ignore") std::list<__VA_ARGS__ >::list(size_type size); +%feature("ignore") std::list<__VA_ARGS__ >::resize(size_type size); +%enddef diff --git a/Lib/python/std_deque.i b/Lib/python/std_deque.i index 2ef88df0d..93d935ccd 100644 --- a/Lib/python/std_deque.i +++ b/Lib/python/std_deque.i @@ -54,18 +54,15 @@ namespace swigpy { template struct traits_asptr > { - typedef std::deque deque_type; - typedef T value_type; - static int asptr(PyObject *obj, deque_type **vec) { - return traits_asptr_stdseq::asptr(obj, vec); + static int asptr(PyObject *obj, std::deque **vec) { + return traits_asptr_stdseq >::asptr(obj, vec); } }; template struct traits_from > { - typedef std::deque deque_type; - static PyObject *from(const deque_type& vec) { - return traits_from_stdseq::from(vec); + static PyObject *from(const std::deque & vec) { + return traits_from_stdseq >::from(vec); } }; } @@ -102,7 +99,7 @@ namespace std { %typemap_traits_ptr(SWIG_CCode(DEQUE), std::deque); - %std_deque_methods(std::deque); + %std_deque_methods(deque); %pysequence_methods(std::deque); }; diff --git a/Lib/python/std_list.i b/Lib/python/std_list.i index faceb8b67..844728b94 100644 --- a/Lib/python/std_list.i +++ b/Lib/python/std_list.i @@ -12,12 +12,8 @@ void pop_front(); void push_front(const value_type& x); - void remove(const value_type& x); - void unique(); void reverse(); - void sort(); - void merge(list& x); %enddef @@ -69,18 +65,15 @@ namespace swigpy { template struct traits_asptr > { - typedef std::list list_type; - typedef T value_type; - static int asptr(PyObject *obj, list_type **lis) { - return traits_asptr_stdseq::asptr(obj, lis); + static int asptr(PyObject *obj, std::list **lis) { + return traits_asptr_stdseq >::asptr(obj, lis); } }; template struct traits_from > { - typedef std::list list_type; - static PyObject *from(const list_type& vec) { - return traits_from_stdseq::from(vec); + static PyObject *from(const std::list & vec) { + return traits_from_stdseq >::from(vec); } }; } @@ -117,7 +110,7 @@ namespace std { %typemap_traits_ptr(SWIG_CCode(LIST), std::list); - %std_list_methods(std::list); + %std_list_methods(list); %pysequence_methods(std::list); }; @@ -145,15 +138,26 @@ namespace std { %typemap_traits_ptr(SWIG_CCode(LIST), std::list); - %std_list_methods_val(std::list); + %std_list_methods_val(list); %pysequence_methods_val(std::list); }; } +%define %std_extequal_list(...) +%extend std::list<__VA_ARGS__ > { + void remove(const value_type& x) { self->remove(x); } + void merge(std::list<__VA_ARGS__ >& x){ self->merge(x); } + void unique() { self->unique(); } + void sort() { self->sort(); } +} +%enddef + %define %std_list_ptypen(...) %std_extcomp(list, __VA_ARGS__); %std_definst(list, __VA_ARGS__); + %evalif(SWIG_EqualType(__VA_ARGS__), + SWIG_arg(%std_extequal_list(__VA_ARGS__))); %enddef #if defined(SWIG_STD_EXTEND_COMPARISON) || defined(SWIG_STD_DEFAULT_INSTANTIATION) diff --git a/Lib/python/std_map.i b/Lib/python/std_map.i index 4e858a633..1d8387449 100644 --- a/Lib/python/std_map.i +++ b/Lib/python/std_map.i @@ -34,6 +34,20 @@ %enddef +// **** Note **** +// +// If you are going to use a map, you need to instantiate both the +// map and the pair class: +// +// %template(pair_ii) std::pair; +// %template(map_ii) std::map; +// +// or +// +// %template() std::pair; +// %template(map_ii) std::map; +// +// **** Note **** // ------------------------------------------------------------------------ // std::map // @@ -82,8 +96,6 @@ 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); @@ -157,7 +169,7 @@ namespace std { %typemap_traits_ptr(SWIG_CCode(MAP), std::map); - %std_map_methods(std::map); + %std_map_methods(map); %pydict_methods(SWIG_arg(std::map)); }; diff --git a/Lib/python/std_multimap.i b/Lib/python/std_multimap.i index 42f13fe67..226cbd63c 100644 --- a/Lib/python/std_multimap.i +++ b/Lib/python/std_multimap.i @@ -57,10 +57,7 @@ template struct traits_asptr > { - typedef std::multimap multimap_type; - typedef K key_type; - - static int asptr(PyObject *obj, multimap_type **val) { + static int asptr(PyObject *obj, std::multimap **val) { if (PyDict_Check(obj)) { PyObject_var items = PyMapping_Items(obj); return traits_asptr_stdseq, std::pair > @@ -133,7 +130,7 @@ namespace std { %typemap_traits_ptr(SWIG_CCode(MULTIMAP), std::multimap); - %std_multimap_methods(std::multimap); + %std_multimap_methods(multimap); %pydict_methods(SWIG_arg(std::multimap)); }; } diff --git a/Lib/python/std_multiset.i b/Lib/python/std_multiset.i index 10a141b70..790e05c69 100644 --- a/Lib/python/std_multiset.i +++ b/Lib/python/std_multiset.i @@ -49,8 +49,7 @@ template struct traits_asptr > { - typedef std::multiset multiset_type; - static int asptr(PyObject *obj, multiset_type **m) { + static int asptr(PyObject *obj, std::multiset **m) { return traits_asptr_stdseq >::asptr(obj, m); } }; @@ -98,7 +97,7 @@ namespace std { %typemap_traits_ptr(SWIG_CCode(MULTISET), std::multiset); - %std_multiset_methods(std::multiset); + %std_multiset_methods(multiset); %pycontainer_methods(std::multiset); }; diff --git a/Lib/python/std_pair.i b/Lib/python/std_pair.i index b60a5c29e..db7a4e2d6 100644 --- a/Lib/python/std_pair.i +++ b/Lib/python/std_pair.i @@ -9,16 +9,13 @@ namespace swigpy { template struct traits_asval > { - typedef std::pair value_type; - typedef T first_type; - typedef U second_type; - - static int asval(PyObject *obj, value_type *val) { + static int asval(PyObject *obj, std::pair *val) { + typedef std::pair value_type; if (PySequence_Check(obj) && (PySequence_Size(obj) == 2)) { swigpy::PyObject_var first = PySequence_GetItem(obj,0); swigpy::PyObject_var second = PySequence_GetItem(obj,1); - first_type *pfirst = val ? &(val->first) : 0; - second_type *psecond = val ? &(val->second) : 0; + T *pfirst = val ? &(val->first) : 0; + U *psecond = val ? &(val->second) : 0; if (swigpy::asval(first,pfirst) && swigpy::asval(second,psecond)) { return 1; } @@ -40,8 +37,7 @@ template struct traits_from > { - typedef std::pair value_type; - static PyObject *from(const value_type& val) { + static PyObject *from(const std::pair& val) { PyObject* obj = PyTuple_New(2); PyTuple_SetItem(obj,0,swigpy::from(val.first)); PyTuple_SetItem(obj,1,swigpy::from(val.second)); @@ -83,17 +79,12 @@ namespace std { U second; %extend - { - const T& f() { - return self->first; - } - + { %pythoncode { def __repr__(self): return "(%s, %s)" %(str(self.first),str(self.second)) } - } - + } }; // *** diff --git a/Lib/python/std_set.i b/Lib/python/std_set.i index 6e1864352..b88fe79d6 100644 --- a/Lib/python/std_set.i +++ b/Lib/python/std_set.i @@ -73,8 +73,7 @@ template struct traits_asptr > { - typedef std::set set_type; - static int asptr(PyObject *obj, set_type **s) { + static int asptr(PyObject *obj, std::set **s) { return traits_asptr_stdseq >::asptr(obj, s); } }; @@ -121,7 +120,7 @@ namespace std { %typemap_traits_ptr(SWIG_CCode(SET), std::set); - %std_set_methods(std::set); + %std_set_methods(set); %pycontainer_methods(std::set); }; diff --git a/Lib/python/std_vector.i b/Lib/python/std_vector.i index c4fb6a2e1..b0adb6a2a 100644 --- a/Lib/python/std_vector.i +++ b/Lib/python/std_vector.i @@ -57,18 +57,15 @@ namespace swigpy { template struct traits_asptr > { - typedef std::vector vector_type; - typedef T value_type; - static int asptr(PyObject *obj, vector_type **vec) { - return traits_asptr_stdseq::asptr(obj, vec); + static int asptr(PyObject *obj, std::vector **vec) { + return traits_asptr_stdseq >::asptr(obj, vec); } }; - + template struct traits_from > { - typedef std::vector vector_type; - static PyObject *from(const vector_type& vec) { - return traits_from_stdseq::from(vec); + static PyObject *from(const std::vector& vec) { + return traits_from_stdseq >::from(vec); } }; }