Added a new Examples folder with modified Examples/java files

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-cherylfoil@10763 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Cheryl Foil 2008-08-15 23:54:53 +00:00
commit 311ddbc2bf
78 changed files with 3668 additions and 0 deletions

View file

@ -0,0 +1,18 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS =
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

View file

@ -0,0 +1,56 @@
/* File : example.i */
%module example
%{
#include <string.h>
typedef struct point {
int x;
int y;
} Point;
Point *point_create(int x, int y) {
Point *p = (Point *) malloc(sizeof(Point));
p->x = x;
p->y = y;
return p;
}
static char *point_toString(char *format, Point *p) {
static char buf[80];
sprintf(buf, format, p->x, p->y);
return buf;
}
/* this function will be wrapped by SWIG */
char *point_toString1(Point *p) {
return point_toString("(%d,%d)", p);
}
/* this one we wrapped manually*/
JNIEXPORT jstring JNICALL Java_exampleJNI_point_1toString2(JNIEnv *jenv, jclass jcls, jlong jpoint) {
Point * p;
jstring result;
(void)jcls;
p = *(Point **)&jpoint;
result = (*jenv)->NewStringUTF(jenv, point_toString("[%d,%d]", p));
return result;
}
%}
Point *point_create(int x, int y);
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);

View 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>
Click <a href="../../../Doc/Manual/Java.html#using_own_jni_functions">here</a> for the relevant section in the SWIG and Java documentation.
<p>
This example compares wrapping a c global function using the manual way and the SWIG way.
</p>
<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>

View file

@ -0,0 +1,19 @@
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[]) {
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
}
}