PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1-3-25/SWIG/Lib/java/typemaps.i

#
Swig | 443 lines | 241 code | 45 blank | 157 comment | 0 complexity | 72352c84dfee25fc40800ac9d0f542b9 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. //
  2. // SWIG Typemap library
  3. // William Fulton
  4. // 4 January 2002
  5. //
  6. // Java implementation
  7. //
  8. // ------------------------------------------------------------------------
  9. // Pointer and reference handling
  10. //
  11. // These mappings provide support for input/output arguments and common
  12. // uses for C/C++ pointers and C++ references.
  13. // ------------------------------------------------------------------------
  14. // INPUT typemaps.
  15. // These remap a C pointer or C++ reference to be an "INPUT" value which is passed by value
  16. // instead of reference.
  17. /*
  18. The following methods can be applied to turn a pointer or reference into a simple
  19. "input" value. That is, instead of passing a pointer or reference to an object,
  20. you would use a real value instead.
  21. bool *INPUT, bool &INPUT
  22. signed char *INPUT, signed char &INPUT
  23. unsigned char *INPUT, unsigned char &INPUT
  24. short *INPUT, short &INPUT
  25. unsigned short *INPUT, unsigned short &INPUT
  26. int *INPUT, int &INPUT
  27. unsigned int *INPUT, unsigned int &INPUT
  28. long *INPUT, long &INPUT
  29. unsigned long *INPUT, unsigned long &INPUT
  30. long long *INPUT, long long &INPUT
  31. unsigned long long *INPUT, unsigned long long &INPUT
  32. float *INPUT, float &INPUT
  33. double *INPUT, double &INPUT
  34. To use these, suppose you had a C function like this :
  35. double fadd(double *a, double *b) {
  36. return *a+*b;
  37. }
  38. You could wrap it with SWIG as follows :
  39. %include "typemaps.i"
  40. double fadd(double *INPUT, double *INPUT);
  41. or you can use the %apply directive :
  42. %include "typemaps.i"
  43. %apply double *INPUT { double *a, double *b };
  44. double fadd(double *a, double *b);
  45. In Java you could then use it like this:
  46. double answer = modulename.fadd(10.0, 20.0);
  47. There are no char *INPUT typemaps, however you can apply the signed char * typemaps instead:
  48. %include "typemaps.i"
  49. %apply signed char *INPUT {char *input};
  50. void f(char *input);
  51. */
  52. %define INPUT_TYPEMAP(TYPE, JNITYPE, JTYPE, JNIDESC)
  53. %typemap(jni) TYPE *INPUT, TYPE &INPUT "JNITYPE"
  54. %typemap(jtype) TYPE *INPUT, TYPE &INPUT "JTYPE"
  55. %typemap(jstype) TYPE *INPUT, TYPE &INPUT "JTYPE"
  56. %typemap(javain) TYPE *INPUT, TYPE &INPUT "$javainput"
  57. %typemap(javadirectorin) TYPE *INPUT, TYPE &INPUT "$jniinput"
  58. %typemap(javadirectorout) TYPE *INPUT, TYPE &INPUT "$javacall"
  59. %typemap(in) TYPE *INPUT, TYPE &INPUT
  60. %{ $1 = ($1_ltype)&$input; %}
  61. %typemap(directorout) TYPE *INPUT, TYPE &INPUT
  62. %{ $1 = ($1_ltype)&$input; %}
  63. %typemap(directorin,descriptor=JNIDESC) TYPE &INPUT
  64. %{ *(($&1_ltype) $input) = (JNITYPE *) &$1; %}
  65. %typemap(directorin,descriptor=JNIDESC) TYPE *INPUT
  66. %{ *(($&1_ltype) $input) = (JNITYPE *) $1; %}
  67. %typemap(freearg) TYPE *INPUT, TYPE &INPUT ""
  68. %typemap(typecheck) TYPE *INPUT = TYPE;
  69. %typemap(typecheck) TYPE &INPUT = TYPE;
  70. %enddef
  71. INPUT_TYPEMAP(bool, jboolean, boolean, "Z");
  72. INPUT_TYPEMAP(signed char, jbyte, byte, "B");
  73. INPUT_TYPEMAP(unsigned char, jshort, short, "S");
  74. INPUT_TYPEMAP(short, jshort, short, "S");
  75. INPUT_TYPEMAP(unsigned short, jint, int, "I");
  76. INPUT_TYPEMAP(int, jint, int, "I");
  77. INPUT_TYPEMAP(unsigned int, jlong, long, "J");
  78. INPUT_TYPEMAP(long, jint, int, "I");
  79. INPUT_TYPEMAP(unsigned long, jlong, long, "J");
  80. INPUT_TYPEMAP(long long, jlong, long, "J");
  81. INPUT_TYPEMAP(unsigned long long, jobject, java.math.BigInteger, "Ljava/math/BigInteger;");
  82. INPUT_TYPEMAP(float, jfloat, float, "F");
  83. INPUT_TYPEMAP(double, jdouble, double, "D");
  84. #undef INPUT_TYPEMAP
  85. /* Convert from BigInteger using the toByteArray member function */
  86. /* Overrides the typemap in the INPUT_TYPEMAP macro */
  87. %typemap(in) unsigned long long *INPUT($*1_ltype temp), unsigned long long &INPUT($*1_ltype temp) {
  88. jclass clazz;
  89. jmethodID mid;
  90. jbyteArray ba;
  91. jbyte* bae;
  92. jsize sz;
  93. int i;
  94. if (!$input) {
  95. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "BigInteger null");
  96. return $null;
  97. }
  98. clazz = JCALL1(GetObjectClass, jenv, $input);
  99. mid = JCALL3(GetMethodID, jenv, clazz, "toByteArray", "()[B");
  100. ba = (jbyteArray)JCALL2(CallObjectMethod, jenv, $input, mid);
  101. bae = JCALL2(GetByteArrayElements, jenv, ba, 0);
  102. sz = JCALL1(GetArrayLength, jenv, ba);
  103. temp = 0;
  104. if (bae[0] == 0) {
  105. for(i=sz-1; i>0; i-- ) {
  106. temp = (temp << 8) | (unsigned char)bae[sz-i];
  107. }
  108. }
  109. else {
  110. for(i=sz; i>=0; i-- ) {
  111. temp = (temp << 8) | (unsigned char)bae[sz-1-i];
  112. }
  113. }
  114. JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0);
  115. $1 = &temp;
  116. }
  117. // OUTPUT typemaps. These typemaps are used for parameters that
  118. // are output only. An array replaces the c pointer or reference parameter.
  119. // The output value is returned in this array passed in.
  120. /*
  121. The following methods can be applied to turn a pointer or reference into an "output"
  122. value. When calling a function, no input value would be given for
  123. a parameter, but an output value would be returned. This works by a
  124. Java array being passed as a parameter where a c pointer or reference is required.
  125. As with any Java function, the array is passed by reference so that
  126. any modifications to the array will be picked up in the calling function.
  127. Note that the array passed in MUST have at least one element, but as the
  128. c function does not require any input, the value can be set to anything.
  129. bool *OUTPUT, bool &OUTPUT
  130. signed char *OUTPUT, signed char &OUTPUT
  131. unsigned char *OUTPUT, unsigned char &OUTPUT
  132. short *OUTPUT, short &OUTPUT
  133. unsigned short *OUTPUT, unsigned short &OUTPUT
  134. int *OUTPUT, int &OUTPUT
  135. unsigned int *OUTPUT, unsigned int &OUTPUT
  136. long *OUTPUT, long &OUTPUT
  137. unsigned long *OUTPUT, unsigned long &OUTPUT
  138. long long *OUTPUT, long long &OUTPUT
  139. unsigned long long *OUTPUT, unsigned long long &OUTPUT
  140. float *OUTPUT, float &OUTPUT
  141. double *OUTPUT, double &OUTPUT
  142. For example, suppose you were trying to wrap the modf() function in the
  143. C math library which splits x into integral and fractional parts (and
  144. returns the integer part in one of its parameters):
  145. double modf(double x, double *ip);
  146. You could wrap it with SWIG as follows :
  147. %include "typemaps.i"
  148. double modf(double x, double *OUTPUT);
  149. or you can use the %apply directive :
  150. %include "typemaps.i"
  151. %apply double *OUTPUT { double *ip };
  152. double modf(double x, double *ip);
  153. The Java output of the function would be the function return value and the
  154. value in the single element array. In Java you would use it like this:
  155. double[] ptr = {0.0};
  156. double fraction = modulename.modf(5.0,ptr);
  157. There are no char *OUTPUT typemaps, however you can apply the signed char * typemaps instead:
  158. %include "typemaps.i"
  159. %apply signed char *OUTPUT {char *output};
  160. void f(char *output);
  161. */
  162. /* Java BigInteger[] */
  163. %typecheck(SWIG_TYPECHECK_INT128_ARRAY) SWIGBIGINTEGERARRAY ""
  164. %define OUTPUT_TYPEMAP(TYPE, JNITYPE, JTYPE, JAVATYPE, JNIDESC, TYPECHECKTYPE)
  165. %typemap(jni) TYPE *OUTPUT, TYPE &OUTPUT %{JNITYPE##Array%}
  166. %typemap(jtype) TYPE *OUTPUT, TYPE &OUTPUT "JTYPE[]"
  167. %typemap(jstype) TYPE *OUTPUT, TYPE &OUTPUT "JTYPE[]"
  168. %typemap(javain) TYPE *OUTPUT, TYPE &OUTPUT "$javainput"
  169. %typemap(javadirectorin) TYPE *OUTPUT, TYPE &OUTPUT "$jniinput"
  170. %typemap(javadirectorout) TYPE *OUTPUT, TYPE &OUTPUT "$javacall"
  171. %typemap(in) TYPE *OUTPUT($*1_ltype temp), TYPE &OUTPUT($*1_ltype temp)
  172. {
  173. if (!$input) {
  174. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null");
  175. return $null;
  176. }
  177. if (JCALL1(GetArrayLength, jenv, $input) == 0) {
  178. SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
  179. return $null;
  180. }
  181. $1 = &temp;
  182. }
  183. %typemap(directorout) TYPE *OUTPUT, TYPE &OUTPUT {
  184. #error "Need to provide OUTPUT directorout typemap"
  185. }
  186. %typemap(directorin,descriptor=JNIDESC) TYPE &OUTPUT
  187. %{ *(($&1_ltype) $input = &$1; %}
  188. %typemap(directorin,descriptor=JNIDESC) TYPE *OUTPUT
  189. %{
  190. #error "Need to provide OUT directorin typemap, TYPE array length is unknown"
  191. %}
  192. %typemap(freearg) TYPE *OUTPUT, TYPE &OUTPUT ""
  193. %typemap(argout) TYPE *OUTPUT, TYPE &OUTPUT
  194. {
  195. JNITYPE jvalue = (JNITYPE)temp$argnum;
  196. JCALL4(Set##JAVATYPE##ArrayRegion, jenv, $input, 0, 1, &jvalue);
  197. }
  198. %typemap(typecheck) TYPE *INOUT = TYPECHECKTYPE;
  199. %typemap(typecheck) TYPE &INOUT = TYPECHECKTYPE;
  200. %enddef
  201. OUTPUT_TYPEMAP(bool, jboolean, boolean, Boolean, "[Ljava/lang/Boolean;", jbooleanArray);
  202. OUTPUT_TYPEMAP(signed char, jbyte, byte, Byte, "[Ljava/lang/Byte;", jbyteArray);
  203. OUTPUT_TYPEMAP(unsigned char, jshort, short, Short, "[Ljava/lang/Short;", jshortArray);
  204. OUTPUT_TYPEMAP(short, jshort, short, Short, "[Ljava/lang/Short;", jshortArray);
  205. OUTPUT_TYPEMAP(unsigned short, jint, int, Int, "[Ljava/lang/Integer;", jintArray);
  206. OUTPUT_TYPEMAP(int, jint, int, Int, "[Ljava/lang/Integer;", jintArray);
  207. OUTPUT_TYPEMAP(unsigned int, jlong, long, Long, "[Ljava/lang/Long;", jlongArray);
  208. OUTPUT_TYPEMAP(long, jint, int, Int, "[Ljava/lang/Integer;", jintArray);
  209. OUTPUT_TYPEMAP(unsigned long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray);
  210. OUTPUT_TYPEMAP(long long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray);
  211. OUTPUT_TYPEMAP(unsigned long long, jobject, java.math.BigInteger, NOTUSED, "[Ljava/lang/BigInteger;", SWIGBIGINTEGERARRAY);
  212. OUTPUT_TYPEMAP(float, jfloat, float, Float, "[Ljava/lang/Float;", jfloatArray);
  213. OUTPUT_TYPEMAP(double, jdouble, double, Double, "[Ljava/lang/Double;", jdoubleArray);
  214. #undef OUTPUT_TYPEMAP
  215. /* Convert to BigInteger - byte array holds number in 2's complement big endian format */
  216. /* Use first element in BigInteger array for output */
  217. /* Overrides the typemap in the OUTPUT_TYPEMAP macro */
  218. %typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT {
  219. jbyteArray ba = JCALL1(NewByteArray, jenv, 9);
  220. jbyte* bae = JCALL2(GetByteArrayElements, jenv, ba, 0);
  221. jclass clazz = JCALL1(FindClass, jenv, "java/math/BigInteger");
  222. jmethodID mid = JCALL3(GetMethodID, jenv, clazz, "<init>", "([B)V");
  223. jobject bigint;
  224. int i;
  225. bae[0] = 0;
  226. for(i=1; i<9; i++ ) {
  227. bae[i] = (jbyte)(temp$argnum>>8*(8-i));
  228. }
  229. JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0);
  230. bigint = JCALL3(NewObject, jenv, clazz, mid, ba);
  231. JCALL3(SetObjectArrayElement, jenv, $input, 0, bigint);
  232. }
  233. // INOUT
  234. // Mappings for an argument that is both an input and output
  235. // parameter
  236. /*
  237. The following methods can be applied to make a function parameter both
  238. an input and output value. This combines the behavior of both the
  239. "INPUT" and "OUTPUT" methods described earlier. Output values are
  240. returned as an element in a Java array.
  241. bool *INOUT, bool &INOUT
  242. signed char *INOUT, signed char &INOUT
  243. unsigned char *INOUT, unsigned char &INOUT
  244. short *INOUT, short &INOUT
  245. unsigned short *INOUT, unsigned short &INOUT
  246. int *INOUT, int &INOUT
  247. unsigned int *INOUT, unsigned int &INOUT
  248. long *INOUT, long &INOUT
  249. unsigned long *INOUT, unsigned long &INOUT
  250. long long *INOUT, long long &INOUT
  251. unsigned long long *INOUT, unsigned long long &INOUT
  252. float *INOUT, float &INOUT
  253. double *INOUT, double &INOUT
  254. For example, suppose you were trying to wrap the following function :
  255. void neg(double *x) {
  256. *x = -(*x);
  257. }
  258. You could wrap it with SWIG as follows :
  259. %include "typemaps.i"
  260. void neg(double *INOUT);
  261. or you can use the %apply directive :
  262. %include "typemaps.i"
  263. %apply double *INOUT { double *x };
  264. void neg(double *x);
  265. This works similarly to C in that the mapping directly modifies the
  266. input value - the input must be an array with a minimum of one element.
  267. The element in the array is the input and the output is the element in
  268. the array.
  269. double x[] = {5.0};
  270. neg(x);
  271. The implementation of the OUTPUT and INOUT typemaps is different to other
  272. languages in that other languages will return the output value as part
  273. of the function return value. This difference is due to Java being a typed language.
  274. There are no char *INOUT typemaps, however you can apply the signed char * typemaps instead:
  275. %include "typemaps.i"
  276. %apply signed char *INOUT {char *inout};
  277. void f(char *inout);
  278. */
  279. %define INOUT_TYPEMAP(TYPE, JNITYPE, JTYPE, JAVATYPE, JNIDESC, TYPECHECKTYPE)
  280. %typemap(jni) TYPE *INOUT, TYPE &INOUT %{JNITYPE##Array%}
  281. %typemap(jtype) TYPE *INOUT, TYPE &INOUT "JTYPE[]"
  282. %typemap(jstype) TYPE *INOUT, TYPE &INOUT "JTYPE[]"
  283. %typemap(javain) TYPE *INOUT, TYPE &INOUT "$javainput"
  284. %typemap(javadirectorin) TYPE *INOUT, TYPE &INOUT "$jniinput"
  285. %typemap(javadirectorout) TYPE *INOUT, TYPE &INOUT "$javacall"
  286. %typemap(in) TYPE *INOUT, TYPE &INOUT {
  287. if (!$input) {
  288. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null");
  289. return $null;
  290. }
  291. if (JCALL1(GetArrayLength, jenv, $input) == 0) {
  292. SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
  293. return $null;
  294. }
  295. $1 = ($1_ltype) JCALL2(Get##JAVATYPE##ArrayElements, jenv, $input, 0);
  296. }
  297. %typemap(directorout) TYPE *INOUT, TYPE &INOUT {
  298. #error "Need to provide INOUT directorout typemap"
  299. }
  300. %typemap(directorin,descriptor=JNIDESC) TYPE &INOUT
  301. %{ *(($&1_ltype)&$input) = &$1; %}
  302. %typemap(directorin,descriptor=JNIDESC) TYPE *INOUT, TYPE &INOUT
  303. {
  304. #error "Need to provide INOUT directorin typemap, TYPE array length is unknown"
  305. }
  306. %typemap(freearg) TYPE *INOUT, TYPE &INOUT ""
  307. %typemap(argout) TYPE *INOUT, TYPE &INOUT
  308. { JCALL3(Release##JAVATYPE##ArrayElements, jenv, $input, (JNITYPE *)$1, 0); }
  309. %typemap(typecheck) TYPE *INOUT = TYPECHECKTYPE;
  310. %typemap(typecheck) TYPE &INOUT = TYPECHECKTYPE;
  311. %enddef
  312. INOUT_TYPEMAP(bool, jboolean, boolean, Boolean, "[Ljava/lang/Boolean;", jbooleanArray);
  313. INOUT_TYPEMAP(signed char, jbyte, byte, Byte, "[Ljava/lang/Byte;", jbyteArray);
  314. INOUT_TYPEMAP(unsigned char, jshort, short, Short, "[Ljava/lang/Short;", jshortArray);
  315. INOUT_TYPEMAP(short, jshort, short, Short, "[Ljava/lang/Short;", jshortArray);
  316. INOUT_TYPEMAP(unsigned short, jint, int, Int, "[Ljava/lang/Integer;", jintArray);
  317. INOUT_TYPEMAP(int, jint, int, Int, "[Ljava/lang/Integer;", jintArray);
  318. INOUT_TYPEMAP(unsigned int, jlong, long, Long, "[Ljava/lang/Long;", jlongArray);
  319. INOUT_TYPEMAP(long, jint, int, Int, "[Ljava/lang/Integer;", jintArray);
  320. INOUT_TYPEMAP(unsigned long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray);
  321. INOUT_TYPEMAP(long long, jlong, long, Long, "[Ljava/lang/Long;", jlongArray);
  322. INOUT_TYPEMAP(unsigned long long, jobject, java.math.BigInteger, NOTUSED, "[Ljava.math.BigInteger;", SWIGBIGINTEGERARRAY);
  323. INOUT_TYPEMAP(float, jfloat, float, Float, "[Ljava/lang/Float;", jfloatArray);
  324. INOUT_TYPEMAP(double, jdouble, double, Double, "[Ljava/lang/Double;", jdoubleArray);
  325. #undef INOUT_TYPEMAP
  326. /* Override the typemap in the INOUT_TYPEMAP macro */
  327. %typemap(in) unsigned long long *INOUT ($*1_ltype temp), unsigned long long &INOUT ($*1_ltype temp) {
  328. jobject bigint;
  329. jclass clazz;
  330. jmethodID mid;
  331. jbyteArray ba;
  332. jbyte* bae;
  333. jsize sz;
  334. int i;
  335. if (!$input) {
  336. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null");
  337. return $null;
  338. }
  339. if (JCALL1(GetArrayLength, jenv, $input) == 0) {
  340. SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element");
  341. return $null;
  342. }
  343. bigint = JCALL2(GetObjectArrayElement, jenv, $input, 0);
  344. if (!bigint) {
  345. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array element null");
  346. return $null;
  347. }
  348. clazz = JCALL1(GetObjectClass, jenv, bigint);
  349. mid = JCALL3(GetMethodID, jenv, clazz, "toByteArray", "()[B");
  350. ba = (jbyteArray)JCALL2(CallObjectMethod, jenv, bigint, mid);
  351. bae = JCALL2(GetByteArrayElements, jenv, ba, 0);
  352. sz = JCALL1(GetArrayLength, jenv, ba);
  353. temp = 0;
  354. if (bae[0] == 0) {
  355. for(i=sz-1; i>0; i-- ) {
  356. temp = (temp << 8) | (unsigned char)bae[sz-i];
  357. }
  358. }
  359. else {
  360. for(i=sz; i>=0; i-- ) {
  361. temp = (temp << 8) | (unsigned char)bae[sz-1-i];
  362. }
  363. }
  364. JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0);
  365. $1 = &temp;
  366. }
  367. %typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
  368. %typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;