diff --git a/Examples/test-suite/cpp11_shared_ptr_template_upcast.i b/Examples/test-suite/cpp11_shared_ptr_template_upcast.i new file mode 100644 index 000000000..69ffec5bf --- /dev/null +++ b/Examples/test-suite/cpp11_shared_ptr_template_upcast.i @@ -0,0 +1,88 @@ +%module cpp11_shared_ptr_template_upcast + +%{ +#include +#include +%} + +%include +%include + +%{ +class Base { +public: + Base() : value(0) {} + Base(int v) : value(v) {} + virtual ~Base() {} + + virtual int GetResult() = 0; + + int value; +}; + +class Derived : public Base { +public: + Derived() : Base() {} + Derived(int v) : Base(v) {} + virtual ~Derived() {} + + int GetResult() { return value*2; } +}; + +template class Printable : virtual public T { +public: + Printable(int param) : T(param) {} + ~Printable() {} + + std::string GetFormatted() { return std::string("The formatted result is: ").append(std::to_string(this->GetResult())); } +}; + +std::shared_ptr > MakePrintableDerived(int param) { + return std::make_shared >(param); +} + +%} + +%shared_ptr(Base); +%shared_ptr(Derived); +%shared_ptr(Printable) + +class Base { +public: + Base(); + Base(int v); + virtual ~Base(); + + virtual int GetResult() = 0; + + int value; +}; + +class Derived : public Base { +public: + Derived(); + Derived(int v); + virtual ~Derived(); + + int GetResult(); +}; + +/* + Contrived for this case (but valid for others, such as if Printable was to be a interface/abstract base class). + Virtual inheritance exposes whether SWIGSmartPtrUpcast generated a correctly typed shared pointer of the upcasted class type - if the pointer type is incorrect, this will result in + a segmentation fault (on Windows, this could manifest as undefined behavior) when trying to access any member inherited from T through a shared_ptr >. +*/ +template class Printable : virtual public T { +public: + Printable(int param); + ~Printable(); + + std::string GetFormatted(); +}; + +std::shared_ptr > MakePrintableDerived(int param); + + +%template(PrintableDerived) Printable; + + diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index 2e788fa07..3954b939a 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -52,6 +52,7 @@ CPP11_TEST_CASES = \ cpp11_shared_ptr_const \ cpp11_shared_ptr_nullptr_in_containers \ cpp11_shared_ptr_overload \ + cpp11_shared_ptr_template_upcast \ cpp11_shared_ptr_upcast \ cpp11_std_unordered_map \ cpp11_std_unordered_set \ diff --git a/Examples/test-suite/java/cpp11_shared_ptr_template_upcast_runme.java b/Examples/test-suite/java/cpp11_shared_ptr_template_upcast_runme.java new file mode 100644 index 000000000..b367fef5e --- /dev/null +++ b/Examples/test-suite/java/cpp11_shared_ptr_template_upcast_runme.java @@ -0,0 +1,25 @@ + +// This is the cpp11_shared_ptr_template_upcast runtime testcase. It checks that SWIG generates the appropriate, upcasted shared_ptr type for a template instantiation deriving from a base class. +// In this case, the expected behavior is that given a cptr (underlying type shared_ptr >), PrintableDerived_SWIGSmartPtrUpcast returns a cptr +// (underlying type std::shared_ptr< Derived >). + +import cpp11_shared_ptr_template_upcast.*; + +public class cpp11_shared_ptr_template_upcast_runme { + + static { + try { + System.loadLibrary("cpp11_shared_ptr_template_upcast"); + } 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 main(String argv[]) { + PrintableDerived pd = cpp11_shared_ptr_template_upcast.MakePrintableDerived(20); + pd.GetResult(); + pd.GetFormatted(); + } +} +