summaryrefslogtreecommitdiff
path: root/Examples/lua/import
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/lua/import')
-rw-r--r--Examples/lua/import/Makefile19
-rw-r--r--Examples/lua/import/README36
-rw-r--r--Examples/lua/import/bar.h21
-rw-r--r--Examples/lua/import/bar.i9
-rw-r--r--Examples/lua/import/base.h16
-rw-r--r--Examples/lua/import/base.i6
-rw-r--r--Examples/lua/import/foo.h21
-rw-r--r--Examples/lua/import/foo.i8
-rw-r--r--Examples/lua/import/runme.lua103
-rw-r--r--Examples/lua/import/spam.h24
-rw-r--r--Examples/lua/import/spam.i9
11 files changed, 272 insertions, 0 deletions
diff --git a/Examples/lua/import/Makefile b/Examples/lua/import/Makefile
new file mode 100644
index 0000000..8f692d1
--- /dev/null
+++ b/Examples/lua/import/Makefile
@@ -0,0 +1,19 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SWIGOPT =
+LIBS =
+
+all::
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='base' INTERFACE='base.i' lua_cpp
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' lua_cpp
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='bar' INTERFACE='bar.i' lua_cpp
+ $(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
+ LIBS='$(LIBS)' TARGET='spam' INTERFACE='spam.i' lua_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile lua_clean
+
+check: all
diff --git a/Examples/lua/import/README b/Examples/lua/import/README
new file mode 100644
index 0000000..1a52e3c
--- /dev/null
+++ b/Examples/lua/import/README
@@ -0,0 +1,36 @@
+This example tests the %import directive and working with multiple modules.
+
+Use 'lua runme.lua' 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
+
+Windows:
+--------
+Sorry, no files here.
+If you know how, you could copy the python or ruby example dsw & dsp and try editing that
+
+
diff --git a/Examples/lua/import/bar.h b/Examples/lua/import/bar.h
new file mode 100644
index 0000000..1c99f28
--- /dev/null
+++ b/Examples/lua/import/bar.h
@@ -0,0 +1,21 @@
+#include "base.h"
+
+class Bar : public Base {
+ public:
+ Bar() { }
+ ~Bar() { }
+ virtual const char * A() const {
+ return "Bar::A";
+ }
+ const char * B() const {
+ return "Bar::B";
+ }
+ virtual Base *toBase() {
+ return static_cast<Base *>(this);
+ }
+ static Bar *fromBase(Base *b) {
+ return dynamic_cast<Bar *>(b);
+ }
+};
+
+
diff --git a/Examples/lua/import/bar.i b/Examples/lua/import/bar.i
new file mode 100644
index 0000000..5816cbe
--- /dev/null
+++ b/Examples/lua/import/bar.i
@@ -0,0 +1,9 @@
+%module bar
+%{
+#include "bar.h"
+%}
+
+%import base.i
+%include "bar.h"
+
+
diff --git a/Examples/lua/import/base.h b/Examples/lua/import/base.h
new file mode 100644
index 0000000..fec0f32
--- /dev/null
+++ b/Examples/lua/import/base.h
@@ -0,0 +1,16 @@
+class Base {
+ public:
+ Base() { };
+ virtual ~Base() { };
+ virtual const char * A() const {
+ return "Base::A";
+ }
+ const char * B() const {
+ return "Base::B";
+ }
+ virtual Base *toBase() {
+ return static_cast<Base *>(this);
+ }
+};
+
+
diff --git a/Examples/lua/import/base.i b/Examples/lua/import/base.i
new file mode 100644
index 0000000..f6e19ef
--- /dev/null
+++ b/Examples/lua/import/base.i
@@ -0,0 +1,6 @@
+%module base
+%{
+#include "base.h"
+%}
+
+%include base.h
diff --git a/Examples/lua/import/foo.h b/Examples/lua/import/foo.h
new file mode 100644
index 0000000..1abe2c0
--- /dev/null
+++ b/Examples/lua/import/foo.h
@@ -0,0 +1,21 @@
+#include "base.h"
+
+class Foo : public Base {
+ public:
+ Foo() { }
+ ~Foo() { }
+ virtual const char * A() const {
+ return "Foo::A";
+ }
+ const char * B() const {
+ return "Foo::B";
+ }
+ virtual Base *toBase() {
+ return static_cast<Base *>(this);
+ }
+ static Foo *fromBase(Base *b) {
+ return dynamic_cast<Foo *>(b);
+ }
+};
+
+
diff --git a/Examples/lua/import/foo.i b/Examples/lua/import/foo.i
new file mode 100644
index 0000000..27feb2e
--- /dev/null
+++ b/Examples/lua/import/foo.i
@@ -0,0 +1,8 @@
+%module foo
+%{
+#include "foo.h"
+%}
+
+%import base.i
+%include "foo.h"
+
diff --git a/Examples/lua/import/runme.lua b/Examples/lua/import/runme.lua
new file mode 100644
index 0000000..9cd7ae0
--- /dev/null
+++ b/Examples/lua/import/runme.lua
@@ -0,0 +1,103 @@
+# Test various properties of classes defined in separate modules
+
+print("Testing the %import directive")
+
+if string.sub(_VERSION,1,7)=='Lua 5.0' then
+ -- lua5.0 doesnt have a nice way to do this
+ function loadit(a)
+ lib=loadlib(a..'.dll','luaopen_'..a) or loadlib(a..'.so','luaopen_'..a)
+ assert(lib)()
+ end
+ loadit('base')
+ loadit('foo')
+ loadit('bar')
+ loadit('spam')
+else
+ -- lua 5.1 does
+ require 'base'
+ require 'foo'
+ require 'bar'
+ require 'spam'
+end
+
+-- Create some objects
+
+print("Creating some objects")
+
+a = base.Base()
+b = foo.Foo()
+c = bar.Bar()
+d = spam.Spam()
+
+-- Try calling some methods
+print("Testing some methods")
+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")
+
+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.Foo_fromBase(x)
+if y then
+ print "bad swig"
+else
+ print "good swig"
+end
+
+print " Spam -> Base -> Bar : "
+y = bar.Bar_fromBase(x)
+if y then
+ print "good swig"
+else
+ print "bad swig"
+end
+
+print " Spam -> Base -> Spam : "
+y = spam.Spam_fromBase(x)
+if y then
+ print "good swig"
+else
+ print "bad swig"
+end
+
+print " Foo -> Spam : "
+y = spam.Spam_fromBase(b)
+if y then
+ print "bad swig"
+else
+ print "good swig"
+end
diff --git a/Examples/lua/import/spam.h b/Examples/lua/import/spam.h
new file mode 100644
index 0000000..57db845
--- /dev/null
+++ b/Examples/lua/import/spam.h
@@ -0,0 +1,24 @@
+#include "bar.h"
+
+class Spam : public Bar {
+ public:
+ Spam() { }
+ ~Spam() { }
+ virtual const char * A() const {
+ return "Spam::A";
+ }
+ const char * B() const {
+ return "Spam::B";
+ }
+ virtual Base *toBase() {
+ return static_cast<Base *>(this);
+ }
+ virtual Bar *toBar() {
+ return static_cast<Bar *>(this);
+ }
+ static Spam *fromBase(Base *b) {
+ return dynamic_cast<Spam *>(b);
+ }
+};
+
+
diff --git a/Examples/lua/import/spam.i b/Examples/lua/import/spam.i
new file mode 100644
index 0000000..d3d9121
--- /dev/null
+++ b/Examples/lua/import/spam.i
@@ -0,0 +1,9 @@
+%module spam
+%{
+#include "spam.h"
+%}
+
+%import bar.i
+%include "spam.h"
+
+