The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
6fcc22a1f8
commit
516036631c
1508 changed files with 125983 additions and 44037 deletions
104
SWIG/Examples/java/template/index.html
Normal file
104
SWIG/Examples/java/template/index.html
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:template</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/template/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>C++ template support</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<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>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
|
||||
%addmethods {
|
||||
T getitem(int index) {
|
||||
return self->get(index);
|
||||
}
|
||||
void setitem(int index, T val) {
|
||||
self->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="main.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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue