[android] use more appropriate numeric types for filter conversion

This commit is contained in:
Ivo van Dongen
2017-03-27 16:40:19 +03:00
committed by Ivo van Dongen
parent 0abaae23b6
commit e1a9a468a9
5 changed files with 22 additions and 5 deletions

View File

@@ -13,6 +13,8 @@ namespace java {
jni::jclass* Number::jclass;
jni::jmethodID* Number::floatValueMethodId;
jni::jmethodID* Number::doubleValueMethodId;
jni::jmethodID* Number::longValueMethodId;
jni::jclass* Map::jclass;
jni::jmethodID* Map::getMethodId;
@@ -27,6 +29,8 @@ namespace java {
Number::jclass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/lang/Number")).release();
Number::floatValueMethodId = &jni::GetMethodID(env, *Number::jclass, "floatValue", "()F");
Number::doubleValueMethodId = &jni::GetMethodID(env, *Number::jclass, "doubleValue", "()D");
Number::longValueMethodId = &jni::GetMethodID(env, *Number::jclass, "longValue", "()J");
Map::jclass = jni::NewGlobalRef(env, &jni::FindClass(env, "java/util/Map")).release();
Map::getMethodId = &jni::GetMethodID(env, *Map::jclass, "get", "(Ljava/lang/Object;)Ljava/lang/Object;");

View File

@@ -22,6 +22,8 @@ namespace java {
struct Number {
static jni::jclass* jclass;
static jni::jmethodID* floatValueMethodId;
static jni::jmethodID* doubleValueMethodId;
static jni::jmethodID* longValueMethodId;
};
struct Map {

View File

@@ -59,7 +59,8 @@ inline optional<bool> toBool(const mbgl::android::Value& value) {
inline optional<float> toNumber(const mbgl::android::Value& value) {
if (value.isNumber()) {
return value.toNumber();
auto num = value.toFloat();
return num;
} else {
return {};
}
@@ -81,8 +82,8 @@ inline optional<Value> toValue(const mbgl::android::Value& value) {
} else if (value.isString()) {
return { value.toString() };
} else if (value.isNumber()) {
// Need to cast to a double here as the float is otherwise considered a bool...
return { (double) value.toNumber() };
auto doubleVal = value.toDouble();
return { doubleVal - (int) doubleVal > 0.0 ? doubleVal : value.toLong() };
} else {
return {};
}

View File

@@ -55,10 +55,18 @@ namespace android {
return jni::Make<std::string>(jenv, jni::String(string));
}
float Value::toNumber() const {
float Value::toFloat() const {
return jni::CallMethod<jni::jfloat>(jenv, value.get(), *java::Number::floatValueMethodId);
}
double Value::toDouble() const {
return jni::CallMethod<jni::jdouble>(jenv, value.get(), *java::Number::doubleValueMethodId);
}
long Value::toLong() const {
return jni::CallMethod<jni::jlong>(jenv, value.get(), *java::Number::longValueMethodId);
}
bool Value::toBool() const {
return jni::CallMethod<jni::jboolean>(jenv, value.get(), *java::Boolean::booleanValueMethodId);
}

View File

@@ -21,7 +21,9 @@ public:
bool isNumber() const;
std::string toString() const;
float toNumber() const;
float toFloat() const;
double toDouble() const;
long toLong() const;
bool toBool() const;
Value get(const char* key) const;
int getLength() const;