Added asdict() method to convert std::map to a python dict.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12646 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a74909f77b
commit
6bd724dbf9
2 changed files with 29 additions and 18 deletions
|
|
@ -4,6 +4,9 @@ See the RELEASENOTES file for a summary of changes in each release.
|
||||||
|
|
||||||
Version 2.0.4 (in progress)
|
Version 2.0.4 (in progress)
|
||||||
===========================
|
===========================
|
||||||
|
2011-04-29: szager
|
||||||
|
Bug 2635919: Convenience method to convert std::map to a python dict.
|
||||||
|
|
||||||
2011-04-29: szager
|
2011-04-29: szager
|
||||||
Fixed bug 2811549: return non-const iterators from STL
|
Fixed bug 2811549: return non-const iterators from STL
|
||||||
methods begin(), end(), rbegin(), rend().
|
methods begin(), end(), rbegin(), rend().
|
||||||
|
|
|
||||||
|
|
@ -43,29 +43,33 @@
|
||||||
typedef std::map<K,T> map_type;
|
typedef std::map<K,T> map_type;
|
||||||
typedef typename map_type::const_iterator const_iterator;
|
typedef typename map_type::const_iterator const_iterator;
|
||||||
typedef typename map_type::size_type size_type;
|
typedef typename map_type::size_type size_type;
|
||||||
|
|
||||||
|
static PyObject *asdict(const map_type& map) {
|
||||||
|
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
|
||||||
|
size_type size = map.size();
|
||||||
|
int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
|
||||||
|
if (pysize < 0) {
|
||||||
|
PyErr_SetString(PyExc_OverflowError,
|
||||||
|
"map size not valid in python");
|
||||||
|
SWIG_PYTHON_THREAD_END_BLOCK;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
PyObject *obj = PyDict_New();
|
||||||
|
for (const_iterator i= map.begin(); i!= map.end(); ++i) {
|
||||||
|
swig::SwigVar_PyObject key = swig::from(i->first);
|
||||||
|
swig::SwigVar_PyObject val = swig::from(i->second);
|
||||||
|
PyDict_SetItem(obj, key, val);
|
||||||
|
}
|
||||||
|
SWIG_PYTHON_THREAD_END_BLOCK;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
static PyObject *from(const map_type& map) {
|
static PyObject *from(const map_type& map) {
|
||||||
swig_type_info *desc = swig::type_info<map_type>();
|
swig_type_info *desc = swig::type_info<map_type>();
|
||||||
if (desc && desc->clientdata) {
|
if (desc && desc->clientdata) {
|
||||||
return SWIG_InternalNewPointerObj(new map_type(map), desc, SWIG_POINTER_OWN);
|
return SWIG_InternalNewPointerObj(new map_type(map), desc, SWIG_POINTER_OWN);
|
||||||
} else {
|
} else {
|
||||||
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
|
return asdict(map);
|
||||||
size_type size = map.size();
|
|
||||||
int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
|
|
||||||
if (pysize < 0) {
|
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
|
||||||
"map size not valid in python");
|
|
||||||
SWIG_PYTHON_THREAD_END_BLOCK;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
PyObject *obj = PyDict_New();
|
|
||||||
for (const_iterator i= map.begin(); i!= map.end(); ++i) {
|
|
||||||
swig::SwigVar_PyObject key = swig::from(i->first);
|
|
||||||
swig::SwigVar_PyObject val = swig::from(i->second);
|
|
||||||
PyDict_SetItem(obj, key, val);
|
|
||||||
}
|
|
||||||
SWIG_PYTHON_THREAD_END_BLOCK;
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -164,6 +168,10 @@
|
||||||
swig::SwigPyIterator* iteritems(PyObject **PYTHON_SELF) {
|
swig::SwigPyIterator* iteritems(PyObject **PYTHON_SELF) {
|
||||||
return swig::make_output_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
|
return swig::make_output_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyObject* asdict() {
|
||||||
|
return swig::traits_from< Map >::asdict(*self);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue