diff options
| author | William S Fulton <wsf@fultondesigns.co.uk> | 2010-06-02 20:53:17 +0000 |
|---|---|---|
| committer | William S Fulton <wsf@fultondesigns.co.uk> | 2010-06-02 20:53:17 +0000 |
| commit | 2824b0cbb66e715490e1ef13250bd675d87b32d9 (patch) | |
| tree | c3bc8d54c6d73f2b7ce08cac34172dbc9f5e5b95 /trunk/Examples/ruby/template | |
| parent | 289cfef4b4766ff266f3b1bdda8ca3a952e5a047 (diff) | |
| download | swig-2.0.0.tar.gz | |
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/tags/rel-2.0.0@12089 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'trunk/Examples/ruby/template')
| -rw-r--r-- | trunk/Examples/ruby/template/Makefile | 20 | ||||
| -rw-r--r-- | trunk/Examples/ruby/template/example.h | 32 | ||||
| -rw-r--r-- | trunk/Examples/ruby/template/example.i | 21 | ||||
| -rw-r--r-- | trunk/Examples/ruby/template/runme.rb | 25 |
4 files changed, 98 insertions, 0 deletions
diff --git a/trunk/Examples/ruby/template/Makefile b/trunk/Examples/ruby/template/Makefile new file mode 100644 index 000000000..15c9d705f --- /dev/null +++ b/trunk/Examples/ruby/template/Makefile @@ -0,0 +1,20 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-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)' \ + SWIGOPT='$(SWIGOPT)' TARGET='myruby' INTERFACE='$(INTERFACE)' ruby_cpp_static + +clean:: + $(MAKE) -f $(TOP)/Makefile ruby_clean + +check: all diff --git a/trunk/Examples/ruby/template/example.h b/trunk/Examples/ruby/template/example.h new file mode 100644 index 000000000..7401df650 --- /dev/null +++ b/trunk/Examples/ruby/template/example.h @@ -0,0 +1,32 @@ +/* File : example.h */ + +// Some template definitions + +template<class T> T max(T a, T b) { return a>b ? a : b; } + +template<class T> class vector { + T *v; + int sz; + public: + vector(int _sz) { + v = new T[_sz]; + sz = _sz; + } + T &get(int index) { + return v[index]; + } + void set(int index, T &val) { + v[index] = val; + } +#ifdef SWIG + %extend { + T getitem(int index) { + return $self->get(index); + } + void setitem(int index, T val) { + $self->set(index,val); + } + } +#endif +}; + diff --git a/trunk/Examples/ruby/template/example.i b/trunk/Examples/ruby/template/example.i new file mode 100644 index 000000000..d5ddf7aaa --- /dev/null +++ b/trunk/Examples/ruby/template/example.i @@ -0,0 +1,21 @@ +/* File : example.i */ +%module example + +%{ +#ifdef max +#undef max +#endif + +#include "example.h" +%} + +/* Let's just grab the original header file here */ +%include "example.h" + +/* Now instantiate some specific template declarations */ + +%template(maxint) max<int>; +%template(maxdouble) max<double>; +%template(Vecint) vector<int>; +%template(Vecdouble) vector<double>; + diff --git a/trunk/Examples/ruby/template/runme.rb b/trunk/Examples/ruby/template/runme.rb new file mode 100644 index 000000000..6c9c4ebc2 --- /dev/null +++ b/trunk/Examples/ruby/template/runme.rb @@ -0,0 +1,25 @@ +# file: runme.rb + +require 'example' + +# Call some templated functions +puts Example::maxint(3, 7) +puts Example::maxdouble(3.14, 2.18) + +# Create some class + +iv = Example::Vecint.new(100) +dv = Example::Vecdouble.new(1000) + +100.times { |i| iv.setitem(i, 2*i) } + +1000.times { |i| dv.setitem(i, 1.0/(i+1)) } + +sum = 0 +100.times { |i| sum = sum + iv.getitem(i) } + +puts sum + +sum = 0.0 +1000.times { |i| sum = sum + dv.getitem(i) } +puts sum |
