Skip to content

Commit

Permalink
fix(Android): fix options type convert
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Oct 23, 2024
1 parent dc446bc commit c8eee68
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
#include <jni.h>
#include <thread>
#include <tuple>
#include <type_traits>

template <typename T>
T get_map_value(JNIEnv *env, jobject params, const char *key) {
jclass map_class = env->FindClass("java/util/Map");
jmethodID get_method = env->GetMethodID(
map_class, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");
jobject value = env->CallObjectMethod(params, get_method, env->NewStringUTF(key));
if constexpr (std::is_same_v<T, jfloat>) {
jclass float_class = env->FindClass("java/lang/Float");
return env->CallFloatMethod(value, env->GetMethodID(float_class, "floatValue", "()F"));
} else if constexpr (std::is_same_v<T, jint>) {
jclass int_class = env->FindClass("java/lang/Integer");
return env->CallIntMethod(value, env->GetMethodID(int_class, "intValue", "()I"));
jclass value_class = env->GetObjectClass(value);
if (env->IsSameObject(value_class, env->FindClass("java/lang/Double"))) {
return env->CallDoubleMethod(value, env->GetMethodID(value_class, "doubleValue", "()D"));
} else if (env->IsSameObject(value_class, env->FindClass("java/lang/Integer"))) {
return env->CallIntMethod(value, env->GetMethodID(value_class, "intValue", "()I"));
} else {
throw std::runtime_error("Unsupported type");
}
Expand Down

0 comments on commit c8eee68

Please sign in to comment.