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
15namespace std {
16
17%naturalvar wstring;
18
19class wstring;
20
21// wstring
22%typemap(jni) wstring "jstring"
23%typemap(jtype) wstring "String"
24%typemap(jstype) wstring "String"
25%typemap(javadirectorin) wstring "$jniinput"
26%typemap(javadirectorout) wstring "$javacall"
27
28%typemap(in) wstring
29%{if(!$input) {
30    SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
31    return $null;
32  }
33  const jchar *$1_pstr = jenv->GetStringChars($input, 0);
34  if (!$1_pstr) return $null;
35  jsize $1_len = jenv->GetStringLength($input);
36  if ($1_len) {
37    $1.reserve($1_len);
38    for (jsize i = 0; i < $1_len; ++i) {
39      $1.push_back((wchar_t)$1_pstr[i]);
40    }
41  }
42  jenv->ReleaseStringChars($input, $1_pstr);
43 %}
44
45%typemap(directorout) wstring
46%{if(!$input) {
47    SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
48    return $null;
49  }
50  const jchar *$1_pstr = jenv->GetStringChars($input, 0);
51  if (!$1_pstr) return $null;
52  jsize $1_len = jenv->GetStringLength($input);
53  if ($1_len) {
54    $result.reserve($1_len);
55    for (jsize i = 0; i < $1_len; ++i) {
56      $result.push_back((wchar_t)$1_pstr[i]);
57    }
58  }
59  jenv->ReleaseStringChars($input, $1_pstr);
60 %}
61
62%typemap(directorin,descriptor="Ljava/lang/String;") wstring {
63  jsize $1_len = $1.length();
64  jchar *conv_buf = new jchar[$1_len];
65  for (jsize i = 0; i < $1_len; ++i) {
66    conv_buf[i] = (jchar)$1[i];
67  }
68  $input = jenv->NewString(conv_buf, $1_len);
69  delete [] conv_buf;
70}
71
72%typemap(out) wstring
73%{jsize $1_len = $1.length();
74  jchar *conv_buf = new jchar[$1_len];
75  for (jsize i = 0; i < $1_len; ++i) {
76    conv_buf[i] = (jchar)$1[i];
77  }
78  $result = jenv->NewString(conv_buf, $1_len);
79  delete [] conv_buf; %}
80
81%typemap(javain) wstring "$javainput"
82
83%typemap(javaout) wstring {
84    return $jnicall;
85  }
86
87//%typemap(typecheck) wstring = wchar_t *;
88
89%typemap(throws) wstring
90%{ std::string message($1.begin(), $1.end());
91   SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, message.c_str());
92   return $null; %}
93
94// const wstring &
95%typemap(jni) const wstring & "jstring"
96%typemap(jtype) const wstring & "String"
97%typemap(jstype) const wstring & "String"
98%typemap(javadirectorin) const wstring & "$jniinput"
99%typemap(javadirectorout) const wstring & "$javacall"
100
101%typemap(in) const wstring &
102%{if(!$input) {
103    SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
104    return $null;
105  }
106  const jchar *$1_pstr = jenv->GetStringChars($input, 0);
107  if (!$1_pstr) return $null;
108  jsize $1_len = jenv->GetStringLength($input);
109  std::wstring $1_str;
110  if ($1_len) {
111    $1_str.reserve($1_len);
112    for (jsize i = 0; i < $1_len; ++i) {
113      $1_str.push_back((wchar_t)$1_pstr[i]);
114    }
115  }
116  $1 = &$1_str;
117  jenv->ReleaseStringChars($input, $1_pstr);
118 %}
119
120%typemap(directorout,warning=SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG) const wstring &
121%{if(!$input) {
122    SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring");
123    return $null;
124  }
125  const jchar *$1_pstr = jenv->GetStringChars($input, 0);
126  if (!$1_pstr) return $null;
127  jsize $1_len = jenv->GetStringLength($input);
128  /* possible thread/reentrant code problem */
129  static std::wstring $1_str;
130  if ($1_len) {
131    $1_str.reserve($1_len);
132    for (jsize i = 0; i < $1_len; ++i) {
133      $1_str.push_back((wchar_t)$1_pstr[i]);
134    }
135  }
136  $result = &$1_str;
137  jenv->ReleaseStringChars($input, $1_pstr); %}
138
139%typemap(directorin,descriptor="Ljava/lang/String;") const wstring & {
140  jsize $1_len = $1.length();
141  jchar *conv_buf = new jchar[$1_len];
142  for (jsize i = 0; i < $1_len; ++i) {
143    conv_buf[i] = (jchar)($1)[i];
144  }
145  $input = jenv->NewString(conv_buf, $1_len);
146  delete [] conv_buf;
147}
148
149%typemap(out) const wstring &
150%{jsize $1_len = $1->length();
151  jchar *conv_buf = new jchar[$1_len];
152  for (jsize i = 0; i < $1_len; ++i) {
153    conv_buf[i] = (jchar)(*$1)[i];
154  }
155  $result = jenv->NewString(conv_buf, $1_len);
156  delete [] conv_buf; %}
157
158%typemap(javain) const wstring & "$javainput"
159
160%typemap(javaout) const wstring & {
161    return $jnicall;
162  }
163
164//%typemap(typecheck) const wstring & = wchar_t *;
165
166%typemap(throws) const wstring &
167%{ std::string message($1.begin(), $1.end());
168   SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, message.c_str());
169   return $null; %}
170
171}
172
173