diff options
Diffstat (limited to 'Examples/ruby/overloading')
-rw-r--r-- | Examples/ruby/overloading/Makefile | 19 | ||||
-rw-r--r-- | Examples/ruby/overloading/example.cxx | 125 | ||||
-rw-r--r-- | Examples/ruby/overloading/example.h | 41 | ||||
-rw-r--r-- | Examples/ruby/overloading/example.i | 24 | ||||
-rw-r--r-- | Examples/ruby/overloading/runme.rb | 88 |
5 files changed, 297 insertions, 0 deletions
diff --git a/Examples/ruby/overloading/Makefile b/Examples/ruby/overloading/Makefile new file mode 100644 index 0000000..56c84c6 --- /dev/null +++ b/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/Examples/ruby/overloading/example.cxx b/Examples/ruby/overloading/example.cxx new file mode 100644 index 0000000..49d998d --- /dev/null +++ b/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/Examples/ruby/overloading/example.h b/Examples/ruby/overloading/example.h new file mode 100644 index 0000000..e47a122 --- /dev/null +++ b/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/Examples/ruby/overloading/example.i b/Examples/ruby/overloading/example.i new file mode 100644 index 0000000..17ad669 --- /dev/null +++ b/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/Examples/ruby/overloading/runme.rb b/Examples/ruby/overloading/runme.rb new file mode 100644 index 0000000..9f12333 --- /dev/null +++ b/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) |