From 2f1c34be58fc148a06d6029d5c32d28ba145d5bd Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 26 Feb 2016 08:56:35 +0000 Subject: [PATCH] Support namespaces and nspace with the interface feature for C# --- Lib/csharp/feature_interface.i | 39 ++++++++++++++---------- Source/Modules/csharp.cxx | 55 +++++++++++++++++++++++++++++++--- 2 files changed, 75 insertions(+), 19 deletions(-) diff --git a/Lib/csharp/feature_interface.i b/Lib/csharp/feature_interface.i index 89d2dbffe..4ed736b05 100644 --- a/Lib/csharp/feature_interface.i +++ b/Lib/csharp/feature_interface.i @@ -1,35 +1,44 @@ -%define DECLARE_INTERFACE_(INTERFACE, IMPL, CTYPE...) +%define DECLARE_INTERFACE_(INTERFACE, CTYPE...) %feature("interface", name="INTERFACE") CTYPE; -%typemap(cstype) CTYPE, CTYPE *, CTYPE [], CTYPE &, CTYPE *const& "INTERFACE" +%typemap(cstype) CTYPE "$&interfacename" +%typemap(cstype) CTYPE *, CTYPE [], CTYPE & "$interfacename" +%typemap(cstype) CTYPE *const& "$*interfacename" %typemap(csin) CTYPE, CTYPE & "$csinput.SWIGInterfaceUpcast()" %typemap(csin) CTYPE *, CTYPE *const&, CTYPE [] "$csinput == null ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : $csinput.SWIGInterfaceUpcast()" %typemap(csout, excode=SWIGEXCODE) CTYPE { - IMPL ret = new IMPL($imcall, true);$excode - return (INTERFACE)ret; + $&csclassname ret = new $&csclassname($imcall, true);$excode + return ($&interfacename)ret; } %typemap(csout, excode=SWIGEXCODE) CTYPE & { - IMPL ret = new IMPL($imcall, $owner);$excode - return (INTERFACE)ret; + $csclassname ret = new $csclassname($imcall, $owner);$excode + return ($interfacename)ret; } -%typemap(csout, excode=SWIGEXCODE) CTYPE *, CTYPE *const&, CTYPE [] { +%typemap(csout, excode=SWIGEXCODE) CTYPE *, CTYPE [] { global::System.IntPtr cPtr = $imcall; - IMPL ret = (cPtr == global::System.IntPtr.Zero) ? null : new IMPL(cPtr, $owner);$excode - return (INTERFACE)ret; + $csclassname ret = (cPtr == global::System.IntPtr.Zero) ? null : new $csclassname(cPtr, $owner);$excode + return ($interfacename)ret; } -%typemap(csdirectorin) CTYPE, CTYPE & "(INTERFACE)new IMPL($iminput, false)" -%typemap(csdirectorin) CTYPE *, CTYPE *const&, CTYPE [] "($iminput == global::System.IntPtr.Zero) ? null : (INTERFACE)new IMPL($iminput, false)" +%typemap(csout, excode=SWIGEXCODE) CTYPE *const& { + global::System.IntPtr cPtr = $imcall; + $*csclassname ret = (cPtr == global::System.IntPtr.Zero) ? null : new $*csclassname(cPtr, $owner);$excode + return ($*interfacename)ret; + } +%typemap(csdirectorin) CTYPE "($&interfacename)new $&csclassname($iminput, false)" +%typemap(csdirectorin) CTYPE & "($interfacename)new $csclassname($iminput, false)" +%typemap(csdirectorin) CTYPE *, CTYPE [] "($iminput == global::System.IntPtr.Zero) ? null : ($interfacename)new $csclassname($iminput, false)" +%typemap(csdirectorin) CTYPE *const& "($iminput == global::System.IntPtr.Zero) ? null : ($*interfacename)new $*csclassname($iminput, false)" %typemap(csdirectorout) CTYPE, CTYPE *, CTYPE *const&, CTYPE [], CTYPE & "$cscall.SWIGInterfaceUpcast()" -%typemap(csinterfacecode, declaration=" [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]\n global::System.Runtime.InteropServices.HandleRef SWIGInterfaceUpcast();\n", cptrmethod="$interfacename_SWIGInterfaceUpcast") CTYPE %{ +%typemap(csinterfacecode, declaration=" [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]\n global::System.Runtime.InteropServices.HandleRef SWIGInterfaceUpcast();\n", cptrmethod=#INTERFACE##"_SWIGInterfaceUpcast") CTYPE %{ [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] - global::System.Runtime.InteropServices.HandleRef $interfacename.SWIGInterfaceUpcast() { - return new global::System.Runtime.InteropServices.HandleRef(($interfacename)this, $imclassname.$csclazzname$interfacename_SWIGInterfaceUpcast(swigCPtr.Handle)); + global::System.Runtime.InteropServices.HandleRef INTERFACE.SWIGInterfaceUpcast() { + return new global::System.Runtime.InteropServices.HandleRef(this, $imclassname.$csclazzname##INTERFACE##_SWIGInterfaceUpcast(swigCPtr.Handle)); } %} %enddef %define DECLARE_INTERFACE_RENAME(INTERFACE, IMPL, CTYPE...) %rename (IMPL) CTYPE; -DECLARE_INTERFACE_(INTERFACE, IMPL, CTYPE) +DECLARE_INTERFACE_(INTERFACE, CTYPE) %enddef %define DECLARE_INTERFACE(INTERFACE, CTYPE...) diff --git a/Source/Modules/csharp.cxx b/Source/Modules/csharp.cxx index 10b98d279..1c7369a8f 100644 --- a/Source/Modules/csharp.cxx +++ b/Source/Modules/csharp.cxx @@ -1669,6 +1669,20 @@ public: return ret; } + /* ----------------------------------------------------------------------------- + * getInterfaceName() + * ----------------------------------------------------------------------------- */ + + String *getInterfaceName(SwigType *t) { + String *interface_name = NULL; + if (proxy_flag) { + Node *n = classLookup(t); + if (n && Getattr(n, "feature:interface:name")) + interface_name = getQualifiedInterfaceName(n); + } + return interface_name; + } + /* ----------------------------------------------------------------------------- * addInterfaceNameAndUpcasts() * ----------------------------------------------------------------------------- */ @@ -1687,13 +1701,11 @@ public: String *interface_code = Copy(typemapLookup(base, "csinterfacecode", Getattr(base, "classtypeobj"), WARN_CSHARP_TYPEMAP_INTERFACECODE_UNDEF, attributes)); String *cptr_method_name = 0; if (interface_code) { - Replaceall(interface_code, "$interfacename", interface_name); Printv(interface_upcasts, interface_code, NIL); cptr_method_name = Copy(Getattr(attributes, "tmap:csinterfacecode:cptrmethod")); } if (!cptr_method_name) - cptr_method_name = NewString("$interfacename_GetInterfaceCPtr"); - Replaceall(cptr_method_name, "$interfacename", interface_name); + cptr_method_name = NewStringf("%s_GetInterfaceCPtr", interface_name); Replaceall(cptr_method_name, ".", "_"); String *upcast_method = Swig_name_member(getNSpace(), proxy_class_name, cptr_method_name); @@ -2011,7 +2023,6 @@ public: if (interface_code) { String *interface_declaration = Copy(Getattr(attributes, "tmap:csinterfacecode:declaration")); if (interface_declaration) { - Replaceall(interface_declaration, "$interfacename", interface_name); Printv(f_interface, interface_declaration, NIL); Delete(interface_declaration); } @@ -3389,6 +3400,28 @@ public: substitution_performed = true; Delete(classnametype); } + if (Strstr(tm, "$interfacename")) { + SwigType *interfacenametype = Copy(strippedtype); + substituteInterfacenameSpecialVariable(interfacenametype, tm, "$interfacename"); + substitution_performed = true; + Delete(interfacenametype); + } + if (Strstr(tm, "$*interfacename")) { + SwigType *interfacenametype = Copy(strippedtype); + Delete(SwigType_pop(interfacenametype)); + if (Len(interfacenametype) > 0) { + substituteInterfacenameSpecialVariable(interfacenametype, tm, "$*interfacename"); + substitution_performed = true; + } + Delete(interfacenametype); + } + if (Strstr(tm, "$&interfacename")) { + SwigType *interfacenametype = Copy(strippedtype); + SwigType_add_pointer(interfacenametype); + substituteInterfacenameSpecialVariable(interfacenametype, tm, "$&interfacename"); + substitution_performed = true; + Delete(interfacenametype); + } Delete(strippedtype); Delete(type); @@ -3434,6 +3467,20 @@ public: Delete(replacementname); } + /* ----------------------------------------------------------------------------- + * substituteInterfacenameSpecialVariable() + * ----------------------------------------------------------------------------- */ + + void substituteInterfacenameSpecialVariable(SwigType *interfacenametype, String *tm, const char *interfacenamespecialvariable) { + + String *interfacename = getInterfaceName(interfacenametype); + if (interfacename) { + String *replacementname = Copy(interfacename); + Replaceall(tm, interfacenamespecialvariable, replacementname); + Delete(replacementname); + } + } + /* ----------------------------------------------------------------------------- * emitTypeWrapperClass() * ----------------------------------------------------------------------------- */