- better support for classes with no default constructor, equal or

comparison methods.

  you will be able to do

    struct Foo {
     Foo(int) {}
    };

    %std_nodefconst_type(Foo); // Says Foo has no def. constructor

    %template(vector_Foo) std::vector<Foo>;

  and the conflicting vector/list/deque methods will not be generated.


more cosmetic, and a note about the relation between std::map and
std::pair.


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5810 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Marcelo Matus 2004-03-31 11:27:53 +00:00
commit 29a30e2dcc
10 changed files with 79 additions and 77 deletions

View file

@ -255,42 +255,37 @@ namespace swigpy
// __getitem__ is required to raise an IndexError for for-loops to work // __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 // other methods which can raise are made to throw an IndexError as well
%exception __getitem__ { %exception __getitem__ {
try { try { $action }
$action; catch (std::out_of_range& e) {
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
} }
} }
%exception __setitem__ { %exception __setitem__ {
try { try { $action }
$action; catch (std::out_of_range& e) {
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
} }
} }
%exception __setslice__ { %exception __setslice__ {
try { try { $action }
$action; catch (std::invalid_argument& e) {
} catch (std::invalid_argument& e) {
SWIG_exception(SWIG_TypeError,const_cast<char*>(e.what())); SWIG_exception(SWIG_TypeError,const_cast<char*>(e.what()));
} }
} }
%exception __delitem__ { %exception __delitem__ {
try { try { $action }
$action; catch (std::out_of_range& e) {
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
} }
} }
%exception pop { %exception pop {
try { try { $action }
$action; catch (std::out_of_range& e) {
} catch (std::out_of_range& e) { SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
} }
} }
@ -301,8 +296,7 @@ namespace swigpy
return !(self->empty()); return !(self->empty());
} }
size_type __len__() const size_type __len__() const {
{
return self->size(); return self->size();
} }
} }

View file

@ -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

View file

@ -54,18 +54,15 @@
namespace swigpy { namespace swigpy {
template <class T> template <class T>
struct traits_asptr<std::deque<T> > { struct traits_asptr<std::deque<T> > {
typedef std::deque<T> deque_type; static int asptr(PyObject *obj, std::deque<T> **vec) {
typedef T value_type; return traits_asptr_stdseq<std::deque<T> >::asptr(obj, vec);
static int asptr(PyObject *obj, deque_type **vec) {
return traits_asptr_stdseq<deque_type>::asptr(obj, vec);
} }
}; };
template <class T> template <class T>
struct traits_from<std::deque<T> > { struct traits_from<std::deque<T> > {
typedef std::deque<T> deque_type; static PyObject *from(const std::deque<T> & vec) {
static PyObject *from(const deque_type& vec) { return traits_from_stdseq<std::deque<T> >::from(vec);
return traits_from_stdseq<deque_type>::from(vec);
} }
}; };
} }
@ -102,7 +99,7 @@ namespace std {
%typemap_traits_ptr(SWIG_CCode(DEQUE), std::deque<T >); %typemap_traits_ptr(SWIG_CCode(DEQUE), std::deque<T >);
%std_deque_methods(std::deque<T >); %std_deque_methods(deque);
%pysequence_methods(std::deque<T >); %pysequence_methods(std::deque<T >);
}; };

View file

@ -12,12 +12,8 @@
void pop_front(); void pop_front();
void push_front(const value_type& x); void push_front(const value_type& x);
void remove(const value_type& x);
void unique();
void reverse(); void reverse();
void sort();
void merge(list& x);
%enddef %enddef
@ -69,18 +65,15 @@
namespace swigpy { namespace swigpy {
template <class T > template <class T >
struct traits_asptr<std::list<T> > { struct traits_asptr<std::list<T> > {
typedef std::list<T> list_type; static int asptr(PyObject *obj, std::list<T> **lis) {
typedef T value_type; return traits_asptr_stdseq<std::list<T> >::asptr(obj, lis);
static int asptr(PyObject *obj, list_type **lis) {
return traits_asptr_stdseq<list_type>::asptr(obj, lis);
} }
}; };
template <class T> template <class T>
struct traits_from<std::list<T> > { struct traits_from<std::list<T> > {
typedef std::list<T> list_type; static PyObject *from(const std::list<T> & vec) {
static PyObject *from(const list_type& vec) { return traits_from_stdseq<std::list<T> >::from(vec);
return traits_from_stdseq<list_type>::from(vec);
} }
}; };
} }
@ -117,7 +110,7 @@ namespace std {
%typemap_traits_ptr(SWIG_CCode(LIST), std::list<T >); %typemap_traits_ptr(SWIG_CCode(LIST), std::list<T >);
%std_list_methods(std::list<T >); %std_list_methods(list);
%pysequence_methods(std::list<T >); %pysequence_methods(std::list<T >);
}; };
@ -145,15 +138,26 @@ namespace std {
%typemap_traits_ptr(SWIG_CCode(LIST), std::list<T* >); %typemap_traits_ptr(SWIG_CCode(LIST), std::list<T* >);
%std_list_methods_val(std::list<T* >); %std_list_methods_val(list);
%pysequence_methods_val(std::list<T* >); %pysequence_methods_val(std::list<T* >);
}; };
} }
%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(...) %define %std_list_ptypen(...)
%std_extcomp(list, __VA_ARGS__); %std_extcomp(list, __VA_ARGS__);
%std_definst(list, __VA_ARGS__); %std_definst(list, __VA_ARGS__);
%evalif(SWIG_EqualType(__VA_ARGS__),
SWIG_arg(%std_extequal_list(__VA_ARGS__)));
%enddef %enddef
#if defined(SWIG_STD_EXTEND_COMPARISON) || defined(SWIG_STD_DEFAULT_INSTANTIATION) #if defined(SWIG_STD_EXTEND_COMPARISON) || defined(SWIG_STD_DEFAULT_INSTANTIATION)

View file

@ -34,6 +34,20 @@
%enddef %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<int, int>;
// %template(map_ii) std::map<int, int>;
//
// or
//
// %template() std::pair<int, int>;
// %template(map_ii) std::map<int, int>;
//
// **** Note ****
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
// std::map // std::map
// //
@ -82,8 +96,6 @@
template <class K, class T> template <class K, class T>
struct traits_asptr<std::map<K,T> > { struct traits_asptr<std::map<K,T> > {
typedef std::map<K,T> map_type; typedef std::map<K,T> map_type;
typedef K key_type;
static int asptr(PyObject *obj, map_type **val) { static int asptr(PyObject *obj, map_type **val) {
if (PyDict_Check(obj)) { if (PyDict_Check(obj)) {
PyObject_var items = PyMapping_Items(obj); PyObject_var items = PyMapping_Items(obj);
@ -157,7 +169,7 @@ namespace std {
%typemap_traits_ptr(SWIG_CCode(MAP), std::map<K, T >); %typemap_traits_ptr(SWIG_CCode(MAP), std::map<K, T >);
%std_map_methods(std::map<K, T >); %std_map_methods(map);
%pydict_methods(SWIG_arg(std::map<K, T >)); %pydict_methods(SWIG_arg(std::map<K, T >));
}; };

View file

@ -57,10 +57,7 @@
template <class K, class T> template <class K, class T>
struct traits_asptr<std::multimap<K,T> > { struct traits_asptr<std::multimap<K,T> > {
typedef std::multimap<K,T> multimap_type; static int asptr(PyObject *obj, std::multimap<K,T> **val) {
typedef K key_type;
static int asptr(PyObject *obj, multimap_type **val) {
if (PyDict_Check(obj)) { if (PyDict_Check(obj)) {
PyObject_var items = PyMapping_Items(obj); PyObject_var items = PyMapping_Items(obj);
return traits_asptr_stdseq<std::multimap<K,T>, std::pair<K, T> > return traits_asptr_stdseq<std::multimap<K,T>, std::pair<K, T> >
@ -133,7 +130,7 @@ namespace std {
%typemap_traits_ptr(SWIG_CCode(MULTIMAP), std::multimap<K, T >); %typemap_traits_ptr(SWIG_CCode(MULTIMAP), std::multimap<K, T >);
%std_multimap_methods(std::multimap<K, T >); %std_multimap_methods(multimap);
%pydict_methods(SWIG_arg(std::multimap<K, T >)); %pydict_methods(SWIG_arg(std::multimap<K, T >));
}; };
} }

View file

@ -49,8 +49,7 @@
template <class T> template <class T>
struct traits_asptr<std::multiset<T> > { struct traits_asptr<std::multiset<T> > {
typedef std::multiset<T> multiset_type; static int asptr(PyObject *obj, std::multiset<T> **m) {
static int asptr(PyObject *obj, multiset_type **m) {
return traits_asptr_stdseq<std::multiset<T> >::asptr(obj, m); return traits_asptr_stdseq<std::multiset<T> >::asptr(obj, m);
} }
}; };
@ -98,7 +97,7 @@ namespace std {
%typemap_traits_ptr(SWIG_CCode(MULTISET), std::multiset<T >); %typemap_traits_ptr(SWIG_CCode(MULTISET), std::multiset<T >);
%std_multiset_methods(std::multiset<T >); %std_multiset_methods(multiset);
%pycontainer_methods(std::multiset<T >); %pycontainer_methods(std::multiset<T >);
}; };

View file

@ -9,16 +9,13 @@
namespace swigpy { namespace swigpy {
template <class T, class U > template <class T, class U >
struct traits_asval<std::pair<T,U> > { struct traits_asval<std::pair<T,U> > {
typedef std::pair<T,U> value_type; static int asval(PyObject *obj, std::pair<T,U> *val) {
typedef T first_type; typedef std::pair<T,U> value_type;
typedef U second_type;
static int asval(PyObject *obj, value_type *val) {
if (PySequence_Check(obj) && (PySequence_Size(obj) == 2)) { if (PySequence_Check(obj) && (PySequence_Size(obj) == 2)) {
swigpy::PyObject_var first = PySequence_GetItem(obj,0); swigpy::PyObject_var first = PySequence_GetItem(obj,0);
swigpy::PyObject_var second = PySequence_GetItem(obj,1); swigpy::PyObject_var second = PySequence_GetItem(obj,1);
first_type *pfirst = val ? &(val->first) : 0; T *pfirst = val ? &(val->first) : 0;
second_type *psecond = val ? &(val->second) : 0; U *psecond = val ? &(val->second) : 0;
if (swigpy::asval(first,pfirst) && swigpy::asval(second,psecond)) { if (swigpy::asval(first,pfirst) && swigpy::asval(second,psecond)) {
return 1; return 1;
} }
@ -40,8 +37,7 @@
template <class T, class U > template <class T, class U >
struct traits_from<std::pair<T,U> > { struct traits_from<std::pair<T,U> > {
typedef std::pair<T,U> value_type; static PyObject *from(const std::pair<T,U>& val) {
static PyObject *from(const value_type& val) {
PyObject* obj = PyTuple_New(2); PyObject* obj = PyTuple_New(2);
PyTuple_SetItem(obj,0,swigpy::from(val.first)); PyTuple_SetItem(obj,0,swigpy::from(val.first));
PyTuple_SetItem(obj,1,swigpy::from(val.second)); PyTuple_SetItem(obj,1,swigpy::from(val.second));
@ -83,17 +79,12 @@ namespace std {
U second; U second;
%extend %extend
{ {
const T& f() {
return self->first;
}
%pythoncode { %pythoncode {
def __repr__(self): def __repr__(self):
return "(%s, %s)" %(str(self.first),str(self.second)) return "(%s, %s)" %(str(self.first),str(self.second))
} }
} }
}; };
// *** // ***

View file

@ -73,8 +73,7 @@
template <class T> template <class T>
struct traits_asptr<std::set<T> > { struct traits_asptr<std::set<T> > {
typedef std::set<T> set_type; static int asptr(PyObject *obj, std::set<T> **s) {
static int asptr(PyObject *obj, set_type **s) {
return traits_asptr_stdseq<std::set<T> >::asptr(obj, s); return traits_asptr_stdseq<std::set<T> >::asptr(obj, s);
} }
}; };
@ -121,7 +120,7 @@ namespace std {
%typemap_traits_ptr(SWIG_CCode(SET), std::set<T >); %typemap_traits_ptr(SWIG_CCode(SET), std::set<T >);
%std_set_methods(std::set<T >); %std_set_methods(set);
%pycontainer_methods(std::set<T >); %pycontainer_methods(std::set<T >);
}; };

View file

@ -57,18 +57,15 @@
namespace swigpy { namespace swigpy {
template <class T> template <class T>
struct traits_asptr<std::vector<T> > { struct traits_asptr<std::vector<T> > {
typedef std::vector<T> vector_type; static int asptr(PyObject *obj, std::vector<T> **vec) {
typedef T value_type; return traits_asptr_stdseq<std::vector<T> >::asptr(obj, vec);
static int asptr(PyObject *obj, vector_type **vec) {
return traits_asptr_stdseq<vector_type>::asptr(obj, vec);
} }
}; };
template <class T> template <class T>
struct traits_from<std::vector<T> > { struct traits_from<std::vector<T> > {
typedef std::vector<T> vector_type; static PyObject *from(const std::vector<T>& vec) {
static PyObject *from(const vector_type& vec) { return traits_from_stdseq<std::vector<T> >::from(vec);
return traits_from_stdseq<vector_type>::from(vec);
} }
}; };
} }