Mercurial > hg > octave-nkf
diff libinterp/octave-value/ov-java.cc @ 20332:81fcf4aa9e03
Automatically convert octave vectors into java primitive arrays (bug #44882)
* libinterp/octave-value/ov-java.cc (unbox): conversion was working fine when
input were scalars. However, when input is a vector, Matlab automatically
converts into a java array of the corresponding primitive types. This seems
to lose some information since a uint8 will be casted into a int8 for example.
The documentation is not super clear, maybe the casting needs to be done based
on the type of the method being called?
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Mon, 20 Apr 2015 17:40:39 +0100 |
parents | 41064c150724 |
children | 8261c4a11250 |
line wrap: on
line diff
--- a/libinterp/octave-value/ov-java.cc +++ b/libinterp/octave-value/ov-java.cc @@ -1281,6 +1281,34 @@ jobj = jni_env->NewStringUTF (s.c_str ()); jcls = jni_env->GetObjectClass (jobj); } + else if (val.numel () > 1 && val.dims ().is_vector ()) + { +#define IF_UNBOX_PRIMITIVE_ARRAY(CHECK_TYPE, METHOD_TYPE, OCTAVE_TYPE, JAVA_TYPE, JAVA_TYPE_CAP) \ + if (val.is_ ## CHECK_TYPE ## _type ()) \ + { \ + const OCTAVE_TYPE ## NDArray v = val.METHOD_TYPE ## array_value (); \ + JAVA_TYPE ## Array jarr = jni_env->New ## JAVA_TYPE_CAP ## Array (v.numel ()); \ + const JAVA_TYPE* jv = reinterpret_cast<const JAVA_TYPE*> (v.data ()); \ + jni_env->Set ## JAVA_TYPE_CAP ## ArrayRegion (jarr, 0, v.numel (), jv); \ + jobj = reinterpret_cast<jobject> (jarr); \ + jcls = jni_env->GetObjectClass (jobj); \ + } + + // Note that we do NOT handle char here because they are unboxed + // into a String[], not into a char array + IF_UNBOX_PRIMITIVE_ARRAY(bool, bool_, bool, jboolean, Boolean) + else IF_UNBOX_PRIMITIVE_ARRAY(float, float_, Float, jfloat, Float) + else IF_UNBOX_PRIMITIVE_ARRAY(int8, int8_, int8, jbyte, Byte) + else IF_UNBOX_PRIMITIVE_ARRAY(uint8, uint8_, uint8, jbyte, Byte) + else IF_UNBOX_PRIMITIVE_ARRAY(int16, int16_, int16, jshort, Short) + else IF_UNBOX_PRIMITIVE_ARRAY(uint16, uint16_, uint16, jshort, Short) + else IF_UNBOX_PRIMITIVE_ARRAY(int32, int32_, int32, jint, Int) + else IF_UNBOX_PRIMITIVE_ARRAY(uint32, uint32_, uint32, jint, Int) + else IF_UNBOX_PRIMITIVE_ARRAY(int64, int64_, int64, jlong, Long) + else IF_UNBOX_PRIMITIVE_ARRAY(uint64, uint64_, uint64, jlong, Long) + +#undef IF_UNBOX_PRIMITIVE_ARRAY + } else if (val.is_real_scalar ()) { if (val.is_double_type ())