summaryrefslogtreecommitdiff
path: root/Examples/ruby/template
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/ruby/template')
-rw-r--r--Examples/ruby/template/Makefile20
-rw-r--r--Examples/ruby/template/example.h32
-rw-r--r--Examples/ruby/template/example.i21
-rw-r--r--Examples/ruby/template/runme.rb25
4 files changed, 98 insertions, 0 deletions
diff --git a/Examples/ruby/template/Makefile b/Examples/ruby/template/Makefile
new file mode 100644
index 0000000..15c9d70
--- /dev/null
+++ b/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/Examples/ruby/template/example.h b/Examples/ruby/template/example.h
new file mode 100644
index 0000000..7401df6
--- /dev/null
+++ b/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/Examples/ruby/template/example.i b/Examples/ruby/template/example.i
new file mode 100644
index 0000000..d5ddf7a
--- /dev/null
+++ b/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/Examples/ruby/template/runme.rb b/Examples/ruby/template/runme.rb
new file mode 100644
index 0000000..6c9c4eb
--- /dev/null
+++ b/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