2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #ifndef __SCILABPYTHONENVIRONMENTWRAPPER_HXX__
14 #define __SCILABPYTHONENVIRONMENTWRAPPER_HXX__
16 #include "ExternalObjects.h"
17 #include "JavaOptionsHelper.hxx"
19 #include "ScilabAbstractEnvironmentWrapper.hxx"
20 #include "ScilabJavaException.hxx"
22 #include "ScilabJavaObject.hxx"
24 #include "GiwsException.hxx"
27 #include "getScilabJavaVM.h"
30 #define SCILABJAVAOBJECT "org/scilab/modules/external_objects_java/ScilabJavaObject"
32 using namespace org_modules_external_objects;
34 namespace org_scilab_modules_external_objects_java
43 class ScilabJavaEnvironmentWrapper : public ScilabAbstractEnvironmentWrapper
46 // JavaVariablesScope & scope;
47 JavaOptionsHelper & helper;
48 jclass ScilabJavaObjectClass_;
50 jmethodID unwrapStringID_;
51 jmethodID unwrapRowStringID_;
52 jmethodID unwrapMatStringID_;
54 jmethodID unwrapBooleanID_;
55 jmethodID unwrapRowBooleanID_;
56 jmethodID unwrapMatBooleanID_;
58 jmethodID unwrapByteID_;
59 jmethodID unwrapRowByteID_;
60 jmethodID unwrapMatByteID_;
62 jmethodID unwrapShortID_;
63 jmethodID unwrapRowShortID_;
64 jmethodID unwrapMatShortID_;
66 jmethodID unwrapIntID_;
67 jmethodID unwrapRowIntID_;
68 jmethodID unwrapMatIntID_;
70 jmethodID unwrapLongID_;
71 jmethodID unwrapRowLongID_;
72 jmethodID unwrapMatLongID_;
74 jmethodID unwrapDoubleID_;
75 jmethodID unwrapRowDoubleID_;
76 jmethodID unwrapMatDoubleID_;
78 jmethodID unwrapFloatID_;
79 jmethodID unwrapRowFloatID_;
80 jmethodID unwrapMatFloatID_;
82 jmethodID unwrapCharID_;
83 jmethodID unwrapRowCharID_;
84 jmethodID unwrapMatCharID_;
86 jmethodID wrapAsDirectByteBufferID_;
87 jmethodID wrapAsDirectDoubleBufferID_;
88 jmethodID wrapAsDirectIntBufferID_;
89 jmethodID wrapAsDirectCharBufferID_;
90 jmethodID wrapAsDirectFloatBufferID_;
91 jmethodID wrapAsDirectLongBufferID_;
92 jmethodID wrapAsDirectShortBufferID_;
96 ScilabJavaEnvironmentWrapper(JavaOptionsHelper & _helper) : helper(_helper)
98 JavaVM * vm = getScilabJavaVM();
99 JNIEnv * curEnv = NULL;
101 vm->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
102 ScilabJavaObjectClass_ = curEnv->FindClass(SCILABJAVAOBJECT);
104 unwrapStringID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapString", "(I)Ljava/lang/String;");
105 unwrapRowStringID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowString", "(I)[Ljava/lang/String;");
106 unwrapMatStringID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatString", "(I)[[Ljava/lang/String;");
108 unwrapBooleanID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapBoolean", "(I)Z");
109 unwrapRowBooleanID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowBoolean", "(I)[Z");
110 unwrapMatBooleanID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatBoolean", "(I)[[Z");
112 unwrapByteID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapByte", "(I)B");
113 unwrapRowByteID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowByte", "(I)Ljava/lang/Object;");
114 unwrapMatByteID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatByte", "(I)[[B");
116 unwrapShortID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapShort", "(I)S");
117 unwrapRowShortID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowShort", "(I)Ljava/lang/Object;");
118 unwrapMatShortID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatShort", "(I)[[S");
120 unwrapIntID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapInt", "(I)I");
121 unwrapRowIntID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowInt", "(I)Ljava/lang/Object;");
122 unwrapMatIntID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatInt", "(I)[[I");
124 unwrapLongID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapLong", "(I)J");
125 unwrapRowLongID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowLong", "(I)Ljava/lang/Object;");
126 unwrapMatLongID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatLong", "(I)[[J");
128 unwrapDoubleID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapDouble", "(I)D");
129 unwrapRowDoubleID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowDouble", "(I)Ljava/lang/Object;");
130 unwrapMatDoubleID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatDouble", "(I)[[D");
132 unwrapFloatID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapFloat", "(I)F");
133 unwrapRowFloatID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowFloat", "(I)Ljava/lang/Object;");
134 unwrapMatFloatID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatFloat", "(I)[[F");
136 unwrapCharID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapChar", "(I)C");
137 unwrapRowCharID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapRowChar", "(I)Ljava/lang/Object;");
138 unwrapMatCharID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "unwrapMatChar", "(I)[[C");
140 wrapAsDirectByteBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectByteBuffer", "(Ljava/nio/ByteBuffer;)I");
141 wrapAsDirectDoubleBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectDoubleBuffer", "(Ljava/nio/ByteBuffer;)I");
142 wrapAsDirectIntBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectIntBuffer", "(Ljava/nio/ByteBuffer;)I");
143 wrapAsDirectCharBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectCharBuffer", "(Ljava/nio/ByteBuffer;)I");
144 wrapAsDirectFloatBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectFloatBuffer", "(Ljava/nio/ByteBuffer;)I");
145 wrapAsDirectLongBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectLongBuffer", "(Ljava/nio/ByteBuffer;)I");
146 wrapAsDirectShortBufferID_ = curEnv->GetStaticMethodID(ScilabJavaObjectClass_, "wrapAsDirectShortBuffer", "(Ljava/nio/ByteBuffer;)I");
149 ~ScilabJavaEnvironmentWrapper() { }
151 VariableType isunwrappable(int id) const;
153 int wrap(double * x, const bool isRef) const;
155 int wrap(double * x, int xSize, const bool isRef) const;
157 int wrap(double * x, int xSize, int xSizeCol, const bool isRef) const;
159 int wrap(double * re, double * im, const bool isRef) const;
161 int wrap(double * re, double * im, int xSize, const bool isRef) const;
163 int wrap(double * re, double * im, int xSize, int xSizeCol, const bool isRef) const;
165 int wrap(char * x, const bool isRef) const;
167 int wrap(char * x, int xSize, const bool isRef) const;
169 int wrap(char * x, int xSize, int xSizeCol, const bool isRef) const;
172 int wrap(unsigned char * x, const bool isRef) const;
174 int wrap(unsigned char * x, int xSize, const bool isRef) const;
176 int wrap(unsigned char * x, int xSize, int xSizeCol, const bool isRef) const;
179 int wrap(short * x, const bool isRef) const;
181 int wrap(short * x, int xSize, const bool isRef) const;
183 int wrap(short * x, int xSize, int xSizeCol, const bool isRef) const;
186 int wrap(unsigned short * x, const bool isRef) const;
188 int wrap(unsigned short * x, int xSize, const bool isRef) const;
190 int wrap(unsigned short * x, int xSize, int xSizeCol, const bool isRef) const;
193 int wrap(int * x, const bool isRef) const;
195 int wrap(int * x, int xSize, const bool isRef) const;
197 int wrap(int * x, int xSize, int xSizeCol, const bool isRef) const;
200 int wrap(unsigned int * x, const bool isRef) const;
202 int wrap(unsigned int * x, int xSize, const bool isRef) const;
204 int wrap(unsigned int * x, int xSize, int xSizeCol, const bool isRef) const;
206 #ifdef __SCILAB_INT64__
208 int wrap(long long * x, const bool isRef) const;
210 int wrap(long long * x, int xSize, const bool isRef) const;
212 int wrap(long long * x, int xSize, int xSizeCol, const bool isRef) const;
215 int wrap(unsigned long long * x, const bool isRef) const;
217 int wrap(unsigned long long * x, int xSize, const bool isRef) const;
219 int wrap(unsigned long long * x, int xSize, int xSizeCol, const bool isRef) const;
223 int wrap(char ** x, const bool isRef) const;
225 int wrap(char ** x, int xSize, const bool isRef) const;
227 int wrap(char ** x, int xSize, int xSizeCol, const bool isRef) const;
230 int wrapFloat(double * x, const bool isRef) const;
232 int wrapFloat(double * x, int xSize, const bool isRef) const;
234 int wrapFloat(double * x, int xSize, int xSizeCol, const bool isRef) const;
237 int wrapBool(int * x, const bool isRef) const;
239 int wrapBool(int * x, int xSize, const bool isRef) const;
241 int wrapBool(int * x, int xSize, int xSizeCol, const bool isRef) const;
244 * Unwrap a the external object with the given id into a double
245 * @param id the value to wrap
246 * @param pos the position in the Scilab stack where to put the result
248 void unwrapdouble(int id, const ScilabDoubleStackAllocator & allocator) const;
250 void unwraprowdouble(int id, const ScilabDoubleStackAllocator & allocator) const;
252 void unwrapmatdouble(int id, const ScilabDoubleStackAllocator & allocator) const;
254 void unwrapcomplex(int id, const ScilabComplexStackAllocator & allocator) const;
256 void unwraprowcomplex(int id, const ScilabComplexStackAllocator & allocator) const;
258 void unwrapmatcomplex(int id, const ScilabComplexStackAllocator & allocator) const;
260 void unwrapchar(int id, const ScilabCharStackAllocator & allocator) const;
262 void unwraprowchar(int id, const ScilabCharStackAllocator & allocator) const;
264 void unwrapmatchar(int id, const ScilabCharStackAllocator & allocator) const;
266 void unwrapuchar(int id, const ScilabUCharStackAllocator & allocator) const;
268 void unwraprowuchar(int id, const ScilabUCharStackAllocator & allocator) const;
270 void unwrapmatuchar(int id, const ScilabUCharStackAllocator & allocator) const ;
272 void unwrapshort(int id, const ScilabShortStackAllocator & allocator) const;
274 void unwraprowshort(int id, const ScilabShortStackAllocator & allocator) const;
276 void unwrapmatshort(int id, const ScilabShortStackAllocator & allocator) const;
278 void unwrapushort(int id, const ScilabUShortStackAllocator & allocator) const;
280 void unwraprowushort(int id, const ScilabUShortStackAllocator & allocator) const;
282 void unwrapmatushort(int id, const ScilabUShortStackAllocator & allocator) const;
284 void unwrapint(int id, const ScilabIntStackAllocator & allocator) const;
286 void unwraprowint(int id, const ScilabIntStackAllocator & allocator) const;
288 void unwrapmatint(int id, const ScilabIntStackAllocator & allocator) const;
290 void unwrapuint(int id, const ScilabUIntStackAllocator & allocator) const;
292 void unwraprowuint(int id, const ScilabUIntStackAllocator & allocator) const;
294 void unwrapmatuint(int id, const ScilabUIntStackAllocator & allocator) const;
296 void unwraplong(int id, const ScilabLongStackAllocator & allocator) const;
298 void unwraprowlong(int id, const ScilabLongStackAllocator & allocator) const;
300 void unwrapmatlong(int id, const ScilabLongStackAllocator & allocator) const;
302 void unwrapulong(int id, const ScilabULongStackAllocator & allocator) const;
304 void unwraprowulong(int id, const ScilabULongStackAllocator & allocator) const;
306 void unwrapmatulong(int id, const ScilabULongStackAllocator & allocator) const;
308 void unwrapboolean(int id, const ScilabBooleanStackAllocator & allocator) const;
310 void unwraprowboolean(int id, const ScilabBooleanStackAllocator & allocator) const;
312 void unwrapmatboolean(int id, const ScilabBooleanStackAllocator & allocator) const;
314 void unwrapstring(int id, const ScilabStringStackAllocator & allocator) const;
316 void unwraprowstring(int id, const ScilabStringStackAllocator & allocator) const;
318 void unwrapmatstring(int id, const ScilabStringStackAllocator & allocator) const;
320 void unwrapfloat(int id, const ScilabFloatStackAllocator & allocator) const;
322 void unwraprowfloat(int id, const ScilabFloatStackAllocator & allocator) const;
324 void unwrapmatfloat(int id, const ScilabFloatStackAllocator & allocator) const;
329 inline int wrap(JavaVM * vm, T * x) const
331 return ScilabJavaObject::wrap(vm, *x);
334 template<typename T, typename U>
335 inline int wrap(JavaVM * vm, T * x) const
337 return ScilabJavaObject::wrap(vm, (U) * x);
341 inline int wrap(JavaVM * vm, T * x, const int len) const
343 return ScilabJavaObject::wrap(vm, x, len);
346 template<typename T, typename U>
347 inline int wrap(JavaVM * vm, T * x, const int len) const
351 for (i = 0; i < len; i++)
356 i = ScilabJavaObject::wrap(vm, xx, len);
363 inline int wrap(JavaVM * vm, T * x, const int r, const int c) const
365 if (helper.getMethodOfConv())
369 for (i = 0; i < r; i++)
372 for (j = 0; j < c; j++)
374 xx[i][j] = x[j * r + i];
377 j = ScilabJavaObject::wrap(vm, xx, r, c);
378 for (i = 0; i < r; delete[] xx[i++]);
387 for (i = 1; i < c; xx[i] = xx[i++ - 1] + r);
388 i = ScilabJavaObject::wrap(vm, xx, c, r);
394 template<typename T, typename U>
395 inline int wrap(JavaVM * vm, T * x, const int r, const int c) const
397 if (helper.getMethodOfConv())
401 for (i = 0; i < r; i++)
404 for (j = 0; j < c; j++)
406 xx[i][j] = (U)(x[j * r + i]);
409 j = ScilabJavaObject::wrap(vm, xx, r, c);
410 for (i = 0; i < r; delete[] xx[i++]);
418 for (i = 0; i < c; i++)
421 for (j = 0; j < r; j++)
423 xx[i][j] = (U)(x[i * r + j]);
426 j = ScilabJavaObject::wrap(vm, xx, c, r);
427 for (i = 0; i < c; delete[] xx[i++]);
433 inline const jmethodID getSingleMethod(const char * x) const
435 return unwrapByteID_;
437 inline const jmethodID getSingleMethod(const short * x) const
439 return unwrapShortID_;
441 inline const jmethodID getSingleMethod(const unsigned short * x) const
443 return unwrapCharID_;
445 inline const jmethodID getSingleMethod(const int * x) const
449 inline const jmethodID getSingleMethod(const long long * x) const
451 return unwrapLongID_;
453 inline const jmethodID getSingleMethod(const double * x) const
455 return unwrapDoubleID_;
457 inline const jmethodID getSingleMethod(const bool * x) const
459 return unwrapBooleanID_;
461 inline const jmethodID getSingleMethod(const float * x) const
463 return unwrapFloatID_;
466 inline const jmethodID getRowMethod(const char * x) const
468 return unwrapRowByteID_;
470 inline const jmethodID getRowMethod(const short * x) const
472 return unwrapRowShortID_;
474 inline const jmethodID getRowMethod(const unsigned short * x) const
476 return unwrapRowCharID_;
478 inline const jmethodID getRowMethod(const int * x) const
480 return unwrapRowIntID_;
482 inline const jmethodID getRowMethod(const long long * x) const
484 return unwrapRowLongID_;
486 inline const jmethodID getRowMethod(const double * x) const
488 return unwrapRowDoubleID_;
490 inline const jmethodID getRowMethod(const bool * x) const
492 return unwrapRowBooleanID_;
494 inline const jmethodID getRowMethod(const float * x) const
496 return unwrapRowFloatID_;
499 inline const jmethodID getMatMethod(const char * x) const
501 return unwrapMatByteID_;
503 inline const jmethodID getMatMethod(const short * x) const
505 return unwrapMatShortID_;
507 inline const jmethodID getMatMethod(const unsigned short * x) const
509 return unwrapMatCharID_;
511 inline const jmethodID getMatMethod(const int * x) const
513 return unwrapMatIntID_;
515 inline const jmethodID getMatMethod(const long long * x) const
517 return unwrapMatLongID_;
519 inline const jmethodID getMatMethod(const double * x) const
521 return unwrapMatDoubleID_;
523 inline const jmethodID getMatMethod(const bool * x) const
525 return unwrapMatBooleanID_;
527 inline const jmethodID getMatMethod(const float * x) const
529 return unwrapMatFloatID_;
532 inline const jmethodID getDBMethod(const char * x) const
534 return wrapAsDirectByteBufferID_;
536 inline const jmethodID getDBMethod(const double * x) const
538 return wrapAsDirectDoubleBufferID_;
540 inline const jmethodID getDBMethod(const int * x) const
542 return wrapAsDirectIntBufferID_;
544 inline const jmethodID getDBMethod(const float * x) const
546 return wrapAsDirectFloatBufferID_;
548 inline const jmethodID getDBMethod(const long long * x) const
550 return wrapAsDirectLongBufferID_;
552 inline const jmethodID getDBMethod(const short * x) const
554 return wrapAsDirectShortBufferID_;
557 template <typename T>
558 inline T CallStatic(JNIEnv * env_, const char * x, int javaID) const
560 return static_cast<T>(env_->CallStaticByteMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
563 template <typename T>
564 inline T CallStatic(JNIEnv * env_, const short * x, int javaID) const
566 return static_cast<T>(env_->CallStaticShortMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
569 template <typename T>
570 inline T CallStatic(JNIEnv * env_, const unsigned short * x, int javaID) const
572 return static_cast<T>(env_->CallStaticShortMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
575 template <typename T>
576 inline T CallStatic(JNIEnv * env_, const int * x, int javaID) const
578 return static_cast<T>(env_->CallStaticIntMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
581 template <typename T>
582 inline T CallStatic(JNIEnv * env_, const long long * x, int javaID) const
584 return static_cast<T>(env_->CallStaticLongMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
587 template <typename T>
588 inline T CallStatic(JNIEnv * env_, const double * x, int javaID) const
590 return static_cast<T>(env_->CallStaticDoubleMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
593 template <typename T>
594 inline T CallStatic(JNIEnv * env_, const bool * x, int javaID) const
596 return static_cast<T>(env_->CallStaticBooleanMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
599 template <typename T>
600 inline T CallStatic(JNIEnv * env_, const float * x, int javaID) const
602 return static_cast<T>(env_->CallStaticFloatMethod(ScilabJavaObjectClass_, getSingleMethod(x), javaID));
605 template <typename T, typename U, class V>
606 inline void unwrapSingle(JavaVM * jvm_, const int javaID, const V & allocator) const
608 unwrapSingle<T, U, U>(jvm_, javaID, allocator);
611 template <typename T, typename U, typename V, class W>
612 inline void unwrapSingle(JavaVM * jvm_, const int javaID, const W & allocator, const bool mustAlloc = false) const
614 JNIEnv * curEnv = NULL;
618 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
620 T res = CallStatic<T>(curEnv, (V *)addr, javaID);
621 if (curEnv->ExceptionCheck())
623 throw GiwsException::JniCallMethodException(curEnv);
628 addr = reinterpret_cast<U *>(allocator.allocate(1, 1, 0));
629 *addr = static_cast<U>(res);
633 allocator.allocate(1, 1, reinterpret_cast<V *>(&res));
637 template <typename T, typename U, class V>
638 inline void unwrapRow(JavaVM * jvm_, const int javaID, const V & allocator) const
640 unwrapRow<T, U, U>(jvm_, javaID, allocator);
643 template <typename T, typename U, typename V, class W>
644 inline void unwrapRow(JavaVM * jvm_, const int javaID, const W & allocator, const bool mustAlloc = false) const
647 jboolean isCopy = JNI_FALSE;
648 JNIEnv * curEnv = NULL;
651 jobjectArray array = 0;
654 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
656 res = curEnv->CallStaticObjectMethod(ScilabJavaObjectClass_, getRowMethod((V *)addr), javaID);
657 if (curEnv->ExceptionCheck())
659 curEnv->DeleteLocalRef(res);
660 throw GiwsException::JniCallMethodException(curEnv);
663 buffer = curEnv->GetDirectBufferAddress(res);
666 const jlong len = curEnv->GetDirectBufferCapacity(res);
667 V * resultsArray = (V *)buffer;
672 addr = reinterpret_cast<U* >(allocator.allocate(1, len, 0));
673 for (jlong i = 0; i < len; i++)
675 addr[i] = static_cast<U>(resultsArray[i]);
678 catch (const ScilabAbstractEnvironmentException & e)
680 curEnv->DeleteLocalRef(res);
688 allocator.allocate(1, len, reinterpret_cast<V *>(resultsArray));
690 catch (const ScilabAbstractEnvironmentException & e)
692 curEnv->DeleteLocalRef(res);
699 array = static_cast<jobjectArray>(res);
700 lenRow = curEnv->GetArrayLength(array);
706 addr = reinterpret_cast<U *>(allocator.allocate(1, lenRow, 0));
708 catch (const ScilabAbstractEnvironmentException & e)
710 curEnv->DeleteLocalRef(array);
715 T * resultsArray = static_cast<T *>(curEnv->GetPrimitiveArrayCritical(array, &isCopy));
719 for (int i = 0; i < lenRow; i++)
721 addr[i] = static_cast<U>(resultsArray[i]);
728 allocator.allocate(1, lenRow, reinterpret_cast<V *>(resultsArray));
730 catch (const ScilabAbstractEnvironmentException & e)
732 curEnv->ReleasePrimitiveArrayCritical(array, resultsArray, JNI_ABORT);
733 curEnv->DeleteLocalRef(array);
738 curEnv->ReleasePrimitiveArrayCritical(array, resultsArray, JNI_ABORT);
739 curEnv->DeleteLocalRef(array);
741 if (curEnv->ExceptionCheck())
743 throw GiwsException::JniCallMethodException(curEnv);
747 template <typename T, typename U, class V>
748 inline void unwrapMat(JavaVM * jvm_, const int javaID, const V & allocator) const
750 unwrapMat<T, U, U>(jvm_, javaID, allocator);
753 template <typename T, typename U, typename V, class W>
754 inline void unwrapMat(JavaVM * jvm_, const int javaID, const W & allocator) const
757 jboolean isCopy = JNI_FALSE;
759 JNIEnv * curEnv = NULL;
762 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
764 jobjectArray res = static_cast<jobjectArray>(curEnv->CallStaticObjectMethod(ScilabJavaObjectClass_, getMatMethod((V *)addr), javaID));
765 if (curEnv->ExceptionCheck())
767 throw GiwsException::JniCallMethodException(curEnv);
770 lenRow = curEnv->GetArrayLength(res);
771 oneDim = reinterpret_cast<jarray>(curEnv->GetObjectArrayElement(res, 0));
772 lenCol = curEnv->GetArrayLength(oneDim);
773 curEnv->DeleteLocalRef(oneDim);
777 if (helper.getMethodOfConv())
779 addr = (U *)allocator.allocate(lenRow, lenCol, 0);
783 addr = (U *)allocator.allocate(lenCol, lenRow, 0);
786 catch (const ScilabAbstractEnvironmentException & e)
788 curEnv->DeleteLocalRef(res);
793 for (int i = 0; i < lenRow; i++)
795 oneDim = reinterpret_cast<jarray>(curEnv->GetObjectArrayElement(res, i));
796 resultsArray = static_cast<T *>(curEnv->GetPrimitiveArrayCritical(oneDim, &isCopy));
797 if (helper.getMethodOfConv())
799 for (int j = 0; j < lenCol; j++)
801 addr[j * lenRow + i] = static_cast<U>(resultsArray[j]);
806 for (int j = 0; j < lenCol; j++)
808 addr[i * lenCol + j] = static_cast<U>(resultsArray[j]);
811 curEnv->ReleasePrimitiveArrayCritical(oneDim, resultsArray, JNI_ABORT);
812 curEnv->DeleteLocalRef(oneDim);
815 curEnv->DeleteLocalRef(res);
816 if (curEnv->ExceptionCheck())
818 throw GiwsException::JniCallMethodException(curEnv);
823 inline int wrapAsDirectBuffer(JavaVM * jvm_, T * data, const int size) const
825 JNIEnv * curEnv = NULL;
826 jvm_->AttachCurrentThread(reinterpret_cast<void **>(&curEnv), NULL);
828 jobject dbuffer = curEnv->NewDirectByteBuffer((void*)data, (jlong)(size * sizeof(T)));
829 int id = static_cast<jint>(curEnv->CallStaticIntMethod(ScilabJavaObjectClass_, getDBMethod(data), dbuffer));
831 curEnv->DeleteLocalRef(dbuffer);
832 if (curEnv->ExceptionCheck())
834 throw GiwsException::JniCallMethodException(curEnv);
842 #endif // __SCILABJAVAENVIRONMENTWRAPPER_HXX__