Add C# support std::unique_ptr inputs
Based on Java implementation.
This commit is contained in:
parent
8bd9eb3f16
commit
c737bd5713
5 changed files with 103 additions and 4 deletions
|
|
@ -53,7 +53,7 @@ struct KlassInheritance : virtual Klass {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(SWIGJAVA)
|
#if defined(SWIGJAVA) || defined (SWIGCSHARP)
|
||||||
std::string takeKlassUniquePtr(std::unique_ptr<Klass> k) {
|
std::string takeKlassUniquePtr(std::unique_ptr<Klass> k) {
|
||||||
return std::string(k->getLabel());
|
return std::string(k->getLabel());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,71 @@ public class cpp11_std_unique_ptr_runme {
|
||||||
System.Threading.Thread.Sleep(10);
|
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()
|
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");
|
Klass k1 = cpp11_std_unique_ptr.makeKlassUniquePtr("first");
|
||||||
if (k1.getLabel() != "first")
|
if (k1.getLabel() != "first")
|
||||||
throw new Exception("wrong object label");
|
throw new Exception("wrong object label");
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
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;
|
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
|
// 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) {
|
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;
|
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
|
%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) {
|
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;
|
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::*) %{
|
%typemap(csbody) TYPE (CLASS::*) %{
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
%define %auto_ptr(TYPE)
|
%define %auto_ptr(TYPE)
|
||||||
%typemap (ctype) std::auto_ptr< TYPE > "void *"
|
%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 (cstype) std::auto_ptr< TYPE > "$typemap(cstype, TYPE)"
|
||||||
%typemap (out) std::auto_ptr< TYPE > %{
|
%typemap (out) std::auto_ptr< TYPE > %{
|
||||||
$result = (void *)$1.release();
|
$result = (void *)$1.release();
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,14 @@
|
||||||
|
|
||||||
%define %unique_ptr(TYPE)
|
%define %unique_ptr(TYPE)
|
||||||
%typemap (ctype) std::unique_ptr< TYPE > "void *"
|
%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 (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 > %{
|
%typemap (out) std::unique_ptr< TYPE > %{
|
||||||
$result = (void *)$1.release();
|
$result = (void *)$1.release();
|
||||||
%}
|
%}
|
||||||
|
|
@ -23,5 +29,5 @@
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
namespace std {
|
namespace std {
|
||||||
template <class T> class unique_ptr {};
|
template <class T> class unique_ptr {};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue