From 4b31a3ee50f2fa9dead26ba00a58fa73e12f576f Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 15 Dec 2006 23:50:39 +0000 Subject: [PATCH] add C# std::string and wchar typemaps git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9621 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- CHANGES.current | 3 + .../test-suite/csharp/li_std_wstring_runme.cs | 76 ++++++++++++ Lib/csharp/std_wstring.i | 117 ++++++++++++++++++ Lib/csharp/wchar.i | 105 ++++++++++++++++ 4 files changed, 301 insertions(+) create mode 100755 Examples/test-suite/csharp/li_std_wstring_runme.cs create mode 100755 Lib/csharp/std_wstring.i create mode 100755 Lib/csharp/wchar.i diff --git a/CHANGES.current b/CHANGES.current index 01b32088c..355a8710e 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -1,6 +1,9 @@ Version 1.3.32 (in progress) ============================ +12/14/2006: wsfulton + [C#] Add std::wstring and wchar_t typemaps + 12/14/2006: olly [php] Fix bug #1613673 (bad PHP5 code generated for getters and setters). diff --git a/Examples/test-suite/csharp/li_std_wstring_runme.cs b/Examples/test-suite/csharp/li_std_wstring_runme.cs new file mode 100755 index 000000000..fe663a3e0 --- /dev/null +++ b/Examples/test-suite/csharp/li_std_wstring_runme.cs @@ -0,0 +1,76 @@ +using System; +using li_std_wstringNamespace; + +public class runme +{ + static void Main() + { + char y='h'; + + if (li_std_wstring.test_wcvalue(y) != y) + throw new Exception("bad string mapping:" + li_std_wstring.test_wcvalue(y)); + + if (li_std_wstring.test_wcvalue_w() != 'W') + throw new Exception("bad string mapping:" + li_std_wstring.test_wcvalue_w()); + + string x="hello"; + + if (li_std_wstring.test_ccvalue(x) != x) + throw new Exception("bad string mapping"); + + if (li_std_wstring.test_cvalue(x) != x) + throw new Exception("bad string mapping"); + + + if (li_std_wstring.test_value(x) != x) + throw new Exception("bad string mapping: " + x + li_std_wstring.test_value(x)); + + if (li_std_wstring.test_const_reference(x) != x) + throw new Exception("bad string mapping"); + + + string s = "he"; + s = s + "llo"; + + if (s != x) + throw new Exception("bad string mapping: " + s + x); + + if (li_std_wstring.test_value(s) != x) + throw new Exception("bad string mapping"); + + if (li_std_wstring.test_const_reference(s) != x) + throw new Exception("bad string mapping"); + + string a = s; + + if (li_std_wstring.test_value(a) != x) + throw new Exception("bad string mapping"); + + if (li_std_wstring.test_const_reference(a) != x) + throw new Exception("bad string mapping"); + + string b = " world"; + + if (a + b != "hello world") + throw new Exception("bad string mapping"); + + if (a + " world" != "hello world") + throw new Exception("bad string mapping"); + + if ("hello" + b != "hello world") + throw new Exception("bad string mapping"); + + s = "hello world"; + + B myB = new B("hi"); + + myB.name = "hello"; + if (myB.name != "hello") + throw new Exception("bad string mapping"); + + myB.a = "hello"; + if (myB.a != "hello") + throw new Exception("bad string mapping"); + } +} + diff --git a/Lib/csharp/std_wstring.i b/Lib/csharp/std_wstring.i new file mode 100755 index 000000000..f1706f563 --- /dev/null +++ b/Lib/csharp/std_wstring.i @@ -0,0 +1,117 @@ +/* ----------------------------------------------------------------------------- + * See the LICENSE file for information on copyright, usage and redistribution + * of SWIG, and the README file for authors - http://www.swig.org/release.html. + * + * std_wstring.i + * + * Typemaps for std::wstring and const std::wstring& + * These are mapped to a C# String and are passed around by value. + * + * To use non-const std::wstring references use the following %apply. Note + * that they are passed by value. + * %apply const std::wstring & {std::wstring &}; + * ----------------------------------------------------------------------------- */ + +%include + +%{ +#include +%} + +namespace std { + +%naturalvar wstring; + +class wstring; + +// wstring +%typemap(ctype, out="void *") wstring "wchar_t *" +%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPWStr)]") wstring "string" +%typemap(cstype) wstring "string" +%typemap(csdirectorin) wstring "$iminput" +%typemap(csdirectorout) wstring "$cscall" + +%typemap(in, canthrow=1) wstring +%{ if (!$input) { + SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0); + return $null; + } + $1 = std::wstring($input); %} +%typemap(out) wstring %{ $result = SWIG_csharp_wstring_callback($1.c_str()); %} + +%typemap(directorout, canthrow=1) wstring +%{ if (!$input) { + SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0); + return $null; + } + $result = std::wstring($input); %} + +%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %} + +%typemap(csin) wstring "$csinput" +%typemap(csout, excode=SWIGEXCODE) wstring { + string ret = $imcall;$excode + return ret; + } + +%typemap(typecheck) wstring = wchar_t *; + +%typemap(throws, canthrow=1) wstring +%{ (void)$1; + SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "wstring exception"); + return $null; %} + +// const wstring & +%typemap(ctype, out="void *") const wstring & "wchar_t *" +%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPWStr)]") const wstring & "string" +%typemap(cstype) const wstring & "string" + +%typemap(csdirectorin) const wstring & "$iminput" +%typemap(csdirectorout) const wstring & "$cscall" + +%typemap(in, canthrow=1) const wstring & +%{ if (!$input) { + SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0); + return $null; + } + std::wstring $1_str($input); + $1 = &$1_str; %} +%typemap(out) const wstring & %{ $result = SWIG_csharp_wstring_callback($1->c_str()); %} + +%typemap(csin) const wstring & "$csinput" +%typemap(csout, excode=SWIGEXCODE) const wstring & { + string ret = $imcall;$excode + return ret; + } + +%typemap(directorout, canthrow=1, warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const wstring & +%{ if (!$input) { + SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0); + return $null; + } + /* possible thread/reentrant code problem */ + static std::wstring $1_str; + $1_str = $input; + $result = &$1_str; %} + +%typemap(directorin) const wstring & %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %} + +%typemap(csvarin, excode=SWIGEXCODE2) const wstring & %{ + set { + $imcall;$excode + } %} +%typemap(csvarout, excode=SWIGEXCODE2) const wstring & %{ + get { + string ret = $imcall;$excode + return ret; + } %} + +%typemap(typecheck) const wstring & = wchar_t *; + +%typemap(throws, canthrow=1) const wstring & +%{ (void)$1; + SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "wstring exception"); + return $null; %} + +} + diff --git a/Lib/csharp/wchar.i b/Lib/csharp/wchar.i new file mode 100755 index 000000000..be87560c3 --- /dev/null +++ b/Lib/csharp/wchar.i @@ -0,0 +1,105 @@ +/* ----------------------------------------------------------------------------- + * See the LICENSE file for information on copyright, usage and redistribution + * of SWIG, and the README file for authors - http://www.swig.org/release.html. + * + * wchar.i + * + * Typemaps for the wchar_t type + * These are mapped to a C# String and are passed around by value. + * + * Support code for wide strings can be turned off by defining SWIG_CSHARP_NO_WSTRING_HELPER + * + * ----------------------------------------------------------------------------- */ + +#if !defined(SWIG_CSHARP_NO_WSTRING_HELPER) +#if !defined(SWIG_CSHARP_WSTRING_HELPER_) +#define SWIG_CSHARP_WSTRING_HELPER_ +%insert(runtime) %{ +/* Callback for returning strings to C# without leaking memory */ +typedef void * (SWIGSTDCALL* SWIG_CSharpWStringHelperCallback)(const wchar_t *); +static SWIG_CSharpWStringHelperCallback SWIG_csharp_wstring_callback = NULL; +%} + +%pragma(csharp) imclasscode=%{ + protected class SWIGWStringHelper { + + public delegate string SWIGWStringDelegate(IntPtr message); + static SWIGWStringDelegate wstringDelegate = new SWIGWStringDelegate(CreateWString); + + [DllImport("$dllimport", EntryPoint="SWIGRegisterWStringCallback_$module")] + public static extern void SWIGRegisterWStringCallback_$module(SWIGWStringDelegate wstringDelegate); + + static string CreateWString([MarshalAs(UnmanagedType.LPWStr)]IntPtr cString) { + return System.Runtime.InteropServices.Marshal.PtrToStringUni(cString); + } + + static SWIGWStringHelper() { + SWIGRegisterWStringCallback_$module(wstringDelegate); + } + } + + static protected SWIGWStringHelper swigWStringHelper = new SWIGWStringHelper(); +%} + +%insert(runtime) %{ +#ifdef __cplusplus +extern "C" +#endif +SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringCallback_$module(SWIG_CSharpWStringHelperCallback callback) { + SWIG_csharp_wstring_callback = callback; +} +%} +#endif // SWIG_CSHARP_WSTRING_HELPER_ +#endif // SWIG_CSHARP_NO_WSTRING_HELPER + + +// wchar_t +%typemap(ctype) wchar_t "wchar_t" +%typemap(imtype) wchar_t "char" +%typemap(cstype) wchar_t "char" + +%typemap(csin) wchar_t "$csinput" +%typemap(csout, excode=SWIGEXCODE) wchar_t { + char ret = $imcall;$excode + return ret; + } +%typemap(csvarin, excode=SWIGEXCODE2) wchar_t %{ + set { + $imcall;$excode + } %} +%typemap(csvarout, excode=SWIGEXCODE2) wchar_t %{ + get { + char ret = $imcall;$excode + return ret; + } %} + +%typemap(in) wchar_t %{ $1 = ($1_ltype)$input; %} +%typemap(out) wchar_t %{ $result = (wchar_t)$1; %} + +%typemap(typecheck) wchar_t = char; + +// wchar_t * +%typemap(ctype) wchar_t * "wchar_t *" +%typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPWStr)]", out="IntPtr" ) wchar_t * "string" +%typemap(cstype) wchar_t * "string" + +%typemap(csin) wchar_t * "$csinput" +%typemap(csout, excode=SWIGEXCODE) wchar_t * { + string ret = System.Runtime.InteropServices.Marshal.PtrToStringUni($imcall);$excode + return ret; + } +%typemap(csvarin, excode=SWIGEXCODE2) wchar_t * %{ + set { + $imcall;$excode + } %} +%typemap(csvarout, excode=SWIGEXCODE2) wchar_t * %{ + get { + string ret = $imcall;$excode + return ret; + } %} + +%typemap(in) wchar_t * %{ $1 = ($1_ltype)$input; %} +%typemap(out) wchar_t * %{ $result = (wchar_t *)$1; %} + +%typemap(typecheck) wchar_t * = char *; +