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/tcl/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/tcl/operator')
| -rw-r--r-- | Examples/tcl/operator/Makefile | 19 | ||||
| -rw-r--r-- | Examples/tcl/operator/example.h | 36 | ||||
| -rw-r--r-- | Examples/tcl/operator/example.i | 28 | ||||
| -rw-r--r-- | Examples/tcl/operator/runme.tcl | 30 |
4 files changed, 113 insertions, 0 deletions
diff --git a/Examples/tcl/operator/Makefile b/Examples/tcl/operator/Makefile new file mode 100644 index 0000000..caf2f79 --- /dev/null +++ b/Examples/tcl/operator/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +CXXSRCS = +TARGET = example +INTERFACE = example.i +LIBS = -lm + +all:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tcl_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \ + TARGET='mytclsh' INTERFACE='$(INTERFACE)' tclsh_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile tcl_clean + +check: all diff --git a/Examples/tcl/operator/example.h b/Examples/tcl/operator/example.h new file mode 100644 index 0000000..4da6a23 --- /dev/null +++ b/Examples/tcl/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/tcl/operator/example.i b/Examples/tcl/operator/example.i new file mode 100644 index 0000000..fa5bccd --- /dev/null +++ b/Examples/tcl/operator/example.i @@ -0,0 +1,28 @@ +/* File : example.i */ +%module example + +%{ +#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/tcl/operator/runme.tcl b/Examples/tcl/operator/runme.tcl new file mode 100644 index 0000000..3b7c068 --- /dev/null +++ b/Examples/tcl/operator/runme.tcl @@ -0,0 +1,30 @@ +# Operator overloading example + +catch { load ./example[info sharedlibextension] example} + +set a [Complex -args 2 3] +set b [Complex -args -5 10] + +puts "a = $a [$a str]" +puts "b = $b [$b str]" + +set c [$a + $b] +Complex -this $c +puts "c = $c [$c str]" + +set d [$a * $b] +Complex -this $d +puts "a*b = [$d str]" + +# Alternative calling convention +set e [Complex_- $a $c] +puts "a-c = [Complex_str $e]" + +set f [new_ComplexCopy $e] +Complex -this $f +puts "f = [$f str]" + +# Call assignment operator +$c = $f +puts "c = [$c str]" + |
