diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2009-08-18 20:56:02 +0000 |
---|---|---|
committer | Lorry <lorry@roadtrain.codethink.co.uk> | 2012-09-25 16:59:08 +0000 |
commit | 9f8a09ed743cedd9547bf0661d518647966ab114 (patch) | |
tree | 9c7803d3b27a8ec22e91792ac7f7932efa128b20 /Examples/python/operator | |
download | swig-tarball-master.tar.gz |
Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz.HEADswig-1.3.40master
Diffstat (limited to 'Examples/python/operator')
-rw-r--r-- | Examples/python/operator/Makefile | 22 | ||||
-rw-r--r-- | Examples/python/operator/example.h | 36 | ||||
-rw-r--r-- | Examples/python/operator/example.i | 28 | ||||
-rw-r--r-- | Examples/python/operator/runme.py | 21 |
4 files changed, 107 insertions, 0 deletions
diff --git a/Examples/python/operator/Makefile b/Examples/python/operator/Makefile new file mode 100644 index 0000000..fe38975 --- /dev/null +++ b/Examples/python/operator/Makefile @@ -0,0 +1,22 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +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)' \ + 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/operator/example.h b/Examples/python/operator/example.h new file mode 100644 index 0000000..4da6a23 --- /dev/null +++ b/Examples/python/operator/example.h @@ -0,0 +1,36 @@ +/* File : example.h */ +#include <math.h> + +class Complex { +private: + double rpart, ipart; +public: + Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { } + Complex(const Complex &c) : rpart(c.rpart), ipart(c.ipart) { } + Complex &operator=(const Complex &c) { + rpart = c.rpart; + ipart = c.ipart; + return *this; + } + Complex operator+(const Complex &c) const { + return Complex(rpart+c.rpart, ipart+c.ipart); + } + Complex operator-(const Complex &c) const { + return Complex(rpart-c.rpart, ipart-c.ipart); + } + Complex operator*(const Complex &c) const { + return Complex(rpart*c.rpart - ipart*c.ipart, + rpart*c.ipart + c.rpart*ipart); + } + Complex operator-() const { + return Complex(-rpart, -ipart); + } + + double re() const { return rpart; } + double im() const { return ipart; } +}; + + + + + diff --git a/Examples/python/operator/example.i b/Examples/python/operator/example.i new file mode 100644 index 0000000..e37e76b --- /dev/null +++ b/Examples/python/operator/example.i @@ -0,0 +1,28 @@ +/* File : example.i */ +%module example +#pragma SWIG nowarn=SWIGWARN_IGNORE_OPERATOR_EQ +%{ +#include "example.h" +%} + +/* This header file is a little tough to handle because it has overloaded + operators and constructors. We're going to try and deal with that here */ + +/* This turns the copy constructor in a function ComplexCopy() that can + be called */ + +%rename(ComplexCopy) Complex::Complex(Complex const &); + +/* Now grab the original header file */ +%include "example.h" + +/* An output method that turns a complex into a short string */ +%extend Complex { + char *__str__() { + static char temp[512]; + sprintf(temp,"(%g,%g)", $self->re(), $self->im()); + return temp; + } +}; + + diff --git a/Examples/python/operator/runme.py b/Examples/python/operator/runme.py new file mode 100644 index 0000000..3687a38 --- /dev/null +++ b/Examples/python/operator/runme.py @@ -0,0 +1,21 @@ +# Operator overloading example +import example + +a = example.Complex(2,3) +b = example.Complex(-5,10) + +print "a =",a +print "b =",b + +c = a + b +print "c =",c +print "a*b =",a*b +print "a-c =",a-c + +e = example.ComplexCopy(a-c) +print "e =",e + +# Big expression +f = ((a+b)*(c+b*e)) + (-a) +print "f =",f + |