/trunk/Examples/java/native/example.i
Swig | 56 lines | 34 code | 18 blank | 4 comment | 0 complexity | 30e5da679acdd3b56302bb80ccd27e36 MD5 | raw file
1/* File : example.i */ 2%module example 3 4%{ 5#include <string.h> 6 7typedef struct point { 8 int x; 9 int y; 10} Point; 11 12 13Point *point_create(int x, int y) { 14 Point *p = (Point *) malloc(sizeof(Point)); 15 p->x = x; 16 p->y = y; 17 18 return p; 19} 20 21static char *point_toString(char *format, Point *p) { 22 static char buf[80]; 23 24 sprintf(buf, format, p->x, p->y); 25 26 return buf; 27} 28 29/* this function will be wrapped by SWIG */ 30char *point_toString1(Point *p) { 31 return point_toString("(%d,%d)", p); 32} 33 34/* this one we wrapped manually*/ 35JNIEXPORT jstring JNICALL Java_exampleJNI_point_1toString2(JNIEnv *jenv, jclass jcls, jlong jpoint) { 36 Point * p; 37 jstring result; 38 39 (void)jcls; 40 41 p = *(Point **)&jpoint; 42 43 result = (*jenv)->NewStringUTF(jenv, point_toString("[%d,%d]", p)); 44 45 return result; 46} 47%} 48 49 50Point *point_create(int x, int y); 51char *point_toString1(Point *p); 52 53/* give access to free() for memory cleanup of the malloc'd Point */ 54extern void free(void *memblock); 55 56%native(point_toString2) char *point_toString2(Point *p);