diff --git a/Examples/python/shadow/Makefile b/Examples/python/shadow/Makefile new file mode 100644 index 000000000..908edc992 --- /dev/null +++ b/Examples/python/shadow/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../swig -shadow +CXXSRCS = example.cxx +TARGET = examplec +INTERFACE = example.i +LIBS = -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static + +clean:: + rm -f *_wrap* *.o *~ *.so mypython *.pyc .~* core + +check: all diff --git a/Examples/python/shadow/example.cxx b/Examples/python/shadow/example.cxx new file mode 100644 index 000000000..21582f4d1 --- /dev/null +++ b/Examples/python/shadow/example.cxx @@ -0,0 +1,28 @@ +/* File : example.c */ + +#include "example.h" +#include + +/* Move the shape to a new location */ +void Shape::move(double dx, double dy) { + x += dx; + y += dy; +} + +int Shape::nshapes = 0; + +double Circle::area() { + return M_PI*radius*radius; +} + +double Circle::perimeter() { + return 2*M_PI*radius; +} + +double Square::area() { + return width*width; +} + +double Square::perimeter() { + return 4*width; +} diff --git a/Examples/python/shadow/example.h b/Examples/python/shadow/example.h new file mode 100644 index 000000000..c0f9b1d57 --- /dev/null +++ b/Examples/python/shadow/example.h @@ -0,0 +1,39 @@ +/* File : example.h */ + +class Shape { +public: + Shape() { + nshapes++; + } + virtual ~Shape() { + nshapes--; + }; + double x, y; + void move(double dx, double dy); + virtual double area() = 0; + virtual double perimeter() = 0; + static int nshapes; +}; + +class Circle : public Shape { +private: + double radius; +public: + Circle(double r) : radius(r) { }; + virtual double area(); + virtual double perimeter(); +}; + +class Square : public Shape { +private: + double width; +public: + Square(double w) : width(w) { }; + virtual double area(); + virtual double perimeter(); +}; + + + + + diff --git a/Examples/python/shadow/example.i b/Examples/python/shadow/example.i new file mode 100644 index 000000000..75700b305 --- /dev/null +++ b/Examples/python/shadow/example.i @@ -0,0 +1,10 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + diff --git a/Examples/python/shadow/index.html b/Examples/python/shadow/index.html new file mode 100644 index 000000000..e28329bc2 --- /dev/null +++ b/Examples/python/shadow/index.html @@ -0,0 +1,21 @@ + + +SWIG:Examples:python:shadow + + + + + +SWIG/Examples/python/shadow/ +
+ +

Wrapping a simple C++ class

+ +$Header$
+ +

+This example illustrates the wrapping of some C++ classes by shadow classes. + +


+ + diff --git a/Examples/python/shadow/runme.py b/Examples/python/shadow/runme.py new file mode 100644 index 000000000..42a5aa363 --- /dev/null +++ b/Examples/python/shadow/runme.py @@ -0,0 +1,51 @@ +# file: runme.py + +# This file illustrates the shadow-class C++ interface generated +# by SWIG. + +import example + +# ----- Object creation ----- + +print "Creating some objects:" +c = example.Circle(10) +print " Created circle", c +s = example.Square(10) +print " Created square", s + +# ----- Access a static member ----- + +print "\nA total of", example.cvar.Shape_nshapes,"shapes were created" + +# ----- Member data access ----- + +# Set the location of the object + +c.x = 20 +c.y = 30 + +s.x = -10 +s.y = 5 + +print "\nHere is their current position:" +print " Circle = (%f, %f)" % (c.x,c.y) +print " Square = (%f, %f)" % (s.x,s.y) + +# ----- Call some methods ----- + +print "\nHere are some properties of the shapes:" +for o in [c,s]: + print " ", o + print " area = ", o.area() + print " perimeter = ", o.perimeter() + +print "\nGuess I'll clean up now" + +# Note: this invokes the virtual destructor +del c +del s + +s = 3 +print example.cvar.Shape_nshapes,"shapes remain" +print "Goodbye" +