diff options
Diffstat (limited to 'Examples/ruby/operator')
-rw-r--r-- | Examples/ruby/operator/.cvsignore | 2 | ||||
-rw-r--r-- | Examples/ruby/operator/Makefile | 20 | ||||
-rw-r--r-- | Examples/ruby/operator/example.h | 36 | ||||
-rw-r--r-- | Examples/ruby/operator/example.i | 23 | ||||
-rw-r--r-- | Examples/ruby/operator/runme.rb | 25 |
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}" + |