diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2018-08-22 10:22:23 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2018-09-07 09:44:12 -0700 |
commit | 115e8625e14d15f3fe18681e1e05a06271e2c2e0 (patch) | |
tree | ca93fe84add2be0d5c87a0a92569a3114fca58dc /platform/android/src/geojson/feature.cpp | |
parent | 53c3c327f0ebea276d977f58a59cdb9449165518 (diff) | |
download | qtlocation-mapboxgl-115e8625e14d15f3fe18681e1e05a06271e2c2e0.tar.gz |
[android] Simplify Feature conversion
Diffstat (limited to 'platform/android/src/geojson/feature.cpp')
-rw-r--r-- | platform/android/src/geojson/feature.cpp | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/platform/android/src/geojson/feature.cpp b/platform/android/src/geojson/feature.cpp index 1e02c756eb..809ac42ef7 100644 --- a/platform/android/src/geojson/feature.cpp +++ b/platform/android/src/geojson/feature.cpp @@ -1,50 +1,66 @@ #include "feature.hpp" - #include "geometry.hpp" +#include "../gson/json_object.hpp" namespace mbgl { namespace android { namespace geojson { +using namespace gson; + mbgl::Feature Feature::convert(jni::JNIEnv& env, jni::Object<Feature> jFeature) { - auto jGeometry = jni::SeizeLocal(env, geometry(env, jFeature)); - auto jProperties = jni::SeizeLocal(env, Feature::properties(env, jFeature)); + static auto javaClass = jni::Class<Feature>::Singleton(env); + static auto id = javaClass.GetMethod<jni::String ()>(env, "id"); + static auto geometry = javaClass.GetMethod<jni::Object<Geometry> ()>(env, "geometry"); + static auto properties = javaClass.GetMethod<jni::Object<gson::JsonObject> ()>(env, "properties"); - std::experimental::optional<mapbox::geometry::identifier> id; - auto jId = jni::SeizeLocal(env, Feature::id(env, jFeature)); - if (jId) { - id = { jni::Make<std::string>(env, *jId) }; - } + auto jId = jni::SeizeLocal(env, jFeature.Call(env, id)); return mbgl::Feature { - Geometry::convert(env, *jGeometry), - gson::JsonObject::convert(env, *jProperties), - id + Geometry::convert(env, *jni::SeizeLocal(env, jFeature.Call(env, geometry))), + JsonObject::convert(env, *jni::SeizeLocal(env, jFeature.Call(env, properties))), + jId ? std::experimental::optional<mapbox::geometry::identifier>(jni::Make<std::string>(env, *jId)) + : std::experimental::nullopt }; } -jni::Object<Geometry> Feature::geometry(jni::JNIEnv& env, jni::Object<Feature> jFeature) { - static auto javaClass = jni::Class<Feature>::Singleton(env); - static auto method = javaClass.GetMethod<jni::Object<Geometry> ()>(env, "geometry"); - return jFeature.Call(env, method); -} +/** + * Turn feature identifier into std::string + */ +class FeatureIdVisitor { +public: + template<class T> + std::string operator()(const T& i) const { + return std::to_string(i); + } -jni::Object<gson::JsonObject> Feature::properties(jni::JNIEnv& env, jni::Object<Feature> jFeature) { - static auto javaClass = jni::Class<Feature>::Singleton(env); - static auto method = javaClass.GetMethod<jni::Object<gson::JsonObject> ()>(env, "properties"); - return jFeature.Call(env, method); -} + std::string operator()(const std::string& i) const { + return i; + } + + std::string operator()(const std::nullptr_t&) const { + return ""; + } +}; -jni::String Feature::id(jni::JNIEnv& env, jni::Object<Feature> jFeature) { +jni::Object<Feature> convertFeature(jni::JNIEnv& env, const mbgl::Feature& value) { static auto javaClass = jni::Class<Feature>::Singleton(env); - static auto method = javaClass.GetMethod<jni::String ()>(env, "id"); - return jFeature.Call(env, method); + static auto method = javaClass.GetStaticMethod<jni::Object<Feature> (jni::Object<Geometry>, jni::Object<JsonObject>, jni::String)>(env, "fromGeometry"); + + return javaClass.Call(env, method, + *jni::SeizeLocal(env, Geometry::New(env, value.geometry)), + *jni::SeizeLocal(env, JsonObject::New(env, value.properties)), + *jni::SeizeLocal(env, jni::Make<jni::String>(env, value.id ? value.id.value().match(FeatureIdVisitor()) : ""))); } -jni::Object<Feature> Feature::fromGeometry(jni::JNIEnv& env, jni::Object<Geometry> geometry, jni::Object<gson::JsonObject> properties, jni::String id) { - static auto javaClass = jni::Class<Feature>::Singleton(env); - static auto method = javaClass.GetStaticMethod<jni::Object<Feature> (jni::Object<Geometry>, jni::Object<gson::JsonObject>, jni::String)>(env, "fromGeometry"); - return javaClass.Call(env, method, geometry, properties, id); +jni::Array<jni::Object<Feature>> Feature::convert(jni::JNIEnv& env, const std::vector<mbgl::Feature>& value) { + auto features = jni::Array<jni::Object<Feature>>::New(env, value.size()); + + for (size_t i = 0; i < value.size(); i = i + 1) { + features.Set(env, i, *jni::SeizeLocal(env, convertFeature(env, value.at(i)))); + } + + return features; } void Feature::registerNative(jni::JNIEnv& env) { |