From a88334f9c2eec11428fb214fa97577fab8af87d8 Mon Sep 17 00:00:00 2001 From: hulucc Date: Wed, 2 Mar 2016 15:07:08 +0800 Subject: [PATCH] add wchar_t caster --- include/pybind11/cast.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index dc0fc9335..1428d4cd4 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -404,6 +404,39 @@ protected: std::string value; }; +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 wchar_t *src, return_value_policy /* policy */, handle /* parent */) { + return PyUnicode_FromWideChar(src, wcslen(src)); + } + + static handle cast(wchar_t src, return_value_policy /* policy */, handle /* parent */) { + wchar_t wstr[2] = { src, L'\0' }; + return PyUnicode_FromWideChar(wstr, 1); + } + + operator wchar_t*() { return (wchar_t *)value.c_str(); } + operator wchar_t() { if (value.length() > 0) return value[0]; else return L'\0'; } + + static PYBIND11_DESCR name() { return type_descr(_(PYBIND11_STRING_NAME)); } +protected: + std::wstring value; +}; + template class type_caster> { typedef std::pair type; public: