21 #ifndef jtie_tconv_array_impl_hpp
22 #define jtie_tconv_array_impl_hpp
27 #include "jtie_tconv_value.hpp"
28 #include "jtie_tconv_value_impl.hpp"
29 #include "jtie_tconv_object_impl.hpp"
45 template<
typename JA,
69 #if 0 // disabled on purpose, only document function
71 getArrayElements(JNIEnv * env, JA j, jboolean * isCopy) {
72 TRACE(
"CA ArrayConv.getArrayElements(JNIEnv *, JA, jboolean *)");
73 (void)env; (void)j; (void)isCopy;
74 static_assert(
false,
"missing specialization of array conversion");
77 #endif // disabled on purpose, only document function
106 #if 0 // disabled on purpose, only document function
108 releaseArrayElements(JNIEnv * env, JA j,
const CA c, jint
mode) {
109 TRACE(
"void ArrayConv.releaseArrayElements(JNIEnv *, JA, const CA, jint)");
110 (void)env; (void)j; (void)c; (void)mode;
111 static_assert(
false,
"missing specialization of array conversion");
114 #endif // disabled on purpose, only document function
136 #if 0 // disabled on purpose, only document function
138 newArray(JNIEnv * env, jsize len,
const CA c) {
139 TRACE(
"JA ArrayConv.newArray(JNIEnv *, jsize, const CA)");
140 (void)env; (void)len; (void)c;
141 static_assert(
false,
"missing specialization of array conversion");
144 #endif // disabled on purpose, only document function
153 template<
typename JA,
156 J * (JNIEnv::*GET)(JA, jboolean *),
157 void (JNIEnv::*REL)(JA, J *, jint),
158 JA (JNIEnv::*
NEW)(jsize),
159 void (JNIEnv::*SET)(JA, jsize, jsize,
const J *) >
162 getArrayElements(JNIEnv * env, JA j, jboolean * isCopy) {
163 TRACE(
"C * PrimArrayConvImpl.getArrayElements(JNIEnv *, JA, jboolean *)");
165 assert(
sizeof(J) ==
sizeof(C));
171 J * ja = (env->*GET)(j, isCopy);
172 if (env->ExceptionCheck() != JNI_OK) {
179 const char * cl =
"java/lang/AssertionError";
180 const char * m = (
"JTie: a JNI Get<PrimitiveType>ArrayElements"
181 " function call returned NULL but has not"
182 " registered an exception with the VM"
183 " (file: " __FILE__
")");
184 registerException(env, cl, m);
187 c =
reinterpret_cast< C *
>(ja);
194 releaseArrayElements(JNIEnv * env, JA j,
const C * c, jint
mode) {
195 TRACE(
"void PrimArrayConvImpl.releaseArrayElements(JNIEnv *, JA, const C *, jint)");
197 assert(
sizeof(J) ==
sizeof(C));
204 C * ca =
const_cast< C *
>(c);
206 (env->*REL)(j, reinterpret_cast< J * >(ca),
mode);
211 newArray(JNIEnv * env, jsize len,
const C * c) {
212 TRACE(
"JA PrimArrayConvImpl.newArray(JNIEnv *, jsize, const C *)");
214 assert(
sizeof(J) ==
sizeof(C));
220 JA ja = (env->*
NEW)(len);
221 if (env->ExceptionCheck() != JNI_OK) {
228 const char * cl =
"java/lang/AssertionError";
229 const char * m = (
"JTie: a JNI New<PrimitiveType>Array"
230 " function call returned NULL but has not"
231 " registered an exception with the VM"
232 " (file: " __FILE__
")");
233 registerException(env, cl, m);
236 const J * cjc =
reinterpret_cast< const J *
>(c);
239 (env->*SET)(ja, 0, len, cjc);
240 if (env->ExceptionCheck() != JNI_OK) {
255 is_valid_primitive_type_mapping< J, C >();
280 template<
typename J,
typename C >
283 getArrayElements(JNIEnv * env, jobjectArray j, jboolean * isCopy) {
284 TRACE(
"C * ObjectArrayConvImpl.getArrayElements(JNIEnv *, jobjectArray, jboolean *)");
290 const jsize
n = env->GetArrayLength(j);
291 if (env->ExceptionCheck() != JNI_OK) {
298 cstatus s = copyToCObjectArray(ca, j, n, env);
301 assert(env->ExceptionCheck() != JNI_OK);
305 if (isCopy != NULL) {
318 releaseArrayElements(JNIEnv * env, jobjectArray j,
const C * c, jint
mode) {
319 TRACE(
"void ObjectArrayConvImpl.releaseArrayElements(JNIEnv *, jobjectArray, const C *, jint)");
326 newArray(JNIEnv * env, jsize len,
const C * c) {
327 TRACE(
"jobjectArray ObjectArrayConvImpl.newArray(JNIEnv *, jsize, const C *)");
331 jobjectArray j = NULL;
332 jobjectArray ja = NULL;
335 jclass cls = ObjectResult< J *, C * >::J_ctor::getClass(env);
339 J * ja = newJavaObjectArray(cls, len, env);
343 cstatus s = copyToJavaObjectArray(ja, c, len, env);
346 assert(env->ExceptionCheck() != JNI_OK);
354 ObjectResult< J *, C * >::J_ctor::releaseRef(env, cls);
363 newJavaObjectArray(jclass cls, jsize
n, JNIEnv * env);
368 copyToCObjectArray(C * c, jobjectArray j, jsize n, JNIEnv * env);
373 copyToJavaObjectArray(jobjectArray j, C * c, jsize n, JNIEnv * env);
376 template<
typename J,
typename C >
385 jobjectArray ja = env->NewObjectArray(n, cls, NULL);
386 if (env->ExceptionCheck() != JNI_OK) {
393 const char * cl =
"java/lang/AssertionError";
394 const char * m = (
"JTie: a JNI NewObjectArray function call"
395 " returned NULL but has not registered an"
396 " exception with the VM"
397 " (file: " __FILE__
")");
398 registerException(env, cl, m);
407 template<
typename J,
typename C >
419 for (i = 0; i <
n; i++) {
421 _jobject * jfo = env->GetObjectArrayElement(j, i);
422 if (env->ExceptionCheck() != JNI_OK) {
427 assert(env->ExceptionCheck() == JNI_OK);
430 _jtie_Object * jao = cast< _jtie_Object *, _jobject * >(jfo);
432 const char * cl =
"java/lang/IllegalArgumentException";
433 const char * m = (
"JTie: the Java Object array must not have"
434 " null as elements when mapped to a"
435 " C object array (file: " __FILE__
")");
436 registerException(env, cl, m);
442 C * co = ObjectParam< _jtie_Object *, C * >::convert(s, jao, env);
443 assert(s != 0 || co != NULL);
466 template<
typename J,
typename C >
478 for (i = 0; i <
n; i++) {
485 J * jao = ObjectResult< J *, C * >::convert(co, env);
488 assert(env->ExceptionCheck() != JNI_OK);
494 _jobject * jfo = cast< _jobject *, J * >(jao);
495 env->SetObjectArrayElement(j, i, jfo);
496 if (env->ExceptionCheck() != JNI_OK) {
501 assert(env->ExceptionCheck() == JNI_OK);
529 #define JTIE_SPECIALIZE_ARRAY_TYPE_HELPER( JA, J, JN, C ) \
531 struct ArrayConv< JA, C * > \
532 : PrimArrayConvImpl< JA, J, C, \
533 &JNIEnv::Get##JN##ArrayElements, \
534 &JNIEnv::Release##JN##ArrayElements, \
535 &JNIEnv::New##JN##Array, \
536 &JNIEnv::Set##JN##ArrayRegion > {}; \
538 struct ArrayConv< JA, const C * > \
539 : ArrayConv< JA, C * > {};
545 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jbooleanArray, jboolean, Boolean,
bool)
547 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jbyteArray, jbyte, Byte,
char)
548 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jbyteArray, jbyte, Byte,
signed char)
549 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jbyteArray, jbyte, Byte,
unsigned char)
551 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jfloatArray, jfloat, Float,
float)
552 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jdoubleArray, jdouble, Double,
double)
560 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jshortArray, jshort, Short,
signed short)
561 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jshortArray, jshort, Short,
unsigned short)
564 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jshortArray, jshort, Short,
signed int)
565 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jshortArray, jshort, Short,
unsigned int)
568 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jintArray, jint, Int,
signed int)
569 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jintArray, jint, Int,
unsigned int)
572 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jintArray, jint, Int,
signed long)
573 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jintArray, jint, Int,
unsigned long)
576 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jlongArray, jlong, Long,
signed int)
577 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jlongArray, jlong, Long,
unsigned int)
580 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jlongArray, jlong, Long,
signed long)
581 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jlongArray, jlong, Long,
unsigned long)
584 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jlongArray, jlong, Long,
signed long long)
585 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jlongArray, jlong, Long,
unsigned long long)
588 JTIE_SPECIALIZE_ARRAY_TYPE_HELPER(jdoubleArray, jdouble, Double,
long double)
592 #endif // jtie_tconv_array_impl_hpp