Fixed issues with C++ classes and hierachies across multiple source files.

Fixed imports test case & added run test.
Added Examples/imports.
Added typename for raw lua_State*
Added documentation on native functions.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9748 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Mark Gossage 2007-05-02 02:20:29 +00:00
commit 61fdde65cc
19 changed files with 820 additions and 481 deletions

View file

@ -4,6 +4,7 @@ constants
funcptr3
functest
functor
import
pointer
simple
variables

View file

@ -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

View file

@ -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

21
Examples/lua/import/bar.h Normal file
View file

@ -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);
}
};

View file

@ -0,0 +1,9 @@
%module bar
%{
#include "bar.h"
%}
%import base.i
%include "bar.h"

View file

@ -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);
}
};

View file

@ -0,0 +1,6 @@
%module base
%{
#include "base.h"
%}
%include base.h

21
Examples/lua/import/foo.h Normal file
View file

@ -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);
}
};

View file

@ -0,0 +1,8 @@
%module foo
%{
#include "foo.h"
%}
%import base.i
%include "foo.h"

View file

@ -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,b)
lib=loadlib(a..':dll',b) or loadlib(a..':so',b)
assert(lib)()
end
loadit('base','Base_Init')
loadit('foo','Foo_Init')
loadit('bar','Bar_Init')
loadit('spam','Spam_Init')
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

View file

@ -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);
}
};

View file

@ -0,0 +1,9 @@
%module spam
%{
#include "spam.h"
%}
%import bar.i
%include "spam.h"