From c737bd57131c02eed9573d13f30662397890ba0b Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 15 Jul 2022 17:41:58 +0100 Subject: [PATCH] Add C# support std::unique_ptr inputs Based on Java implementation. --- Examples/test-suite/cpp11_std_unique_ptr.i | 2 +- .../csharp/cpp11_std_unique_ptr_runme.cs | 63 +++++++++++++++++++ Lib/csharp/csharp.swg | 30 +++++++++ Lib/csharp/std_auto_ptr.i | 2 +- Lib/csharp/std_unique_ptr.i | 10 ++- 5 files changed, 103 insertions(+), 4 deletions(-) diff --git a/Examples/test-suite/cpp11_std_unique_ptr.i b/Examples/test-suite/cpp11_std_unique_ptr.i index c96047f64..5ef49456c 100644 --- a/Examples/test-suite/cpp11_std_unique_ptr.i +++ b/Examples/test-suite/cpp11_std_unique_ptr.i @@ -53,7 +53,7 @@ struct KlassInheritance : virtual Klass { } }; -#if defined(SWIGJAVA) +#if defined(SWIGJAVA) || defined (SWIGCSHARP) std::string takeKlassUniquePtr(std::unique_ptr k) { return std::string(k->getLabel()); } diff --git a/Examples/test-suite/csharp/cpp11_std_unique_ptr_runme.cs b/Examples/test-suite/csharp/cpp11_std_unique_ptr_runme.cs index db5c8cff0..5b730aa05 100644 --- a/Examples/test-suite/csharp/cpp11_std_unique_ptr_runme.cs +++ b/Examples/test-suite/csharp/cpp11_std_unique_ptr_runme.cs @@ -9,8 +9,71 @@ public class cpp11_std_unique_ptr_runme { System.Threading.Thread.Sleep(10); } + private static void checkCount(int expected_count) + { + int actual_count = Klass.getTotal_count(); + if (actual_count != expected_count) + throw new ApplicationException("Counts incorrect, expected:" + expected_count + " actual:" + actual_count); + } + public static void Main() { + // unique_ptr as input + using (Klass kin = new Klass("KlassInput")) { + checkCount(1); + string s = cpp11_std_unique_ptr.takeKlassUniquePtr(kin); + checkCount(0); + if (s != "KlassInput") + throw new ApplicationException("Incorrect string: " + s); + if (!cpp11_std_unique_ptr.is_nullptr(kin)) + throw new ApplicationException("is_nullptr failed"); + } // Dispose should not fail, even though already deleted + checkCount(0); + + using (Klass kin = new Klass("KlassInput")) { + checkCount(1); + string s = cpp11_std_unique_ptr.takeKlassUniquePtr(kin); + checkCount(0); + if (s != "KlassInput") + throw new ApplicationException("Incorrect string: " + s); + if (!cpp11_std_unique_ptr.is_nullptr(kin)) + throw new ApplicationException("is_nullptr failed"); + bool exception_thrown = false; + try { + cpp11_std_unique_ptr.takeKlassUniquePtr(kin); + } catch (ApplicationException) { + exception_thrown = true; + } + if (!exception_thrown) + throw new ApplicationException("double usage of takeKlassUniquePtr should have been an error"); + } // Dispose should not fail, even though already deleted + checkCount(0); + + using (Klass kin = new Klass("KlassInput")) { + bool exception_thrown = false; + try { + Klass notowned = cpp11_std_unique_ptr.get_not_owned_ptr(kin); + cpp11_std_unique_ptr.takeKlassUniquePtr(notowned); + } catch (ApplicationException) { + exception_thrown = true; + } + if (!exception_thrown) + throw new ApplicationException("Should have thrown 'Cannot release ownership as memory is not owned' error"); + } + checkCount(0); + + using (KlassInheritance kini = new KlassInheritance("KlassInheritanceInput")) { + checkCount(1); + string s = cpp11_std_unique_ptr.takeKlassUniquePtr(kini); + checkCount(0); + if (s != "KlassInheritanceInput") + throw new ApplicationException("Incorrect string: " + s); + if (!cpp11_std_unique_ptr.is_nullptr(kini)) + throw new ApplicationException("is_nullptr failed"); + } // Dispose should not fail, even though already deleted + checkCount(0); + + // unique_ptr as output Klass k1 = cpp11_std_unique_ptr.makeKlassUniquePtr("first"); if (k1.getLabel() != "first") throw new Exception("wrong object label"); diff --git a/Lib/csharp/csharp.swg b/Lib/csharp/csharp.swg index 94e0458a6..60ab388f3 100644 --- a/Lib/csharp/csharp.swg +++ b/Lib/csharp/csharp.swg @@ -913,6 +913,19 @@ SWIGINTERN const char * SWIG_UnpackData(const char *c, void *ptr, size_t sz) { CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef getCPtr($csclassname obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; } + + CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef swigRelease($csclassname obj) { + if (obj != null) { + if (!obj.swigCMemOwn) + throw new global::System.ApplicationException("Cannot release ownership as memory is not owned"); + global::System.Runtime.InteropServices.HandleRef ptr = obj.swigCPtr; + obj.swigCMemOwn = false; + obj.Dispose(); + return ptr; + } else { + return new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero); + } + } %} // Derived proxy classes @@ -926,6 +939,19 @@ SWIGINTERN const char * SWIG_UnpackData(const char *c, void *ptr, size_t sz) { CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef getCPtr($csclassname obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; } + + CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef swigRelease($csclassname obj) { + if (obj != null) { + if (!obj.swigCMemOwn) + throw new global::System.ApplicationException("Cannot release ownership as memory is not owned"); + global::System.Runtime.InteropServices.HandleRef ptr = obj.swigCPtr; + obj.swigCMemOwn = false; + obj.Dispose(); + return ptr; + } else { + return new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero); + } + } %} %enddef @@ -945,6 +971,10 @@ SWIGINTERN const char * SWIG_UnpackData(const char *c, void *ptr, size_t sz) { CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef getCPtr($csclassname obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; } + + CPTR_VISIBILITY static global::System.Runtime.InteropServices.HandleRef swigRelease($csclassname obj) { + return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; + } %} %typemap(csbody) TYPE (CLASS::*) %{ diff --git a/Lib/csharp/std_auto_ptr.i b/Lib/csharp/std_auto_ptr.i index 068d3a9d1..b1ec3d569 100644 --- a/Lib/csharp/std_auto_ptr.i +++ b/Lib/csharp/std_auto_ptr.i @@ -9,7 +9,7 @@ %define %auto_ptr(TYPE) %typemap (ctype) std::auto_ptr< TYPE > "void *" -%typemap (imtype, out="System.IntPtr") std::auto_ptr< TYPE > "HandleRef" +%typemap (imtype, out="System.IntPtr") std::auto_ptr< TYPE > "global::System.Runtime.InteropServices.HandleRef" %typemap (cstype) std::auto_ptr< TYPE > "$typemap(cstype, TYPE)" %typemap (out) std::auto_ptr< TYPE > %{ $result = (void *)$1.release(); diff --git a/Lib/csharp/std_unique_ptr.i b/Lib/csharp/std_unique_ptr.i index b2716756a..ba13fce4e 100644 --- a/Lib/csharp/std_unique_ptr.i +++ b/Lib/csharp/std_unique_ptr.i @@ -9,8 +9,14 @@ %define %unique_ptr(TYPE) %typemap (ctype) std::unique_ptr< TYPE > "void *" -%typemap (imtype, out="System.IntPtr") std::unique_ptr< TYPE > "HandleRef" +%typemap (imtype, out="System.IntPtr") std::unique_ptr< TYPE > "global::System.Runtime.InteropServices.HandleRef" %typemap (cstype) std::unique_ptr< TYPE > "$typemap(cstype, TYPE)" + +%typemap(in) std::unique_ptr< TYPE > +%{ $1.reset((TYPE *)$input); %} + +%typemap(csin) std::unique_ptr< TYPE > "$typemap(cstype, TYPE).swigRelease($csinput)" + %typemap (out) std::unique_ptr< TYPE > %{ $result = (void *)$1.release(); %} @@ -23,5 +29,5 @@ %enddef namespace std { - template class unique_ptr {}; + template class unique_ptr {}; }