diff options
Diffstat (limited to 'src/pyutils.h')
-rw-r--r-- | src/pyutils.h | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/pyutils.h b/src/pyutils.h index a9e968e0..54d6fa28 100644 --- a/src/pyutils.h +++ b/src/pyutils.h @@ -106,6 +106,35 @@ struct register_optional_to_python : public boost::noncopyable } }; +template <typename T1, typename T2> +struct PairToTupleConverter +{ + static PyObject * convert(const std::pair<T1, T2>& pair) { + return boost::python::incref + (boost::python::make_tuple(pair.first, pair.second).ptr()); + } +}; + +template <typename MapType> +struct map_value_type_converter +{ + map_value_type_converter() { + boost::python::to_python_converter + <typename MapType::value_type, + PairToTupleConverter<const typename MapType::key_type, + typename MapType::mapped_type> >(); + } +}; + +template <typename T> +PyObject * str_to_py_unicode(const T& str) +{ + using namespace boost::python; + PyObject * pstr = PyString_FromString(str.c_str()); + PyObject * uni = PyUnicode_FromEncodedObject(pstr, "UTF-8", NULL); + return object(handle<>(borrowed(uni))).ptr(); +} + namespace boost { namespace python { // Use expr to create the PyObject corresponding to x |