The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
6fcc22a1f8
commit
516036631c
1508 changed files with 125983 additions and 44037 deletions
11
SWIG/Examples/java/native/.cvsignore
Normal file
11
SWIG/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
SWIG/Examples/java/native/index.html
Normal file
33
SWIG/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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue