summaryrefslogtreecommitdiff
path: root/Examples/python/operator
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/python/operator')
-rw-r--r--Examples/python/operator/Makefile22
-rw-r--r--Examples/python/operator/example.h36
-rw-r--r--Examples/python/operator/example.i28
-rw-r--r--Examples/python/operator/runme.py21
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
+