summaryrefslogtreecommitdiff
path: root/Examples
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2020-01-17 18:45:30 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2020-01-17 18:45:30 +0000
commit6910e5f09cb0d6d6ccfb6487247958e5126b3981 (patch)
treeeff7097d5a830b59a545db4798b5430638348c1d /Examples
parent7051753fdfb7d74bb1a8e419eb450bbbc6e79141 (diff)
parent50b42f855a11fb9c68109353adcd4e61b6a47555 (diff)
downloadswig-6910e5f09cb0d6d6ccfb6487247958e5126b3981.tar.gz
Merge branch 'shared-ptr-template-upcast'
* shared-ptr-template-upcast: comments Applying shared_ptr template upcast fix to CSharp, adding CSharp test, and cleanup Adding test case demonstrating issue where SWIG does not generate a correctly typed, upcasted shared_ptr for a template instantiation deriving from a base class WIP - Use the non-encoded type string for upcasting a shared_ptr of a derived type to a shared_ptr of the base type comments Applying shared_ptr template upcast fix to CSharp, adding CSharp test, and cleanup Adding test case demonstrating issue where SWIG does not generate a correctly typed, upcasted shared_ptr for a template instantiation deriving from a base class WIP - Use the non-encoded type string for upcasting a shared_ptr of a derived type to a shared_ptr of the base type
Diffstat (limited to 'Examples')
-rw-r--r--Examples/test-suite/cpp11_shared_ptr_template_upcast.i88
-rw-r--r--Examples/test-suite/csharp/Makefile.in1
-rw-r--r--Examples/test-suite/csharp/cpp11_shared_ptr_template_upcast_runme.cs15
-rw-r--r--Examples/test-suite/java/Makefile.in1
-rw-r--r--Examples/test-suite/java/cpp11_shared_ptr_template_upcast_runme.java24
5 files changed, 129 insertions, 0 deletions
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..38968bb60
--- /dev/null
+++ b/Examples/test-suite/cpp11_shared_ptr_template_upcast.i
@@ -0,0 +1,88 @@
+%module cpp11_shared_ptr_template_upcast
+
+%{
+#include <memory>
+#include <string>
+%}
+
+%include <std_shared_ptr.i>
+%include <std_string.i>
+
+%{
+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 T> 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<Printable<Derived> > MakePrintableDerived(int param) {
+ return std::make_shared<Printable<Derived> >(param);
+}
+
+%}
+
+%shared_ptr(Base);
+%shared_ptr(Derived);
+%shared_ptr(Printable<Derived>)
+
+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();
+};
+
+/*
+ Virtual inheritance is contrived for this case, but 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 members
+ inherited from T through a shared_ptr<Printable<T> >.
+*/
+template <class T> class Printable : virtual public T {
+public:
+ Printable(int param);
+ ~Printable();
+
+ std::string GetFormatted();
+};
+
+std::shared_ptr<Printable<Derived> > MakePrintableDerived(int param);
+
+
+%template(PrintableDerived) Printable<Derived>;
+
+
diff --git a/Examples/test-suite/csharp/Makefile.in b/Examples/test-suite/csharp/Makefile.in
index 57a81e357..b0ad0c7cf 100644
--- a/Examples/test-suite/csharp/Makefile.in
+++ b/Examples/test-suite/csharp/Makefile.in
@@ -35,6 +35,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_strongly_typed_enumerations_simple \
diff --git a/Examples/test-suite/csharp/cpp11_shared_ptr_template_upcast_runme.cs b/Examples/test-suite/csharp/cpp11_shared_ptr_template_upcast_runme.cs
new file mode 100644
index 000000000..e76d2bada
--- /dev/null
+++ b/Examples/test-suite/csharp/cpp11_shared_ptr_template_upcast_runme.cs
@@ -0,0 +1,15 @@
+// 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.
+// For this case, the expected behavior is: given a cptr with underlying type shared_ptr<Printable<Derived> >, PrintableDerived_SWIGSmartPtrUpcast returns a cptr with
+// underlying type std::shared_ptr< Derived >, where Printable<Derived> inherits from Derived.
+using System;
+using cpp11_shared_ptr_template_upcastNamespace;
+
+public class cpp11_shared_ptr_template_upcast_runme
+{
+ static void Main()
+ {
+ PrintableDerived pd = cpp11_shared_ptr_template_upcast.MakePrintableDerived(20);
+ pd.GetResult();
+ pd.GetFormatted();
+ }
+}
diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in
index f8e290ee3..a449f287c 100644
--- a/Examples/test-suite/java/Makefile.in
+++ b/Examples/test-suite/java/Makefile.in
@@ -53,6 +53,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..2826f580d
--- /dev/null
+++ b/Examples/test-suite/java/cpp11_shared_ptr_template_upcast_runme.java
@@ -0,0 +1,24 @@
+// 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.
+// For this case, the expected behavior is: given a cptr with underlying type shared_ptr<Printable<Derived> >, PrintableDerived_SWIGSmartPtrUpcast returns a cptr with
+// underlying type std::shared_ptr< Derived >, where Printable<Derived> inherits from 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();
+ }
+}
+