and_string.i 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if defined(SWIGJAVA) && defined(__ANDROID__)
  2. %typemap(out) std::string
  3. %{
  4. jsize $1_len = (jsize)$1.length();
  5. jbyteArray $1_bytes = jenv->NewByteArray($1_len);
  6. jenv->SetByteArrayRegion($1_bytes, 0, $1_len, (jbyte *) $1.c_str());
  7. jclass $1_strClass = jenv->FindClass("java/lang/String");
  8. jmethodID $1_ctorID = jenv->GetMethodID($1_strClass, "<init>", "([BLjava/lang/String;)V");
  9. jstring $1_encoding = jenv->NewStringUTF("UTF-8");
  10. jstring $1_jstr = (jstring) jenv->NewObject($1_strClass, $1_ctorID, $1_bytes, $1_encoding);
  11. jenv->DeleteLocalRef($1_encoding);
  12. jenv->DeleteLocalRef($1_bytes);
  13. $result = $1_jstr;
  14. %}
  15. %typemap(directorin,descriptor="Ljava/lang/String;") std::string
  16. %{
  17. jsize $1_len = (jsize)$1.length();
  18. jbyteArray $1_bytes = jenv->NewByteArray($1_len);
  19. jenv->SetByteArrayRegion($1_bytes, 0, $1_len, (jbyte *) $1.c_str());
  20. jclass $1_strClass = jenv->FindClass("java/lang/String");
  21. jmethodID $1_ctorID = jenv->GetMethodID($1_strClass, "<init>", "([BLjava/lang/String;)V");
  22. jstring $1_encoding = jenv->NewStringUTF("UTF-8");
  23. jstring $1_jstr = (jstring) jenv->NewObject($1_strClass, $1_ctorID, $1_bytes, $1_encoding);
  24. jenv->DeleteLocalRef($1_encoding);
  25. jenv->DeleteLocalRef($1_bytes);
  26. $input = $1_jstr;
  27. %}
  28. %typemap(directorin,descriptor="Ljava/lang/String;") const std::string &
  29. %{
  30. jsize $1_len = (jsize)$1.length();
  31. jbyteArray $1_bytes = jenv->NewByteArray($1_len);
  32. jenv->SetByteArrayRegion($1_bytes, 0, $1_len, (jbyte *) $1.c_str());
  33. jclass $1_strClass = jenv->FindClass("java/lang/String");
  34. jmethodID $1_ctorID = jenv->GetMethodID($1_strClass, "<init>", "([BLjava/lang/String;)V");
  35. jstring $1_encoding = jenv->NewStringUTF("UTF-8");
  36. jstring $1_jstr = (jstring) jenv->NewObject($1_strClass, $1_ctorID, $1_bytes, $1_encoding);
  37. jenv->DeleteLocalRef($1_encoding);
  38. jenv->DeleteLocalRef($1_bytes);
  39. $input = $1_jstr;
  40. %}
  41. %typemap(out) const std::string &
  42. %{
  43. jsize $1_len = (jsize)$1->length();
  44. jbyteArray $1_bytes = jenv->NewByteArray($1_len);
  45. jenv->SetByteArrayRegion($1_bytes, 0, $1_len, (jbyte *) $1->c_str());
  46. jclass $1_strClass = jenv->FindClass("java/lang/String");
  47. jmethodID $1_ctorID = jenv->GetMethodID($1_strClass, "<init>", "([BLjava/lang/String;)V");
  48. jstring $1_encoding = jenv->NewStringUTF("UTF-8");
  49. jstring $1_jstr = (jstring) jenv->NewObject($1_strClass, $1_ctorID, $1_bytes, $1_encoding);
  50. jenv->DeleteLocalRef($1_encoding);
  51. jenv->DeleteLocalRef($1_bytes);
  52. $result = $1_jstr;
  53. %}
  54. %typemap(in) std::string
  55. %{ if(!$input) {
  56. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
  57. return $null;
  58. }
  59. const jclass $1_strClass = jenv->GetObjectClass($input);
  60. const jmethodID $1_getBytes = jenv->GetMethodID($1_strClass, "getBytes", "(Ljava/lang/String;)[B");
  61. const jbyteArray $1_strJbytes = (jbyteArray) jenv->CallObjectMethod($input, $1_getBytes, jenv->NewStringUTF("UTF-8"));
  62. size_t $1_length = (size_t) jenv->GetArrayLength($1_strJbytes);
  63. jbyte* $1_pBytes = jenv->GetByteArrayElements($1_strJbytes, NULL);
  64. if (!$1_pBytes) return $null;
  65. $1.assign((char *)$1_pBytes, $1_length);
  66. jenv->ReleaseByteArrayElements($1_strJbytes, $1_pBytes, JNI_ABORT);
  67. jenv->DeleteLocalRef($1_strJbytes);
  68. jenv->DeleteLocalRef($1_strClass);
  69. %}
  70. %typemap(directorout) std::string
  71. %{ if(!$input) {
  72. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
  73. return $null;
  74. }
  75. const jclass $1_strClass = jenv->GetObjectClass($input);
  76. const jmethodID $1_getBytes = jenv->GetMethodID($1_strClass, "getBytes", "(Ljava/lang/String;)[B");
  77. const jbyteArray $1_strJbytes = (jbyteArray) jenv->CallObjectMethod($input, $1_getBytes, jenv->NewStringUTF("UTF-8"));
  78. size_t $1_length = (size_t) jenv->GetArrayLength($1_strJbytes);
  79. jbyte* $1_pBytes = jenv->GetByteArrayElements($1_strJbytes, NULL);
  80. if (!$1_pBytes) return $null;
  81. $result.assign((char *)$1_pBytes, $1_length);
  82. jenv->ReleaseByteArrayElements($1_strJbytes, $1_pBytes, JNI_ABORT);
  83. jenv->DeleteLocalRef($1_strJbytes);
  84. jenv->DeleteLocalRef($1_strClass);
  85. %}
  86. %typemap(in) const std::string &
  87. %{ if(!$input) {
  88. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
  89. return $null;
  90. }
  91. const jclass $1_strClass = jenv->GetObjectClass($input);
  92. const jmethodID $1_getBytes = jenv->GetMethodID($1_strClass, "getBytes", "(Ljava/lang/String;)[B");
  93. const jbyteArray $1_strJbytes = (jbyteArray) jenv->CallObjectMethod($input, $1_getBytes, jenv->NewStringUTF("UTF-8"));
  94. size_t $1_length = (size_t) jenv->GetArrayLength($1_strJbytes);
  95. jbyte* $1_pBytes = jenv->GetByteArrayElements($1_strJbytes, NULL);
  96. if (!$1_pBytes) return $null;
  97. $*1_ltype $1_str((char *)$1_pBytes, $1_length);
  98. $1 = &$1_str;
  99. jenv->ReleaseByteArrayElements($1_strJbytes, $1_pBytes, JNI_ABORT);
  100. jenv->DeleteLocalRef($1_strJbytes);
  101. jenv->DeleteLocalRef($1_strClass);
  102. %}
  103. %typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const std::string &
  104. %{ if(!$input) {
  105. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
  106. return $null;
  107. }
  108. const jclass $1_strClass = jenv->GetObjectClass($input);
  109. const jmethodID $1_getBytes = jenv->GetMethodID($1_strClass, "getBytes", "(Ljava/lang/String;)[B");
  110. const jbyteArray $1_strJbytes = (jbyteArray) jenv->CallObjectMethod($input, $1_getBytes, jenv->NewStringUTF("UTF-8"));
  111. size_t $1_length = (size_t) jenv->GetArrayLength($1_strJbytes);
  112. jbyte* $1_pBytes = jenv->GetByteArrayElements($1_strJbytes, NULL);
  113. if (!$1_pBytes) return $null;
  114. /* possible thread/reentrant code problem */
  115. static $*1_ltype $1_str;
  116. $1_str = (char *)$1_pBytes;
  117. $result = &$1_str;
  118. jenv->ReleaseByteArrayElements($1_strJbytes, $1_pBytes, JNI_ABORT);
  119. jenv->DeleteLocalRef($1_strJbytes);
  120. jenv->DeleteLocalRef($1_strClass);
  121. %}
  122. #endif