From c8f68b3d92a50d48b545a96737d25877775fbd6a Mon Sep 17 00:00:00 2001 From: hulucc Date: Wed, 2 Mar 2016 13:59:39 +0800 Subject: [PATCH] add wstring caster --- include/pybind11/cast.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index a9202f6b..dc0fc933 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -348,6 +348,29 @@ public: PYBIND11_TYPE_CASTER(std::string, _(PYBIND11_STRING_NAME)); }; +template <> class type_caster { +public: + bool load(handle src, bool) { + object temp; + handle load_src = src; + if (!PyUnicode_Check(load_src.ptr())) { + temp = object(PyUnicode_FromObject(load_src.ptr()), false); + if (!temp) { PyErr_Clear(); return false; } + load_src = temp; + } + wchar_t *buffer = PyUnicode_AS_UNICODE(load_src.ptr()); + size_t length = PyUnicode_GET_SIZE(load_src.ptr()); + value = std::wstring(buffer, length); + return true; + } + + static handle cast(const std::wstring &src, return_value_policy /* policy */, handle /* parent */) { + return PyUnicode_FromWideChar(src.c_str(), src.length()); + } + + PYBIND11_TYPE_CASTER(std::wstring, _(PYBIND11_STRING_NAME)); +}; + template <> class type_caster { public: bool load(handle src, bool) {