add C# std::string and wchar typemaps

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9621 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2006-12-15 23:50:39 +00:00
commit 4b31a3ee50
4 changed files with 301 additions and 0 deletions

View file

@ -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).

View file

@ -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");
}
}

117
Lib/csharp/std_wstring.i Executable file
View file

@ -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 <wchar.i>
%{
#include <string>
%}
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; %}
}

105
Lib/csharp/wchar.i Executable file
View file

@ -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 *;