PageRenderTime 18ms CodeModel.GetById 16ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/java/typemap/example.i

#
Swig | 101 lines | 50 code | 23 blank | 28 comment | 0 complexity | a3f47a1771ee7452e497596747586d1d MD5 | raw file
  1/* File : example.i */
  2%module example
  3%{
  4/*
  5   example of a function that returns a value in the char * argument
  6   normally used like:
  7
  8   char buf[bigenough];
  9   f1(buf);
 10*/
 11
 12void f1(char *s) {
 13  if(s != NULL) {
 14    sprintf(s, "hello world");
 15  }
 16}
 17
 18void f2(char *s) {
 19  f1(s);
 20}
 21
 22void f3(char *s) {
 23  f1(s);
 24}
 25
 26%}
 27
 28/* default behaviour is that of input arg, Java cannot return a value in a 
 29 * string argument, so any changes made by f1(char*) will not be seen in the Java
 30 * string passed to the f1 function.
 31*/
 32void f1(char *s);
 33
 34%include various.i
 35
 36/* use the BYTE argout typemap to get around this. Changes in the string by 
 37 * f2 can be seen in Java. */
 38void f2(char *BYTE);
 39
 40
 41
 42/* Alternative approach uses a StringBuffer typemap for argout */
 43
 44/* Define the types to use in the generated JNI C code and Java code */
 45%typemap(jni) char *SBUF "jobject"
 46%typemap(jtype) char *SBUF "StringBuffer"
 47%typemap(jstype) char *SBUF "StringBuffer"
 48
 49/* How to convert Java(JNI) type to requested C type */
 50%typemap(in) char *SBUF {
 51
 52  $1 = NULL;
 53  if($input != NULL) {
 54    /* Get the String from the StringBuffer */
 55    jmethodID setLengthID;
 56    jclass sbufClass = (*jenv)->GetObjectClass(jenv, $input);
 57    jmethodID toStringID = (*jenv)->GetMethodID(jenv, sbufClass, "toString", "()Ljava/lang/String;");
 58    jstring js = (jstring) (*jenv)->CallObjectMethod(jenv, $input, toStringID);
 59
 60    /* Convert the String to a C string */
 61    const char *pCharStr = (*jenv)->GetStringUTFChars(jenv, js, 0);
 62
 63    /* Take a copy of the C string as the typemap is for a non const C string */
 64    jmethodID capacityID = (*jenv)->GetMethodID(jenv, sbufClass, "capacity", "()I");
 65    jint capacity = (*jenv)->CallIntMethod(jenv, $input, capacityID);
 66    $1 = (char *) malloc(capacity+1);
 67    strcpy($1, pCharStr);
 68
 69    /* Release the UTF string we obtained with GetStringUTFChars */
 70    (*jenv)->ReleaseStringUTFChars(jenv,  js, pCharStr);
 71
 72    /* Zero the original StringBuffer, so we can replace it with the result */
 73    setLengthID = (*jenv)->GetMethodID(jenv, sbufClass, "setLength", "(I)V");
 74    (*jenv)->CallVoidMethod(jenv, $input, setLengthID, (jint) 0);
 75  }
 76}
 77
 78/* How to convert the C type to the Java(JNI) type */
 79%typemap(argout) char *SBUF {
 80
 81  if($1 != NULL) {
 82    /* Append the result to the empty StringBuffer */
 83    jstring newString = (*jenv)->NewStringUTF(jenv, $1);
 84    jclass sbufClass = (*jenv)->GetObjectClass(jenv, $input);
 85    jmethodID appendStringID = (*jenv)->GetMethodID(jenv, sbufClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
 86    (*jenv)->CallObjectMethod(jenv, $input, appendStringID, newString);
 87
 88    /* Clean up the string object, no longer needed */
 89    free($1);
 90    $1 = NULL;
 91  }  
 92}
 93/* Prevent the default freearg typemap from being used */
 94%typemap(freearg) char *SBUF ""
 95
 96/* Convert the jstype to jtype typemap type */
 97%typemap(javain) char *SBUF "$javainput"
 98
 99/* apply the new typemap to our function */
100void f3(char *SBUF);
101