Initial commit

This commit is contained in:
Bassem Girgis
2018-12-20 17:34:07 -06:00
parent 7a2d899662
commit 81b4b9e273
34743 changed files with 5940233 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SWIGOPT =
LIBS =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' python_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='base' INTERFACE='base.i' python_cpp
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='foo' INTERFACE='foo.i' python_cpp
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='bar' INTERFACE='bar.i' python_cpp
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' LIBS='$(LIBS)' TARGET='spam' INTERFACE='spam.i' python_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' python_clean
rm -f foo.py bar.py spam.py base.py

View File

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

View File

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

View File

@@ -0,0 +1,11 @@
%module bar
%{
#include "bar.h"
%}
%import base.i
%include "bar.h"
%template(intBar) Bar<int>;

View File

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

View File

@@ -0,0 +1,7 @@
%module base
%{
#include "base.h"
%}
%include base.h
%template(intBase) Base<int>;

View File

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

View File

@@ -0,0 +1,10 @@
%module foo
%{
#include "foo.h"
%}
%import base.i
%include "foo.h"
%template(intFoo) Foo<int>;

View File

@@ -0,0 +1,107 @@
# 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"

View File

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

View File

@@ -0,0 +1,10 @@
%module spam
%{
#include "spam.h"
%}
%import bar.i
%include "spam.h"
%template(intSpam) Spam<int>;