summaryrefslogtreecommitdiff
path: root/Examples/test-suite
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2022-10-07 18:41:14 +0100
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2022-10-07 18:41:14 +0100
commit4a397869a2e2be3c196fac3b404aabe0cc19f494 (patch)
tree159a1e08de816d9aac741d2435038624f47e9c1e /Examples/test-suite
parente1fdb67f094d3cb616a2b1b999b95f969f6096cb (diff)
parentdeb7cbf7416a5d1503f490b087f4da0b2693709c (diff)
downloadswig-4a397869a2e2be3c196fac3b404aabe0cc19f494.tar.gz
Merge branch 'director-unwrap-result'
* director-unwrap-result: Unwrap director classes only when returning a pointer or reference to an object
Diffstat (limited to 'Examples/test-suite')
-rw-r--r--Examples/test-suite/common.mk1
-rw-r--r--Examples/test-suite/director_unwrap_result.i82
-rw-r--r--Examples/test-suite/javascript/Makefile.in1
-rw-r--r--Examples/test-suite/ruby/director_unwrap_result_runme.rb106
4 files changed, 190 insertions, 0 deletions
diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk
index 091293016..2808763c7 100644
--- a/Examples/test-suite/common.mk
+++ b/Examples/test-suite/common.mk
@@ -218,6 +218,7 @@ CPP_TEST_CASES += \
director_smartptr \
director_thread \
director_unroll \
+ director_unwrap_result \
director_using \
director_using_member_scopes \
director_void \
diff --git a/Examples/test-suite/director_unwrap_result.i b/Examples/test-suite/director_unwrap_result.i
new file mode 100644
index 000000000..bcb7f0fcc
--- /dev/null
+++ b/Examples/test-suite/director_unwrap_result.i
@@ -0,0 +1,82 @@
+%module(directors="1") director_unwrap_result
+
+%warnfilter(SWIGWARN_TYPEMAP_DIRECTOROUT_PTR) Storage;
+%warnfilter(SWIGWARN_TYPEMAP_DIRECTOROUT_PTR) StorageTmpl;
+
+%feature("director") Element;
+%feature("director") Storage;
+%feature("director") StorageTmpl;
+
+%inline %{
+
+class Element {
+ Element* self;
+ Element** selfptr;
+ public:
+ Element() {
+ self = this;
+ selfptr = &self;
+ }
+ virtual ~Element() {}
+ Element **getPtrPtr() {
+ return &self;
+ }
+ Element ***getPtrPtrPtr() {
+ return &selfptr;
+ }
+};
+
+class Storage {
+ public:
+ virtual ~Storage() {}
+ virtual Element **getIt() = 0;
+ Element getElement() {
+ return **getIt();
+ }
+ Element* const getElementPtr() {
+ return *getIt();
+ }
+ Element& getElementRef() {
+ return **getIt();
+ }
+ Element* const *getElementPtrPtr() {
+ return getIt();
+ }
+ Element *&getElementPtrRef() {
+ return *getIt();
+ }
+};
+
+template<class T> class StorageTmpl {
+ public:
+ virtual ~StorageTmpl() {}
+ virtual T &getIt() = 0;
+ T getVal() {
+ return getIt();
+ }
+ T *getPtr() {
+ return &getIt();
+ }
+ T &getRef() {
+ return getIt();
+ }
+};
+
+%}
+
+%template(ElementStorage) StorageTmpl<Element>;
+%template(ElementPtrStorage) StorageTmpl<Element *>;
+%template(ElementPtrPtrStorage) StorageTmpl<Element *const *>;
+
+%inline %{
+
+template<class T> T getParam(T t) {
+ return t;
+}
+
+%}
+
+%template(getIntParam) getParam<int>;
+%template(getIntPtrParam) getParam<int*>;
+%template(getElementPtrParam) getParam<Element *>;
+
diff --git a/Examples/test-suite/javascript/Makefile.in b/Examples/test-suite/javascript/Makefile.in
index 61cc5ff3d..8772851a8 100644
--- a/Examples/test-suite/javascript/Makefile.in
+++ b/Examples/test-suite/javascript/Makefile.in
@@ -59,6 +59,7 @@ ifeq (node,$(JSENGINE))
director_basic.cpptest: GYP_CFLAGS = \"-Wno-ignored-qualifiers\"
enum_thorough.cpptest: GYP_CFLAGS = \"-Wno-ignored-qualifiers\"
member_funcptr_galore.cpptest: GYP_CFLAGS = \"-Wno-ignored-qualifiers\"
+ director_unwrap_result.cpptest: GYP_CFLAGS = \"-Wno-ignored-qualifiers\"
setup_node = \
test -d $* || mkdir $* && \
diff --git a/Examples/test-suite/ruby/director_unwrap_result_runme.rb b/Examples/test-suite/ruby/director_unwrap_result_runme.rb
new file mode 100644
index 000000000..56970b3e8
--- /dev/null
+++ b/Examples/test-suite/ruby/director_unwrap_result_runme.rb
@@ -0,0 +1,106 @@
+#!/usr/bin/env ruby
+#
+# This test checks the proper unwrapping of director objects before returning
+# a pointer to the (wrapped) instance.
+# Unwrapping must not happen for return-by-value and returning higher
+# reference levels (pointer to pointer, reference to pointer, etc.), but this
+# is already checked by the C++ compiler.
+#
+
+require 'swig_assert'
+
+require 'director_unwrap_result'
+
+############################
+# test with a regular (non-template) class
+
+class MyElement < Director_unwrap_result::Element
+end
+
+class MyStorage < Director_unwrap_result::Storage
+ def initialize(e)
+ super()
+ @elem = e
+ end
+ def getIt
+ @elem.getPtrPtr
+ end
+end
+
+e = MyElement.new
+s = MyStorage.new(e)
+
+swig_assert_equal('s.getElement.class', 'Director_unwrap_result::Element', binding)
+swig_assert('s.getElement != e', binding)
+
+# this shows that the director class was unwrapped:
+swig_assert_equal('s.getElementPtr.class', 'MyElement', binding)
+swig_assert_equal('s.getElementPtr', 'e', binding)
+
+# this shows that the director class was unwrapped:
+swig_assert_equal('s.getElementRef.class', 'MyElement', binding)
+swig_assert_equal('s.getElementRef', 'e', binding)
+
+swig_assert_equal('s.getElementPtrPtr.class', 'SWIG::TYPE_p_p_Element', binding)
+swig_assert_equal('s.getElementPtrPtr.class', 'SWIG::TYPE_p_p_Element', binding)
+
+swig_assert_equal('s.getElementPtrRef.class', 'SWIG::TYPE_p_p_Element', binding)
+swig_assert_equal('s.getElementPtrRef.class', 'SWIG::TYPE_p_p_Element', binding)
+
+############################
+# test with a template class
+
+class MyElementStorage < Director_unwrap_result::ElementStorage
+ def initialize(e)
+ super()
+ @elem = e
+ end
+ def getIt
+ @elem
+ end
+end
+
+class MyElementPtrStorage < Director_unwrap_result::ElementPtrStorage
+ def initialize(e)
+ super()
+ @elem = e
+ end
+ def getIt
+ @elem.getPtrPtr
+ end
+end
+
+class MyElementPtrPtrStorage < Director_unwrap_result::ElementPtrPtrStorage
+ def initialize(e)
+ super()
+ @elem = e
+ end
+ def getIt
+ @elem.getPtrPtrPtr
+ end
+end
+
+e = MyElement.new
+
+s = MyElementStorage.new(e)
+swig_assert_equal('s.getVal.class', 'Director_unwrap_result::Element', binding)
+swig_assert('s.getVal != e', binding)
+# this shows that the director class was unwrapped:
+swig_assert_equal('s.getPtr.class', 'MyElement', binding)
+swig_assert_equal('s.getPtr', 'e', binding)
+# this shows that the director class was unwrapped:
+swig_assert_equal('s.getRef.class', 'MyElement', binding)
+swig_assert_equal('s.getRef', 'e', binding)
+
+s = MyElementPtrStorage.new(e)
+# this shows that the director class was unwrapped:
+swig_assert_equal('s.getVal.class', 'MyElement', binding)
+swig_assert_equal('s.getVal', 'e', binding)
+swig_assert_equal('s.getPtr.class', 'SWIG::TYPE_p_p_Element', binding)
+swig_assert_equal('s.getRef.class', 'SWIG::TYPE_p_p_Element', binding)
+
+s = MyElementPtrPtrStorage.new(e)
+swig_assert_equal('s.getVal.class', 'SWIG::TYPE_p_p_Element', binding)
+swig_assert_equal('s.getPtr.class', 'SWIG::TYPE_p_p_p_Element', binding)
+swig_assert_equal('s.getRef.class', 'SWIG::TYPE_p_p_p_Element', binding)
+