summaryrefslogtreecommitdiff
path: root/Examples/ruby/operator
diff options
context:
space:
mode:
authorDave Beazley <dave-swig@dabeaz.com>2002-11-30 22:01:28 +0000
committerDave Beazley <dave-swig@dabeaz.com>2002-11-30 22:01:28 +0000
commit12a43edc2df8853e8e0315f742e57be88f0c4269 (patch)
treee3237f5f8c0a67c9bfa9bb5d6d095a739a49e4b2 /Examples/ruby/operator
parent5fcae5eb66d377e1c3f81da7465c44a62295a72b (diff)
downloadswig-12a43edc2df8853e8e0315f742e57be88f0c4269.tar.gz
The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/ruby/operator')
-rw-r--r--Examples/ruby/operator/.cvsignore2
-rw-r--r--Examples/ruby/operator/Makefile20
-rw-r--r--Examples/ruby/operator/example.h36
-rw-r--r--Examples/ruby/operator/example.i23
-rw-r--r--Examples/ruby/operator/runme.rb25
5 files changed, 106 insertions, 0 deletions
diff --git a/Examples/ruby/operator/.cvsignore b/Examples/ruby/operator/.cvsignore
new file mode 100644
index 000000000..f0cef3048
--- /dev/null
+++ b/Examples/ruby/operator/.cvsignore
@@ -0,0 +1,2 @@
+example_wrap.cxx
+example.dll
diff --git a/Examples/ruby/operator/Makefile b/Examples/ruby/operator/Makefile
new file mode 100644
index 000000000..43c0c1596
--- /dev/null
+++ b/Examples/ruby/operator/Makefile
@@ -0,0 +1,20 @@
+TOP = ../..
+SWIG = $(TOP)/../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)' ruby_cpp
+
+static::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile ruby_clean
+
+check: all
diff --git a/Examples/ruby/operator/example.h b/Examples/ruby/operator/example.h
new file mode 100644
index 000000000..4da6a2307
--- /dev/null
+++ b/Examples/ruby/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/ruby/operator/example.i b/Examples/ruby/operator/example.i
new file mode 100644
index 000000000..8d86694b3
--- /dev/null
+++ b/Examples/ruby/operator/example.i
@@ -0,0 +1,23 @@
+/* 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 */
+
+/* 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/ruby/operator/runme.rb b/Examples/ruby/operator/runme.rb
new file mode 100644
index 000000000..518d91e9e
--- /dev/null
+++ b/Examples/ruby/operator/runme.rb
@@ -0,0 +1,25 @@
+# Operator overloading example
+require 'example'
+
+include Example
+
+a = Complex.new(2, 3)
+b = Complex.new(-5, 10)
+
+puts "a = #{a}"
+puts "b = #{b}"
+
+c = a + b
+puts "c = #{c}"
+puts "a*b = #{a*b}"
+puts "a-c = #{a-c}"
+
+# This should invoke Complex's copy constructor
+e = Complex.new(a-c)
+e = a - c
+puts "e = #{e}"
+
+# Big expression
+f = ((a+b)*(c+b*e)) + (-a)
+puts "f = #{f}"
+