summaryrefslogtreecommitdiff
path: root/Examples/java/template
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/java/template')
-rw-r--r--Examples/java/template/Makefile18
-rw-r--r--Examples/java/template/example.h32
-rw-r--r--Examples/java/template/example.i17
-rw-r--r--Examples/java/template/index.html102
-rw-r--r--Examples/java/template/runme.java45
5 files changed, 214 insertions, 0 deletions
diff --git a/Examples/java/template/Makefile b/Examples/java/template/Makefile
new file mode 100644
index 0000000..2b3d35c
--- /dev/null
+++ b/Examples/java/template/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS =
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: java
+
+java::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java_cpp
+ javac *.java
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile java_clean
+
+check: all
diff --git a/Examples/java/template/example.h b/Examples/java/template/example.h
new file mode 100644
index 0000000..7401df6
--- /dev/null
+++ b/Examples/java/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/java/template/example.i b/Examples/java/template/example.i
new file mode 100644
index 0000000..8f94c4d
--- /dev/null
+++ b/Examples/java/template/example.i
@@ -0,0 +1,17 @@
+/* File : example.i */
+%module example
+
+%{
+#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/java/template/index.html b/Examples/java/template/index.html
new file mode 100644
index 0000000..f4408e5
--- /dev/null
+++ b/Examples/java/template/index.html
@@ -0,0 +1,102 @@
+<html>
+<head>
+<title>SWIG:Examples:java:template</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/java/template/</tt>
+<hr>
+
+<H2>C++ template support</H2>
+
+<p>
+This example illustrates how C++ templates can be used from Java using SWIG.
+
+<h2>The C++ Code</h2>
+
+Lets take a templated function and a templated class as follows:
+
+<blockquote>
+<pre>
+/* File : example.h */
+
+// Some template definitions
+
+template<class T> T max(T a, T b) { return a&gt;b ? a : b; }
+
+template<class T> class vector {
+ T *v;
+ int sz;
+ public:
+ vector(int _sz) {
+ v = new T[_sz];
+ sz = _sz;
+ }
+ T &amp;get(int index) {
+ return v[index];
+ }
+ void set(int index, T &amp;val) {
+ v[index] = val;
+ }
+#ifdef SWIG
+ %addmethods {
+ T getitem(int index) {
+ return self-&gt;get(index);
+ }
+ void setitem(int index, T val) {
+ self-&gt;set(index,val);
+ }
+ }
+#endif
+};
+</pre>
+</blockquote>
+The %addmethods is used for a neater interface from Java as the functions <tt>get</tt> and <tt>set</tt> use C++ references to primitive types. These are tricky to use from Java as they end up as a pointer in Java (Java long).
+
+<h2>The SWIG interface</h2>
+
+A simple SWIG interface for this can be built by simply grabbing the header file
+like this:
+
+<blockquote>
+<pre>
+/* File : example.i */
+%module example
+
+%{
+#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>;
+</pre>
+</blockquote>
+
+Note that SWIG parses the templated function <tt>max</tt> and templated class <tt>vector</tt> and so knows about them. However to generate code for use from Java, SWIG has to be told which class/type to use as the template parameter. The SWIG directive %template is used for this.
+
+<h2>A sample Java program</h2>
+
+Click <a href="runme.java">here</a> to see a Java program that calls the C++ functions from Java.
+
+<h2>Notes</h2>
+Use templated classes just like you would any other SWIG generated Java class. Use the classnames specified by the %template directive.
+
+<blockquote>
+<pre>
+vecdouble dv = new vecdouble(1000);
+dv.setitem(i, 12.34));
+</pre>
+</blockquote>
+
+<hr>
+</body>
+</html>
diff --git a/Examples/java/template/runme.java b/Examples/java/template/runme.java
new file mode 100644
index 0000000..5d1097b
--- /dev/null
+++ b/Examples/java/template/runme.java
@@ -0,0 +1,45 @@
+// This example illustrates how C++ templates can be used from Java.
+
+public class runme {
+ static {
+ try {
+ System.loadLibrary("example");
+ } catch (UnsatisfiedLinkError e) {
+ System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
+ System.exit(1);
+ }
+ }
+
+ public static void main(String argv[])
+ {
+ // Call some templated functions
+ System.out.println(example.maxint(3,7));
+ System.out.println(example.maxdouble(3.14,2.18));
+
+ // Create some class
+
+ vecint iv = new vecint(100);
+ vecdouble dv = new vecdouble(1000);
+
+ for (int i=0; i<100; i++)
+ iv.setitem(i,2*i);
+
+ for (int i=0; i<1000; i++)
+ dv.setitem(i, 1.0/(i+1));
+
+ {
+ int sum = 0;
+ for (int i=0; i<100; i++)
+ sum = sum + iv.getitem(i);
+
+ System.out.println(sum);
+ }
+
+ {
+ double sum = 0.0;
+ for (int i=0; i<1000; i++)
+ sum = sum + dv.getitem(i);
+ System.out.println(sum);
+ }
+ }
+}