PageRenderTime 25ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/java/std_wstring.i

#
Swig | 172 lines | 135 code | 23 blank | 14 comment | 0 complexity | b6d6f0e5954af85eea2f8a9f05da37a4 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * std_wstring.i
  3. *
  4. * Typemaps for std::wstring and const std::wstring&
  5. *
  6. * These are mapped to a Java String and are passed around by value.
  7. * Warning: Unicode / multibyte characters are handled differently on different
  8. * OSs so the std::wstring typemaps may not always work as intended.
  9. *
  10. * To use non-const std::wstring references use the following %apply. Note
  11. * that they are passed by value.
  12. * %apply const std::wstring & {std::wstring &};
  13. * ----------------------------------------------------------------------------- */
  14. namespace std {
  15. %naturalvar wstring;
  16. class wstring;
  17. // wstring
  18. %typemap(jni) wstring "jstring"
  19. %typemap(jtype) wstring "String"
  20. %typemap(jstype) wstring "String"
  21. %typemap(javadirectorin) wstring "$jniinput"
  22. %typemap(javadirectorout) wstring "$javacall"
  23. %typemap(in) wstring
  24. %{if(!$input) {
  25. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
  26. return $null;
  27. }
  28. const jchar *$1_pstr = jenv->GetStringChars($input, 0);
  29. if (!$1_pstr) return $null;
  30. jsize $1_len = jenv->GetStringLength($input);
  31. if ($1_len) {
  32. $1.reserve($1_len);
  33. for (jsize i = 0; i < $1_len; ++i) {
  34. $1.push_back((wchar_t)$1_pstr[i]);
  35. }
  36. }
  37. jenv->ReleaseStringChars($input, $1_pstr);
  38. %}
  39. %typemap(directorout) wstring
  40. %{if(!$input) {
  41. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
  42. return $null;
  43. }
  44. const jchar *$1_pstr = jenv->GetStringChars($input, 0);
  45. if (!$1_pstr) return $null;
  46. jsize $1_len = jenv->GetStringLength($input);
  47. if ($1_len) {
  48. $result.reserve($1_len);
  49. for (jsize i = 0; i < $1_len; ++i) {
  50. $result.push_back((wchar_t)$1_pstr[i]);
  51. }
  52. }
  53. jenv->ReleaseStringChars($input, $1_pstr);
  54. %}
  55. %typemap(directorin,descriptor="Ljava/lang/String;") wstring {
  56. jsize $1_len = $1.length();
  57. jchar *conv_buf = new jchar[$1_len];
  58. for (jsize i = 0; i < $1_len; ++i) {
  59. conv_buf[i] = (jchar)$1[i];
  60. }
  61. $input = jenv->NewString(conv_buf, $1_len);
  62. delete [] conv_buf;
  63. }
  64. %typemap(out) wstring
  65. %{jsize $1_len = $1.length();
  66. jchar *conv_buf = new jchar[$1_len];
  67. for (jsize i = 0; i < $1_len; ++i) {
  68. conv_buf[i] = (jchar)$1[i];
  69. }
  70. $result = jenv->NewString(conv_buf, $1_len);
  71. delete [] conv_buf; %}
  72. %typemap(javain) wstring "$javainput"
  73. %typemap(javaout) wstring {
  74. return $jnicall;
  75. }
  76. //%typemap(typecheck) wstring = wchar_t *;
  77. %typemap(throws) wstring
  78. %{ std::string message($1.begin(), $1.end());
  79. SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, message.c_str());
  80. return $null; %}
  81. // const wstring &
  82. %typemap(jni) const wstring & "jstring"
  83. %typemap(jtype) const wstring & "String"
  84. %typemap(jstype) const wstring & "String"
  85. %typemap(javadirectorin) const wstring & "$jniinput"
  86. %typemap(javadirectorout) const wstring & "$javacall"
  87. %typemap(in) const wstring &
  88. %{if(!$input) {
  89. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
  90. return $null;
  91. }
  92. const jchar *$1_pstr = jenv->GetStringChars($input, 0);
  93. if (!$1_pstr) return $null;
  94. jsize $1_len = jenv->GetStringLength($input);
  95. std::wstring $1_str;
  96. if ($1_len) {
  97. $1_str.reserve($1_len);
  98. for (jsize i = 0; i < $1_len; ++i) {
  99. $1_str.push_back((wchar_t)$1_pstr[i]);
  100. }
  101. }
  102. $1 = &$1_str;
  103. jenv->ReleaseStringChars($input, $1_pstr);
  104. %}
  105. %typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const wstring &
  106. %{if(!$input) {
  107. SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
  108. return $null;
  109. }
  110. const jchar *$1_pstr = jenv->GetStringChars($input, 0);
  111. if (!$1_pstr) return $null;
  112. jsize $1_len = jenv->GetStringLength($input);
  113. /* possible thread/reentrant code problem */
  114. static std::wstring $1_str;
  115. if ($1_len) {
  116. $1_str.reserve($1_len);
  117. for (jsize i = 0; i < $1_len; ++i) {
  118. $1_str.push_back((wchar_t)$1_pstr[i]);
  119. }
  120. }
  121. $result = &$1_str;
  122. jenv->ReleaseStringChars($input, $1_pstr); %}
  123. %typemap(directorin,descriptor="Ljava/lang/String;") const wstring & {
  124. jsize $1_len = $1.length();
  125. jchar *conv_buf = new jchar[$1_len];
  126. for (jsize i = 0; i < $1_len; ++i) {
  127. conv_buf[i] = (jchar)($1)[i];
  128. }
  129. $input = jenv->NewString(conv_buf, $1_len);
  130. delete [] conv_buf;
  131. }
  132. %typemap(out) const wstring &
  133. %{jsize $1_len = $1->length();
  134. jchar *conv_buf = new jchar[$1_len];
  135. for (jsize i = 0; i < $1_len; ++i) {
  136. conv_buf[i] = (jchar)(*$1)[i];
  137. }
  138. $result = jenv->NewString(conv_buf, $1_len);
  139. delete [] conv_buf; %}
  140. %typemap(javain) const wstring & "$javainput"
  141. %typemap(javaout) const wstring & {
  142. return $jnicall;
  143. }
  144. //%typemap(typecheck) const wstring & = wchar_t *;
  145. %typemap(throws) const wstring &
  146. %{ std::string message($1.begin(), $1.end());
  147. SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, message.c_str());
  148. return $null; %}
  149. }