diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2010-06-02 20:53:17 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2010-06-02 20:53:17 +0000 |
commit | 2824b0cbb66e715490e1ef13250bd675d87b32d9 (patch) | |
tree | c3bc8d54c6d73f2b7ce08cac34172dbc9f5e5b95 /trunk/Examples/python/operator | |
parent | 289cfef4b4766ff266f3b1bdda8ca3a952e5a047 (diff) | |
download | swig-2.0.0.tar.gz |
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'trunk/Examples/python/operator')
-rw-r--r-- | trunk/Examples/python/operator/Makefile | 22 | ||||
-rw-r--r-- | trunk/Examples/python/operator/example.h | 36 | ||||
-rw-r--r-- | trunk/Examples/python/operator/example.i | 28 | ||||
-rw-r--r-- | trunk/Examples/python/operator/runme.py | 21 |
4 files changed, 107 insertions, 0 deletions
diff --git a/trunk/Examples/python/operator/Makefile b/trunk/Examples/python/operator/Makefile new file mode 100644 index 000000000..fe389757a --- /dev/null +++ b/trunk/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/trunk/Examples/python/operator/example.h b/trunk/Examples/python/operator/example.h new file mode 100644 index 000000000..4da6a2307 --- /dev/null +++ b/trunk/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/trunk/Examples/python/operator/example.i b/trunk/Examples/python/operator/example.i new file mode 100644 index 000000000..e37e76b3e --- /dev/null +++ b/trunk/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/trunk/Examples/python/operator/runme.py b/trunk/Examples/python/operator/runme.py new file mode 100644 index 000000000..3687a38de --- /dev/null +++ b/trunk/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 + |