various.i 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* -----------------------------------------------------------------------------
  2. * various.i
  3. *
  4. * SWIG Typemap library for Java.
  5. * Various useful typemaps.
  6. * ----------------------------------------------------------------------------- */
  7. /*
  8. * char **STRING_ARRAY typemaps.
  9. * These typemaps are for C String arrays which are NULL terminated.
  10. * char *values[] = { "one", "two", "three", NULL }; // note NULL
  11. * char ** is mapped to a Java String[].
  12. *
  13. * Example usage wrapping:
  14. * %apply char **STRING_ARRAY { char **input };
  15. * char ** foo(char **input);
  16. *
  17. * Java usage:
  18. * String numbers[] = { "one", "two", "three" };
  19. * String[] ret = modulename.foo( numbers };
  20. */
  21. %typemap(jni) char **STRING_ARRAY "jobjectArray"
  22. %typemap(jtype) char **STRING_ARRAY "String[]"
  23. %typemap(jstype) char **STRING_ARRAY "String[]"
  24. %typemap(in) char **STRING_ARRAY (jint size) {
  25. int i = 0;
  26. if ($input) {
  27. size = JCALL1(GetArrayLength, jenv, $input);
  28. #ifdef __cplusplus
  29. $1 = new char*[size+1];
  30. #else
  31. $1 = (char **)malloc((size+1) * sizeof(char *));
  32. #endif
  33. for (i = 0; i<size; i++) {
  34. jstring j_string = (jstring)JCALL2(GetObjectArrayElement, jenv, $input, i);
  35. const char *c_string = JCALL2(GetStringUTFChars, jenv, j_string, 0);
  36. #ifdef __cplusplus
  37. $1[i] = new char [strlen(c_string)+1];
  38. #else
  39. $1[i] = (char *)malloc((strlen(c_string)+1) * sizeof(const char *));
  40. #endif
  41. strcpy($1[i], c_string);
  42. JCALL2(ReleaseStringUTFChars, jenv, j_string, c_string);
  43. JCALL1(DeleteLocalRef, jenv, j_string);
  44. }
  45. $1[i] = 0;
  46. } else {
  47. $1 = 0;
  48. size = 0;
  49. }
  50. }
  51. %typemap(freearg) char **STRING_ARRAY {
  52. int i;
  53. for (i=0; i<size$argnum; i++)
  54. #ifdef __cplusplus
  55. delete[] $1[i];
  56. delete[] $1;
  57. #else
  58. free($1[i]);
  59. free($1);
  60. #endif
  61. }
  62. %typemap(out) char **STRING_ARRAY {
  63. if ($1) {
  64. int i;
  65. jsize len=0;
  66. jstring temp_string;
  67. const jclass clazz = JCALL1(FindClass, jenv, "java/lang/String");
  68. while ($1[len]) len++;
  69. $result = JCALL3(NewObjectArray, jenv, len, clazz, NULL);
  70. /* exception checking omitted */
  71. for (i=0; i<len; i++) {
  72. temp_string = JCALL1(NewStringUTF, jenv, *$1++);
  73. JCALL3(SetObjectArrayElement, jenv, $result, i, temp_string);
  74. JCALL1(DeleteLocalRef, jenv, temp_string);
  75. }
  76. }
  77. }
  78. %typemap(javain) char **STRING_ARRAY "$javainput"
  79. %typemap(javaout) char **STRING_ARRAY {
  80. return $jnicall;
  81. }
  82. /*
  83. * char **STRING_OUT typemaps.
  84. * These are typemaps for returning strings when using a C char ** parameter type.
  85. * The returned string appears in the 1st element of the passed in Java String array.
  86. *
  87. * Example usage wrapping:
  88. * %apply char **STRING_OUT { char **string_out };
  89. * void foo(char **string_out);
  90. *
  91. * Java usage:
  92. * String stringOutArray[] = { "" };
  93. * modulename.foo(stringOutArray);
  94. * System.out.println( stringOutArray[0] );
  95. */
  96. %typemap(jni) char **STRING_OUT "jobjectArray"
  97. %typemap(jtype) char **STRING_OUT "String[]"
  98. %typemap(jstype) char **STRING_OUT "String[]"
  99. %typemap(javain) char **STRING_OUT "$javainput"
  100. %typemap(in) char **STRING_OUT($*1_ltype temp) {
  101. if (!$input) {
  102. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null");
  103. return $null;
  104. }
  105. if (JCALL1(GetArrayLength, jenv, $input) == 0) {
  106. SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
  107. return $null;
  108. }
  109. $1 = &temp;
  110. *$1 = 0;
  111. }
  112. %typemap(argout) char **STRING_OUT {
  113. jstring jnewstring = NULL;
  114. if ($1) {
  115. jnewstring = JCALL1(NewStringUTF, jenv, *$1);
  116. }
  117. JCALL3(SetObjectArrayElement, jenv, $input, 0, jnewstring);
  118. }
  119. /*
  120. * char *BYTE typemaps.
  121. * These are input typemaps for mapping a Java byte[] array to a C char array.
  122. * Note that as a Java array is used and thus passeed by reference, the C routine
  123. * can return data to Java via the parameter.
  124. *
  125. * Example usage wrapping:
  126. * void foo(char *array);
  127. *
  128. * Java usage:
  129. * byte b[] = new byte[20];
  130. * modulename.foo(b);
  131. */
  132. %typemap(jni) char *BYTE "jbyteArray"
  133. %typemap(jtype) char *BYTE "byte[]"
  134. %typemap(jstype) char *BYTE "byte[]"
  135. %typemap(in) char *BYTE {
  136. $1 = (char *) JCALL2(GetByteArrayElements, jenv, $input, 0);
  137. }
  138. %typemap(argout) char *BYTE {
  139. JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0);
  140. }
  141. %typemap(javain) char *BYTE "$javainput"
  142. /* Prevent default freearg typemap from being used */
  143. %typemap(freearg) char *BYTE ""
  144. /*
  145. * unsigned char *NIOBUFFER typemaps.
  146. * This is for mapping Java nio buffers to C char arrays.
  147. * It is useful for performance critical code as it reduces the memory copy an marshaling overhead.
  148. * Note: The Java buffer has to be allocated with allocateDirect.
  149. *
  150. * Example usage wrapping:
  151. * %apply unsigned char *NIOBUFFER { unsigned char *buf };
  152. * void foo(unsigned char *buf);
  153. *
  154. * Java usage:
  155. * java.nio.ByteBuffer b = ByteBuffer.allocateDirect(20);
  156. * modulename.foo(b);
  157. */
  158. %typemap(jni) unsigned char *NIOBUFFER "jobject"
  159. %typemap(jtype) unsigned char *NIOBUFFER "java.nio.ByteBuffer"
  160. %typemap(jstype) unsigned char *NIOBUFFER "java.nio.ByteBuffer"
  161. %typemap(javain,
  162. pre=" assert $javainput.isDirect() : \"Buffer must be allocated direct.\";") unsigned char *NIOBUFFER "$javainput"
  163. %typemap(javaout) unsigned char *NIOBUFFER {
  164. return $jnicall;
  165. }
  166. %typemap(in) unsigned char *NIOBUFFER {
  167. $1 = (unsigned char *) JCALL1(GetDirectBufferAddress, jenv, $input);
  168. if ($1 == NULL) {
  169. SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unable to get address of a java.nio.ByteBuffer direct byte buffer. Buffer must be a direct buffer and not a non-direct buffer.");
  170. }
  171. }
  172. %typemap(memberin) unsigned char *NIOBUFFER {
  173. if ($input) {
  174. $1 = $input;
  175. } else {
  176. $1 = 0;
  177. }
  178. }
  179. %typemap(freearg) unsigned char *NIOBUFFER ""