From bb39235c9ce53b8587a929c7e0f98beda2891be3 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 4 May 2022 19:33:47 +0100 Subject: [PATCH] Add C# wchar_t * director typemaps More Python std::wstring directors Python testing --- CHANGES.current | 3 +++ .../test-suite/csharp/director_wstring_runme.cs | 2 -- Examples/test-suite/director_wstring.i | 6 +----- .../test-suite/python/director_wstring_runme.py | 16 ++++++++++++++++ Lib/csharp/wchar.i | 11 ++++++++++- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index cfa0af2db..f14dc24ad 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-05-04: wsfulton + [C#] Add C# wchar_t * director typemaps + 2022-04-20: cminyard Fix an issue where newlines were not properly generated for godirectorin typemaps. If you have a virtual function diff --git a/Examples/test-suite/csharp/director_wstring_runme.cs b/Examples/test-suite/csharp/director_wstring_runme.cs index fcb6e7afa..7258e7f94 100644 --- a/Examples/test-suite/csharp/director_wstring_runme.cs +++ b/Examples/test-suite/csharp/director_wstring_runme.cs @@ -17,11 +17,9 @@ public class runme if (b.get_first() != "hello world!") throw new ApplicationException("Incorrect get_first:" + b.get_first()); - /* b.call_process_func(); if (b.smem != "hello") throw new ApplicationException("Incorrect smem:" + b.smem); - */ b.call_process_wstring_func(); if (b.smem != "hello (wstring)") diff --git a/Examples/test-suite/director_wstring.i b/Examples/test-suite/director_wstring.i index 10dd032d0..02df9d64c 100644 --- a/Examples/test-suite/director_wstring.i +++ b/Examples/test-suite/director_wstring.i @@ -39,15 +39,11 @@ struct A std::vector m_strings; + virtual const wchar_t * wchar_out() { return L"ciao"; } -#if !defined(SWIGCSHARP) virtual void process_text(const wchar_t *text) {} -#else -// temp until wchar_t* fixed - virtual void process_text(std::wstring text) {} virtual void process_wstring_text(std::wstring text) {} virtual void process_wstring_ref_text(const std::wstring& text) {} -#endif virtual std::wstring multiple_params_val(const std::wstring& p1, const std::wstring& p2, std::wstring p3, std::wstring p4) const { return get_first(); } diff --git a/Examples/test-suite/python/director_wstring_runme.py b/Examples/test-suite/python/director_wstring_runme.py index 5facc1f1d..659cf1854 100644 --- a/Examples/test-suite/python/director_wstring_runme.py +++ b/Examples/test-suite/python/director_wstring_runme.py @@ -12,6 +12,12 @@ class B(A): def process_text(self, s): self.smem = s + def process_wstring_text(self, s): + self.smem = s + " (wstring)" + + def process_wstring_ref_text(self, s): + self.smem = s + " (wstring ref)" + b = B("hello") @@ -24,3 +30,13 @@ b.call_process_func() if b.smem != "hello": raise RuntimeError("smem: {}".format(smem)) + +b.call_process_wstring_func() + +if b.smem != "hello (wstring)": + raise RuntimeError("smem: {}".format(smem)) + +b.call_process_wstring_ref_func() + +if b.smem != "hello (wstring ref)": + raise RuntimeError("smem: {}".format(smem)) diff --git a/Lib/csharp/wchar.i b/Lib/csharp/wchar.i index d0f1fe0f6..2ada4df75 100644 --- a/Lib/csharp/wchar.i +++ b/Lib/csharp/wchar.i @@ -211,9 +211,13 @@ static void Swig_csharp_UTF16ToWCharPtrFree(wchar_t *str) { %typemap(ctype, out="void *") wchar_t * "unsigned short *" %typemap(imtype, inattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]", - outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]" + outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]", + directorinattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]", + directoroutattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]" ) wchar_t * "string" %typemap(cstype) wchar_t * "string" +%typemap(csdirectorin) wchar_t * "$iminput" +%typemap(csdirectorout) wchar_t * "$cscall" %typemap(csin) wchar_t * "$csinput" %typemap(csout, excode=SWIGEXCODE) wchar_t * { @@ -238,6 +242,11 @@ static void Swig_csharp_UTF16ToWCharPtrFree(wchar_t *str) { %typemap(freearg, fragment="Swig_csharp_UTF16ToWCharPtrFree") wchar_t * %{ Swig_csharp_UTF16ToWCharPtrFree($1); %} +%typemap(directorout, warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) wchar_t * +%{ $result = Swig_csharp_UTF16ToWCharPtr($input); %} + +%typemap(directorin) wchar_t * %{ $input = SWIG_csharp_wstring_with_length_callback($1, (int)wcslen($1)); %} + %typemap(typecheck) wchar_t * = char *; %typemap(throws, canthrow=1, fragment="") wchar_t *