summaryrefslogtreecommitdiff
path: root/trunk/Examples/ruby/std_vector
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2010-06-02 20:53:17 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2010-06-02 20:53:17 +0000
commit2824b0cbb66e715490e1ef13250bd675d87b32d9 (patch)
treec3bc8d54c6d73f2b7ce08cac34172dbc9f5e5b95 /trunk/Examples/ruby/std_vector
parent289cfef4b4766ff266f3b1bdda8ca3a952e5a047 (diff)
downloadswig-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/std_vector')
-rw-r--r--trunk/Examples/ruby/std_vector/Makefile20
-rw-r--r--trunk/Examples/ruby/std_vector/example.h25
-rw-r--r--trunk/Examples/ruby/std_vector/example.i17
-rw-r--r--trunk/Examples/ruby/std_vector/runme.rb36
4 files changed, 98 insertions, 0 deletions
diff --git a/trunk/Examples/ruby/std_vector/Makefile b/trunk/Examples/ruby/std_vector/Makefile
new file mode 100644
index 000000000..15c9d705f
--- /dev/null
+++ b/trunk/Examples/ruby/std_vector/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/std_vector/example.h b/trunk/Examples/ruby/std_vector/example.h
new file mode 100644
index 000000000..4f0dac70d
--- /dev/null
+++ b/trunk/Examples/ruby/std_vector/example.h
@@ -0,0 +1,25 @@
+/* File : example.h */
+
+#include <vector>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+
+double average(std::vector<int> v) {
+ return std::accumulate(v.begin(),v.end(),0.0)/v.size();
+}
+
+std::vector<double> half(const std::vector<double>& v) {
+ std::vector<double> w(v);
+ for (unsigned int i=0; i<w.size(); i++)
+ w[i] /= 2.0;
+ return w;
+}
+
+void halve_in_place(std::vector<double>& v) {
+ // would you believe this is the same as the above?
+ std::transform(v.begin(),v.end(),v.begin(),
+ std::bind2nd(std::divides<double>(),2.0));
+}
+
+
diff --git a/trunk/Examples/ruby/std_vector/example.i b/trunk/Examples/ruby/std_vector/example.i
new file mode 100644
index 000000000..aa58b66e0
--- /dev/null
+++ b/trunk/Examples/ruby/std_vector/example.i
@@ -0,0 +1,17 @@
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+%include stl.i
+/* instantiate the required template specializations */
+namespace std {
+ %template(IntVector) vector<int>;
+ %template(DoubleVector) vector<double>;
+}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
diff --git a/trunk/Examples/ruby/std_vector/runme.rb b/trunk/Examples/ruby/std_vector/runme.rb
new file mode 100644
index 000000000..851190536
--- /dev/null
+++ b/trunk/Examples/ruby/std_vector/runme.rb
@@ -0,0 +1,36 @@
+# file: runme.rb
+
+require 'example'
+
+# Call average with a Ruby array...
+
+puts Example::average([1,2,3,4])
+
+# ... or a wrapped std::vector<int>
+
+v = Example::IntVector.new(4)
+0.upto(v.size-1) { |i| v[i] = i+1 }
+puts Example::average(v)
+
+
+# half will return a Ruby array.
+# Call it with a Ruby array...
+
+w = Example::half([1.0, 1.5, 2.0, 2.5, 3.0])
+0.upto(w.size-1) { |i| print w[i],"; " }
+puts
+
+# ... or a wrapped std::vector<double>
+
+v = Example::DoubleVector.new
+[1,2,3,4].each { |i| v.push(i) }
+w = Example::half(v)
+0.upto(w.size-1) { |i| print w[i],"; " }
+puts
+
+# now halve a wrapped std::vector<double> in place
+
+Example::halve_in_place(v)
+0.upto(v.size-1) { |i| print v[i],"; " }
+puts
+