From 0aaacdea88915710e8d773192ae957f4f594d7af Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Tue, 4 Jan 2022 19:26:49 +0100 Subject: Add helper_jni.hpp: convert_vector_uniqueptr_to_jarraylist(..) variant ... ... without passing java class type nor ctor name, just relying on ctor code. --- include/jau/jni/helper_jni.hpp | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/include/jau/jni/helper_jni.hpp b/include/jau/jni/helper_jni.hpp index 9a60895..20b66d7 100644 --- a/include/jau/jni/helper_jni.hpp +++ b/include/jau/jni/helper_jni.hpp @@ -464,6 +464,60 @@ namespace jau { return result; } + template + jobject convert_vector_uniqueptr_to_jarraylist(JNIEnv *env, T& array, std::function ctor) + { + unsigned int array_size = array.size(); + + jmethodID arraylist_add; + jobject result = get_new_arraylist(env, array_size, &arraylist_add); + + if (array_size == 0) + { + return result; + } + + for (unsigned int i = 0; i < array_size; ++i) + { + U *elem = array[i].release(); + jobject object = ctor(env, elem); + if (!object) + { + throw jau::RuntimeException("Cannot create instance of class", E_FILE_LINE); + } + env->CallBooleanMethod(result, arraylist_add, object); + java_exception_check_and_throw(env, E_FILE_LINE); + } + return result; + } + + template + jobject convert_vector_sharedptr_to_jarraylist(JNIEnv *env, T& array, std::function ctor) + { + unsigned int array_size = array.size(); + + jmethodID arraylist_add; + jobject result = get_new_arraylist(env, array_size, &arraylist_add); + + if (array_size == 0) + { + return result; + } + + for (unsigned int i = 0; i < array_size; ++i) + { + U *elem = array[i].get(); + jobject object = ctor(env, elem); + if (!object) + { + throw jau::RuntimeException("Cannot create instance of class", E_FILE_LINE); + } + env->CallBooleanMethod(result, arraylist_add, object); + java_exception_check_and_throw(env, E_FILE_LINE); + } + return result; + } + } // namespace direct_bt #endif /* JAU_HELPER_JNI_HPP_ */ -- cgit v1.2.3