summaryrefslogtreecommitdiff
path: root/Examples/python/import_template
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2009-08-18 20:56:02 +0000
committerLorry <lorry@roadtrain.codethink.co.uk>2012-09-25 16:59:08 +0000
commit9f8a09ed743cedd9547bf0661d518647966ab114 (patch)
tree9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/python/import_template
downloadswig-tarball-master.tar.gz
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/python/import_template')
-rw-r--r--Examples/python/import_template/Makefile22
-rw-r--r--Examples/python/import_template/README30
-rw-r--r--Examples/python/import_template/bar.h22
-rw-r--r--Examples/python/import_template/bar.i11
-rw-r--r--Examples/python/import_template/base.h18
-rw-r--r--Examples/python/import_template/base.i7
-rw-r--r--Examples/python/import_template/foo.h21
-rw-r--r--Examples/python/import_template/foo.i10
-rw-r--r--Examples/python/import_template/runme.py111
-rw-r--r--Examples/python/import_template/spam.h24
-rw-r--r--Examples/python/import_template/spam.i10
11 files changed, 286 insertions, 0 deletions
diff --git a/Examples/python/import_template/Makefile b/Examples/python/import_template/Makefile
new file mode 100644
index 0000000..ee47e99
--- /dev/null
+++ b/Examples/python/import_template/Makefile
@@ -0,0 +1,22 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SWIGOPT =
+LIBS =
+
+all::
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='base' INTERFACE='base.i' python_cpp
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' python_cpp
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='bar' INTERFACE='bar.i' python_cpp
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='spam' INTERFACE='spam.i' python_cpp
+
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile python_clean
+ @rm -f foo.py bar.py spam.py base.py
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile python_run
diff --git a/Examples/python/import_template/README b/Examples/python/import_template/README
new file mode 100644
index 0000000..f8cf02d
--- /dev/null
+++ b/Examples/python/import_template/README
@@ -0,0 +1,30 @@
+This example tests the %import directive and working with multiple modules.
+
+Use 'python runme.py' to run a test.
+
+Overview:
+---------
+
+The example defines 4 different extension modules--each wrapping
+a separate C++ class.
+
+ base.i - Base class
+ foo.i - Foo class derived from Base
+ bar.i - Bar class derived from Base
+ spam.i - Spam class derived from Bar
+
+Each module uses %import to refer to another module. For
+example, the 'foo.i' module uses '%import base.i' to get
+definitions for its base class.
+
+If everything is okay, all of the modules will load properly and
+type checking will work correctly. Caveat: Some compilers, for example
+gcc-3.2.x, generate broken vtables with the inline methods in this test.
+This is not a SWIG problem and can usually be solved with non-inlined
+destructors compiled into separate shared objects/DLLs.
+
+Unix:
+-----
+- Run make
+- Run the test as described above
+
diff --git a/Examples/python/import_template/bar.h b/Examples/python/import_template/bar.h
new file mode 100644
index 0000000..500b67a
--- /dev/null
+++ b/Examples/python/import_template/bar.h
@@ -0,0 +1,22 @@
+#include "base.h"
+
+template<class T> class Bar : public Base<T> {
+ public:
+ Bar() { }
+ ~Bar() { }
+ virtual void A() {
+ printf("I'm Bar::A\n");
+ }
+ void B() {
+ printf("I'm Bar::B\n");
+ }
+ virtual Base<T> *toBase() {
+ return static_cast<Base<T> *>(this);
+ }
+ static Bar<T> *fromBase(Base<T> *b) {
+ return dynamic_cast<Bar<T> *>(b);
+ }
+
+};
+
+
diff --git a/Examples/python/import_template/bar.i b/Examples/python/import_template/bar.i
new file mode 100644
index 0000000..155c08d
--- /dev/null
+++ b/Examples/python/import_template/bar.i
@@ -0,0 +1,11 @@
+%module bar
+%{
+#include "bar.h"
+%}
+
+%import base.i
+%include "bar.h"
+
+%template(intBar) Bar<int>;
+
+
diff --git a/Examples/python/import_template/base.h b/Examples/python/import_template/base.h
new file mode 100644
index 0000000..c755a6f
--- /dev/null
+++ b/Examples/python/import_template/base.h
@@ -0,0 +1,18 @@
+#include <stdio.h>
+
+template<class T> class Base {
+ public:
+ Base() { };
+ virtual ~Base() { };
+ virtual void A() {
+ printf("I'm Base::A\n");
+ }
+ void B() {
+ printf("I'm Base::B\n");
+ }
+ virtual Base<T> *toBase() {
+ return static_cast<Base<T> *>(this);
+ }
+};
+
+
diff --git a/Examples/python/import_template/base.i b/Examples/python/import_template/base.i
new file mode 100644
index 0000000..a6da063
--- /dev/null
+++ b/Examples/python/import_template/base.i
@@ -0,0 +1,7 @@
+%module base
+%{
+#include "base.h"
+%}
+
+%include base.h
+%template(intBase) Base<int>;
diff --git a/Examples/python/import_template/foo.h b/Examples/python/import_template/foo.h
new file mode 100644
index 0000000..3df13d2
--- /dev/null
+++ b/Examples/python/import_template/foo.h
@@ -0,0 +1,21 @@
+#include "base.h"
+
+template<class T> class Foo : public Base<T> {
+ public:
+ Foo() { }
+ ~Foo() { }
+ virtual void A() {
+ printf("I'm Foo::A\n");
+ }
+ void B() {
+ printf("I'm Foo::B\n");
+ }
+ virtual Base<T> *toBase() {
+ return static_cast<Base<T> *>(this);
+ }
+ static Foo<T> *fromBase(Base<T> *b) {
+ return dynamic_cast<Foo<T> *>(b);
+ }
+};
+
+
diff --git a/Examples/python/import_template/foo.i b/Examples/python/import_template/foo.i
new file mode 100644
index 0000000..e271672
--- /dev/null
+++ b/Examples/python/import_template/foo.i
@@ -0,0 +1,10 @@
+%module foo
+%{
+#include "foo.h"
+%}
+
+%import base.i
+%include "foo.h"
+
+%template(intFoo) Foo<int>;
+
diff --git a/Examples/python/import_template/runme.py b/Examples/python/import_template/runme.py
new file mode 100644
index 0000000..0d5aded
--- /dev/null
+++ b/Examples/python/import_template/runme.py
@@ -0,0 +1,111 @@
+# file: runme.py
+# Test various properties of classes defined in separate modules
+
+print "Testing the %import directive with templates"
+import base
+import foo
+import bar
+import spam
+
+# Create some objects
+
+print "Creating some objects"
+
+a = base.intBase()
+b = foo.intFoo()
+c = bar.intBar()
+d = spam.intSpam()
+
+# Try calling some methods
+print "Testing some methods"
+print "",
+print "Should see 'Base::A' ---> ",
+a.A()
+print "Should see 'Base::B' ---> ",
+a.B()
+
+print "Should see 'Foo::A' ---> ",
+b.A()
+print "Should see 'Foo::B' ---> ",
+b.B()
+
+print "Should see 'Bar::A' ---> ",
+c.A()
+print "Should see 'Bar::B' ---> ",
+c.B()
+
+print "Should see 'Spam::A' ---> ",
+d.A()
+print "Should see 'Spam::B' ---> ",
+d.B()
+
+# Try some casts
+
+print "\nTesting some casts\n"
+print "",
+
+x = a.toBase()
+print "Should see 'Base::A' ---> ",
+x.A()
+print "Should see 'Base::B' ---> ",
+x.B()
+
+x = b.toBase()
+print "Should see 'Foo::A' ---> ",
+x.A()
+
+print "Should see 'Base::B' ---> ",
+x.B()
+
+x = c.toBase()
+print "Should see 'Bar::A' ---> ",
+x.A()
+
+print "Should see 'Base::B' ---> ",
+x.B()
+
+x = d.toBase()
+print "Should see 'Spam::A' ---> ",
+x.A()
+
+print "Should see 'Base::B' ---> ",
+x.B()
+
+x = d.toBar()
+print "Should see 'Bar::B' ---> ",
+x.B()
+
+print "\nTesting some dynamic casts\n"
+x = d.toBase()
+
+print " Spam -> Base -> Foo : ",
+y = foo.intFoo_fromBase(x)
+if y:
+ print "bad swig"
+else:
+ print "good swig"
+
+print " Spam -> Base -> Bar : ",
+y = bar.intBar_fromBase(x)
+if y:
+ print "good swig"
+else:
+ print "bad swig"
+
+print " Spam -> Base -> Spam : ",
+y = spam.intSpam_fromBase(x)
+if y:
+ print "good swig"
+else:
+ print "bad swig"
+
+print " Foo -> Spam : ",
+y = spam.intSpam_fromBase(b)
+if y:
+ print "bad swig"
+else:
+ print "good swig"
+
+
+
+
diff --git a/Examples/python/import_template/spam.h b/Examples/python/import_template/spam.h
new file mode 100644
index 0000000..c72f49a
--- /dev/null
+++ b/Examples/python/import_template/spam.h
@@ -0,0 +1,24 @@
+#include "bar.h"
+
+template<class T> class Spam : public Bar<T> {
+ public:
+ Spam() { }
+ ~Spam() { }
+ virtual void A() {
+ printf("I'm Spam::A\n");
+ }
+ void B() {
+ printf("I'm Spam::B\n");
+ }
+ virtual Base<T> *toBase() {
+ return static_cast<Base<T> *>(this);
+ }
+ virtual Bar<T> *toBar() {
+ return static_cast<Bar<T> *>(this);
+ }
+ static Spam<T> *fromBase(Base<T> *b) {
+ return dynamic_cast<Spam<T> *>(b);
+ }
+};
+
+
diff --git a/Examples/python/import_template/spam.i b/Examples/python/import_template/spam.i
new file mode 100644
index 0000000..dd94b03
--- /dev/null
+++ b/Examples/python/import_template/spam.i
@@ -0,0 +1,10 @@
+%module spam
+%{
+#include "spam.h"
+%}
+
+%import bar.i
+%include "spam.h"
+
+%template(intSpam) Spam<int>;
+