comparison libinterp/octave-value/ov-java.cc @ 20157: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
comparison
equal deleted inserted replaced
20156:41064c150724 20157:81fcf4aa9e03
1279 std::string s = val.string_value (); 1279 std::string s = val.string_value ();
1280 1280
1281 jobj = jni_env->NewStringUTF (s.c_str ()); 1281 jobj = jni_env->NewStringUTF (s.c_str ());
1282 jcls = jni_env->GetObjectClass (jobj); 1282 jcls = jni_env->GetObjectClass (jobj);
1283 } 1283 }
1284 else if (val.numel () > 1 && val.dims ().is_vector ())
1285 {
1286 #define IF_UNBOX_PRIMITIVE_ARRAY(CHECK_TYPE, METHOD_TYPE, OCTAVE_TYPE, JAVA_TYPE, JAVA_TYPE_CAP) \
1287 if (val.is_ ## CHECK_TYPE ## _type ()) \
1288 { \
1289 const OCTAVE_TYPE ## NDArray v = val.METHOD_TYPE ## array_value (); \
1290 JAVA_TYPE ## Array jarr = jni_env->New ## JAVA_TYPE_CAP ## Array (v.numel ()); \
1291 const JAVA_TYPE* jv = reinterpret_cast<const JAVA_TYPE*> (v.data ()); \
1292 jni_env->Set ## JAVA_TYPE_CAP ## ArrayRegion (jarr, 0, v.numel (), jv); \
1293 jobj = reinterpret_cast<jobject> (jarr); \
1294 jcls = jni_env->GetObjectClass (jobj); \
1295 }
1296
1297 // Note that we do NOT handle char here because they are unboxed
1298 // into a String[], not into a char array
1299 IF_UNBOX_PRIMITIVE_ARRAY(bool, bool_, bool, jboolean, Boolean)
1300 else IF_UNBOX_PRIMITIVE_ARRAY(float, float_, Float, jfloat, Float)
1301 else IF_UNBOX_PRIMITIVE_ARRAY(int8, int8_, int8, jbyte, Byte)
1302 else IF_UNBOX_PRIMITIVE_ARRAY(uint8, uint8_, uint8, jbyte, Byte)
1303 else IF_UNBOX_PRIMITIVE_ARRAY(int16, int16_, int16, jshort, Short)
1304 else IF_UNBOX_PRIMITIVE_ARRAY(uint16, uint16_, uint16, jshort, Short)
1305 else IF_UNBOX_PRIMITIVE_ARRAY(int32, int32_, int32, jint, Int)
1306 else IF_UNBOX_PRIMITIVE_ARRAY(uint32, uint32_, uint32, jint, Int)
1307 else IF_UNBOX_PRIMITIVE_ARRAY(int64, int64_, int64, jlong, Long)
1308 else IF_UNBOX_PRIMITIVE_ARRAY(uint64, uint64_, uint64, jlong, Long)
1309
1310 #undef IF_UNBOX_PRIMITIVE_ARRAY
1311 }
1284 else if (val.is_real_scalar ()) 1312 else if (val.is_real_scalar ())
1285 { 1313 {
1286 if (val.is_double_type ()) 1314 if (val.is_double_type ())
1287 { 1315 {
1288 double dval = val.double_value (); 1316 double dval = val.double_value ();