From 3563de51eee2ff3240122be505f7c7f81f835bd0 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 29 Mar 2005 20:51:58 +0000 Subject: [PATCH] director typemaps added git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7109 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/java/enums.swg | 4 + Lib/java/enumsimple.swg | 4 + Lib/java/enumtypesafe.swg | 4 + Lib/java/enumtypeunsafe.swg | 4 + Lib/java/java.swg | 147 ++++++++++++++++++++++++++++++++++++ Lib/java/std_string.i | 63 +++++++++++++++- Lib/java/typemaps.i | 14 +++- 7 files changed, 238 insertions(+), 2 deletions(-) diff --git a/Lib/java/enums.swg b/Lib/java/enums.swg index 4a390cf81..8d144afd2 100644 --- a/Lib/java/enums.swg +++ b/Lib/java/enums.swg @@ -15,6 +15,9 @@ $1 = &temp; %} %typemap(out) const enum SWIGTYPE & %{ $result = (jint)*$1; %} +%typemap(directorout) const enum SWIGTYPE & ($*1_ltype temp) +%{ temp = ($*1_ltype)$input; + $1 = &temp; %} %typemap(directorin, descriptor="L$packagepath/$*javaclassname;") const enum SWIGTYPE & "$input = (jint)$1_name;" %typemap(javadirectorin) const enum SWIGTYPE & "$*javaclassname.swigToEnum($jniinput)" %typemap(javadirectorout) const enum SWIGTYPE & "($javacall).swigValue()" @@ -40,6 +43,7 @@ %typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %} +%typemap(directorout) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(directorin, descriptor="L$packagepath/$javaclassname;") enum SWIGTYPE "$input = (jint) $1;" %typemap(javadirectorin) enum SWIGTYPE "$javaclassname.swigToEnum($jniinput)" %typemap(javadirectorout) enum SWIGTYPE "($javacall).swigValue()" diff --git a/Lib/java/enumsimple.swg b/Lib/java/enumsimple.swg index 6e15b2292..0f6bed2f9 100644 --- a/Lib/java/enumsimple.swg +++ b/Lib/java/enumsimple.swg @@ -15,6 +15,9 @@ $1 = &temp; %} %typemap(out) const enum SWIGTYPE & %{ $result = (jint)*$1; %} +%typemap(directorout) const enum SWIGTYPE & ($*1_ltype temp) +%{ temp = ($*1_ltype)$input; + $1 = &temp; %} %typemap(directorin, descriptor="I") const enum SWIGTYPE & "$input = (jint)$1_name;" %typemap(javadirectorin) const enum SWIGTYPE & "$jniinput" %typemap(javadirectorout) const enum SWIGTYPE & "$javacall" @@ -40,6 +43,7 @@ %typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %} +%typemap(directorout) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(directorin, descriptor="I") enum SWIGTYPE "$input = (jint) $1;" %typemap(javadirectorin) enum SWIGTYPE "$jniinput" %typemap(javadirectorout) enum SWIGTYPE "$javacall" diff --git a/Lib/java/enumtypesafe.swg b/Lib/java/enumtypesafe.swg index 19a151c32..6917eaee0 100644 --- a/Lib/java/enumtypesafe.swg +++ b/Lib/java/enumtypesafe.swg @@ -14,6 +14,9 @@ $1 = &temp; %} %typemap(out) const enum SWIGTYPE & %{ $result = (jint)*$1; %} +%typemap(directorout) const enum SWIGTYPE & ($*1_ltype temp) +%{ temp = ($*1_ltype)$input; + $1 = &temp; %} %typemap(directorin, descriptor="L$packagepath/$*javaclassname;") const enum SWIGTYPE & "$input = (jint)$1_name;" %typemap(javadirectorin) const enum SWIGTYPE & "$*javaclassname.swigToEnum($jniinput)" %typemap(javadirectorout) const enum SWIGTYPE & "($javacall).swigValue()" @@ -38,6 +41,7 @@ %typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %} +%typemap(directorout) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(directorin, descriptor="L$packagepath/$javaclassname;") enum SWIGTYPE "$input = (jint) $1;" %typemap(javadirectorin) enum SWIGTYPE "$javaclassname.swigToEnum($jniinput)" %typemap(javadirectorout) enum SWIGTYPE "($javacall).swigValue()" diff --git a/Lib/java/enumtypeunsafe.swg b/Lib/java/enumtypeunsafe.swg index fcc76714e..632d017b4 100644 --- a/Lib/java/enumtypeunsafe.swg +++ b/Lib/java/enumtypeunsafe.swg @@ -15,6 +15,9 @@ $1 = &temp; %} %typemap(out) const enum SWIGTYPE & %{ $result = (jint)*$1; %} +%typemap(directorout) const enum SWIGTYPE & ($*1_ltype temp) +%{ temp = ($*1_ltype)$input; + $1 = &temp; %} %typemap(directorin, descriptor="I") const enum SWIGTYPE & "$input = (jint)$1_name;" %typemap(javadirectorin) const enum SWIGTYPE & "$jniinput" %typemap(javadirectorout) const enum SWIGTYPE & "$javacall" @@ -40,6 +43,7 @@ %typemap(in) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(out) enum SWIGTYPE %{ $result = (jint)$1; %} +%typemap(directorout) enum SWIGTYPE %{ $1 = ($1_ltype)$input; %} %typemap(directorin, descriptor="I") enum SWIGTYPE "$input = (jint) $1;" %typemap(javadirectorin) enum SWIGTYPE "$jniinput" %typemap(javadirectorout) enum SWIGTYPE "$javacall" diff --git a/Lib/java/java.swg b/Lib/java/java.swg index 3cb467076..d59d3c87d 100644 --- a/Lib/java/java.swg +++ b/Lib/java/java.swg @@ -154,6 +154,9 @@ %typemap(in) bool %{ $1 = $input ? true : false; %} +%typemap(directorout) bool +%{ $1 = $input ? true : false; %} + %typemap(javadirectorin) bool "$jniinput" %typemap(javadirectorout) bool "$javacall" @@ -171,6 +174,20 @@ double %{ $1 = ($1_ltype)$input; %} +%typemap(directorout) char, + signed char, + unsigned char, + short, + unsigned short, + int, + unsigned int, + long, + unsigned long, + long long, + float, + double +%{ $1 = ($1_ltype)$input; %} + %typemap(directorin, descriptor="Z") bool "$input = (jboolean) $1;" %typemap(directorin, descriptor="C") char "$input = (jint) $1;" %typemap(directorin, descriptor="B") signed char "$input = (jbyte) $1;" @@ -260,6 +277,38 @@ JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0); } +%typemap(directorout) unsigned long long { + jclass clazz; + jmethodID mid; + jbyteArray ba; + jbyte* bae; + jsize sz; + int i; + + if (!$input) { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "BigInteger null"); + return $null; + } + clazz = JCALL1(GetObjectClass, jenv, $input); + mid = JCALL3(GetMethodID, jenv, clazz, "toByteArray", "()[B"); + ba = (jbyteArray)JCALL2(CallObjectMethod, jenv, $input, mid); + bae = JCALL2(GetByteArrayElements, jenv, ba, 0); + sz = JCALL1(GetArrayLength, jenv, ba); + $1 = 0; + if (bae[0] == 0) { + for(i=sz-1; i>0; i-- ) { + $1 = ($1 << 8) | (unsigned char)bae[sz-i]; + } + } + else { + for(i=sz; i>=0; i-- ) { + $1 = ($1 << 8) | (unsigned char)bae[sz-1-i]; + } + } + JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0); +} + + /* Convert to BigInteger - byte array holds number in 2's complement big endian format */ %typemap(out) unsigned long long { jbyteArray ba = JCALL1(NewByteArray, jenv, 9); @@ -310,6 +359,14 @@ } } +%typemap(directorout) char * { + $1 = 0; + if ($input) { + $1 = ($1_ltype)JCALL2(GetStringUTFChars, jenv, $input, 0); + if (!$1) return $null; + } +} + %typemap(directorin, descriptor="Ljava/lang/String;") char * { $input = 0; if ($1) { @@ -333,6 +390,10 @@ %{ temp = $input ? true : false; $1 = &temp; %} +%typemap(directorout) const bool & (bool temp) +%{ temp = $input ? true : false; + $1 = &temp; %} + %typemap(javadirectorin) const bool & "$jniinput" %typemap(javadirectorout) const bool & "$javacall" @@ -351,6 +412,21 @@ %{ temp = ($*1_ltype)$input; $1 = &temp; %} +%typemap(directorout) const char & (char temp), + const signed char & (signed char temp), + const unsigned char & (unsigned char temp), + const short & (short temp), + const unsigned short & (unsigned short temp), + const int & (int temp), + const unsigned int & (unsigned int temp), + const long & (long temp), + const unsigned long & (unsigned long temp), + const long long & ($*1_ltype temp), + const float & (float temp), + const double & (double temp) +%{ temp = ($*1_ltype)$input; + $1 = &temp; %} + %typemap(directorin, descriptor="Z") const bool & "$input = (jboolean)$1_name;" %typemap(directorin, descriptor="C") const char & "$input = (jchar)$1_name;" %typemap(directorin, descriptor="B") const signed char & "$input = (jbyte)$1_name;" @@ -442,6 +518,38 @@ JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0); } +%typemap(directorout) const unsigned long long & ($*1_ltype temp) { + jclass clazz; + jmethodID mid; + jbyteArray ba; + jbyte* bae; + jsize sz; + int i; + + if (!$input) { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "BigInteger null"); + return $null; + } + clazz = JCALL1(GetObjectClass, jenv, $input); + mid = JCALL3(GetMethodID, jenv, clazz, "toByteArray", "()[B"); + ba = (jbyteArray)JCALL2(CallObjectMethod, jenv, $input, mid); + bae = JCALL2(GetByteArrayElements, jenv, ba, 0); + sz = JCALL1(GetArrayLength, jenv, ba); + $1 = &temp; + temp = 0; + if (bae[0] == 0) { + for(i=sz-1; i>0; i-- ) { + temp = (temp << 8) | (unsigned char)bae[sz-i]; + } + } + else { + for(i=sz; i>=0; i-- ) { + temp = (temp << 8) | (unsigned char)bae[sz-1-i]; + } + } + JCALL3(ReleaseByteArrayElements, jenv, ba, bae, 0); +} + %typemap(out) const unsigned long long & { jbyteArray ba = JCALL1(NewByteArray, jenv, 9); jbyte* bae = JCALL2(GetByteArrayElements, jenv, ba, 0); @@ -472,6 +580,14 @@ } $1 = *argp; %} +%typemap(directorout) SWIGTYPE ($&1_type argp) +%{ argp = *($&1_ltype*)&$input; + if (!argp) { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "Attempt to dereference null $1_type"); + return $null; + } + $1 = *argp; %} + %typemap(out) SWIGTYPE #ifdef __cplusplus %{*($&1_ltype*)&$result = new $1_ltype(($1_ltype &)$1); %} @@ -499,6 +615,8 @@ %typemap(out) SWIGTYPE & %{ *($&1_ltype)&$result = $1; %} +%typemap(directorout) SWIGTYPE *, SWIGTYPE (CLASS::*) +%{ $1 = *($&1_ltype)&$input; %} %typemap(directorin,descriptor="L$packagepath/$javaclassname;") SWIGTYPE *, SWIGTYPE (CLASS::*) %{ *(($&1_ltype)&$input) = ($1_ltype) $1; %} @@ -522,6 +640,14 @@ } } +%typemap(directorout) char[ANY], char[] { + $1 = 0; + if ($input) { + $1 = ($1_ltype)JCALL2(GetStringUTFChars, jenv, $input, 0); + if (!$1) return $null; + } +} + %typemap(directorin, descriptor="Ljava/lang/String;") char[ANY], char[] { $input = 0; if ($1) { @@ -558,6 +684,27 @@ jobjectArray %{ $1 = $input; %} +%typemap(directorout) jboolean, + jchar, + jbyte, + jshort, + jint, + jlong, + jfloat, + jdouble, + jstring, + jobject, + jbooleanArray, + jcharArray, + jbyteArray, + jshortArray, + jintArray, + jlongArray, + jfloatArray, + jdoubleArray, + jobjectArray +%{ $1 = $input; %} + %typemap(out) jboolean, jchar, jbyte, diff --git a/Lib/java/std_string.i b/Lib/java/std_string.i index d0169cb6b..0c21b2228 100644 --- a/Lib/java/std_string.i +++ b/Lib/java/std_string.i @@ -41,6 +41,16 @@ class string; $1 = std::string($1_pstr); jenv->ReleaseStringUTFChars($input, $1_pstr); %} +%typemap(directorout) string +%{if(!$input) { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::string"); + return $null; + } + const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0); + if (!$1_pstr) return $null; + $1 = std::string($1_pstr); + jenv->ReleaseStringUTFChars($input, $1_pstr); %} + %typemap(directorin,descriptor="Ljava/lang/String;") string %{ $input = jenv->NewStringUTF($1.c_str()); %} @@ -78,6 +88,18 @@ class string; $1 = &$1_str; jenv->ReleaseStringUTFChars($input, $1_pstr); %} +%typemap(directorout,warning="470:Possible thread/reentrant unsafe wrapping, consider using a plain 'const string' or 'string' return type instead.") const string & +%{if(!$input) { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::string"); + return $null; + } + const char *$1_pstr = (const char *)jenv->GetStringUTFChars($input, 0); + if (!$1_pstr) return $null; + /* possible thread/reentrant code problem */ + static std::string $1_str($1_pstr); + $1 = &$1_str; + jenv->ReleaseStringUTFChars($input, $1_pstr); %} + %typemap(directorin,descriptor="Ljava/lang/String;") const string & %{ $input = jenv->NewStringUTF($1.c_str()); %} @@ -144,6 +166,25 @@ class wstring; jenv->ReleaseStringChars($input, $1_pstr); %} +%typemap(directorout) wstring +%{if(!$input) { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring"); + return $null; + } + const jchar *$1_pstr = jenv->GetStringChars($input, 0); + if (!$1_pstr) return $null; + jsize $1_len = jenv->GetStringLength($input); + if ($1_len) { + wchar_t *conv_buf = new wchar_t[$1_len]; + for (jsize i = 0; i < $1_len; ++i) { + conv_buf[i] = $1_pstr[i]; + } + $1 = std::wstring(conv_buf, $1_len); + delete [] conv_buf; + } + jenv->ReleaseStringChars($input, $1_pstr); + %} + %typemap(directorin,descriptor="Ljava/lang/String;") wstring %{jsize $1_len = $1.length(); jchar *conv_buf = new jchar[$1_len]; @@ -175,7 +216,27 @@ class wstring; %typemap(javadirectorin) const wstring & "$jniinput" %typemap(javadirectorout) const wstring & "$javacall" -%typemap(in) const wstring & +%typemap(directorout,warning="470:Possible thread/reentrant unsafe wrapping, consider using a plain 'const wstring' or 'wstring' return type instead.") const wstring & +%{if(!$input) { + SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring"); + return $null; + } + const jchar *$1_pstr = jenv->GetStringChars($input, 0); + if (!$1_pstr) return $null; + jsize $1_len = jenv->GetStringLength($input); + static std::wstring $1_str; + if ($1_len) { + wchar_t *conv_buf = new wchar_t[$1_len]; + for (jsize i = 0; i < $1_len; ++i) { + conv_buf[i] = $1_pstr[i]; + } + $1_str = std::wstring(conv_buf, $1_len); + delete [] conv_buf; + } + $1 = &$1_str; + jenv->ReleaseStringChars($input, $1_pstr); %} + +%typemap(directorout) const wstring & %{if(!$input) { SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null std::wstring"); return $null; diff --git a/Lib/java/typemaps.i b/Lib/java/typemaps.i index b97452199..28ebd5a34 100644 --- a/Lib/java/typemaps.i +++ b/Lib/java/typemaps.i @@ -73,7 +73,10 @@ There are no char *INPUT typemaps, however you can apply the signed char * typem %typemap(in) TYPE *INPUT, TYPE &INPUT %{ $1 = ($1_ltype)&$input; %} -%typemap(directorin,descriptor=JNIDESC) CYTPE &INPUT +%typemap(directorout) TYPE *INPUT, TYPE &INPUT +%{ $1 = ($1_ltype)&$input; %} + +%typemap(directorin,descriptor=JNIDESC) TYPE &INPUT %{ *(($&1_ltype) $input) = (JNITYPE *) &$1; %} %typemap(directorin,descriptor=JNIDESC) TYPE *INPUT @@ -216,6 +219,11 @@ There are no char *OUTPUT typemaps, however you can apply the signed char * type $1 = &temp; } +%typemap(directorout) TYPE *OUTPUT($*1_ltype temp), TYPE &OUTPUT($*1_ltype temp) +{ +#error "Need to provide OUTPUT directorout typemap +} + %typemap(directorin,descriptor=JNIDESC) TYPE &OUTPUT %{ *(($&1_ltype) $input = &$1; %} @@ -352,6 +360,10 @@ There are no char *INOUT typemaps, however you can apply the signed char * typem $1 = ($1_ltype) JCALL2(Get##JAVATYPE##ArrayElements, jenv, $input, 0); } +%typemap(directorout) TYPE *INOUT, TYPE &INOUT { +#error "Need to provide INOUT directorout typemap +} + %typemap(directorin,descriptor=JNIDESC) TYPE &INOUT %{ *(($&1_ltype)&$input) = &$1; %}