diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 86d688799..bf88b0035 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -245,6 +245,7 @@ CPP_TEST_CASES += \ li_boost_shared_ptr \ li_boost_shared_ptr_bits \ li_boost_shared_ptr_template \ + li_boost_shared_ptr_attribute \ li_carrays \ li_cdata \ li_cpointer \ diff --git a/Examples/test-suite/java/li_boost_shared_ptr_attribute_runme.java b/Examples/test-suite/java/li_boost_shared_ptr_attribute_runme.java new file mode 100644 index 000000000..afc23eff0 --- /dev/null +++ b/Examples/test-suite/java/li_boost_shared_ptr_attribute_runme.java @@ -0,0 +1,38 @@ +import li_boost_shared_ptr_attribute.*; + +public class li_boost_shared_ptr_attribute_runme { + static { + try { + System.loadLibrary("li_boost_shared_ptr_attribute"); + } 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); + } + } + + public static void check(GetSetMe g, int expected) { + int got = g.getN(); + if (got != expected) + throw new RuntimeException("GetSetMe value is " + got + " but should be " + expected); + } + + public static void check(GetMe g, int expected) { + int got = g.getN(); + if (got != expected) + throw new RuntimeException("GetMe value is " + got + " but should be " + expected); + } + + public static void main(String argv[]) + { + GetterSetter gs = new GetterSetter(5); + check(gs.getMyval(), 25); + check(gs.getAddedAttrib(), 25); + gs.setAddedAttrib(new GetSetMe(6)); + check(gs.getMyval(), 6); + check(gs.getAddedAttrib(), 6); + + GetterOnly g = new GetterOnly(4); + check(g.getMyval(), 16); + check(g.getAddedAttrib(), 16); + } +} diff --git a/Examples/test-suite/li_boost_shared_ptr_attribute.i b/Examples/test-suite/li_boost_shared_ptr_attribute.i new file mode 100644 index 000000000..c4d3dca36 --- /dev/null +++ b/Examples/test-suite/li_boost_shared_ptr_attribute.i @@ -0,0 +1,46 @@ +%module li_boost_shared_ptr_attribute + +#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON) || defined(SWIGD) +#define SHARED_PTR_WRAPPERS_IMPLEMENTED +#endif + +#if defined(SHARED_PTR_WRAPPERS_IMPLEMENTED) + +%include "attribute.i" +%include "boost_shared_ptr.i" + +%inline %{ +#include +using namespace boost; +%} +%shared_ptr(GetMe); +%shared_ptr(GetSetMe); +%attributestring(GetterOnly, shared_ptr, AddedAttrib, GetIt) +%attributestring(GetterSetter, shared_ptr, AddedAttrib, GetIt, SetIt) + +%inline %{ +struct GetMe { + explicit GetMe(int n) : n(n) {} + ~GetMe() {} + int n; +}; +struct GetSetMe { + explicit GetSetMe(int n) : n(n) {} + ~GetSetMe() {} + int n; +}; + +struct GetterOnly { + explicit GetterOnly(int n) : myval(new GetMe(n*n)) {} + shared_ptr GetIt() const { return myval; } + shared_ptr myval; +}; +struct GetterSetter { + explicit GetterSetter(int n) : myval(new GetSetMe(n*n)) {} + shared_ptr GetIt() const { return myval; } + void SetIt(shared_ptr newval) { myval = newval; } + shared_ptr myval; +}; +%} + +#endif diff --git a/Lib/typemaps/attribute.swg b/Lib/typemaps/attribute.swg index 46fc80fd2..f06c8c0f3 100644 --- a/Lib/typemaps/attribute.swg +++ b/Lib/typemaps/attribute.swg @@ -130,6 +130,9 @@ }; %} + The %attributestring also works for class types that have %naturalvar turned + on and so is also useful for shared_ptr which has %naturalvar turned on in %shared_ptr. + */ // @@ -275,7 +278,7 @@ %ignore Class::GetMethod(); %ignore Class::GetMethod() const; %newobject Class::AttributeName; - %typemap(newfree) const AttributeType &AttributeName "delete $1;// my newfree override" + %typemap(newfree) const AttributeType &AttributeName "delete $1;" %extend Class { AttributeType AttributeName; }