summaryrefslogtreecommitdiff
path: root/Examples/tcl
diff options
context:
space:
mode:
authorOlly Betts <olly@survex.com>2007-10-15 02:02:47 +0000
committerOlly Betts <olly@survex.com>2007-10-15 02:02:47 +0000
commit6fdeeafb88751dde97a0c3103cc6b71d76e2fcd0 (patch)
treeaa50d4e84086c0f446608047ce2cf0c00f335ab0 /Examples/tcl
parentb1b889059d2ca9ef5c9a248e3805277719e41351 (diff)
downloadswig-6fdeeafb88751dde97a0c3103cc6b71d76e2fcd0.tar.gz
[Tcl] Prevent SWIG_Tcl_ConvertPtr from calling the unknown proc.
Add Examples/tcl/std_vector/ which this change fixes. Patch is from "Cliff C" in SF#1809819. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9989 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/tcl')
-rw-r--r--Examples/tcl/check.list1
-rw-r--r--Examples/tcl/std_vector/Makefile20
-rw-r--r--Examples/tcl/std_vector/example.h25
-rw-r--r--Examples/tcl/std_vector/example.i17
-rw-r--r--Examples/tcl/std_vector/runme.tcl40
5 files changed, 103 insertions, 0 deletions
diff --git a/Examples/tcl/check.list b/Examples/tcl/check.list
index 9f7e1b11e..8e7a8a9d7 100644
--- a/Examples/tcl/check.list
+++ b/Examples/tcl/check.list
@@ -11,5 +11,6 @@ operator
pointer
reference
simple
+std_vector
value
variables
diff --git a/Examples/tcl/std_vector/Makefile b/Examples/tcl/std_vector/Makefile
new file mode 100644
index 000000000..ce6a3c7ce
--- /dev/null
+++ b/Examples/tcl/std_vector/Makefile
@@ -0,0 +1,20 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS =
+TARGET = my_tclsh
+DLTARGET = example
+INTERFACE = example.i
+LIBS = -lm
+
+all::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ TARGET='$(DLTARGET)' INTERFACE='$(INTERFACE)' tcl_cpp
+
+static::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh_cpp_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile tcl_clean
+
+check: all
diff --git a/Examples/tcl/std_vector/example.h b/Examples/tcl/std_vector/example.h
new file mode 100644
index 000000000..4f0dac70d
--- /dev/null
+++ b/Examples/tcl/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/Examples/tcl/std_vector/example.i b/Examples/tcl/std_vector/example.i
new file mode 100644
index 000000000..aa58b66e0
--- /dev/null
+++ b/Examples/tcl/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/Examples/tcl/std_vector/runme.tcl b/Examples/tcl/std_vector/runme.tcl
new file mode 100644
index 000000000..2cece9691
--- /dev/null
+++ b/Examples/tcl/std_vector/runme.tcl
@@ -0,0 +1,40 @@
+# file: runme.tcl
+
+catch { load ./example[info sharedlibextension] example}
+
+# Exercise IntVector
+
+set iv [IntVector]
+$iv push 1
+$iv push 3
+$iv push 5
+
+puts "IntVector size: [$iv size] (should be 3)"
+puts "IntVector average: [average $iv] (should be 3.0)"
+puts "IntVector pop: [$iv pop] (should be 5)"
+puts "IntVector pop: [$iv pop] (should be 3)"
+puts "IntVector get 0: [$iv get 0] (should be 1)"
+puts ""
+
+# Exercise DoubleVector
+
+set dv [DoubleVector]
+$dv push 2
+$dv push 4
+$dv push 6
+
+puts "DoubleVector size: [$dv size] (should be 3)"
+puts "DoubleVector data: [$dv get 0] [$dv get 1] [$dv get 2] (should be 2.0 4.0 6.0)"
+halve_in_place $dv
+puts "DoubleVector halved: [$dv get 0] [$dv get 1] [$dv get 2] (should be 1.0 2.0 3.0)"
+puts ""
+
+# Complain if unknown is called
+rename unknown unknown_orig
+proc unknown {args} {
+ puts "ERROR: unknown called with: $args"
+ uplevel 1 unknown_orig $args
+}
+
+puts "average \"1 2 3\": [average [list 1 2 3]]"
+