Fix Java shared_ptr and directors for derived classes java compilation error.

For shared_ptr proxy proxy classes, add a protected method swigSetCMemOwn for modifying
the swigCMemOwn and swigCMemOwnDerived member variables which are used by various other
methods for controlling memory ownership.

Closes #230
Closes #759
This commit is contained in:
William S Fulton 2017-05-23 20:56:15 +01:00
commit 4bf607589f
7 changed files with 138 additions and 6 deletions

View file

@ -7,6 +7,14 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.0.0 (in progress)
===========================
2017-05-23: wsfulton
[Java] #230 #759 Fix Java shared_ptr and directors for derived classes java compilation
error.
For shared_ptr proxy proxy classes, add a protected method swigSetCMemOwn for modifying
the swigCMemOwn and swigCMemOwnDerived member variables which are used by various other
methods for controlling memory ownership.
2017-05-21: Sghirate
[Java, C#, D] #449 Remove unnecessary use of dynamic_cast in directors to enable
non-RTTI compilation.

View file

@ -44,7 +44,6 @@ public:
%include <boost_shared_ptr.i>
%shared_ptr(Foo)
%feature("director") Foo;
class FooBar {
@ -72,5 +71,12 @@ public:
static Foo* get_self(Foo *self_);
};
%shared_ptr(FooDerived)
%feature("director") FooDerived;
%inline %{
struct FooDerived : Foo {
};
%}
#endif

View file

@ -33,6 +33,12 @@ public class director_smartptr_runme {
director_smartptr.Foo myFoo2 = new director_smartptr.Foo().makeFoo();
check(myFoo2.pong(), "Foo::pong();Foo::ping()");
check(director_smartptr.Foo.callPong(myFoo2), "Foo::pong();Foo::ping()");
director_smartptr.FooDerived myBarFooDerived = new director_smartptr_MyBarFooDerived();
check(myBarFooDerived.ping(), "director_smartptr_MyBarFooDerived.ping()");
check(director_smartptr.FooDerived.callPong(myBarFooDerived), "director_smartptr_MyBarFooDerived.pong();director_smartptr_MyBarFooDerived.ping()");
check(director_smartptr.FooDerived.callUpcall(myBarFooDerived, fooBar), "overrideDerived;Bar::Foo2::Foo2Bar()");
}
}
@ -58,3 +64,26 @@ class director_smartptr_MyBarFoo extends director_smartptr.Foo {
return new director_smartptr.Foo();
}
}
class director_smartptr_MyBarFooDerived extends director_smartptr.FooDerived {
@Override
public String ping() {
return "director_smartptr_MyBarFooDerived.ping()";
}
@Override
public String pong() {
return "director_smartptr_MyBarFooDerived.pong();" + ping();
}
@Override
public String upcall(director_smartptr.FooBar fooBarPtr) {
return "overrideDerived;" + fooBarPtr.FooBarDo();
}
@Override
public director_smartptr.Foo makeFoo() {
return new director_smartptr.Foo();
}
}

View file

@ -0,0 +1,60 @@
public class li_boost_shared_ptr_runme {
static {
try {
System.loadLibrary("li_boost_shared_ptr");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
System.exit(1);
}
}
private static void check(String got, String expected) {
if (!got.equals(expected))
throw new RuntimeException("Failed, got: " + got + " expected: " + expected);
}
public static void main(String argv[]) {
li_boost_shared_ptr_director_Derived a = li_boost_shared_ptr_director_Derived.new(false);
li_boost_shared_ptr_director_Derived b = li_boost_shared_ptr_director_Derived.new(true);
check(call_ret_c_shared_ptr(a) == 1);
check(call_ret_c_shared_ptr(b) == -1);
check(call_ret_c_by_value(a) == 1);
check(call_take_c_by_value(a) == 5);
check(call_take_c_shared_ptr_by_value(a) == 6);
check(call_take_c_shared_ptr_by_ref(a) == 7);
check(call_take_c_shared_ptr_by_pointer(a) == 8);
check(call_take_c_shared_ptr_by_pointer_ref(a) == 9);
check(call_take_c_shared_ptr_by_value_with_null(a) == -2);
check(call_take_c_shared_ptr_by_ref_with_null(a) == -3);
check(call_take_c_shared_ptr_by_pointer_with_null(a) == -4);
check(call_take_c_shared_ptr_by_pointer_ref_with_null(a) == -5);
}
}
class li_boost_shared_ptr_director_Derived extends li_boost_shared_ptr_director.Base {
@Override
public String ping() {
return "li_boost_shared_ptr_director_MyBarFoo.ping()";
}
@Override
public String pong() {
return "li_boost_shared_ptr_director_MyBarFoo.pong();" + ping();
}
@Override
public String upcall(li_boost_shared_ptr_director.FooBar fooBarPtr) {
return "override;" + fooBarPtr.FooBarDo();
}
@Override
public li_boost_shared_ptr_director.Foo makeFoo() {
return new li_boost_shared_ptr_director.Foo();
}
}

View file

@ -10,14 +10,14 @@
%inline %{
struct C {
C() : m(1) {};
C(int n) : m(n) {};
int get_m() { return m; };
C() : m(1) {}
C(int n) : m(n) {}
int get_m() { return m; }
int m;
};
struct Base {
Base() {};
Base() {}
virtual boost::shared_ptr<C> ret_c_shared_ptr() = 0;
virtual C ret_c_by_value() = 0;
virtual int take_c_by_value(C c) = 0;

View file

@ -156,6 +156,10 @@
CPTR_VISIBILITY static long getCPtr($javaclassname obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
CPTR_VISIBILITY void swigSetCMemOwn(boolean own) {
swigCMemOwn = own;
}
%}
// Derived proxy classes
@ -172,6 +176,11 @@
CPTR_VISIBILITY static long getCPtr($javaclassname obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
CPTR_VISIBILITY void swigSetCMemOwn(boolean own) {
swigCMemOwnDerived = own;
super.swigSetCMemOwn(own);
}
%}
%typemap(javadestruct, methodname="delete", methodmodifiers="public synchronized") TYPE {
@ -195,6 +204,26 @@
super.delete();
}
%typemap(directordisconnect, methodname="swigDirectorDisconnect") TYPE %{
protected void $methodname() {
swigSetCMemOwn(false);
$jnicall;
}
%}
%typemap(directorowner_release, methodname="swigReleaseOwnership") TYPE %{
public void $methodname() {
swigSetCMemOwn(false);
$jnicall;
}
%}
%typemap(directorowner_take, methodname="swigTakeOwnership") TYPE %{
public void $methodname() {
swigSetCMemOwn(true);
$jnicall;
}
%}
%template() SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< CONST TYPE >;
%enddef

View file

@ -1260,7 +1260,7 @@ SWIG_JAVABODY_TYPEWRAPPER(protected, protected, protected, SWIGTYPE)
*/
%define SWIG_PROXY_CONSTRUCTOR(OWNERSHIP, WEAKREF, TYPENAME...)
%typemap(javaconstruct,directorconnect="\n $imclassname.$javaclazznamedirector_connect(this, swigCPtr, swigCMemOwn, WEAKREF);") TYPENAME {
%typemap(javaconstruct,directorconnect="\n $imclassname.$javaclazznamedirector_connect(this, swigCPtr, OWNERSHIP, WEAKREF);") TYPENAME {
this($imcall, OWNERSHIP);$directorconnect
}
%enddef