PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/java/native/example.i

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