diff options
Diffstat (limited to 'Examples/python/smartptr')
-rw-r--r-- | Examples/python/smartptr/Makefile | 22 | ||||
-rw-r--r-- | Examples/python/smartptr/example.cxx | 31 | ||||
-rw-r--r-- | Examples/python/smartptr/example.h | 39 | ||||
-rw-r--r-- | Examples/python/smartptr/example.i | 20 | ||||
-rw-r--r-- | Examples/python/smartptr/runme.py | 55 | ||||
-rw-r--r-- | Examples/python/smartptr/smartptr.h | 13 |
6 files changed, 180 insertions, 0 deletions
diff --git a/Examples/python/smartptr/Makefile b/Examples/python/smartptr/Makefile new file mode 100644 index 0000000..f73802a --- /dev/null +++ b/Examples/python/smartptr/Makefile @@ -0,0 +1,22 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = example.cxx +TARGET = example +INTERFACE = example.i +LIBS = -lm +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' python_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='mypython' INTERFACE='$(INTERFACE)' python_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile python_clean + rm -f $(TARGET).py + +check: all + $(MAKE) -f $(TOP)/Makefile python_run diff --git a/Examples/python/smartptr/example.cxx b/Examples/python/smartptr/example.cxx new file mode 100644 index 0000000..92a3add --- /dev/null +++ b/Examples/python/smartptr/example.cxx @@ -0,0 +1,31 @@ +/* File : example.c */ + +#include "example.h" +#include <math.h> +#ifndef M_PI +# define M_PI 3.14159265358979323846 +#endif + +/* 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/smartptr/example.h b/Examples/python/smartptr/example.h new file mode 100644 index 0000000..c0f9b1d --- /dev/null +++ b/Examples/python/smartptr/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/smartptr/example.i b/Examples/python/smartptr/example.i new file mode 100644 index 0000000..83da4f1 --- /dev/null +++ b/Examples/python/smartptr/example.i @@ -0,0 +1,20 @@ +/* File : example.i */ +%module example + +%{ +#include "example.h" +#include "smartptr.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + +/* Grab smart pointer template */ + +%include "smartptr.h" + +/* Instantiate smart-pointers */ + +%template(ShapePtr) SmartPtr<Shape>; + + diff --git a/Examples/python/smartptr/runme.py b/Examples/python/smartptr/runme.py new file mode 100644 index 0000000..5ea1fb9 --- /dev/null +++ b/Examples/python/smartptr/runme.py @@ -0,0 +1,55 @@ +# file: runme.py + +# This file illustrates the proxy class C++ interface generated +# by SWIG. + +import example + +# ----- Object creation ----- + +print "Creating some objects:" +cc = example.Circle(10) +c = example.ShapePtr(cc) +print " Created circle", c +ss = example.Square(10) +s = example.ShapePtr(ss) +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 +del cc +del ss + +s = 3 +print example.cvar.Shape_nshapes,"shapes remain" +print "Goodbye" + diff --git a/Examples/python/smartptr/smartptr.h b/Examples/python/smartptr/smartptr.h new file mode 100644 index 0000000..2ffa1ca --- /dev/null +++ b/Examples/python/smartptr/smartptr.h @@ -0,0 +1,13 @@ +template<class T> class SmartPtr { +public: + SmartPtr(T *realPtr = 0) { pointee = realPtr; } + T *operator->() const { + return pointee; + } + T &operator*() const { + return *pointee; + } +private: + T *pointee; +}; + |