Ignore:
Timestamp:
Jul 11, 2013 5:35:05 AM (11 years ago)
Author:
nanang
Message:

JNI project:

  • mapped pj_str_t and java String
  • cosmetics: tabs consistency
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/projects/jni/pjsip-apps/src/jni/my_typemaps.i

    r4549 r4557  
    44%include "typemaps.i" 
    55 
    6 /* Add director typemaps for "int *INOUT" */ 
     6/* Auto destroy JNI object, useful for director */ 
     7%inline %{ 
     8class LocalRefGuard { 
     9    JNIEnv* jenv; 
     10    jobject jobj; 
     11public: 
     12    LocalRefGuard(JNIEnv* jenv_, jobject jobj_) : jenv(jenv_), jobj(jobj_) {} 
     13    ~LocalRefGuard() { if (jobj) jenv->DeleteLocalRef(jobj); } 
     14}; 
     15%} 
     16 
     17/* 
     18 * Add director typemaps for "int *INOUT" 
     19 */ 
    720%typemap(javadirectorin) int *INOUT "$jniinput" 
    821%typemap(directorin, descriptor="[I") int *INOUT %{ 
    922  $input = jenv->NewIntArray(1); 
     23  LocalRefGuard g_$input(jenv, $input); 
    1024  jenv->SetIntArrayRegion($input, 0, 1, (jint*)$1); 
    1125%} 
    1226%typemap(directorout) int *INOUT %{ 
    13   jint* tmp$result = jenv->GetIntArrayElements($input, 0); 
    14   *$result = tmp$result[0]; 
    15   jenv->ReleaseIntArrayElements($input, tmp$result, JNI_ABORT); 
     27  jint tmp$result; 
     28  jenv->GetIntArrayRegion($input, 0, 1, &tmp$result); 
     29  *$result = ($*1_ltype)tmp$result; 
    1630%} 
    1731%typemap(directorargout) int *INOUT %{ 
    18   jint* tmp$1 = jenv->GetIntArrayElements($input, 0); 
    19   *$1 = ($*1_ltype) tmp$1[0]; 
    20   jenv->ReleaseIntArrayElements($input, tmp$1, JNI_ABORT); 
    21 %} 
    22  
    23 /* Add director typemaps for "unsigned *INOUT" */ 
     32  jint tmp$1; 
     33  jenv->GetIntArrayRegion($input, 0, 1, &tmp$1); 
     34  *$1 = ($*1_ltype)tmp$1; 
     35%} 
     36 
     37/* 
     38 * Add director typemaps for "unsigned *INOUT" 
     39 */ 
    2440%typemap(javadirectorin) unsigned *INOUT "$jniinput" 
    2541%typemap(directorin, descriptor="[I") unsigned *INOUT %{ 
    2642  $input = jenv->NewLongArray(1); 
     43  LocalRefGuard g_$input(jenv, $input); 
    2744  jenv->SetLongArrayRegion($input, 0, 1, (jlong*)$1); 
    2845%} 
    2946%typemap(directorout) unsigned *INOUT %{ 
    30   jlong* tmp$result = jenv->GetLongArrayElements($input, 0); 
    31   *$result = tmp$result[0]; 
    32   jenv->ReleaseLongArrayElements($input, tmp$result, JNI_ABORT); 
     47  jlong tmp$result; 
     48  jenv->GetLongArrayRegion($input, 0, 1, &tmp$result); 
     49  *$result = ($*1_ltype)tmp$result; 
    3350%} 
    3451%typemap(directorargout) unsigned *INOUT %{ 
    35   jlong* tmp$1 = jenv->GetLongArrayElements($input, 0); 
    36   *$1 = ($*1_ltype) tmp$1[0]; 
    37   jenv->ReleaseLongArrayElements($input, tmp$1, JNI_ABORT); 
    38 %} 
     52  jlong tmp$1; 
     53  jenv->GetLongArrayRegion($input, 0, 1, &tmp$1); 
     54  *$1 = ($*1_ltype)tmp$1; 
     55%} 
     56 
     57/* 
     58 * Map pj_str_t to java String 
     59 */ 
     60%ignore pj_str_t; 
     61%typemap(jni)             pj_str_t* "jstring" 
     62%typemap(jtype)           pj_str_t* "String" 
     63%typemap(jstype)          pj_str_t* "String" 
     64%typemap(javain)          pj_str_t* "$javainput" 
     65%typemap(javaout)         pj_str_t* { return $jnicall; } 
     66%typemap(javadirectorin)  pj_str_t* "$jniinput" 
     67%typemap(javadirectorout) pj_str_t* "$javacall" 
     68 
     69%typemap(in)              pj_str_t* (pj_str_t str_tmp, char* str_ptr=NULL) %{ 
     70  $1 = &str_tmp; 
     71  if(!$input) pj_strset($1, NULL, 0); 
     72  else { 
     73    str_ptr = (char*)jenv->GetStringUTFChars($input, 0); 
     74    pj_size_t str_len = jenv->GetStringUTFLength($input); 
     75    if (!str_ptr || !str_len) pj_strset($1, NULL, 0); 
     76    else pj_strset($1, str_ptr, str_len); 
     77  } 
     78%} 
     79 
     80%typemap(freearg)         pj_str_t* %{ 
     81  if ($input) 
     82    jenv->ReleaseStringUTFChars($input, str_ptr$argnum); 
     83%} 
     84 
     85%typemap(memberin)        pj_str_t %{ 
     86  if ($1.ptr) free($1.ptr); 
     87  if ($input->ptr) { 
     88    $1.ptr = (char*)malloc($input->slen);                               /* <-- will leak! */ 
     89    memcpy($1.ptr, $input->ptr, $input->slen); 
     90    $1.slen = $input->slen; 
     91  } 
     92%} 
     93 
     94%typemap(out)             pj_str_t* %{ 
     95  char *$1_ptr = (char*)malloc($1->slen+1); 
     96  memcpy($1_ptr, $1->ptr, $1->slen); 
     97  $1_ptr[$1->slen] = '\0'; 
     98  $result = jenv->NewStringUTF($1_ptr); 
     99  free($1_ptr); 
     100%} 
     101 
     102%typemap(directorin,descriptor="Ljava/lang/String;") pj_str_t* %{ 
     103  char *$input_ptr = (char*)malloc($1->slen+1); 
     104  memcpy($input_ptr, $1->ptr, $1->slen); 
     105  $input_ptr[$1->slen] = '\0'; 
     106  $input = jenv->NewStringUTF($input_ptr); 
     107  LocalRefGuard g_$input(jenv, $input); 
     108  free($input_ptr); 
     109%} 
     110 
     111/* 
     112 * Map pj_str_t[ANY] to java String[] 
     113 */ 
     114%typemap(jni)     pj_str_t[ANY] "jobjectArray" 
     115%typemap(jtype)   pj_str_t[ANY] "String[]" 
     116%typemap(jstype)  pj_str_t[ANY] "String[]" 
     117%typemap(javain)  pj_str_t[ANY] "$javainput" 
     118%typemap(javaout) pj_str_t[ANY] { return $jnicall; } 
     119%typemap(in)      pj_str_t[ANY] %{ 
     120  int $1_len = jenv->GetArrayLength($input); 
     121  char** $1_tmpstr = new char*[$1_len]; 
     122  jstring* $1_tmp = new jstring[$1_len]; 
     123  $1 = new pj_str_t[$1_len]; 
     124  int $1_ii; 
     125  for ($1_ii = 0; $1_ii<$1_len; $1_ii++) { 
     126    $1_tmp[$1_ii] = (jstring)jenv->GetObjectArrayElement($input, $1_ii); 
     127    if ($1_tmp[$1_ii]) { 
     128      $1[$1_ii].ptr = $1_tmpstr[$1_ii] = (char*)jenv->GetStringUTFChars($1_tmp[$1_ii], 0); 
     129      $1[$1_ii].slen = jenv->GetStringUTFLength($1_tmp[$1_ii]); 
     130    } else { 
     131      pj_strset(&$1[$1_ii], NULL, 0); 
     132    } 
     133    jenv->DeleteLocalRef($1_tmp[$1_ii]); /* is this ok here? */ 
     134  } 
     135%} 
     136 
     137%typemap(freearg) pj_str_t[ANY] %{ 
     138  for ($1_ii=0; $1_ii<$1_len; $1_ii++) 
     139    if ($1_tmp[$1_ii]) jenv->ReleaseStringUTFChars($1_tmp[$1_ii], $1_tmpstr[$1_ii]); 
     140  delete [] $1; 
     141  delete [] $1_tmp; 
     142  delete [] $1_tmpstr; 
     143%} 
     144 
     145/*  
     146 * Handle setter/getter of a struct member with type of 'array of pj_str_t'. 
     147 */ 
     148%define MY_JAVA_MEMBER_ARRAY_OF_STR(CLASS_NAME, NAME, COUNT_NAME) 
     149 
     150%ignore CLASS_NAME::COUNT_NAME; 
     151 
     152%typemap(memberin)      pj_str_t NAME[ANY], pj_str_t NAME[] { 
     153  int i; 
     154  for (i=0; i<arg1->COUNT_NAME; ++i) 
     155    if ($1[i].ptr) free($1[i].ptr); 
     156   
     157  arg1->COUNT_NAME = $input_len; 
     158  for (i=0; i<arg1->COUNT_NAME; ++i) { 
     159    if ($input->ptr) { 
     160      $1[i].ptr = (char*)malloc($input->slen);                          /* <-- will leak! */ 
     161      memcpy($1[i].ptr, $input->ptr, $input->slen); 
     162      $1[i].slen = $input->slen; 
     163    } 
     164  } 
     165} 
     166 
     167%typemap(out)           pj_str_t NAME[ANY] { 
     168  int i; 
     169  jstring temp_string; 
     170  const jclass clazz = jenv->FindClass("java/lang/String"); 
     171 
     172  $result = jenv->NewObjectArray(arg1->COUNT_NAME, clazz, NULL); 
     173  for (i=0; i<arg1->COUNT_NAME; i++) { 
     174    temp_string = jenv->NewStringUTF($1[i].ptr); 
     175    jenv->SetObjectArrayElement($result, i, temp_string); 
     176    jenv->DeleteLocalRef(temp_string); 
     177  } 
     178} 
     179 
     180%enddef 
     181 
     182/* 
     183 * pj_str_t *INOUT, string as input and output param, handled as single element array. 
     184 */ 
     185%apply pj_str_t[ANY] { pj_str_t *INOUT }; 
     186 
     187%typemap(argout) pj_str_t *INOUT { 
     188  char *$1_ptr = new char[$1[0].slen+1]; 
     189  memcpy($1_ptr, $1[0].ptr, $1[0].slen); 
     190  $1_ptr[$1[0].slen] = '\0'; 
     191  jstring temp_string = jenv->NewStringUTF($1_ptr); 
     192  jenv->SetObjectArrayElement($input, 0, temp_string); 
     193  delete [] $1_ptr; 
     194} 
     195 
     196%typemap(directorin,descriptor="[Ljava/lang/String;") pj_str_t *INOUT %{ 
     197  jstring tmp_$input; 
     198  $input = jenv->NewObjectArray(1, jenv->FindClass("java/lang/String"), NULL); 
     199  LocalRefGuard g_$input(jenv, $input); 
     200  tmp_$input = jenv->NewStringUTF($1[0].ptr); 
     201  LocalRefGuard g_temp_$input(jenv, tmp_$input); 
     202  jenv->SetObjectArrayElement($input, 0, tmp_$input); 
     203%} 
     204 
     205%typemap(directorargout)  pj_str_t *INOUT { 
     206  if(!$input) pj_strset($1, NULL, 0); 
     207  else { 
     208    str_ptr = (char*)jenv->GetStringUTFChars($input, 0); 
     209    pj_size_t str_len = jenv->GetStringUTFLength($input); 
     210    if (!str_ptr || !str_len) pj_strset($1, NULL, 0); 
     211    else { 
     212      $1->ptr = (char*)malloc(str_len);                                 /* <-- will leak! */ 
     213      memcpy($1->ptr, str_ptr, str_len); 
     214      $1->slen = str_len; 
     215    } 
     216  } 
     217} 
     218 
    39219 
    40220/*  
     
    45225%typemap(jtype)  TYPE **NAME "long" 
    46226%typemap(jstype) TYPE **NAME "TYPE" 
    47 %typemap(in)     TYPE **NAME (TYPE *temp = NULL) { temp = (TYPE*)$input; $1 = &temp; } 
    48 %typemap(argout) TYPE **NAME "$input = (jlong)*$1;"  
     227%typemap(in)     TYPE **NAME (TYPE *temp = NULL) "temp = (TYPE*)$input; $1 = &temp;" 
     228%typemap(argout) TYPE **NAME "$input = (jlong)*$1;" 
    49229%typemap(directorin,descriptor="L$packagepath/TYPE;") TYPE **NAME "$input = (jlong)*$1;" 
    50 %typemap(directorargout) TYPE **NAME " *$1 = (TYPE*)$input; " 
    51 %typemap(javain) TYPE **NAME "TYPE.getCPtr($javainput)" 
    52 %typemap(javadirectorin) TYPE **NAME "($jniinput == 0) ? null : new TYPE($jniinput, false)" 
     230%typemap(directorargout)  TYPE **NAME " *$1 = (TYPE*)$input; " 
     231%typemap(javain)          TYPE **NAME "TYPE.getCPtr($javainput)" 
     232%typemap(javadirectorin)  TYPE **NAME "($jniinput == 0) ? null : new TYPE($jniinput, false)" 
    53233%typemap(javadirectorout) TYPE **NAME "TYPE.getCPtr($javacall)" 
    54234%enddef 
     
    57237/*  
    58238 * Generate setter/getter of a struct member typed of 'array of enum' with variable length. 
    59  * Sample usage, given enum array member in this struct: 
    60  * -- 
    61  *   struct pjsip_tls_setting { 
    62  *     .. 
    63  *     unsigned ciphers_num; 
    64  *     pj_ssl_cipher *ciphers; 
    65  *     .. 
    66  *   }; 
    67  * -- 
    68  * apply: 
    69  *   MY_JAVA_MEMBER_ARRAY_OF_ENUM(pjsip_tls_setting, pj_ssl_cipher, ciphers, ciphers_num) 
    70239 */ 
    71240%define MY_JAVA_MEMBER_ARRAY_OF_ENUM(CLASS_NAME, TYPE, NAME, COUNT_NAME) 
    72241 
    73 %apply ARRAYSOFENUMS[] { TYPE* }; 
    74 %typemap(out) TYPE* %{ $result = SWIG_JavaArrayOutInt(jenv, (int*)$1, arg1->COUNT_NAME); %} 
    75 %ignore CLASS_NAME::NAME; 
    76242%ignore CLASS_NAME::COUNT_NAME; 
    77 %extend CLASS_NAME { 
    78     %rename ("%(lowercamelcase)s") set_##NAME; 
    79     %rename ("%(lowercamelcase)s") get_##NAME; 
    80     void set_##NAME(TYPE *NAME, int num) { 
    81         int i; 
    82         if ($self->NAME) free($self->NAME); 
    83         $self->NAME = (TYPE*)calloc(num, sizeof(TYPE)); 
    84         for (i=0; i<num; ++i) $self->NAME[i] = NAME[i]; 
    85         $self->COUNT_NAME = num; 
    86     } 
    87     TYPE* get_##NAME() { 
    88         return $self->NAME; 
    89     } 
    90     ~CLASS_NAME() { 
    91         if ($self->NAME) free($self->NAME); 
    92     } 
    93 }; 
     243%apply ARRAYSOFENUMS[] { TYPE* NAME }; 
     244 
     245%typemap(in)       TYPE* NAME (jint *jarr) %{ 
     246  int size$1 = jenv->GetArrayLength($input); 
     247  if (!SWIG_JavaArrayInInt(jenv, &jarr, (int **)&$1, $input)) return $null; 
     248%} 
     249 
     250%typemap(memberin) TYPE* NAME %{ 
     251  if ($1) free($1); 
     252  arg1->COUNT_NAME = size$input; 
     253  $1 = (TYPE*)calloc(arg1->COUNT_NAME, sizeof(TYPE));                   /* <-- will leak! */ 
     254  for (size_t i = 0; i < (size_t)arg1->COUNT_NAME; i++) 
     255    $1[i] = $input[i]; 
     256%} 
     257 
     258%typemap(out)      TYPE* NAME %{ 
     259  $result = SWIG_JavaArrayOutInt(jenv, (int*)$1, arg1->COUNT_NAME); 
     260%} 
     261 
     262// Hack for avoiding leak, may not work when another macro defines class destructor too 
     263%extend CLASS_NAME { ~CLASS_NAME() { if ($self->NAME) free($self->NAME); } }; 
    94264 
    95265%enddef 
     
    97267 
    98268/*  
    99  * Handle getter of a struct member with type of 'array of pointer to struct/class'. 
    100  * Currently this is applicable for read-only member. 
     269 * Handle setter/getter of a struct member with type of 'array of pointer to struct/class'. 
    101270 */ 
    102271%define MY_JAVA_MEMBER_ARRAY_OF_POINTER(CLASS_NAME, TYPE, NAME, COUNT_NAME) 
    103272 
    104273%ignore CLASS_NAME::COUNT_NAME; 
    105 %immutable CLASS_NAME::NAME; 
    106 %typemap(jni) TYPE *[ANY] "jlongArray" 
    107 %typemap(jtype) TYPE *[ANY] "long[]" 
    108 %typemap(jstype) TYPE *[ANY] "TYPE[]" 
    109 %typemap(javain) TYPE *[ANY] "TYPE.cArrayUnwrap($javainput)" 
    110 %typemap(javaout) TYPE *[ANY] {return TYPE.cArrayWrap($jnicall, $owner);} 
    111  
    112 // somehow "$result" is not preprocessed in "typemap(ret)", use 'jresult' directly then 
    113 %typemap(ret) TYPE *[ANY] { 
    114   jlong *arr; 
    115   int i; 
    116   jresult = JCALL1(NewLongArray, jenv, arg1->COUNT_NAME); 
    117   if (!jresult) { 
     274%typemap(jni)      TYPE* NAME[ANY] "jlongArray" 
     275%typemap(jtype)    TYPE* NAME[ANY] "long[]" 
     276%typemap(jstype)   TYPE* NAME[ANY] "TYPE[]" 
     277%typemap(javain)   TYPE* NAME[ANY] "TYPE.cArrayUnwrap($javainput)" 
     278%typemap(javaout)  TYPE* NAME[ANY] {return TYPE.cArrayWrap($jnicall, $owner);} 
     279 
     280%typemap(in)       TYPE* NAME[ANY] (jlong *jarr) %{ 
     281  if (!SWIG_JavaArrayInUlong(jenv, &jarr, (unsigned long**)&$1, $input)) 
    118282    return $null; 
    119   } 
    120   arr = JCALL2(GetLongArrayElements, jenv, jresult, 0); 
    121   if (!arr) { 
    122     return $null; 
    123   } 
    124   for (i=0; i<arg1->COUNT_NAME; i++) { 
    125     arr[i] = 0; 
    126     *($1_ltype)&arr[i] = $1[i]; 
    127   } 
    128   JCALL3(ReleaseLongArrayElements, jenv, jresult, arr, 0); 
    129 } 
     283  arg1->COUNT_NAME = jenv->GetArrayLength($input); 
     284%} 
     285 
     286%typemap(freearg)  TYPE* NAME[ANY] %{ if ($1) delete [] $1; %} 
     287 
     288%typemap(memberin) TYPE* NAME[ANY] %{ 
     289  for (size_t i = 0; i < (size_t)arg1->COUNT_NAME; i++) $1[i] = $input[i]; 
     290%} 
     291 
     292%typemap(out)      TYPE* NAME[ANY] %{ 
     293  $result = SWIG_JavaArrayOutUlong(jenv, (unsigned long*)$1, arg1->COUNT_NAME); 
     294%} 
    130295 
    131296%typemap(javacode) TYPE %{ 
    132297  protected static long[] cArrayUnwrap($javaclassname[] arrayWrapper) { 
    133       long[] cArray = new long[arrayWrapper.length]; 
    134       for (int i=0; i<arrayWrapper.length; i++) 
    135         cArray[i] = $javaclassname.getCPtr(arrayWrapper[i]); 
    136       return cArray; 
     298    long[] cArray = new long[arrayWrapper.length]; 
     299    for (int i=0; i<arrayWrapper.length; i++) 
     300      cArray[i] = $javaclassname.getCPtr(arrayWrapper[i]); 
     301    return cArray; 
    137302  } 
    138303  protected static $javaclassname[] cArrayWrap(long[] cArray, boolean cMemoryOwn) { 
Note: See TracChangeset for help on using the changeset viewer.