From 8a2343f31dfb128f01816b197734c8d3dea77e6e Mon Sep 17 00:00:00 2001 From: Marcelo Matus Date: Tue, 6 Apr 2004 06:31:24 +0000 Subject: [PATCH] added wchar_t type git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5835 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Lib/python/pyprimtypes.swg | 38 ++++++++++++++++++++++++++++++--- SWIG/Lib/python/python.swg | 1 + SWIG/Lib/python/pywstrings.swg | 4 ++-- SWIG/Lib/python/std_common.i | 7 ++++-- SWIG/Lib/python/std_wstring.i | 4 ++-- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/SWIG/Lib/python/pyprimtypes.swg b/SWIG/Lib/python/pyprimtypes.swg index a15793dae..8e23870c6 100644 --- a/SWIG/Lib/python/pyprimtypes.swg +++ b/SWIG/Lib/python/pyprimtypes.swg @@ -18,8 +18,6 @@ SWIG_define(SWIG_From(int), PyInt_FromLong); SWIG_define(SWIG_From(long), PyInt_FromLong); SWIG_define(SWIG_From(float), PyFloat_FromDouble); SWIG_define(SWIG_From(double), PyFloat_FromDouble); -SWIG_define(SWIG_From(float), PyFloat_FromDouble); -SWIG_define(SWIG_From(double), PyFloat_FromDouble); } %fragment("","header") %{ @@ -499,6 +497,38 @@ SWIGSTATICINLINE(int) } } +%fragment(SWIG_From_frag(wchar_t),"header") { +SWIGSTATICINLINE(PyObject*) + SWIG_From(wchar_t)(wchar_t c) +{ + return PyUnicode_FromWideChar(&c, 1); +} +} + +%fragment(SWIG_AsVal_frag(wchar_t),"header", + fragment="SWIG_AsWCharArray", + fragment="SWIG_CheckLongInRange", + fragment=SWIG_AsVal_frag(long)) { +#include +SWIGSTATICINLINE(int) + SWIG_AsVal(wchar_t)(PyObject *obj, wchar_t *val) +{ + const char* errmsg = val ? "wchar_t" : 0; + long v; + if (SWIG_AsVal(long)(obj, &v)) { + if (SWIG_CheckLongInRange(v, WCHAR_MIN, WCHAR_MAX, errmsg)) { + if (val) *val = v; + return 1; + } else { + return 0; + } + } else { + PyErr_Clear(); + return SWIG_AsWCharArray(obj, val, 1); + } + } +} + %fragment(SWIG_From_frag(bool),"header") { SWIGSTATICINLINE(PyObject*) SWIG_From(bool)(bool value) @@ -529,7 +559,7 @@ SWIGSTATICINLINE(int) return 1; } if (val) { - PyErr_SetString(PyExc_TypeError, "a float is expected"); + PyErr_SetString(PyExc_TypeError, "a bool is expected"); } return 0; } @@ -576,6 +606,7 @@ _apply_macro(Macro, unsigned long long , __VA_ARGS__); _apply_macro(Macro, float , __VA_ARGS__); _apply_macro(Macro, double , __VA_ARGS__); _apply_macro(Macro, char , __VA_ARGS__); +_apply_macro(Macro, wchar_t , __VA_ARGS__); %enddef /* apply the Macro(Type) to all the C++ types */ @@ -606,6 +637,7 @@ Macro(SWIG_CCode(UINT128), unsigned long long); Macro(SWIG_CCode(FLOAT), float); Macro(SWIG_CCode(DOUBLE), double); Macro(SWIG_CCode(CHAR), char); +Macro(SWIG_CCode(UNICHAR), wchar_t); %enddef /* ------------------------------------------------------------ diff --git a/SWIG/Lib/python/python.swg b/SWIG/Lib/python/python.swg index 29c0ab956..d38e0ff16 100644 --- a/SWIG/Lib/python/python.swg +++ b/SWIG/Lib/python/python.swg @@ -36,6 +36,7 @@ %include "pyvoid.swg" %include "pyobject.swg" %include "pystrings.swg" +%include "pywstrings.swg" %include "pyvaltypes.swg" %include "pyptrtypes.swg" %include "pyprimtypes.swg" diff --git a/SWIG/Lib/python/pywstrings.swg b/SWIG/Lib/python/pywstrings.swg index 37b165e94..50edbc736 100644 --- a/SWIG/Lib/python/pywstrings.swg +++ b/SWIG/Lib/python/pywstrings.swg @@ -241,7 +241,7 @@ SWIG_FromWCharArray(const wchar_t * carray, size_t size) /* typecheck */ -%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, +%typemap(typecheck,precedence=SWIG_TYPECHECK_UNISTRING, fragment="SWIG_AsWCharPtr") wchar_t *, wchar_t const*, wchar_t *const, wchar_t const *const, wchar_t const*&, wchar_t *const&, wchar_t const *const & @@ -335,7 +335,7 @@ SWIG_FromWCharArray(const wchar_t * carray, size_t size) /* typecheck */ -%typemap(typecheck,precedence=SWIG_TYPECHECK_STRING, +%typemap(typecheck,precedence=SWIG_TYPECHECK_UNISTRING, fragment="SWIG_AsWCharArray") wchar_t [ANY], const wchar_t[ANY] { diff --git a/SWIG/Lib/python/std_common.i b/SWIG/Lib/python/std_common.i index 4c23bf5f2..4a33921e7 100644 --- a/SWIG/Lib/python/std_common.i +++ b/SWIG/Lib/python/std_common.i @@ -465,8 +465,6 @@ namespace swigpy { %swig_equal_type(std::complex); /* the operators <,>,<=,=> can used with these types */ -%swig_order_type(std::string); -%swig_order_type(std::basic_string); %swig_order_type(signed char); %swig_order_type(unsigned char); %swig_order_type(short); @@ -480,6 +478,11 @@ namespace swigpy { %swig_order_type(float); %swig_order_type(double); %swig_order_type(char); +%swig_order_type(wchar_t); +%swig_order_type(std::string); +%swig_order_type(std::basic_string); +%swig_order_type(std::wstring); +%swig_order_type(std::basic_string); // // Backward compatibility diff --git a/SWIG/Lib/python/std_wstring.i b/SWIG/Lib/python/std_wstring.i index 3603d3565..e5083b55a 100644 --- a/SWIG/Lib/python/std_wstring.i +++ b/SWIG/Lib/python/std_wstring.i @@ -82,5 +82,5 @@ SWIGSTATICINLINE(int) } } -%typemap_asptrfromn(SWIG_CCode(STRING), std::basic_string); -%typemap_asptrfromn(SWIG_CCode(STRING), std::wstring); +%typemap_asptrfromn(SWIG_CCode(UNISTRING), std::basic_string); +%typemap_asptrfromn(SWIG_CCode(UNISTRING), std::wstring);