From 5b0b80a96466759dcd3cce64c3def067364ea577 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Thu, 28 Apr 2005 21:59:17 +0000 Subject: [PATCH] support for asymmetric type marshalling - added out attribute for ctype, imtype and cstype typemaps git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@7172 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Doc/Manual/CSharp.html | 38 ++++++++++++++++++++++++++++++---- SWIG/Source/Modules/csharp.cxx | 18 ++++++++++++++-- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/SWIG/Doc/Manual/CSharp.html b/SWIG/Doc/Manual/CSharp.html index aa3d1f5a4..a7be3ed24 100644 --- a/SWIG/Doc/Manual/CSharp.html +++ b/SWIG/Doc/Manual/CSharp.html @@ -162,6 +162,40 @@ $jnicall -> $imcall +
  • +

    +The intermediary classname has PINVOKE appended after the module name instead of JNI, for example modulenamePINVOKE. +

    +
  • + +
  • +

    +Support for asymmetric type marshalling. The 'ctype', 'imtype' and 'cstype' typemaps support an optional out attribute which is used for output types. +If this typemap attribute is specified, then the type specified in the attribute is used for output types. +the type specified in the typemap itself is used for the input type. +If this typemap attribute is not specified, then the type used for both input and output is the type specified in the typemap. +An example shows that char * could be marshalled in different ways, +

    + +
    +
    +%typemap(imtype, out="IntPtr") char * "string"
    +char * function(char *);
    +
    +
    + +

    +The output type is thus IntPtr and the input type is string. The resulting intermediary C# code is: +

    + +
    +
    +public static extern IntPtr function(string jarg1);
    +
    +
    + +
  • +

    @@ -171,10 +205,6 @@ The special variable will get translated into the value specified by the -dl if specified, otherwise it is equivalent to the $module special variable.

    -

    -The intermediary classname has PINVOKE appended after the module name instead of JNI, for example modulenamePINVOKE. -

    -

    The directory Examples/csharp has a number of simple examples. Visual Studio .NET 2003 solution and project files are available for compiling with the Microsoft .NET C# compiler on Windows. diff --git a/SWIG/Source/Modules/csharp.cxx b/SWIG/Source/Modules/csharp.cxx index 43c0204c5..8e4d33b95 100644 --- a/SWIG/Source/Modules/csharp.cxx +++ b/SWIG/Source/Modules/csharp.cxx @@ -482,6 +482,9 @@ class CSHARP : public Language { /* Get return types */ if ((tm = Swig_typemap_lookup_new("ctype",n,"",0))) { + String *ctypeout = Getattr(n,"tmap:ctype:out"); // the type in the ctype typemap's out attribute overrides the type in the typemap + if (ctypeout) + tm = ctypeout; Printf(c_return_type,"%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CTYPE_UNDEF, input_file, line_number, @@ -489,6 +492,9 @@ class CSHARP : public Language { } if ((tm = Swig_typemap_lookup_new("imtype",n,"",0))) { + String *imtypeout = Getattr(n,"tmap:imtype:out"); // the type in the imtype typemap's out attribute overrides the type in the typemap + if (imtypeout) + tm = imtypeout; Printf(im_return_type,"%s", tm); } else { Swig_warning(WARN_CSHARP_TYPEMAP_CSTYPE_UNDEF, input_file, line_number, @@ -1060,6 +1066,9 @@ class CSHARP : public Language { bool classname_substituted_flag = false; if ((tm = Swig_typemap_lookup_new("cstype",n,"",0))) { + String *cstypeout = Getattr(n,"tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap + if (cstypeout) + tm = cstypeout; classname_substituted_flag = substituteClassname(t, tm); Printf(return_type, "%s", tm); } else { @@ -1513,6 +1522,9 @@ class CSHARP : public Language { if ((tm = Swig_typemap_lookup_new("cstype",n,"",0))) { // Note that in the case of polymorphic (covariant) return types, the method's return type is changed to be the base of the C++ return type SwigType *virtualtype = Getattr(n,"virtual:type"); + String *cstypeout = Getattr(n,"tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap + if (cstypeout) + tm = cstypeout; substituteClassname(virtualtype ? virtualtype : t, tm); Printf(return_type, "%s", tm); if (virtualtype) @@ -1869,8 +1881,7 @@ class CSHARP : public Language { String *getOverloadedName(Node *n) { - /* Although PInvoke functions are designed to handle overloaded functions, - * a C# IntPtr is used for all classes in the SWIG intermediary class. + /* A C# HandleRef is used for all classes in the SWIG intermediary class. * The intermediary class methods are thus mangled when overloaded to give * a unique name. */ String *overloaded_name = NewStringf("%s", Getattr(n,"sym:name")); @@ -1914,6 +1925,9 @@ class CSHARP : public Language { /* Get return types */ if ((tm = Swig_typemap_lookup_new("cstype",n,"",0))) { + String *cstypeout = Getattr(n,"tmap:cstype:out"); // the type in the cstype typemap's out attribute overrides the type in the typemap + if (cstypeout) + tm = cstypeout; substituteClassname(t, tm); Printf(return_type, "%s", tm); } else {