The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
5fcae5eb66
commit
12a43edc2d
1508 changed files with 125983 additions and 44037 deletions
14
Examples/java/check.list
Normal file
14
Examples/java/check.list
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# see top-level Makefile.in
|
||||
class
|
||||
constants
|
||||
enum
|
||||
funcptr
|
||||
mpointer
|
||||
multimap
|
||||
native
|
||||
pointer
|
||||
reference
|
||||
simple
|
||||
template
|
||||
typemap
|
||||
variables
|
||||
11
Examples/java/class/.cvsignore
Normal file
11
Examples/java/class/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/class/Makefile
Normal file
18
Examples/java/class/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
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
|
||||
28
Examples/java/class/example.cxx
Normal file
28
Examples/java/class/example.cxx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
162
Examples/java/class/example.dsp
Normal file
162
Examples/java/class/example.dsp
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
# Microsoft Developer Studio Project File - Name="example" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=example - Win32 Release
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak" CFG="example - Win32 Release"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "example - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "example - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "_DEBUG"
|
||||
# ADD RSC /l 0x809 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept
|
||||
# Begin Special Build Tool
|
||||
SOURCE="$(InputPath)"
|
||||
PostBuild_Desc=Java compile post-build step
|
||||
PostBuild_Cmds=echo on %JAVA_BIN%\javac *.java
|
||||
# End Special Build Tool
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W3 /GX /O2 /I "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "NDEBUG"
|
||||
# ADD RSC /l 0x809 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"example.dll"
|
||||
# Begin Special Build Tool
|
||||
SOURCE="$(InputPath)"
|
||||
PostBuild_Desc=Java compile post-build step
|
||||
PostBuild_Cmds=echo on %JAVA_BIN%\javac *.java
|
||||
# End Special Build Tool
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "example - Win32 Debug"
|
||||
# Name "example - Win32 Release"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.cxx
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example_wrap.cxx
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.i
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo JAVA_INCLUDE: %JAVA_INCLUDE%
|
||||
echo JAVA_BIN: %JAVA_BIN%
|
||||
echo on
|
||||
..\..\..\swig.exe -c++ -java $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.cxx" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo JAVA_INCLUDE: %JAVA_INCLUDE%
|
||||
echo JAVA_BIN: %JAVA_BIN%
|
||||
echo on
|
||||
..\..\..\swig.exe -c++ -java $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# End Target
|
||||
# End Project
|
||||
39
Examples/java/class/example.h
Normal file
39
Examples/java/class/example.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
10
Examples/java/class/example.i
Normal file
10
Examples/java/class/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
199
Examples/java/class/index.html
Normal file
199
Examples/java/class/index.html
Normal file
|
|
@ -0,0 +1,199 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:class</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/class/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping a simple C++ class</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example illustrates the high level form of C++ class wrapping performed
|
||||
by SWIG. In this case, a C++ class has a proxy Java class, which
|
||||
provides access to C++ class members.
|
||||
|
||||
<h2>The C++ Code</h2>
|
||||
|
||||
Suppose you have some C++ classes described by the following (and admittedly lame)
|
||||
header file:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area() = 0;
|
||||
virtual double perimeter() = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area();
|
||||
virtual double perimeter();
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area();
|
||||
virtual double perimeter();
|
||||
};
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<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"
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this:
|
||||
<blockquote>
|
||||
<pre>
|
||||
% swig -c++ -java example.i
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<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>Key points</h2>
|
||||
|
||||
<ul>
|
||||
<li>To create a new object, you call a constructor like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
Circle c = new Circle(10);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>To access member data, a pair of accessor functions are used.
|
||||
For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
c.setX(15); // Set member data
|
||||
x = c.getX(); // Get member data
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>To invoke a member function, you simply do this
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
System.out.println( "The area is " + c.area() );
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>To invoke a destructor, simply do this
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
c.delete(); // Deletes a shape
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>Static member variables are wrapped with java static get and set access functions. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
n = Shape.getNshapes(); // Get a static data member
|
||||
Shape.setNshapes(13); // Set a static data member
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
</ul>
|
||||
|
||||
<h2>General Comments</h2>
|
||||
|
||||
<ul>
|
||||
<li>This high-level interface using shadow classes is not the only way to handle C++ code.
|
||||
A low level interface using c functions to access member variables and member functions is the alternative SWIG
|
||||
approach. This entails passing around the c pointer or c++ 'this' pointer and as such it is not difficult to crash the JVM.
|
||||
The abstraction of the underlying pointer by the java shadow classes far better fits the java programming paradigm.
|
||||
|
||||
<p>
|
||||
<li>SWIG *does* know how to properly perform upcasting of objects in an inheritance
|
||||
hierarchy (including multiple inheritance). However Java classes can only derive from one base class so multiple inheritance
|
||||
is not implemented. Java classes can implement more than one interface so there is scope for improvement in the future.
|
||||
|
||||
<p>
|
||||
<li>A wide variety of C++ features are not currently supported by SWIG. Here is the
|
||||
short and incomplete list:
|
||||
|
||||
<p>
|
||||
<ul>
|
||||
<li>Overloaded methods and functions. SWIG wrappers don't know how to resolve name
|
||||
conflicts so you must give an alternative name to any overloaded method name using the
|
||||
%name directive like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
void foo(int a);
|
||||
%name(foo2) void foo(double a, double b);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>Overloaded operators. Not supported at all. The only workaround for this is
|
||||
to write a helper function. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%inline %{
|
||||
Vector *vector_add(Vector *a, Vector *b) {
|
||||
... whatever ...
|
||||
}
|
||||
%}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>Namespaces. Not supported at all. Won't be supported until SWIG2.0 (if at all).
|
||||
|
||||
</ul>
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
70
Examples/java/class/main.java
Normal file
70
Examples/java/class/main.java
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// This example illustrates how C++ classes can be used from Java using SWIG.
|
||||
// The Java class gets mapped onto the C++ class and behaves as if it is a Java class.
|
||||
|
||||
public class main {
|
||||
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[])
|
||||
{
|
||||
// ----- Object creation -----
|
||||
|
||||
System.out.println( "Creating some objects:" );
|
||||
Circle c = new Circle(10);
|
||||
System.out.println( " Created circle " + c );
|
||||
Square s = new Square(10);
|
||||
System.out.println( " Created square " + s );
|
||||
|
||||
// ----- Access a static member -----
|
||||
|
||||
System.out.println( "\nA total of " + Shape.getNshapes() + " shapes were created" );
|
||||
|
||||
// ----- Member data access -----
|
||||
|
||||
// Notice how we can do this using functions specific to
|
||||
// the 'Circle' class.
|
||||
c.setX(20);
|
||||
c.setY(30);
|
||||
|
||||
// Now use the same functions in the base class
|
||||
Shape shape = s;
|
||||
shape.setX(-10);
|
||||
shape.setY(5);
|
||||
|
||||
System.out.println( "\nHere is their current position:" );
|
||||
System.out.println( " Circle = (" + c.getX() + " " + c.getY() + ")" );
|
||||
System.out.println( " Square = (" + s.getX() + " " + s.getY() + ")" );
|
||||
|
||||
// ----- Call some methods -----
|
||||
|
||||
System.out.println( "\nHere are some properties of the shapes:" );
|
||||
Shape[] shapes = {c,s};
|
||||
for (int i=0; i<shapes.length; i++)
|
||||
{
|
||||
System.out.println( " " + shapes[i].toString() );
|
||||
System.out.println( " area = " + shapes[i].area() );
|
||||
System.out.println( " perimeter = " + shapes[i].perimeter() );
|
||||
}
|
||||
|
||||
// Notice how the area() and perimeter() functions really
|
||||
// invoke the appropriate virtual method on each object.
|
||||
|
||||
// ----- Delete everything -----
|
||||
|
||||
System.out.println( "\nGuess I'll clean up now" );
|
||||
|
||||
// Note: this invokes the virtual destructor
|
||||
// You could leave this to the garbage collector
|
||||
c.delete();
|
||||
s.delete();
|
||||
|
||||
System.out.println( Shape.getNshapes() + " shapes remain" );
|
||||
System.out.println( "Goodbye" );
|
||||
}
|
||||
}
|
||||
11
Examples/java/constants/.cvsignore
Normal file
11
Examples/java/constants/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/constants/Makefile
Normal file
18
Examples/java/constants/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../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
|
||||
27
Examples/java/constants/example.i
Normal file
27
Examples/java/constants/example.i
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* A few preprocessor macros */
|
||||
|
||||
#define ICONST 42
|
||||
#define FCONST 2.1828
|
||||
#define CCONST 'x'
|
||||
#define CCONST2 '\n'
|
||||
#define SCONST "Hello World"
|
||||
#define SCONST2 "\"Hello World\""
|
||||
|
||||
/* This should work just fine */
|
||||
#define EXPR ICONST + 3*(FCONST)
|
||||
|
||||
/* This shouldn't do anything */
|
||||
#define EXTERN extern
|
||||
|
||||
/* Neither should this (BAR isn't defined) */
|
||||
#define FOO (ICONST + BAR)
|
||||
|
||||
/* The following directives also produce constants */
|
||||
|
||||
%constant int iconst = 37;
|
||||
%constant double fconst = 3.14;
|
||||
|
||||
|
||||
53
Examples/java/constants/index.html
Normal file
53
Examples/java/constants/index.html
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:constants</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
<tt>SWIG/Examples/java/constants/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping C Constants</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
When SWIG encounters C preprocessor macros and C declarations that look like constants,
|
||||
it creates Java constant with an identical value. Click <a href="example.i">here</a>
|
||||
to see a SWIG interface with some constant declarations in it.
|
||||
|
||||
<h2>Accessing Constants from Java</h2>
|
||||
|
||||
Click <a href="main.java">here</a> to see a Java program that prints out the values
|
||||
of the constants contained in the above file.
|
||||
|
||||
<h2>Key points</h2>
|
||||
|
||||
<ul>
|
||||
<li>The values of preprocessor macros are converted into Java constants.
|
||||
<li>Types are inferred by syntax (e.g., "3" is an integer and "3.5" is a float).
|
||||
<li>Character constants such as 'x' are converted into Java strings.
|
||||
<li>C string literals such as "Hello World" are converted into Java strings.
|
||||
<li>Macros that are not fully defined are simply ignored. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
#define EXTERN extern
|
||||
</pre>
|
||||
</blockquote>
|
||||
is ignored because SWIG has no idea what type of variable this would be.
|
||||
|
||||
<p>
|
||||
<li>Expressions are allowed provided that all of their components are defined. Otherwise, the constant is ignored.
|
||||
|
||||
<li>Certain C declarations involving 'const' are also turned into Java constants.
|
||||
<li>The constants that appear in a SWIG interface file do not have to appear in any sort
|
||||
of matching C source file since the creation of a constant does not require linkage
|
||||
to a stored value (i.e., a value held in a C global variable or memory location).
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
44
Examples/java/constants/main.java
Normal file
44
Examples/java/constants/main.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import java.lang.reflect.*;
|
||||
|
||||
public class main {
|
||||
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[])
|
||||
{
|
||||
System.out.println("ICONST = " + example.ICONST + " (should be 42)");
|
||||
System.out.println("FCONST = " + example.FCONST + " (should be 2.1828)");
|
||||
System.out.println("CCONST = " + example.CCONST + " (should be 'x')");
|
||||
System.out.println("CCONST2 = " + example.CCONST2 + " (this should be on a new line)");
|
||||
System.out.println("SCONST = " + example.SCONST + " (should be 'Hello World')");
|
||||
System.out.println("SCONST2 = " + example.SCONST2 + " (should be '\"Hello World\"')");
|
||||
System.out.println("EXPR = " + example.EXPR + " (should be 48.5484)");
|
||||
System.out.println("iconst = " + example.iconst + " (should be 37)");
|
||||
System.out.println("fconst = " + example.fconst + " (should be 3.14)");
|
||||
|
||||
// Use reflection to check if these variables are defined:
|
||||
try
|
||||
{
|
||||
System.out.println("EXTERN = " + example.class.getField("EXTERN") + " (Arg! This shouldn't print anything)");
|
||||
}
|
||||
catch (NoSuchFieldException e)
|
||||
{
|
||||
System.out.println("EXTERN isn't defined (good)");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
System.out.println("FOO = " + example.class.getField("FOO") + " (Arg! This shouldn't print anything)");
|
||||
}
|
||||
catch (NoSuchFieldException e)
|
||||
{
|
||||
System.out.println("FOO isn't defined (good)");
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Examples/java/enum/.cvsignore
Normal file
11
Examples/java/enum/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/enum/Makefile
Normal file
18
Examples/java/enum/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
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
|
||||
37
Examples/java/enum/example.cxx
Normal file
37
Examples/java/enum/example.cxx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void Foo::enum_test(speed s) {
|
||||
if (s == IMPULSE) {
|
||||
printf("IMPULSE speed\n");
|
||||
} else if (s == WARP) {
|
||||
printf("WARP speed\n");
|
||||
} else if (s == LUDICROUS) {
|
||||
printf("LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("Unknown speed\n");
|
||||
}
|
||||
}
|
||||
|
||||
void enum_test(color c, Foo::speed s) {
|
||||
if (c == RED) {
|
||||
printf("color = RED, ");
|
||||
} else if (c == BLUE) {
|
||||
printf("color = BLUE, ");
|
||||
} else if (c == GREEN) {
|
||||
printf("color = GREEN, ");
|
||||
} else {
|
||||
printf("color = Unknown color!, ");
|
||||
}
|
||||
if (s == Foo::IMPULSE) {
|
||||
printf("speed = IMPULSE speed\n");
|
||||
} else if (s == Foo::WARP) {
|
||||
printf("speed = WARP speed\n");
|
||||
} else if (s == Foo::LUDICROUS) {
|
||||
printf("speed = LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("speed = Unknown speed!\n");
|
||||
}
|
||||
}
|
||||
13
Examples/java/enum/example.h
Normal file
13
Examples/java/enum/example.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* File : example.h */
|
||||
|
||||
enum color { RED, BLUE, GREEN };
|
||||
|
||||
class Foo {
|
||||
public:
|
||||
Foo() { }
|
||||
enum speed { IMPULSE, WARP, LUDICROUS };
|
||||
void enum_test(speed s);
|
||||
};
|
||||
|
||||
void enum_test(color c, Foo::speed s);
|
||||
|
||||
13
Examples/java/enum/example.i
Normal file
13
Examples/java/enum/example.i
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%pragma make_default
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
|
||||
%include "example.h"
|
||||
|
||||
37
Examples/java/enum/index.html
Normal file
37
Examples/java/enum/index.html
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:enum</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/enum/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping enumerations</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example tests SWIG's ability to wrap enumerations. By default, SWIG
|
||||
converts enumeration specifications into integer constants. Further use
|
||||
of enumerated types are handled as integers.
|
||||
|
||||
<ul>
|
||||
<li><a href="example.h">example.h</a>. Header file containing some enums.
|
||||
<li><a href="example.i">example.i</a>. Interface file.
|
||||
<li><a href="main.java">main.java</a>. Sample Java program.
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
||||
<ul>
|
||||
<li>SWIG allows arbitrary integers to be passed as enum values. However,
|
||||
the result of passing an integer not corresponding to any of the values
|
||||
specified in the <tt>enum</tt> specification is undefined.
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
39
Examples/java/enum/main.java
Normal file
39
Examples/java/enum/main.java
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
|
||||
public class main {
|
||||
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[])
|
||||
{
|
||||
// Print out the value of some enums
|
||||
System.out.println("*** color ***");
|
||||
System.out.println(" RED = " + example.RED);
|
||||
System.out.println(" BLUE = " + example.BLUE);
|
||||
System.out.println(" GREEN = " + example.GREEN);
|
||||
|
||||
System.out.println("\n*** Foo::speed ***");
|
||||
System.out.println(" Foo::IMPULSE = " + Foo.IMPULSE);
|
||||
System.out.println(" Foo::WARP = " + Foo.WARP);
|
||||
System.out.println(" Foo::LUDICROUS = " + Foo.LUDICROUS);
|
||||
|
||||
System.out.println("\nTesting use of enums with functions\n");
|
||||
|
||||
example.enum_test(example.RED, Foo.IMPULSE);
|
||||
example.enum_test(example.BLUE, Foo.WARP);
|
||||
example.enum_test(example.GREEN, Foo.LUDICROUS);
|
||||
example.enum_test(1234,5678);
|
||||
|
||||
System.out.println( "\nTesting use of enum with class method" );
|
||||
Foo f = new Foo();
|
||||
|
||||
f.enum_test(Foo.IMPULSE);
|
||||
f.enum_test(Foo.WARP);
|
||||
f.enum_test(Foo.LUDICROUS);
|
||||
}
|
||||
}
|
||||
11
Examples/java/funcptr/.cvsignore
Normal file
11
Examples/java/funcptr/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/funcptr/Makefile
Normal file
18
Examples/java/funcptr/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
|
||||
all:: java
|
||||
|
||||
java::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
|
||||
javac *.java
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile java_clean
|
||||
|
||||
check: all
|
||||
19
Examples/java/funcptr/example.c
Normal file
19
Examples/java/funcptr/example.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
/* File : example.c */
|
||||
|
||||
int do_op(int a, int b, int (*op)(int,int)) {
|
||||
return (*op)(a,b);
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
int sub(int a, int b) {
|
||||
return a-b;
|
||||
}
|
||||
|
||||
int mul(int a, int b) {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
int (*funcvar)(int,int) = add;
|
||||
7
Examples/java/funcptr/example.h
Normal file
7
Examples/java/funcptr/example.h
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* file: example.h */
|
||||
|
||||
extern int do_op(int,int, int (*op)(int,int));
|
||||
extern int add(int,int);
|
||||
extern int sub(int,int);
|
||||
extern int mul(int,int);
|
||||
|
||||
16
Examples/java/funcptr/example.i
Normal file
16
Examples/java/funcptr/example.i
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Wrap a function taking a pointer to a function */
|
||||
extern int do_op(int a, int b, int (*op)(int, int));
|
||||
|
||||
/* Now install a bunch of "ops" as constants */
|
||||
%constant int (*ADD)(int,int) = add;
|
||||
%constant int (*SUB)(int,int) = sub;
|
||||
%constant int (*MUL)(int,int) = mul;
|
||||
|
||||
extern int (*funcvar)(int,int);
|
||||
|
||||
93
Examples/java/funcptr/index.html
Normal file
93
Examples/java/funcptr/index.html
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:funcptr</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/funcptr/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Pointers to Functions</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
Okay, just what in the heck does SWIG do with a declaration like this?
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
int do_op(int a, int b, int (*op)(int, int));
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Well, it creates a wrapper as usual. Of course, that does raise some
|
||||
questions about the third argument (the pointer to a function).
|
||||
|
||||
<p>
|
||||
In this case, SWIG will wrap the function pointer as it does for all other
|
||||
pointers. However, in order to actually call this function from a Java program,
|
||||
you will need to pass some kind of C function pointer object. In C,
|
||||
this is easy, you just supply a function name as an argument like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* Some callback function */
|
||||
int add(int a, int b) {
|
||||
return a+b;
|
||||
}
|
||||
...
|
||||
int r = do_op(x,y,add);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
To make this work with SWIG, you will need to do a little extra work. Specifically,
|
||||
you need to create some function pointer objects using the %constant directive like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%constant(int (*)(int,int)) ADD = add;
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Now, in a Java program, you would do this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
int r = do_op(x,y, example.ADD)
|
||||
</pre>
|
||||
</blockquote>
|
||||
where <tt>example</tt> is the module name.
|
||||
|
||||
<h2>An Example</h2>
|
||||
|
||||
Here are some files that illustrate this with a simple example:
|
||||
|
||||
<ul>
|
||||
<li><a href="example.c">example.c</a>
|
||||
<li><a href="example.h">example.h</a>
|
||||
<li><a href="example.i">example.i</a> (SWIG interface)
|
||||
<li><a href="main.java">main.java</a> (Sample program)
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
||||
<ul>
|
||||
<li>The value of a function pointer must correspond to a function written in C or C++.
|
||||
It is not possible to pass an arbitrary Java function in as a substitute for a C
|
||||
function pointer.
|
||||
|
||||
<p>
|
||||
<li>A Java function can be used as a C/C++ callback if you write some
|
||||
clever typemaps and are very careful about how you create your extension.
|
||||
This is an advanced topic not covered here.
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
|
||||
|
||||
33
Examples/java/funcptr/main.java
Normal file
33
Examples/java/funcptr/main.java
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
public class main {
|
||||
|
||||
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[]) {
|
||||
|
||||
|
||||
int a = 37;
|
||||
int b = 42;
|
||||
|
||||
// Now call our C function with a bunch of callbacks
|
||||
|
||||
System.out.println( "Trying some C callback functions" );
|
||||
System.out.println( " a = " + a );
|
||||
System.out.println( " b = " + b );
|
||||
System.out.println( " ADD(a,b) = " + example.do_op(a,b,example.ADD) );
|
||||
System.out.println( " SUB(a,b) = " + example.do_op(a,b,example.SUB) );
|
||||
System.out.println( " MUL(a,b) = " + example.do_op(a,b,example.MUL) );
|
||||
|
||||
System.out.println( "Here is what the C callback function classes are called in Java" );
|
||||
System.out.println( " ADD = " + example.ADD.getClass().getName() );
|
||||
System.out.println( " SUB = " + example.SUB.getClass().getName() );
|
||||
System.out.println( " MUL = " + example.MUL.getClass().getName() );
|
||||
}
|
||||
}
|
||||
68
Examples/java/index.html
Normal file
68
Examples/java/index.html
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
<H1>SWIG Java Examples</H1>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
The following examples illustrate the use of SWIG with Java.
|
||||
|
||||
<ul>
|
||||
<li><a href="simple/index.html">simple</a>. A minimal example showing how SWIG can
|
||||
be used to wrap a C function, a global variable, and a constant.
|
||||
<li><a href="native/index.html">native</a>. Comparing the manual and the SWIG approach to calling native code.
|
||||
<li><a href="typemap/index.html">typemap</a>. Modifying the Java module's default behaviour by using typemaps.
|
||||
<li><a href="constants/index.html">constants</a>. This shows how preprocessor macros and
|
||||
certain C declarations are turned into constants.
|
||||
<li><a href="variables/index.html">variables</a>. An example showing how to access C global variables.
|
||||
<li><a href="enum/index.html">enum</a>. Wrapping enumerations.
|
||||
<li><a href="class/index.html">class</a>. How to wrap a simple C++ class.
|
||||
<li><a href="reference/index.html">reference</a>. C++ references.
|
||||
<li><a href="pointer/index.html">pointer</a>. Simple pointer handling.
|
||||
<li><a href="template/index.html">template</a>. C++ templates.
|
||||
<li><a href="funcptr/index.html">funcptr</a>. Pointers to functions.
|
||||
</ul>
|
||||
|
||||
<h2>Running the examples</h2>
|
||||
Please see the <a href="../../Doc/Manual/Windows.html">Windows</a> page in the main manual for information on using the examples on Windows. <p>
|
||||
|
||||
On Unix most of the examples work by making the Makefile before executing the program main.java. The Makefile will output the swig generated JNI c code as well as the Java wrapper classes. Additionally the JNI c/c++ code is compiled into the shared object (dynamic link library) which is needed for dynamic linking to the native code. The Makefiles also compile the Java files using javac.
|
||||
<p>
|
||||
Ensure that the dynamic link library file is in the appropriate path before executing the Java program. For example in Unix, libexample.so must be in the LD_LIBRARY_PATH.
|
||||
<p>
|
||||
A Unix example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
$ make
|
||||
$ export LD_LIBRARY_PATH=. #ksh
|
||||
$ java main
|
||||
</pre>
|
||||
</blockquote>
|
||||
<p>
|
||||
|
||||
</ul>
|
||||
|
||||
<h2>Compatibility</h2>
|
||||
|
||||
The examples have been extensively tested on the following platforms:
|
||||
|
||||
<ul>
|
||||
<li>Solaris
|
||||
</ul>
|
||||
|
||||
The examples have been extensively tested using Sun's JDK on:
|
||||
|
||||
<ul>
|
||||
<li>Sparc Solaris 2.6.
|
||||
</ul>
|
||||
|
||||
Your mileage may vary. If you experience a problem, please let us know by
|
||||
sending a message to <a href="mailto:swig-dev@cs.uchicago.edu">swig-dev@cs.uchicago.edu</a>.
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
18
Examples/java/mpointer/Makefile
Normal file
18
Examples/java/mpointer/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
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
|
||||
45
Examples/java/mpointer/example.cxx
Normal file
45
Examples/java/mpointer/example.cxx
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
double do_op(Shape *s, double (Shape::*m)(void)) {
|
||||
return (s->*m)();
|
||||
}
|
||||
|
||||
double (Shape::*areapt())(void) {
|
||||
return &Shape::area;
|
||||
}
|
||||
|
||||
double (Shape::*perimeterpt())(void) {
|
||||
return &Shape::perimeter;
|
||||
}
|
||||
|
||||
/* Member pointer variables */
|
||||
double (Shape::*areavar)(void) = &Shape::area;
|
||||
double (Shape::*perimetervar)(void) = &Shape::perimeter;
|
||||
|
||||
50
Examples/java/mpointer/example.h
Normal file
50
Examples/java/mpointer/example.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
double *z;
|
||||
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
extern double do_op(Shape *s, double (Shape::*m)(void));
|
||||
|
||||
/* Functions that return member pointers */
|
||||
|
||||
extern double (Shape::*areapt())(void);
|
||||
extern double (Shape::*perimeterpt())(void);
|
||||
|
||||
/* Global variables that are member pointers */
|
||||
extern double (Shape::*areavar)(void);
|
||||
extern double (Shape::*perimetervar)(void);
|
||||
|
||||
|
||||
|
||||
16
Examples/java/mpointer/example.i
Normal file
16
Examples/java/mpointer/example.i
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
/* Some constants */
|
||||
|
||||
%constant double (Shape::*AREAPT)(void) = &Shape::area;
|
||||
%constant double (Shape::*PERIMPT)(void) = &Shape::perimeter;
|
||||
%constant double (Shape::*NULLPT)(void) = 0;
|
||||
|
||||
61
Examples/java/mpointer/main.java
Normal file
61
Examples/java/mpointer/main.java
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// Example using pointers to member functions
|
||||
|
||||
public class main {
|
||||
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[])
|
||||
{
|
||||
// Get the pointers
|
||||
|
||||
SWIGTYPE_m_Shape__f_void__double area_pt = example.areapt();
|
||||
SWIGTYPE_m_Shape__f_void__double perim_pt = example.perimeterpt();
|
||||
|
||||
System.out.println( "area_pt =" + area_pt );
|
||||
System.out.println( "perim_pt = " + perim_pt );
|
||||
|
||||
// Create some objects
|
||||
|
||||
Circle c = new Circle(4);
|
||||
Square s = new Square(10);
|
||||
|
||||
// Do some calculations
|
||||
|
||||
System.out.println( "Circle area = " + example.do_op(c,area_pt) );
|
||||
System.out.println( "Circle perim = " + example.do_op(c,perim_pt) );
|
||||
System.out.println( "Square area = " + example.do_op(s,area_pt) );
|
||||
System.out.println( "Square perim = " + example.do_op(s,perim_pt) );
|
||||
|
||||
System.out.println( "areavar = " + example.getAreavar() );
|
||||
System.out.println( "perimetervar = " + example.getPerimetervar() );
|
||||
|
||||
// Try the variables
|
||||
System.out.println( "Circle area = " + example.do_op(c,example.getAreavar()) );
|
||||
System.out.println( "Circle perim = " + example.do_op(c,example.getPerimetervar()) );
|
||||
System.out.println( "Square area = " + example.do_op(s,example.getAreavar()) );
|
||||
System.out.println( "Square perim = " + example.do_op(s,example.getPerimetervar()) );
|
||||
|
||||
// Modify one of the variables
|
||||
example.setAreavar(perim_pt);
|
||||
|
||||
System.out.println( "Circle perimeter = " + example.do_op(c,example.getAreavar()) );
|
||||
|
||||
// Try the constants
|
||||
|
||||
System.out.println( "example.AREAPT =" + example.AREAPT );
|
||||
System.out.println( "example.PERIMPT=" + example.PERIMPT );
|
||||
System.out.println( "example.NULLPT =" + example.NULLPT );
|
||||
|
||||
System.out.println( "Circle area = " + example.do_op(c,example.AREAPT) );
|
||||
System.out.println( "Circle perim = " + example.do_op(c,example.PERIMPT) );
|
||||
System.out.println( "Square area = " + example.do_op(s,example.AREAPT) );
|
||||
System.out.println( "Square perim = " + example.do_op(s,example.PERIMPT) );
|
||||
|
||||
}
|
||||
}
|
||||
11
Examples/java/multimap/.cvsignore
Normal file
11
Examples/java/multimap/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/multimap/Makefile
Normal file
18
Examples/java/multimap/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
|
||||
all:: java
|
||||
|
||||
java::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
|
||||
javac *.java
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile java_clean
|
||||
|
||||
check: all
|
||||
53
Examples/java/multimap/example.c
Normal file
53
Examples/java/multimap/example.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* File : example.c */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
int gcdmain(int argc, char *argv[]) {
|
||||
int x,y;
|
||||
if (argc != 3) {
|
||||
printf("usage: gcd x y\n");
|
||||
return -1;
|
||||
}
|
||||
x = atoi(argv[1]);
|
||||
y = atoi(argv[2]);
|
||||
printf("gcd(%d,%d) = %d\n", x,y,gcd(x,y));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count(char *bytes, int len, char c) {
|
||||
int i;
|
||||
int count = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (bytes[i] == c) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void capitalize(char *str, int len) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
str[i] = toupper(str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void circle(double x, double y) {
|
||||
double a = x*x + y*y;
|
||||
if (a > 1.0) {
|
||||
printf("Bad points %g, %g\n", x,y);
|
||||
} else {
|
||||
printf("Good points %g, %g\n", x,y);
|
||||
}
|
||||
}
|
||||
101
Examples/java/multimap/example.i
Normal file
101
Examples/java/multimap/example.i
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
extern int gcd(int x, int y);
|
||||
|
||||
%typemap(jni) (int argc, char *argv[]) "jobjectArray"
|
||||
%typemap(jtype) (int argc, char *argv[]) "String[]"
|
||||
%typemap(jstype) (int argc, char *argv[]) "String[]"
|
||||
|
||||
%typemap(javain) (int argc, char *argv[]) "$javainput"
|
||||
|
||||
%typemap(in) (int argc, char *argv[]) (jstring *jsarray) {
|
||||
int i;
|
||||
|
||||
$1 = (*jenv)->GetArrayLength(jenv, $input);
|
||||
if ($1 == 0) {
|
||||
SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
|
||||
return $null;
|
||||
}
|
||||
$2 = (char **) malloc(($1+1)*sizeof(char *));
|
||||
jsarray = (jstring *) malloc($1*sizeof(jstring));
|
||||
for (i = 0; i < $1; i++) {
|
||||
jsarray[i] = (jstring) (*jenv)->GetObjectArrayElement(jenv, $input, i);
|
||||
$2[i] = (char *) (*jenv)->GetStringUTFChars(jenv, jsarray[i], 0);
|
||||
}
|
||||
$2[i] = 0;
|
||||
}
|
||||
|
||||
%typemap(argout) (int argc, char *argv[]) "" /* override char *[] default */
|
||||
|
||||
%typemap(freearg) (int argc, char *argv[]) {
|
||||
int i;
|
||||
for (i = 0; i < $1; i++) {
|
||||
(*jenv)->ReleaseStringUTFChars(jenv, jsarray$argnum[i], $2[i]);
|
||||
}
|
||||
free($2);
|
||||
}
|
||||
|
||||
extern int gcdmain(int argc, char *argv[]);
|
||||
|
||||
%typemap(jni) (char *bytes, int len) "jstring"
|
||||
%typemap(jtype) (char *bytes, int len) "String"
|
||||
%typemap(jstype) (char *bytes, int len) "String"
|
||||
|
||||
%typemap(javain) (char *bytes, int len) "$javainput"
|
||||
|
||||
%typemap(in) (char *bytes, int len) {
|
||||
$1 = ($1_type)(*jenv)->GetStringUTFChars(jenv, $input, 0);
|
||||
$2 = (*jenv)->GetStringUTFLength(jenv, $input);
|
||||
}
|
||||
|
||||
%typemap(freearg) (char *bytes, int len) %{
|
||||
(*jenv)->ReleaseStringUTFChars(jenv, $input, $1);
|
||||
%}
|
||||
|
||||
extern int count(char *bytes, int len, char c);
|
||||
|
||||
/* This example shows how to wrap a function that mutates a c string. A one
|
||||
* element Java string array is used so that the string can be returned modified.*/
|
||||
|
||||
%typemap(jni) (char *str, int len) "jobjectArray"
|
||||
%typemap(jtype) (char *str, int len) "String[]"
|
||||
%typemap(jstype) (char *str, int len) "String[]"
|
||||
|
||||
%typemap(javain) (char *str, int len) "$javainput"
|
||||
|
||||
%typemap(in) (char *str, int len) (jstring js) %{
|
||||
int index=0;
|
||||
|
||||
js = (jstring) (*jenv)->GetObjectArrayElement(jenv, $input, index);
|
||||
$1 = (char *) (*jenv)->GetStringUTFChars(jenv, js, 0);
|
||||
$2 = (*jenv)->GetStringUTFLength(jenv, js);
|
||||
%}
|
||||
|
||||
/* Return the mutated string as a modified element in the array. */
|
||||
%typemap(argout) (char *str, int len) {
|
||||
jstring newstring = (*jenv)->NewStringUTF(jenv, $1);
|
||||
(*jenv)->SetObjectArrayElement(jenv, $input, 0, newstring);
|
||||
}
|
||||
|
||||
/* Release memory */
|
||||
%typemap(freearg) (char *str, int len) {
|
||||
(*jenv)->ReleaseStringUTFChars(jenv, js$argnum, $1);
|
||||
}
|
||||
|
||||
extern void capitalize(char *str, int len);
|
||||
|
||||
/* A multi-valued constraint. Force two arguments to lie
|
||||
inside the unit circle */
|
||||
|
||||
%typemap(check) (double cx, double cy) {
|
||||
double a = $1*$1 + $2*$2;
|
||||
if (a > 1.0) {
|
||||
SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, "$1_name and $2_name must be in unit circle");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
extern void circle(double cx, double cy);
|
||||
|
||||
|
||||
40
Examples/java/multimap/main.java
Normal file
40
Examples/java/multimap/main.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
|
||||
public class main {
|
||||
|
||||
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 our gcd() function
|
||||
int x = 42;
|
||||
int y = 105;
|
||||
int g = example.gcd(x,y);
|
||||
System.out.println("The gcd of " + x + " and " + y + " is " + g);
|
||||
|
||||
// Call the gcdmain() function
|
||||
String[] args = {"gcdmain","42","105"};
|
||||
example.gcdmain(args);
|
||||
|
||||
// Call the count function
|
||||
System.out.println(example.count("Hello World", 'l'));
|
||||
|
||||
// Call the capitalize function
|
||||
String[] capitalizeMe = {"hello world"};
|
||||
example.capitalize(capitalizeMe);
|
||||
System.out.println(capitalizeMe[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
11
Examples/java/native/.cvsignore
Normal file
11
Examples/java/native/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -1,17 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SWIGOPT =
|
||||
SRCS =
|
||||
TARGET = libexample
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT = -noproxy
|
||||
|
||||
all:: java
|
||||
|
||||
java::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
|
||||
javac *.java
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* example.java *.class *.o *~ .~* core *.so *.sl so_locations
|
||||
$(MAKE) -f $(TOP)/Makefile java_clean
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
type:
|
||||
make
|
||||
javac *.java
|
||||
export LD_LIBRARY_PATH=. # sh
|
||||
setenv LD_LIBRARY_PATH . # csh
|
||||
java main
|
||||
|
|
@ -18,17 +18,17 @@ Point *point_create(int x, int y) {
|
|||
return p;
|
||||
}
|
||||
|
||||
/* this function will be wrapped by jswig */
|
||||
/* this function will be wrapped by SWIG */
|
||||
char *point_toString1(Point *p) {
|
||||
char buf[80];
|
||||
static char buf[80];
|
||||
|
||||
sprintf(buf, "(%d,%d)", p->x, p->y);
|
||||
|
||||
return strdup(buf); /* memory leak */
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* this one we wrapped manually*/
|
||||
JNIEXPORT jstring JNICALL Java_example_point_1toString2(JNIEnv *jenv, jclass jcls, jlong jpoint) {
|
||||
JNIEXPORT jstring JNICALL Java_exampleJNI_point_1toString2(JNIEnv *jenv, jclass jcls, jlong jpoint) {
|
||||
Point * p;
|
||||
char buf[80];
|
||||
jstring result;
|
||||
|
|
@ -45,10 +45,8 @@ JNIEXPORT jstring JNICALL Java_example_point_1toString2(JNIEnv *jenv, jclass jcl
|
|||
|
||||
Point *point_create(int x, int y);
|
||||
char *point_toString1(Point *p);
|
||||
/*
|
||||
Use %new to free the memory returned by point_toString1
|
||||
|
||||
%new char *point_toString1(Point *p);
|
||||
*/
|
||||
/* give access to free() for memory cleanup of the malloc'd Point */
|
||||
extern void free(void *memblock);
|
||||
|
||||
%native(point_toString2) char *point_toString2(Point *p);
|
||||
|
|
|
|||
33
Examples/java/native/index.html
Normal file
33
Examples/java/native/index.html
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:native</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/native/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>SWIG wrapped and manually wrapped functions in Java</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example compares wrapping a c global function using the manual way and the SWIG way.
|
||||
|
||||
<ul>
|
||||
<li><a href="example.i">example.i</a>. Interface file comparing code wrapped by SWIG and wrapped manually.
|
||||
<li><a href="main.java">main.java</a>. Sample Java program showing calls to both manually wrapped and SWIG wrapped c functions.
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
||||
<ul>
|
||||
<li>SWIG writes all the awkward JNI code for you. You just have to tell SWIG which functions to wrap.
|
||||
<li>If memory is allocated in c it needs to be free'd. A function, such as free(), can be provided with access from Java to free the memory.
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
import example;
|
||||
|
||||
public class main {
|
||||
|
||||
|
|
@ -6,14 +5,15 @@ public class main {
|
|||
try {
|
||||
System.loadLibrary("example");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Cannot load the native code.\nMake sure your LD_LIBRARY_PATH contains \'.\'\n" + 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[]) {
|
||||
long p = example.point_create(1, 2);
|
||||
SWIGTYPE_p_Point p = example.point_create(1, 2);
|
||||
System.out.println("auto wrapped : " + example.point_toString1(p));
|
||||
System.out.println("manual wrapped: " + example.point_toString2(p));
|
||||
example.free(new SWIGTYPE_p_void(SWIGTYPE_p_Point.getCPtr(p), false)); //clean up c allocated memory
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
Examples/java/pointer/.cvsignore
Normal file
11
Examples/java/pointer/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/pointer/Makefile
Normal file
18
Examples/java/pointer/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
|
||||
all:: java
|
||||
|
||||
java::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
|
||||
javac *.java
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile java_clean
|
||||
|
||||
check: all
|
||||
16
Examples/java/pointer/example.c
Normal file
16
Examples/java/pointer/example.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.c */
|
||||
|
||||
void add(int *x, int *y, int *result) {
|
||||
*result = *x + *y;
|
||||
}
|
||||
|
||||
void sub(int *x, int *y, int *result) {
|
||||
*result = *x - *y;
|
||||
}
|
||||
|
||||
int divide(int n, int d, int *r) {
|
||||
int q;
|
||||
q = n/d;
|
||||
*r = n - q*d;
|
||||
return q;
|
||||
}
|
||||
24
Examples/java/pointer/example.i
Normal file
24
Examples/java/pointer/example.i
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
%include typemaps.i
|
||||
extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
|
||||
/* Next we'll use typemaps and the %apply directive */
|
||||
|
||||
%apply int *OUTPUT { int *r };
|
||||
extern int divide(int n, int d, int *r);
|
||||
|
||||
|
||||
|
||||
|
||||
167
Examples/java/pointer/index.html
Normal file
167
Examples/java/pointer/index.html
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:pointer</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
<tt>SWIG/Examples/java/pointer/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Simple Pointer Handling</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example illustrates a couple of techniques for handling
|
||||
simple pointers in SWIG. The prototypical example is a C function
|
||||
that operates on pointers such as this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
void add(int *x, int *y, int *r) {
|
||||
*r = *x + *y;
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
By default, SWIG wraps this function exactly as specified and creates
|
||||
an interface that expects pointer objects for arguments.
|
||||
SWIG wraps a C pointer with a type wrapper class, for example, SWIGTYPE_p_int for an int*.
|
||||
The only problem is how does one go about creating these objects from a Java program?
|
||||
<p>
|
||||
|
||||
|
||||
<h2>Possible Solutions</h2>
|
||||
|
||||
<ul>
|
||||
<li>Write some helper functions to explicitly create objects. For
|
||||
example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
int *new_int(int ivalue) {
|
||||
int *i = (int *) malloc(sizeof(ivalue));
|
||||
*i = ivalue;
|
||||
return i;
|
||||
}
|
||||
int get_int(int *i) {
|
||||
return *i;
|
||||
}
|
||||
|
||||
void delete_int(int *i) {
|
||||
free(i);
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>The SWIG pointer library provides an easier way. <br>
|
||||
For example, in the interface file
|
||||
you would do this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
and from Java you would use pointers like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
SWIGTYPE_p_int a = example.new_intp();
|
||||
SWIGTYPE_p_int b = example.new_intp();
|
||||
SWIGTYPE_p_int c = example.new_intp();
|
||||
example.intp_assign(a,37);
|
||||
example.intp_assign(b,42);
|
||||
|
||||
// Note that getCPtr() has package access by default
|
||||
System.out.println(" a =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(a)));
|
||||
System.out.println(" b =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(b)));
|
||||
System.out.println(" c =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(c)));
|
||||
|
||||
// Call the add() function with some pointers
|
||||
example.add(a,b,c);
|
||||
|
||||
// Now get the result
|
||||
int res = example.intp_value(c);
|
||||
System.out.println(" 37 + 42 =" + res);
|
||||
|
||||
// Clean up the pointers
|
||||
example.delete_intp(a);
|
||||
example.delete_intp(b);
|
||||
example.delete_intp(c);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<p>
|
||||
<li>Use the SWIG typemap library. This library allows you to completely
|
||||
change the way arguments are processed by SWIG. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%include "typemaps.i"
|
||||
void add(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
And in a Java program:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
int[] r = {0};
|
||||
example.sub(37,42,r);
|
||||
System.out.println("Result =" + r[0]);
|
||||
</pre>
|
||||
</blockquote>
|
||||
Needless to say, this is substantially easier although a bit unusual.
|
||||
|
||||
<p>
|
||||
<li>A final alternative is to use the typemaps library in combination
|
||||
with the %apply directive. This allows you to change the names of parameters
|
||||
that behave as input or output parameters. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%include "typemaps.i"
|
||||
%apply int *INPUT {int *x, int *y};
|
||||
%apply int *OUTPUT {int *r};
|
||||
|
||||
void add(int *x, int *y, int *r);
|
||||
void sub(int *x, int *y, int *r);
|
||||
void mul(int *x, int *y, int *r);
|
||||
... etc ...
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
</ul>
|
||||
|
||||
<h2>Example</h2>
|
||||
|
||||
The following example illustrates the use of these features for pointer
|
||||
extraction.
|
||||
|
||||
<ul>
|
||||
<li> <a href="example.c">example.c</a> (C Source)
|
||||
<li> <a href="example.i">example.i</a> (Swig interface)
|
||||
<li> <a href="main.java">main.java</a> (Java program)
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
||||
<ul>
|
||||
<li>Since pointers are used for so many different things (arrays, output values,
|
||||
etc...) the complexity of pointer handling can be as complicated as you want to
|
||||
make it.
|
||||
|
||||
<p>
|
||||
<li>More documentation on the typemaps.i and cpointer.i library files can be
|
||||
found in the SWIG user manual. The files also contain documentation.
|
||||
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
55
Examples/java/pointer/main.java
Normal file
55
Examples/java/pointer/main.java
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
|
||||
public class main {
|
||||
|
||||
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[]) {
|
||||
|
||||
// First create some objects using the pointer library.
|
||||
System.out.println("Testing the pointer library");
|
||||
SWIGTYPE_p_int a = example.new_intp();
|
||||
SWIGTYPE_p_int b = example.new_intp();
|
||||
SWIGTYPE_p_int c = example.new_intp();
|
||||
example.intp_assign(a,37);
|
||||
example.intp_assign(b,42);
|
||||
|
||||
// Note that getCPtr() has package access by default
|
||||
System.out.println(" a =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(a)));
|
||||
System.out.println(" b =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(b)));
|
||||
System.out.println(" c =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(c)));
|
||||
|
||||
// Call the add() function with some pointers
|
||||
example.add(a,b,c);
|
||||
|
||||
// Now get the result
|
||||
int res = example.intp_value(c);
|
||||
System.out.println(" 37 + 42 =" + res);
|
||||
|
||||
// Clean up the pointers
|
||||
example.delete_intp(a);
|
||||
example.delete_intp(b);
|
||||
example.delete_intp(c);
|
||||
|
||||
// Now try the typemap library
|
||||
// Now it is no longer necessary to manufacture pointers.
|
||||
// Instead we use a single element array which in Java is modifiable.
|
||||
|
||||
System.out.println("Trying the typemap library");
|
||||
int[] r = {0};
|
||||
example.sub(37,42,r);
|
||||
System.out.println(" 37 - 42 = " + r[0]);
|
||||
|
||||
// Now try the version with return value
|
||||
|
||||
System.out.println("Testing return value");
|
||||
int q = example.divide(42,37,r);
|
||||
System.out.println(" 42/37 = " + q + " remainder " + r[0]);
|
||||
}
|
||||
}
|
||||
11
Examples/java/reference/.cvsignore
Normal file
11
Examples/java/reference/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/reference/Makefile
Normal file
18
Examples/java/reference/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
CXXSRCS = example.cxx
|
||||
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
|
||||
41
Examples/java/reference/example.cxx
Normal file
41
Examples/java/reference/example.cxx
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Vector operator+(const Vector &a, const Vector &b) {
|
||||
Vector r;
|
||||
r.x = a.x + b.x;
|
||||
r.y = a.y + b.y;
|
||||
r.z = a.z + b.z;
|
||||
return r;
|
||||
}
|
||||
|
||||
char *Vector::print() {
|
||||
static char temp[512];
|
||||
sprintf(temp,"Vector %x (%g,%g,%g)", this, x,y,z);
|
||||
return temp;
|
||||
}
|
||||
|
||||
VectorArray::VectorArray(int size) {
|
||||
items = new Vector[size];
|
||||
maxsize = size;
|
||||
}
|
||||
|
||||
VectorArray::~VectorArray() {
|
||||
delete [] items;
|
||||
}
|
||||
|
||||
Vector &VectorArray::operator[](int index) {
|
||||
if ((index < 0) || (index >= maxsize)) {
|
||||
printf("Panic! Array index out of bounds.\n");
|
||||
exit(1);
|
||||
}
|
||||
return items[index];
|
||||
}
|
||||
|
||||
int VectorArray::size() {
|
||||
return maxsize;
|
||||
}
|
||||
|
||||
26
Examples/java/reference/example.h
Normal file
26
Examples/java/reference/example.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Vector {
|
||||
private:
|
||||
double x,y,z;
|
||||
public:
|
||||
Vector() : x(0), y(0), z(0) { };
|
||||
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
|
||||
friend Vector operator+(const Vector &a, const Vector &b);
|
||||
char *print();
|
||||
};
|
||||
|
||||
class VectorArray {
|
||||
private:
|
||||
Vector *items;
|
||||
int maxsize;
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
Vector &operator[](int);
|
||||
int size();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
46
Examples/java/reference/example.i
Normal file
46
Examples/java/reference/example.i
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* File : example.i */
|
||||
|
||||
/* This file has a few "typical" uses of C++ references. */
|
||||
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
class Vector {
|
||||
public:
|
||||
Vector(double x, double y, double z);
|
||||
~Vector();
|
||||
char *print();
|
||||
};
|
||||
|
||||
/* This helper function calls an overloaded operator */
|
||||
%inline %{
|
||||
Vector addv(Vector &a, Vector &b) {
|
||||
return a+b;
|
||||
}
|
||||
%}
|
||||
|
||||
/* Wrapper around an array of vectors class */
|
||||
|
||||
class VectorArray {
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
int size();
|
||||
|
||||
/* This wrapper provides an alternative to the [] operator */
|
||||
%extend {
|
||||
Vector &get(int index) {
|
||||
return (*self)[index];
|
||||
}
|
||||
void set(int index, Vector &a) {
|
||||
(*self)[index] = a;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
149
Examples/java/reference/index.html
Normal file
149
Examples/java/reference/index.html
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:reference</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/reference/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>C++ Reference Handling</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example tests SWIG's handling of C++ references. Since C++
|
||||
references are closely related to pointers (as both refer to a
|
||||
location in memory), SWIG simply collapses all references into
|
||||
pointers when creating wrappers.
|
||||
|
||||
<h2>Some examples</h2>
|
||||
|
||||
References are most commonly used as function parameter. For example,
|
||||
you might have an operator like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
Vector operator+(const Vector &a, const Vector &b) {
|
||||
Vector result;
|
||||
result.x = a.x + b.x;
|
||||
result.y = a.y + b.y;
|
||||
result.z = a.z + b.z;
|
||||
return result;
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
or a function:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
Vector addv(const Vector &a, const Vector &b) {
|
||||
Vector result;
|
||||
result.x = a.x + b.x;
|
||||
result.y = a.y + b.y;
|
||||
result.z = a.z + b.z;
|
||||
return result;
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
In these cases, SWIG transforms everything into a pointer and creates a wrapper
|
||||
that looks like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
Vector wrap_addv(Vector *a, Vector *b) {
|
||||
return addv(*a,*b);
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Occasionally, a reference is used as a return value of a function
|
||||
when the return result is to be used as an lvalue in an expression.
|
||||
The prototypical example is an operator like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
Vector &operator[](int index);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
or a method:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
Vector &get(int index);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
For functions returning references, a wrapper like this is created:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
Vector *wrap_Object_get(Object *self, int index) {
|
||||
Vector &result = self->get(index);
|
||||
return &result;
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
The following <a href="example.h">header file</a> contains some class
|
||||
definitions with some operators and use of references.
|
||||
|
||||
<h2>SWIG Interface</h2>
|
||||
|
||||
SWIG does NOT support overloaded operators so it can not directly build
|
||||
an interface to the classes in the above file. However, a number of workarounds
|
||||
can be made. For example, an overloaded operator can be stuck behind a function
|
||||
call such as the <tt>addv()</tt> function above. Array access can be handled
|
||||
with a pair of set/get functions like this:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
class VectorArray {
|
||||
public:
|
||||
...
|
||||
%addmethods {
|
||||
Vector &get(int index) {
|
||||
return (*self)[index];
|
||||
}
|
||||
void set(int index, Vector &a) {
|
||||
(*self)[index] = a;
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Click <a href="example.i">here</a> to see a SWIG interface file with these additions.
|
||||
|
||||
<h2>Sample Java program</h2>
|
||||
|
||||
Click <a href="main.java">here</a> to see a Java program that manipulates some C++ references.
|
||||
|
||||
<h2>Notes:</h2>
|
||||
|
||||
<ul>
|
||||
<li>C++ references primarily provide notational convenience for C++
|
||||
source code. However, Java only supports the 'x.a'
|
||||
notation so it doesn't much matter.
|
||||
|
||||
<p>
|
||||
<li>When a program returns a reference, a pointer is returned.
|
||||
Unlike return by value, memory is not allocated to hold the
|
||||
return result.
|
||||
|
||||
<p>
|
||||
<li>SWIG has particular trouble handling various combinations of references
|
||||
and pointers. This is side effect of an old parsing scheme and
|
||||
type representation that will be replaced in future versions.
|
||||
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
79
Examples/java/reference/main.java
Normal file
79
Examples/java/reference/main.java
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
// This example illustrates the manipulation of C++ references in Java.
|
||||
|
||||
public class main {
|
||||
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[])
|
||||
{
|
||||
System.out.println( "Creating some objects:" );
|
||||
Vector a = new Vector(3,4,5);
|
||||
Vector b = new Vector(10,11,12);
|
||||
|
||||
System.out.println( " Created " + a.print() );
|
||||
System.out.println( " Created " + b.print() );
|
||||
|
||||
// ----- Call an overloaded operator -----
|
||||
|
||||
// This calls the wrapper we placed around
|
||||
//
|
||||
// operator+(const Vector &a, const Vector &)
|
||||
//
|
||||
// It returns a new allocated object.
|
||||
|
||||
System.out.println( "Adding a+b" );
|
||||
Vector c = example.addv(a,b);
|
||||
System.out.println( " a+b = " + c.print() );
|
||||
|
||||
// Note: Unless we free the result, a memory leak will occur if the -noproxy commandline
|
||||
// is used as the proxy classes define finalizers which call the delete() method. When
|
||||
// -noproxy is not specified the memory management is controlled by the garbage collector.
|
||||
// You can still call delete(). It will free the c++ memory immediately, but not the
|
||||
// Java memory! You then must be careful not to call any member functions as it will
|
||||
// use a NULL c pointer on the underlying c++ object. We set the Java object to null
|
||||
// which will then throw a Java exception should we attempt to use it again.
|
||||
c.delete();
|
||||
c = null;
|
||||
|
||||
// ----- Create a vector array -----
|
||||
|
||||
System.out.println( "Creating an array of vectors" );
|
||||
VectorArray va = new VectorArray(10);
|
||||
System.out.println( " va = " + va.toString() );
|
||||
|
||||
// ----- Set some values in the array -----
|
||||
|
||||
// These operators copy the value of Vector a and Vector b to the vector array
|
||||
va.set(0,a);
|
||||
va.set(1,b);
|
||||
|
||||
// This works, but it would cause a memory leak if -noproxy was used!
|
||||
|
||||
va.set(2,example.addv(a,b));
|
||||
|
||||
|
||||
// Get some values from the array
|
||||
|
||||
System.out.println( "Getting some array values" );
|
||||
for (int i=0; i<5; i++)
|
||||
System.out.println( " va(" + i + ") = " + va.get(i).print() );
|
||||
|
||||
// Watch under resource meter to check on this
|
||||
System.out.println( "Making sure we don't leak memory." );
|
||||
for (int i=0; i<1000000; i++)
|
||||
c = va.get(i%10);
|
||||
|
||||
// ----- Clean up -----
|
||||
// This could be omitted. The garbage collector would then clean up for us.
|
||||
System.out.println( "Cleaning up" );
|
||||
va.delete();
|
||||
a.delete();
|
||||
b.delete();
|
||||
}
|
||||
}
|
||||
11
Examples/java/simple/.cvsignore
Normal file
11
Examples/java/simple/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -1,17 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = libexample
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
SWIGOPT =
|
||||
|
||||
all:: java
|
||||
|
||||
java::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
|
||||
javac *.java
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o core *~ *.so *.class example.java
|
||||
$(MAKE) -f $(TOP)/Makefile java_clean
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
Simple example from users manual.
|
||||
|
||||
type:
|
||||
make
|
||||
javac *.java
|
||||
export LD_LIBRARY_PATH=. # sh
|
||||
setenv LD_LIBRARY_PATH . # csh
|
||||
java main
|
||||
|
|
@ -1,24 +1,18 @@
|
|||
/* Simple example from documentation */
|
||||
/* File : example.c */
|
||||
|
||||
#include <time.h>
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
double My_variable = 3.0;
|
||||
|
||||
/* Compute factorial of n */
|
||||
int fact(int n) {
|
||||
if (n <= 1) return 1;
|
||||
else return n*fact(n-1);
|
||||
}
|
||||
|
||||
/* Compute n mod m */
|
||||
int my_mod(int n, int m) {
|
||||
return (n % m);
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
char *get_time() {
|
||||
long ltime;
|
||||
time(<ime);
|
||||
return ctime(<ime);
|
||||
}
|
||||
|
|
|
|||
158
Examples/java/simple/example.dsp
Normal file
158
Examples/java/simple/example.dsp
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
# Microsoft Developer Studio Project File - Name="example" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=example - Win32 Release
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "example.mak" CFG="example - Win32 Release"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "example - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "example - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "_DEBUG"
|
||||
# ADD RSC /l 0x809 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /out:"example.dll" /pdbtype:sept
|
||||
# Begin Special Build Tool
|
||||
SOURCE="$(InputPath)"
|
||||
PostBuild_Desc=Java compile post-build step
|
||||
PostBuild_Cmds=echo on %JAVA_BIN%\javac *.java
|
||||
# End Special Build Tool
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD CPP /nologo /MT /W3 /GX /O2 /I "$(JAVA_INCLUDE)" /I "$(JAVA_INCLUDE)\win32" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "EXAMPLE_EXPORTS" /YX /FD /c
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "NDEBUG"
|
||||
# ADD RSC /l 0x809 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"example.dll"
|
||||
# Begin Special Build Tool
|
||||
SOURCE="$(InputPath)"
|
||||
PostBuild_Desc=Java compile post-build step
|
||||
PostBuild_Cmds=echo on %JAVA_BIN%\javac *.java
|
||||
# End Special Build Tool
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "example - Win32 Debug"
|
||||
# Name "example - Win32 Release"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.c
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example_wrap.c
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\example.i
|
||||
|
||||
!IF "$(CFG)" == "example - Win32 Debug"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.c" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo JAVA_INCLUDE: %JAVA_INCLUDE%
|
||||
echo JAVA_BIN: %JAVA_BIN%
|
||||
echo on
|
||||
..\..\..\swig.exe -java $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ELSEIF "$(CFG)" == "example - Win32 Release"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\example.i
|
||||
InputName=example
|
||||
|
||||
"$(InputName)_wrap.c" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
echo In order to function correctly, please ensure the following environment variables are correctly set:
|
||||
echo JAVA_INCLUDE: %JAVA_INCLUDE%
|
||||
echo JAVA_BIN: %JAVA_BIN%
|
||||
echo on
|
||||
..\..\..\swig.exe -java $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# End Target
|
||||
# End Project
|
||||
|
|
@ -1,11 +1,5 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
/* Put headers and other declarations here */
|
||||
%}
|
||||
|
||||
|
||||
extern double My_variable;
|
||||
extern int fact(int);
|
||||
%name(mod) extern int my_mod(int n, int m);
|
||||
extern char *get_time();
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
|
|
|
|||
110
Examples/java/simple/index.html
Normal file
110
Examples/java/simple/index.html
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:simple</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/simple/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Simple Java Example</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example illustrates how you can hook Java to a very simple C program containing
|
||||
a function and a global variable.
|
||||
|
||||
<h2>The C Code</h2>
|
||||
|
||||
Suppose you have the following C code:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* File : example.c */
|
||||
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h2>The SWIG interface</h2>
|
||||
|
||||
Here is a simple SWIG interface file:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* File: example.i */
|
||||
%module example
|
||||
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h2>Compilation</h2>
|
||||
|
||||
<ol>
|
||||
<li><tt>swig -java <a href="example.i">example.i</a></tt>
|
||||
<p>
|
||||
<li>Compile <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.c">example.c</a></tt>
|
||||
to create the extension <tt>libexample.so (unix)</tt>.
|
||||
</ol>
|
||||
|
||||
<h2>Using the extension</h2>
|
||||
|
||||
Click <a href="main.java">here</a> to see a program that calls our C functions from Java.
|
||||
<p>
|
||||
Compile the java files <tt><a href="example.java">example.java</a></tt> and <tt><a href="main.java">main.java</a></tt>
|
||||
to create the class files example.class and main.class before running main in the JVM. Ensure that the libexample.so file is in your LD_LIBRARY_PATH before running. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
export LD_LIBRARY_PATH=. #ksh
|
||||
javac *.java
|
||||
java main
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h2>Key points</h2>
|
||||
|
||||
<ul>
|
||||
<li>Use the <tt>loadLibrary</tt> statement from java to load and access the generated java classes. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
System.loadLibrary("example");
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<li>C functions work just like Java functions. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
int g = example.gcd(42,105);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<li>C global variables are accessed through get and set functions in the module class. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
double a = example.get_Foo();
|
||||
example.set_Foo(20.0);
|
||||
</pre>
|
||||
</blockquote>
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
import example;
|
||||
|
||||
public class main {
|
||||
|
||||
|
|
@ -6,27 +5,28 @@ public class main {
|
|||
try {
|
||||
System.loadLibrary("example");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Cannot load the example native code.\nMake sure your LD_LIBRARY_PATH contains \'.\'\n" + 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[]) {
|
||||
System.out.println(example.get_time());
|
||||
|
||||
System.out.println("My Variable = " + example.get_My_variable());
|
||||
|
||||
for(int i=0; i<14; i++) {
|
||||
System.out.println("" + i + " factorial is " + example.fact(i));
|
||||
}
|
||||
|
||||
for(int i=1; i<100; i++) {
|
||||
for(int j=1; j<100; j++) {
|
||||
int n = example.mod(i, j);
|
||||
example.set_My_variable(example.get_My_variable() + n);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("My_variable = " + example.get_My_variable());
|
||||
// Call our gcd() function
|
||||
|
||||
int x = 42;
|
||||
int y = 105;
|
||||
int g = example.gcd(x,y);
|
||||
System.out.println("The gcd of " + x + " and " + y + " is " + g);
|
||||
|
||||
// Manipulate the Foo global variable
|
||||
|
||||
// Output its current value
|
||||
System.out.println("Foo = " + example.getFoo());
|
||||
|
||||
// Change its value
|
||||
example.setFoo(3.1415926);
|
||||
|
||||
// See if the change took effect
|
||||
System.out.println("Foo = " + example.getFoo());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
Examples/java/template/.cvsignore
Normal file
11
Examples/java/template/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/template/Makefile
Normal file
18
Examples/java/template/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../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
|
||||
32
Examples/java/template/example.h
Normal file
32
Examples/java/template/example.h
Normal file
|
|
@ -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
|
||||
};
|
||||
|
||||
17
Examples/java/template/example.i
Normal file
17
Examples/java/template/example.i
Normal file
|
|
@ -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>;
|
||||
|
||||
104
Examples/java/template/index.html
Normal file
104
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>
|
||||
45
Examples/java/template/main.java
Normal file
45
Examples/java/template/main.java
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
// This example illustrates how C++ templates can be used from Java.
|
||||
|
||||
public class main {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Examples/java/typemap/.cvsignore
Normal file
11
Examples/java/typemap/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
|
|
@ -1,17 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS =
|
||||
TARGET = libexample
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
SWIGOPT = -noproxy
|
||||
|
||||
all:: java
|
||||
|
||||
java::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
|
||||
javac *.java
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o core *~ *.so *.class example.java
|
||||
$(MAKE) -f $(TOP)/Makefile java_clean
|
||||
|
||||
check: all
|
||||
|
|
|
|||
|
|
@ -1,8 +0,0 @@
|
|||
Example of a typemap to handle return values in char * arguments.
|
||||
|
||||
type:
|
||||
make
|
||||
javac *.java
|
||||
export LD_LIBRARY_PATH=. # sh
|
||||
setenv LD_LIBRARY_PATH . # csh
|
||||
java main
|
||||
34
Examples/java/typemap/index.html
Normal file
34
Examples/java/typemap/index.html
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:typemap</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/java/typemap/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Typemaps in Java</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example shows how typemaps can be used to modify the default behaviour of the Java SWIG module.
|
||||
|
||||
<ul>
|
||||
<li><a href="example.i">example.i</a>. Interface file.
|
||||
<li><a href="main.java">main.java</a>. Sample Java program.
|
||||
</ul>
|
||||
|
||||
<h2>Notes</h2>
|
||||
|
||||
<ul>
|
||||
<li>Shows how to pass strings to Java from c and visa versa.
|
||||
<li>Typemaps can modify the default behaviour of the Java SWIG module.
|
||||
<li>The default c to java mapping can be modified using typemaps.
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
import example;
|
||||
|
||||
public class main {
|
||||
|
||||
|
|
@ -6,7 +5,7 @@ public class main {
|
|||
try {
|
||||
System.loadLibrary("example");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Cannot load the example native code.\nMake sure your LD_LIBRARY_PATH contains \'.\'\n" + 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
Examples/java/variables/.cvsignore
Normal file
11
Examples/java/variables/.cvsignore
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
*.class
|
||||
*.java
|
||||
*_wrap.c
|
||||
*_wrap.cxx
|
||||
example.dll
|
||||
example.dsw
|
||||
example.ncb
|
||||
example.opt
|
||||
example.plg
|
||||
Release
|
||||
Debug
|
||||
18
Examples/java/variables/Makefile
Normal file
18
Examples/java/variables/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
|
||||
all:: java
|
||||
|
||||
java::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java
|
||||
javac *.java
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile java_clean
|
||||
|
||||
check: all
|
||||
86
Examples/java/variables/example.c
Normal file
86
Examples/java/variables/example.c
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/* File : example.c */
|
||||
|
||||
/* I'm a file containing some C global variables */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "example.h"
|
||||
|
||||
int ivar = 0;
|
||||
short svar = 0;
|
||||
long lvar = 0;
|
||||
unsigned int uivar = 0;
|
||||
unsigned short usvar = 0;
|
||||
unsigned long ulvar = 0;
|
||||
signed char scvar = 0;
|
||||
unsigned char ucvar = 0;
|
||||
char cvar = 0;
|
||||
float fvar = 0;
|
||||
double dvar = 0;
|
||||
char *strvar = 0;
|
||||
const char *cstrvar = 0;
|
||||
int *iptrvar = 0;
|
||||
char name[256] = "Dave";
|
||||
char path[256] = "/home/beazley";
|
||||
|
||||
|
||||
/* Global variables involving a structure */
|
||||
Point *ptptr = 0;
|
||||
Point pt = { 10, 20 };
|
||||
|
||||
/* A variable that we will make read-only in the interface */
|
||||
int status = 1;
|
||||
|
||||
/* A debugging function to print out their values */
|
||||
|
||||
void print_vars() {
|
||||
printf("ivar = %d\n", ivar);
|
||||
printf("svar = %d\n", svar);
|
||||
printf("lvar = %ld\n", lvar);
|
||||
printf("uivar = %u\n", uivar);
|
||||
printf("usvar = %u\n", usvar);
|
||||
printf("ulvar = %lu\n", ulvar);
|
||||
printf("scvar = %d\n", scvar);
|
||||
printf("ucvar = %u\n", ucvar);
|
||||
printf("fvar = %g\n", fvar);
|
||||
printf("dvar = %g\n", dvar);
|
||||
printf("cvar = %c\n", cvar);
|
||||
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
||||
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
||||
printf("iptrvar = %x\n", iptrvar);
|
||||
printf("name = %s\n", name);
|
||||
printf("ptptr = %x (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
|
||||
printf("pt = (%d, %d)\n", pt.x, pt.y);
|
||||
printf("status = %d\n", status);
|
||||
}
|
||||
|
||||
/* A function to create an integer (to test iptrvar) */
|
||||
|
||||
int *new_int(int value) {
|
||||
int *ip = (int *) malloc(sizeof(int));
|
||||
*ip = value;
|
||||
return ip;
|
||||
}
|
||||
|
||||
/* A function to create a point */
|
||||
|
||||
Point *new_Point(int x, int y) {
|
||||
Point *p = (Point *) malloc(sizeof(Point));
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
return p;
|
||||
}
|
||||
|
||||
char * Point_print(Point *p) {
|
||||
static char buffer[256];
|
||||
if (p) {
|
||||
sprintf(buffer,"(%d,%d)", p->x,p->y);
|
||||
} else {
|
||||
sprintf(buffer,"null");
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void pt_print() {
|
||||
printf("(%d, %d)\n", pt.x, pt.y);
|
||||
}
|
||||
6
Examples/java/variables/example.h
Normal file
6
Examples/java/variables/example.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/* File: example.h */
|
||||
|
||||
typedef struct {
|
||||
int x,y;
|
||||
} Point;
|
||||
|
||||
44
Examples/java/variables/example.i
Normal file
44
Examples/java/variables/example.i
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Some global variable declarations */
|
||||
extern int ivar;
|
||||
extern short svar;
|
||||
extern long lvar;
|
||||
extern unsigned int uivar;
|
||||
extern unsigned short usvar;
|
||||
extern unsigned long ulvar;
|
||||
extern signed char scvar;
|
||||
extern unsigned char ucvar;
|
||||
extern char cvar;
|
||||
extern float fvar;
|
||||
extern double dvar;
|
||||
extern char *strvar;
|
||||
extern const char *cstrvar;
|
||||
extern int *iptrvar;
|
||||
extern char name[256];
|
||||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
|
||||
|
||||
/* Some read-only variables */
|
||||
|
||||
%immutable;
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
%mutable;
|
||||
|
||||
/* Some helper functions to make it easier to test */
|
||||
extern void print_vars();
|
||||
extern int *new_int(int value);
|
||||
extern Point *new_Point(int x, int y);
|
||||
extern char *Point_print(Point *p);
|
||||
extern void pt_print();
|
||||
|
||||
|
||||
|
||||
|
||||
85
Examples/java/variables/index.html
Normal file
85
Examples/java/variables/index.html
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:java:variables</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
<tt>SWIG/Examples/java/variables/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping C Global Variables</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
When a C global variable appears in an interface file, SWIG tries to
|
||||
wrap it using a technique known as "variable linking." The idea is
|
||||
pretty simple---we try to create a Java variable that magically
|
||||
retrieves or updates the value of the underlying C variable when it is
|
||||
accessed. Click <a href="example.i">here</a> to see a SWIG interface with some variable
|
||||
declarations in it.
|
||||
|
||||
<h2>Manipulating Variables from Java</h2>
|
||||
|
||||
C variables are accessed through getters and setters from Java. Unfortunately this is the only way to get current values from variables because it is not possible to overload the dot operator in Java. All global variables are accessible from the module class. For example if the module class is called 'example', the global variable
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
double foo;
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
will be accessed in the Java module as
|
||||
<blockquote>
|
||||
<pre>
|
||||
example.get_foo();
|
||||
example.set_foo(12.3);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
Click <a href="main.java">here</a> to see the example program that updates and prints
|
||||
out the values of the variables using this technique.
|
||||
|
||||
<h2>Key points</h2>
|
||||
|
||||
<ul>
|
||||
<li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
|
||||
string. However, whenever the value of such a variable is set from Java, the old
|
||||
value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
|
||||
on whether or not SWIG was run with the -c++ option).
|
||||
<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
|
||||
<li>String array variables such as '<tt>char name[256]</tt>' are managed as Java strings, but
|
||||
when setting the value, the result is truncated to the maximum length of the array. Furthermore, the string is assumed to be null-terminated.
|
||||
<li>When structures and classes are used as global variables, they are mapped into pointers.
|
||||
Getting the "value" returns a pointer to the global variable. Setting the value of a structure results in a memory copy from a pointer to the global.
|
||||
</ul>
|
||||
|
||||
<h2>Creating read-only variables</h2>
|
||||
|
||||
The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
|
||||
specify a collection of read-only variables. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%immutable;
|
||||
int status;
|
||||
double blah;
|
||||
...
|
||||
%mutable;
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
|
||||
using the <tt>%mutable</tt> directive.
|
||||
|
||||
<h2>Comments</h2>
|
||||
<ul>
|
||||
<li>Management of global variables is one of the most problematic aspects
|
||||
of C/C++ wrapping because the Java interface and resulting memory management
|
||||
is much trickier than simply creating a wrapper function.
|
||||
</ul>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
<hr>
|
||||
98
Examples/java/variables/main.java
Normal file
98
Examples/java/variables/main.java
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
// This example illustrates global variable access from Java.
|
||||
|
||||
import java.lang.reflect.*;
|
||||
|
||||
public class main {
|
||||
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[]) {
|
||||
|
||||
// Try to set the values of some global variables
|
||||
|
||||
example.setIvar(42);
|
||||
example.setSvar((short)-31000);
|
||||
example.setLvar(65537);
|
||||
example.setUivar(123456);
|
||||
example.setUsvar(61000);
|
||||
example.setUlvar(654321);
|
||||
example.setScvar((byte)-13);
|
||||
example.setUcvar((short)251);
|
||||
example.setCvar('S');
|
||||
example.setFvar((float)3.14159);
|
||||
example.setDvar(2.1828);
|
||||
example.setStrvar("Hello World");
|
||||
example.setCstrvar("Goodbye");
|
||||
example.setIptrvar(example.new_int(37));
|
||||
example.setPtptr(example.new_Point(37,42));
|
||||
example.setName("Bill");
|
||||
|
||||
// Now print out the values of the variables
|
||||
|
||||
System.out.println( "Variables (values printed from Java)" );
|
||||
|
||||
System.out.println( "ivar =" + example.getIvar() );
|
||||
System.out.println( "svar =" + example.getSvar() );
|
||||
System.out.println( "lvar =" + example.getLvar() );
|
||||
System.out.println( "uivar =" + example.getUivar() );
|
||||
System.out.println( "usvar =" + example.getUsvar() );
|
||||
System.out.println( "ulvar =" + example.getUlvar() );
|
||||
System.out.println( "scvar =" + example.getScvar() );
|
||||
System.out.println( "ucvar =" + example.getUcvar() );
|
||||
System.out.println( "fvar =" + example.getFvar() );
|
||||
System.out.println( "dvar =" + example.getDvar() );
|
||||
System.out.println( "cvar =" + (char)example.getCvar() );
|
||||
System.out.println( "strvar =" + example.getStrvar() );
|
||||
System.out.println( "cstrvar =" + example.getCstrvar() );
|
||||
System.out.println( "iptrvar =" + Long.toHexString(SWIGTYPE_p_int.getCPtr(example.getIptrvar())) );
|
||||
System.out.println( "name =" + example.getName() );
|
||||
System.out.println( "ptptr =" + Long.toHexString(SWIGTYPE_p_Point.getCPtr(example.getPtptr())) + example.Point_print(example.getPtptr()) );
|
||||
System.out.println( "pt =" + Long.toHexString(SWIGTYPE_p_Point.getCPtr(example.getPt())) + example.Point_print(example.getPt()) );
|
||||
|
||||
System.out.println( "\nVariables (values printed from C)" );
|
||||
|
||||
example.print_vars();
|
||||
|
||||
System.out.println( "\nNow I'm going to try and modify some read only variables" );
|
||||
|
||||
System.out.println( " Trying to set 'path'" );
|
||||
try {
|
||||
Method m = example.class.getDeclaredMethod("setPath", new Class[] {String.class});
|
||||
m.invoke(example.class, new Object[] {"Whoa!"} );
|
||||
System.out.println( "Hey, what's going on?!?! This shouldn't work" );
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
System.out.println( "Good." );
|
||||
}
|
||||
catch (Throwable t) {
|
||||
System.out.println( "You shouldn't see this!" );
|
||||
}
|
||||
|
||||
System.out.println( " Trying to set 'status'" );
|
||||
try {
|
||||
Method m = example.class.getDeclaredMethod("setStatus", new Class[] {Integer.class});
|
||||
m.invoke(example.class, new Object[] {new Integer(0)} );
|
||||
System.out.println( "Hey, what's going on?!?! This shouldn't work" );
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
System.out.println( "Good." );
|
||||
}
|
||||
catch (Throwable t) {
|
||||
System.out.println( "You shouldn't see this!" );
|
||||
}
|
||||
|
||||
System.out.println( "\nI'm going to try and update a structure variable.\n" );
|
||||
|
||||
example.setPt(example.getPtptr());
|
||||
|
||||
System.out.println( "The new value is" );
|
||||
example.pt_print();
|
||||
System.out.println( "You should see the value" + example.Point_print(example.getPtptr()) );
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue