Home | History | Annotate | Download | only in java
      1 /* -----------------------------------------------------------------------------
      2  * std_string.i
      3  *
      4  * Typemaps for std::string and const std::string&
      5  * These are mapped to a Java String and are passed around by value.
      6  *
      7  * To use non-const std::string references use the following %apply.  Note
      8  * that they are passed by value.
      9  * %apply const std::string & {std::string &};
     10  * ----------------------------------------------------------------------------- */
     11 
     12 %{
     13 #include <string>
     14 %}
     15 
     16 namespace std {
     17 
     18 %naturalvar string;
     19 
     20 class string;
     21 
     22 // string
     23 %typemap(jni) string "jstring"
     24 %typemap(jtype) string "String"
     25 %typemap(jstype) string "String"
     26 %typemap(javadirectorin) string "$jniinput"
     27 %typemap(javadirectorout) string "$javacall"
     28 
     29 %typemap(in) string
     30 %{ if(!$input) {
     31      SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
     32      return $null;
     33     }
     34     const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0);
     35     if (!$1_pstr) return $null;
     36     $1.assign($1_pstr);
     37     jenv->ReleaseStringUTFChars($input, $1_pstr); %}
     38 
     39 %typemap(directorout) string
     40 %{ if(!$input) {
     41      SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
     42      return $null;
     43    }
     44    const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0);
     45    if (!$1_pstr) return $null;
     46    $result.assign($1_pstr);
     47    jenv->ReleaseStringUTFChars($input, $1_pstr); %}
     48 
     49 %typemap(directorin,descriptor="Ljava/lang/String;") string
     50 %{ $input = jenv->NewStringUTF($1.c_str()); %}
     51 
     52 %typemap(out) string
     53 %{ $result = jenv->NewStringUTF($1.c_str()); %}
     54 
     55 %typemap(javain) string "$javainput"
     56 
     57 %typemap(javaout) string {
     58     return $jnicall;
     59   }
     60 
     61 %typemap(typecheck) string = char *;
     62 
     63 %typemap(throws) string
     64 %{ SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, $1.c_str());
     65    return $null; %}
     66 
     67 // const string &
     68 %typemap(jni) const string & "jstring"
     69 %typemap(jtype) const string & "String"
     70 %typemap(jstype) const string & "String"
     71 %typemap(javadirectorin) const string & "$jniinput"
     72 %typemap(javadirectorout) const string & "$javacall"
     73 
     74 %typemap(in) const string &
     75 %{ if(!$input) {
     76      SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
     77      return $null;
     78    }
     79    const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0);
     80    if (!$1_pstr) return $null;
     81    $*1_ltype $1_str($1_pstr);
     82    $1 = &$1_str;
     83    jenv->ReleaseStringUTFChars($input, $1_pstr); %}
     84 
     85 %typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const string &
     86 %{ if(!$input) {
     87      SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
     88      return $null;
     89    }
     90    const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0);
     91    if (!$1_pstr) return $null;
     92    /* possible thread/reentrant code problem */
     93    static $*1_ltype $1_str;
     94    $1_str = $1_pstr;
     95    $result = &$1_str;
     96    jenv->ReleaseStringUTFChars($input, $1_pstr); %}
     97 
     98 %typemap(directorin,descriptor="Ljava/lang/String;") const string &
     99 %{ $input = jenv->NewStringUTF($1.c_str()); %}
    100 
    101 %typemap(out) const string &
    102 %{ $result = jenv->NewStringUTF($1->c_str()); %}
    103 
    104 %typemap(javain) const string & "$javainput"
    105 
    106 %typemap(javaout) const string & {
    107     return $jnicall;
    108   }
    109 
    110 %typemap(typecheck) const string & = char *;
    111 
    112 %typemap(throws) const string &
    113 %{ SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, $1.c_str());
    114    return $null; %}
    115 
    116 }
    117 
    118