diff --git a/src/android/kiwixreader.cpp b/src/android/kiwixreader.cpp index 891b6650a..2a4259d53 100644 --- a/src/android/kiwixreader.cpp +++ b/src/android/kiwixreader.cpp @@ -95,7 +95,7 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getFileSize(JNIEnv* env, jobject obj) try { int cSize = READER->getFileSize(); - size = c2jni(cSize); + size = c2jni(cSize, env); } catch (std::exception& e) { __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get ZIM file size"); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); @@ -234,7 +234,7 @@ JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_checkUrl( entry = entry.getFinalEntry(); finalUrl = c2jni(entry.getPath(), env); } catch (std::exception& e) { - finalUrl = c2jni("", env); + finalUrl = c2jni(std::string(), env); } return finalUrl; } @@ -433,7 +433,7 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getArticleCount(JNIEnv* env, jobject obj) jint articleCount = 0; try { auto cArticleCount = READER->getArticleCount(); - articleCount = c2jni(cArticleCount); + articleCount = c2jni(cArticleCount, env); } catch (std::exception& e) { __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get article count."); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); @@ -447,7 +447,7 @@ Java_org_kiwix_kiwixlib_JNIKiwixReader_getMediaCount(JNIEnv* env, jobject obj) jint mediaCount = 0; try { auto cMediaCount = READER->getMediaCount(); - mediaCount = c2jni(cMediaCount); + mediaCount = c2jni(cMediaCount, env); } catch (std::exception& e) { __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get media count."); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); diff --git a/src/android/utils.h b/src/android/utils.h index aeb5a4d35..87eac0ca2 100644 --- a/src/android/utils.h +++ b/src/android/utils.h @@ -93,15 +93,31 @@ struct LockedHandle : public Lock { operator bool() const { return (h->h != nullptr); } }; -/* c2jni type conversion functions */ -inline jboolean c2jni(const bool& val) { return val ? JNI_TRUE : JNI_FALSE; } +template +struct JType { }; + +template<> struct JType{ typedef jboolean type_t; }; +template<> struct JType{ typedef jint type_t; }; +template<> struct JType{ typedef jlong type_t; }; +template<> struct JType { typedef jlong type_t; }; +template<> struct JType { typedef jlong type_t; }; +template<> struct JType{ typedef jstring type_t; }; + +template +inline typename JType::type_t c2jni(const T& val, JNIEnv* env) { + return static_cast::type_t>(val); +} + +template<> +inline jboolean c2jni(const bool& val, JNIEnv* env) { return val ? JNI_TRUE : JNI_FALSE; } + +template<> inline jstring c2jni(const std::string& val, JNIEnv* env) { return env->NewStringUTF(val.c_str()); } -inline jint c2jni(const int val) { return (jint)val; } -inline jint c2jni(const unsigned val) { return (unsigned)val; } + /* jni2c type conversion functions */ inline bool jni2c(const jboolean& val) { return val == JNI_TRUE; } inline std::string jni2c(const jstring& val, JNIEnv* env) @@ -141,7 +157,7 @@ inline void setBoolObjValue(const bool value, const jobject obj, JNIEnv* env) { jclass objClass = env->GetObjectClass(obj); jfieldID objFid = env->GetFieldID(objClass, "value", "Z"); - env->SetIntField(obj, objFid, c2jni(value)); + env->SetIntField(obj, objFid, c2jni(value, env)); } inline void setPairObjValue(const std::string& filename, const int offset,