summaryrefslogtreecommitdiff
path: root/trunk/Examples/ruby/overloading
diff options
context:
space:
mode:
Diffstat (limited to 'trunk/Examples/ruby/overloading')
-rw-r--r--trunk/Examples/ruby/overloading/Makefile19
-rw-r--r--trunk/Examples/ruby/overloading/example.cxx125
-rw-r--r--trunk/Examples/ruby/overloading/example.h41
-rw-r--r--trunk/Examples/ruby/overloading/example.i24
-rw-r--r--trunk/Examples/ruby/overloading/runme.rb88
5 files changed, 297 insertions, 0 deletions
diff --git a/trunk/Examples/ruby/overloading/Makefile b/trunk/Examples/ruby/overloading/Makefile
new file mode 100644
index 000000000..56c84c651
--- /dev/null
+++ b/trunk/Examples/ruby/overloading/Makefile
@@ -0,0 +1,19 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS = example.cxx
+TARGET = example
+INTERFACE = example.i
+LIBS = -lm
+
+all::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ 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/trunk/Examples/ruby/overloading/example.cxx b/trunk/Examples/ruby/overloading/example.cxx
new file mode 100644
index 000000000..49d998d5a
--- /dev/null
+++ b/trunk/Examples/ruby/overloading/example.cxx
@@ -0,0 +1,125 @@
+#include <iostream>
+
+#include "example.h"
+
+// Overloaded constructors for class Bar
+Bar::Bar() {
+ std::cout << "Called Bar::Bar()" << std::endl;
+}
+
+Bar::Bar(const Bar&) {
+ std::cout << "Called Bar::Bar(const Bar&)" << std::endl;
+}
+
+Bar::Bar(double x) {
+ std::cout << "Called Bar::Bar(double) with x = " << x << std::endl;
+}
+
+Bar::Bar(double x, char *y) {
+ std::cout << "Called Bar::Bar(double, char *) with x, y = " << x << ", \"" << y << "\"" << std::endl;
+}
+
+Bar::Bar(int x, int y) {
+ std::cout << "Called Bar::Bar(int, int) with x, y = " << x << ", " << y << std::endl;
+}
+
+Bar::Bar(char *x) {
+ std::cout << "Called Bar::Bar(char *) with x = \"" << x << "\"" << std::endl;
+}
+
+Bar::Bar(int x) {
+ std::cout << "Called Bar::Bar(int) with x = " << x << std::endl;
+}
+
+Bar::Bar(long x) {
+ std::cout << "Called Bar::Bar(long) with x = " << x << std::endl;
+}
+
+Bar::Bar(Bar *x) {
+ std::cout << "Called Bar::Bar(Bar *) with x = " << x << std::endl;
+}
+
+// Overloaded member functions
+void Bar::foo(const Bar& x) {
+ std::cout << "Called Bar::foo(const Bar&) with &x = " << &x << std::endl;
+}
+
+void Bar::foo(double x) {
+ std::cout << "Called Bar::foo(double) with x = " << x << std::endl;
+}
+
+void Bar::foo(double x, char *y) {
+ std::cout << "Called Bar::foo(double, char *) with x, y = " << x << ", \"" << y << "\"" << std::endl;
+}
+
+void Bar::foo(int x, int y) {
+ std::cout << "Called Bar::foo(int, int) with x, y = " << x << ", " << y << std::endl;
+}
+
+void Bar::foo(char *x) {
+ std::cout << "Called Bar::foo(char *) with x = \"" << x << "\"" << std::endl;
+}
+
+void Bar::foo(int x) {
+ std::cout << "Called Bar::foo(int) with x = " << x << std::endl;
+}
+
+void Bar::foo(long x) {
+ std::cout << "Called Bar::foo(long) with x = " << x << std::endl;
+}
+
+void Bar::foo(Bar *x) {
+ std::cout << "Called Bar::foo(Bar *) with x = " << x << std::endl;
+}
+
+void Bar::spam(int x, int y, int z) {
+ std::cout << "Called Bar::spam(int, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
+}
+
+void Bar::spam(double x, int y, int z) {
+ std::cout << "Called Bar::spam(double, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
+}
+
+// Overloaded global methods
+void foo(const Bar& x) {
+ std::cout << "Called foo(const Bar& x) with &x = " << &x << std::endl;
+}
+
+void foo(double x) {
+ std::cout << "Called foo(double) with x = " << x << std::endl;
+}
+
+void foo(double x, char *y) {
+ std::cout << "Called foo(double, char *) with x, y = " << x << ", \"" << y << "\"" << std::endl;
+}
+
+void foo(int x, int y) {
+ std::cout << "Called foo(int, int) with x, y = " << x << ", " << y << std::endl;
+}
+
+void foo(char *x) {
+ std::cout << "Called foo(char *) with x = \"" << x << "\"" << std::endl;
+}
+
+void foo(int x) {
+ std::cout << "Called foo(int) with x = " << x << std::endl;
+}
+
+void foo(long x) {
+ std::cout << "Called foo(long) with x = " << x << std::endl;
+}
+
+void foo(Bar *x) {
+ std::cout << "Called foo(Bar *) with x = " << x << std::endl;
+}
+
+// Overloaded global spam() functions
+void spam(int x, int y, int z) {
+ std::cout << "Called spam(int, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
+}
+
+void spam(double x, int y, int z) {
+ std::cout << "Called spam(double, int, int) with x, y, z = " << x << ", " << y << ", " << z << std::endl;
+}
+
+
diff --git a/trunk/Examples/ruby/overloading/example.h b/trunk/Examples/ruby/overloading/example.h
new file mode 100644
index 000000000..e47a122ee
--- /dev/null
+++ b/trunk/Examples/ruby/overloading/example.h
@@ -0,0 +1,41 @@
+#ifndef EXAMPLE_H
+#define EXAMPLE_H
+
+class Bar {
+public:
+ Bar();
+ Bar(const Bar&);
+ Bar(double);
+ Bar(double, char *);
+ Bar(int, int);
+ Bar(char *);
+ Bar(long);
+ Bar(int);
+ Bar(Bar *);
+
+ void foo(const Bar&);
+ void foo(double);
+ void foo(double, char *);
+ void foo(int, int);
+ void foo(char *);
+ void foo(long);
+ void foo(int);
+ void foo(Bar *);
+
+ void spam(int x, int y=2, int z=3);
+ void spam(double x, int y=2, int z=3);
+};
+
+void foo(const Bar&);
+void foo(double);
+void foo(double, char *);
+void foo(int, int);
+void foo(char *);
+void foo(int);
+void foo(long);
+void foo(Bar *);
+
+void spam(int x, int y=2, int z=3);
+void spam(double x, int y=2, int z=3);
+
+#endif
diff --git a/trunk/Examples/ruby/overloading/example.i b/trunk/Examples/ruby/overloading/example.i
new file mode 100644
index 000000000..17ad66997
--- /dev/null
+++ b/trunk/Examples/ruby/overloading/example.i
@@ -0,0 +1,24 @@
+%module example
+
+%{
+#include "example.h"
+%}
+
+/**
+ * These overloaded declarations conflict with other overloads (as far as
+ * SWIG's Ruby module's implementation for overloaded methods is concerned).
+ * One option is use the %rename directive to rename the conflicting methods;
+ * here, we're just using %ignore to avoid wrapping some of the overloaded
+ * functions altogether.
+ */
+
+%ignore Bar::Bar(Bar *);
+%ignore Bar::Bar(long);
+
+%ignore Bar::foo(const Bar&);
+%ignore Bar::foo(long);
+
+%ignore ::foo(const Bar&);
+%ignore ::foo(int);
+
+%include example.h
diff --git a/trunk/Examples/ruby/overloading/runme.rb b/trunk/Examples/ruby/overloading/runme.rb
new file mode 100644
index 000000000..9f1233347
--- /dev/null
+++ b/trunk/Examples/ruby/overloading/runme.rb
@@ -0,0 +1,88 @@
+require 'example'
+
+# This should invoke foo(double)
+Example.foo(3.14159)
+
+# This should invoke foo(double, char *)
+Example.foo(3.14159, "Pi")
+
+# This should invoke foo(int, int)
+Example.foo(3, 4)
+
+# This should invoke foo(char *)
+Example.foo("This is a test")
+
+# This should invoke foo(long)
+Example.foo(42)
+
+# This should invoke Bar::Bar() followed by foo(Bar *)
+Example.foo(Example::Bar.new)
+
+# Skip a line
+puts ""
+
+# Each of the following three calls should invoke spam(int, int, int)
+Example.spam(3)
+Example.spam(3, 4)
+Example.spam(3, 4, 5)
+
+# Skip a line
+puts ""
+
+# Each of the following three calls should invoke spam(double, int, int)
+Example.spam(3.0)
+Example.spam(3.0, 4)
+Example.spam(3.0, 4, 5)
+
+# Skip a line
+puts ""
+
+# This should invoke Bar::Bar(double)
+Example::Bar.new(3.14159)
+
+# This should invoke Bar::Bar(double, char *)
+Example::Bar.new(3.14159, "Pi")
+
+# This should invoke Bar::Bar(int, int)
+Example::Bar.new(3, 4)
+
+# This should invoke Bar::Bar(char *)
+Example::Bar.new("This is a test")
+
+# This should invoke Bar::Bar(int)
+Example::Bar.new(42)
+
+# This should invoke Bar::Bar() for the input argument,
+# followed by Bar::Bar(const Bar&).
+Example::Bar.new(Example::Bar.new)
+
+# Skip a line
+puts ""
+
+# Construct a new Bar instance (invokes Bar::Bar())
+bar = Example::Bar.new
+
+# This should invoke Bar::foo(double)
+bar.foo(3.14159)
+
+# This should invoke Bar::foo(double, char *)
+bar.foo(3.14159, "Pi")
+
+# This should invoke Bar::foo(int, int)
+bar.foo(3, 4)
+
+# This should invoke Bar::foo(char *)
+bar.foo("This is a test")
+
+# This should invoke Bar::foo(int)
+bar.foo(42)
+
+# This should invoke Bar::Bar() to construct the input
+# argument, followed by Bar::foo(Bar *).
+bar.foo(Example::Bar.new)
+
+# This should invoke Bar::spam(int x, int y, int z)
+bar.spam(1)
+
+# This should invoke Bar::spam(double x, int y, int z)
+bar.spam(3.14159)