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