diff options
author | Dave Beazley <dave-swig@dabeaz.com> | 2002-11-30 22:01:28 +0000 |
---|---|---|
committer | Dave Beazley <dave-swig@dabeaz.com> | 2002-11-30 22:01:28 +0000 |
commit | 12a43edc2df8853e8e0315f742e57be88f0c4269 (patch) | |
tree | e3237f5f8c0a67c9bfa9bb5d6d095a739a49e4b2 /Lib/java/std_string.i | |
parent | 5fcae5eb66d377e1c3f81da7465c44a62295a72b (diff) | |
download | swig-12a43edc2df8853e8e0315f742e57be88f0c4269.tar.gz |
The great merge
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4141 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Lib/java/std_string.i')
-rw-r--r-- | Lib/java/std_string.i | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/Lib/java/std_string.i b/Lib/java/std_string.i new file mode 100644 index 000000000..7023d7ba4 --- /dev/null +++ b/Lib/java/std_string.i @@ -0,0 +1,183 @@ +// +// SWIG typemaps for std::string +// Luigi Ballabio, Tal Shalif and William Fulton +// May 7, 2002 +// +// Java implementation +// +// ------------------------------------------------------------------------ +// These are mapped to a Java String and are passed around by value. +// ------------------------------------------------------------------------ + +%include exception.i + +%{ +#include <string> +%} + +// std::string +%typemap(jni) std::string "jstring" +%typemap(jtype) std::string "String" +%typemap(jstype) std::string "String" + +%typemap(in) std::string +%{if($input) { + const char *pstr = (const char *)jenv->GetStringUTFChars($input, 0); + if (!pstr) return $null; + $1 = std::string(pstr); + jenv->ReleaseStringUTFChars($input, pstr); + } + else { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::string"); + return $null; + } %} + +%typemap(out) std::string +%{ $result = jenv->NewStringUTF($1.c_str()); %} + +%typemap(javain) std::string "$javainput" + +%typemap(javaout) std::string { + return $jnicall; + } + +%typemap(typecheck) std::string = char *; + +// const std::string & +%typemap(jni) const std::string & "jstring" +%typemap(jtype) const std::string & "String" +%typemap(jstype) const std::string & "String" + +%typemap(in) const std::string & +%{$1 = NULL; + if($input) { + const char *pstr = (const char *)jenv->GetStringUTFChars($input, 0); + if (!pstr) return $null; + $1 = new std::string(pstr); + jenv->ReleaseStringUTFChars($input, pstr); + } + else { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::string"); + return $null; + } %} + +%typemap(freearg) const std::string & +%{ delete $1; %} + +%typemap(out) const std::string & +%{ $result = jenv->NewStringUTF($1->c_str()); %} + +%typemap(javain) const std::string & "$javainput" + +%typemap(javaout) const std::string & { + return $jnicall; + } + +%typemap(typecheck) const std::string & = char *; + +// For using std::string in the global namespace +%apply const std::string & {const string &}; +%apply std::string {string}; + +/* To use non-const std::string references use the following %apply. Note that they are passed by value. +// std::string & +%apply const std::string & {std::string &}; +%apply std::string & {string &}; +*/ + +#ifdef SWIGJAVA_WSTRING +// Warning: Unicode / multibyte characters are handled differently on different OSs so the std::wstring +// typemaps may not always work as intended. Therefore a #define is required to use them. +// std::wstring +%typemap(jni) std::wstring "jstring" +%typemap(jtype) std::wstring "String" +%typemap(jstype) std::wstring "String" + +%typemap(in) std::wstring +%{if($input) { + const jchar *pstr = jenv->GetStringChars($input, 0); + if (!pstr) return $null; + jsize len = jenv->GetStringLength($input); + if (len) { + wchar_t *conv_buf = new wchar_t[len]; + for (jsize i = 0; i < len; ++i) { + conv_buf[i] = pstr[i]; + } + $1 = std::wstring(conv_buf, len); + delete [] conv_buf; + } + jenv->ReleaseStringChars($input, pstr); + } + else { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring"); + return $null; + } %} + +%typemap(out) std::wstring +%{jsize len = $1.length(); + jchar *conv_buf = new jchar[len]; + for (jsize i = 0; i < len; ++i) { + conv_buf[i] = (jchar)$1[i]; + } + $result = jenv->NewString(conv_buf, len); + delete [] conv_buf; %} + +%typemap(javain) std::wstring "$javainput" + +%typemap(javaout) std::wstring { + return $jnicall; + } + +// const std::wstring & +%typemap(jni) const std::wstring & "jstring" +%typemap(jtype) const std::wstring & "String" +%typemap(jstype) const std::wstring & "String" + +%typemap(in) const std::wstring & +%{$1 = NULL; + if($input) { + const jchar *pstr = jenv->GetStringChars($input, 0); + if (!pstr) return $null; + jsize len = jenv->GetStringLength($input); + if (len) { + wchar_t *conv_buf = new wchar_t[len]; + for (jsize i = 0; i < len; ++i) { + conv_buf[i] = pstr[i]; + } + $1 = new std::wstring(conv_buf, len); + delete [] conv_buf; + } + jenv->ReleaseStringChars($input, pstr); + } + else { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring"); + return $null; + } %} + +%typemap(out) const std::wstring & +%{jsize len = $1->length(); + jchar *conv_buf = new jchar[len]; + for (jsize i = 0; i < len; ++i) { + conv_buf[i] = (jchar)(*$1)[i]; + } + $result = jenv->NewString(conv_buf, len); + delete [] conv_buf; %} + +%typemap(javain) const std::wstring & "$javainput" + +%typemap(javaout) const std::wstring & { + return $jnicall; + } + +// For using std::wstring in the global namespace +%apply const std::wstring & {const wstring &}; +%apply std::wstring {wstring}; + +/* To use non-const std::wstring references use the following %apply. Note that they are passed by value. +// std::wstring & +%apply const std::wstring & {std::wstring &}; +%apply std::wstring & {wstring &}; +*/ + +#endif + |