diff options
author | Anand Thakker <github@anandthakker.net> | 2017-10-13 16:57:59 -0400 |
---|---|---|
committer | Anand Thakker <github@anandthakker.net> | 2017-10-13 18:56:03 -0400 |
commit | e63088b33545d5d30a118211a11a8219115b0578 (patch) | |
tree | f41335db5ad4c62774218efed14c4dd3d0d86d8e | |
parent | ff60a10d73320b5e226320058b2b5d6492de7e36 (diff) | |
download | qtlocation-mapboxgl-e63088b33545d5d30a118211a11a8219115b0578.tar.gz |
Update android conversion to use runtime polymorphic conversion
-rw-r--r-- | include/mbgl/style/conversion.hpp | 3 | ||||
-rw-r--r-- | platform/android/build.gradle | 2 | ||||
-rw-r--r-- | platform/android/config.cmake | 2 | ||||
-rw-r--r-- | platform/android/src/style/android_conversion.cpp | 106 | ||||
-rw-r--r-- | platform/android/src/style/android_conversion.hpp | 86 | ||||
-rw-r--r-- | platform/android/src/style/conversion/geojson.hpp | 24 | ||||
-rw-r--r-- | platform/android/src/style/conversion/url_or_tileset.hpp | 9 | ||||
-rw-r--r-- | platform/android/src/style/layers/layer.cpp | 9 | ||||
-rw-r--r-- | platform/android/src/style/sources/geojson_source.cpp | 10 |
9 files changed, 132 insertions, 119 deletions
diff --git a/include/mbgl/style/conversion.hpp b/include/mbgl/style/conversion.hpp index 79a45231d3..3dfdbb8edc 100644 --- a/include/mbgl/style/conversion.hpp +++ b/include/mbgl/style/conversion.hpp @@ -37,7 +37,8 @@ namespace detail { // iOS/macOS: JSValue* or id // Qt: JSValue* or QVariant -using Storage = std::aligned_storage_t<16, 8>; +// TODO: use platform-specific size +using Storage = std::aligned_storage_t<32, 8>; } // namespace detail diff --git a/platform/android/build.gradle b/platform/android/build.gradle index e298b84da8..ec23fde819 100644 --- a/platform/android/build.gradle +++ b/platform/android/build.gradle @@ -3,7 +3,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.1' + classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.amazonaws:aws-devicefarm-gradle-plugin:1.2' classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.1' } diff --git a/platform/android/config.cmake b/platform/android/config.cmake index dc6b1c80a6..a36d6e94d9 100644 --- a/platform/android/config.cmake +++ b/platform/android/config.cmake @@ -133,7 +133,7 @@ add_library(mbgl-android STATIC # Style conversion Java -> C++ platform/android/src/style/android_conversion.hpp - platform/android/src/style/conversion/geojson.hpp + platform/android/src/style/android_conversion.cpp platform/android/src/style/value.cpp platform/android/src/style/value.hpp platform/android/src/style/conversion/url_or_tileset.hpp diff --git a/platform/android/src/style/android_conversion.cpp b/platform/android/src/style/android_conversion.cpp new file mode 100644 index 0000000000..43940c2acd --- /dev/null +++ b/platform/android/src/style/android_conversion.cpp @@ -0,0 +1,106 @@ +#include "android_conversion.hpp" +#include <mbgl/style/conversion/geojson.hpp> + + +namespace mbgl { +namespace style { +namespace conversion { + +using AndroidValue = mbgl::android::Value; + +template<> bool ValueTraits<AndroidValue>::isUndefined(const AndroidValue& value) { + return value.isNull(); +} + +template<> bool ValueTraits<AndroidValue>::isArray(const AndroidValue& value) { + return value.isArray(); +} + +template<> bool ValueTraits<AndroidValue>::isObject(const AndroidValue& value) { + return value.isObject(); +} + +template<> std::size_t ValueTraits<AndroidValue>::arrayLength(const AndroidValue& value) { + return value.getLength();; +} + +template<> AndroidValue ValueTraits<AndroidValue>::arrayMember(const AndroidValue& value, std::size_t i) { + return value.get(i); +} + +template<> optional<AndroidValue> ValueTraits<AndroidValue>::objectMember(const AndroidValue& value, const char* key) { + AndroidValue member = value.get(key); + + if (!member.isNull()) { + return member; + } else { + return {}; + } +} + +template<> optional<Error> ValueTraits<AndroidValue>::eachMember(const AndroidValue&, const std::function<optional<Error> (const std::string&, const AndroidValue&)>&) { + // TODO + mbgl::Log::Warning(mbgl::Event::Android, "eachMember not implemented"); + return {}; +} + +template<> optional<bool> ValueTraits<AndroidValue>::toBool(const AndroidValue& value) { + if (value.isBool()) { + return value.toBool(); + } else { + return {}; + } +} + +template<> optional<float> ValueTraits<AndroidValue>::toNumber(const AndroidValue& value) { + if (value.isNumber()) { + auto num = value.toFloat(); + return num; + } else { + return {}; + } +} + +template<> optional<double> ValueTraits<AndroidValue>::toDouble(const AndroidValue& value) { + if (value.isNumber()) { + return value.toDouble(); + } else { + return {}; + } +} + +template<> optional<std::string> ValueTraits<AndroidValue>::toString(const AndroidValue& value) { + if (value.isString()) { + return value.toString(); + } else { + return {}; + } +} + +template<> optional<mbgl::Value> ValueTraits<AndroidValue>::toValue(const AndroidValue& value) { + if (value.isNull()) { + return {}; + } else if (value.isBool()) { + return { value.toBool() }; + } else if (value.isString()) { + return { value.toString() }; + } else if (value.isNumber()) { + auto doubleVal = value.toDouble(); + return { doubleVal - (int) doubleVal > 0.0 ? doubleVal : value.toLong() }; + } else { + return {}; + } +} + +template<> optional<GeoJSON> ValueTraits<AndroidValue>::toGeoJSON(const AndroidValue &value, Error &error) { + if(value.isNull() || !value.isString()) { + error = { "no json data found" }; + return {}; + } + + return parseGeoJSON(value.toString(), error); +} + +} // namespace conversion +} // namespace style +} // namespace mbgl diff --git a/platform/android/src/style/android_conversion.hpp b/platform/android/src/style/android_conversion.hpp index 082fe411e2..3969553816 100644 --- a/platform/android/src/style/android_conversion.hpp +++ b/platform/android/src/style/android_conversion.hpp @@ -13,89 +13,9 @@ namespace mbgl { namespace style { namespace conversion { -inline bool isUndefined(const mbgl::android::Value& value) { - return value.isNull(); -} - -inline bool isArray(const mbgl::android::Value& value) { - return value.isArray(); -} - -inline bool isObject(const mbgl::android::Value& value) { - return value.isObject(); -} - -inline std::size_t arrayLength(const mbgl::android::Value& value) { - return value.getLength();; -} - -inline mbgl::android::Value arrayMember(const mbgl::android::Value& value, std::size_t i) { - return value.get(i); -} - -inline optional<mbgl::android::Value> objectMember(const mbgl::android::Value& value, const char* key) { - mbgl::android::Value member = value.get(key); - - if (!member.isNull()) { - return member; - } else { - return {}; - } -} - -template <class Fn> -optional<Error> eachMember(const mbgl::android::Value&, Fn&&) { - // TODO - mbgl::Log::Warning(mbgl::Event::Android, "eachMember not implemented"); - return {}; -} - -inline optional<bool> toBool(const mbgl::android::Value& value) { - if (value.isBool()) { - return value.toBool(); - } else { - return {}; - } -} - -inline optional<float> toNumber(const mbgl::android::Value& value) { - if (value.isNumber()) { - auto num = value.toFloat(); - return num; - } else { - return {}; - } -} - -inline optional<double> toDouble(const mbgl::android::Value& value) { - if (value.isNumber()) { - return value.toDouble(); - } else { - return {}; - } -} - -inline optional<std::string> toString(const mbgl::android::Value& value) { - if (value.isString()) { - return value.toString(); - } else { - return {}; - } -} - -inline optional<Value> toValue(const mbgl::android::Value& value) { - if (value.isNull()) { - return {}; - } else if (value.isBool()) { - return { value.toBool() }; - } else if (value.isString()) { - return { value.toString() }; - } else if (value.isNumber()) { - auto doubleVal = value.toDouble(); - return { doubleVal - (int) doubleVal > 0.0 ? doubleVal : value.toLong() }; - } else { - return {}; - } +template <class T, class...Args> +optional<T> convert(const mbgl::android::Value& value, Error& error, Args&&...args) { + return convert<T>(Value(value), error, std::forward<Args>(args)...); } } // namespace conversion diff --git a/platform/android/src/style/conversion/geojson.hpp b/platform/android/src/style/conversion/geojson.hpp deleted file mode 100644 index 748fe7361e..0000000000 --- a/platform/android/src/style/conversion/geojson.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include <mapbox/geojson.hpp> -#include <mbgl/style/conversion.hpp> -#include <mbgl/style/conversion/geojson.hpp> -#include <jni/jni.hpp> - -namespace mbgl { -namespace style { -namespace conversion { - -template <> -optional<GeoJSON> Converter<GeoJSON>::operator()(const mbgl::android::Value& value, Error& error) const { - if(value.isNull() || !value.isString()) { - error = { "no json data found" }; - return {}; - } - - return convert<GeoJSON>(value.toString(), error); -} - -} // namespace conversion -} // namespace style -} // namespace mbgl diff --git a/platform/android/src/style/conversion/url_or_tileset.hpp b/platform/android/src/style/conversion/url_or_tileset.hpp index 00ef913d41..dae1209697 100644 --- a/platform/android/src/style/conversion/url_or_tileset.hpp +++ b/platform/android/src/style/conversion/url_or_tileset.hpp @@ -17,18 +17,19 @@ namespace android { // This conversion is expected not to fail because it's used only in contexts where // the value was originally a String or TileSet object on the Java side. If it fails // to convert, it's a bug in our serialization or Java-side static typing. -inline variant<std::string, Tileset> convertURLOrTileset(const Value& value) { +inline variant<std::string, Tileset> convertURLOrTileset(const mbgl::android::Value& value) { using namespace mbgl::style::conversion; - if (isObject(value)) { + const mbgl::style::conversion::Value converted(value); + if (isObject(converted)) { Error error; - optional<Tileset> tileset = convert<Tileset>(value, error); + optional<Tileset> tileset = convert<Tileset>(converted, error); if (!tileset) { throw std::logic_error(error.message); } return { *tileset }; } else { - return { *toString(value) }; + return { *toString(converted) }; } } diff --git a/platform/android/src/style/layers/layer.cpp b/platform/android/src/style/layers/layer.cpp index 02a1f0be82..54d896ec7c 100644 --- a/platform/android/src/style/layers/layer.cpp +++ b/platform/android/src/style/layers/layer.cpp @@ -4,11 +4,20 @@ #include <jni/jni.hpp> #include <mbgl/style/style.hpp> +#include <mbgl/style/filter.hpp> #include <mbgl/style/transition_options.hpp> +#include <mbgl/style/layers/background_layer.hpp> +#include <mbgl/style/layers/circle_layer.hpp> +#include <mbgl/style/layers/fill_layer.hpp> +#include <mbgl/style/layers/fill_extrusion_layer.hpp> +#include <mbgl/style/layers/line_layer.hpp> +#include <mbgl/style/layers/raster_layer.hpp> +#include <mbgl/style/layers/symbol_layer.hpp> #include <mbgl/util/logging.hpp> // Java -> C++ conversion #include <mbgl/style/conversion.hpp> +#include <mbgl/style/conversion/filter.hpp> #include <mbgl/style/conversion/layer.hpp> #include <mbgl/style/conversion/source.hpp> diff --git a/platform/android/src/style/sources/geojson_source.cpp b/platform/android/src/style/sources/geojson_source.cpp index 90ef851eba..4468b453f3 100644 --- a/platform/android/src/style/sources/geojson_source.cpp +++ b/platform/android/src/style/sources/geojson_source.cpp @@ -5,15 +5,15 @@ // Java -> C++ conversion #include "../android_conversion.hpp" #include "../conversion/filter.hpp" -#include "../conversion/geojson.hpp" +#include <mbgl/style/conversion.hpp> +#include <mbgl/style/conversion/geojson.hpp> +#include <mbgl/style/conversion/geojson_options.hpp> // C++ -> Java conversion #include "../../conversion/conversion.hpp" #include "../../conversion/collection.hpp" #include "../../geojson/conversion/feature.hpp" #include "../conversion/url_or_tileset.hpp" -#include <mbgl/style/conversion.hpp> -#include <mbgl/style/conversion/geojson_options.hpp> #include <string> @@ -29,7 +29,7 @@ namespace android { return style::GeoJSONOptions(); } Error error; - optional<style::GeoJSONOptions> result = convert<style::GeoJSONOptions>(Value(env, options), error); + optional<style::GeoJSONOptions> result = convert<style::GeoJSONOptions>(mbgl::android::Value(env, options), error); if (!result) { throw std::logic_error(error.message); } @@ -54,7 +54,7 @@ namespace android { // Convert the jni object Error error; - optional<GeoJSON> converted = convert<GeoJSON>(Value(env, json), error); + optional<GeoJSON> converted = convert<GeoJSON>(mbgl::android::Value(env, json), error); if(!converted) { mbgl::Log::Error(mbgl::Event::JNI, "Error setting geo json: " + error.message); return; |